Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
utils consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
kremalicious committed Oct 22, 2020
1 parent 4ae3202 commit dd7e323
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 75 deletions.
9 changes: 2 additions & 7 deletions src/hooks/useCompute/useCompute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ComputeValue } from './ComputeOptions'
import { Logger, ServiceCompute } from '@oceanprotocol/lib'
import { MetadataAlgorithm } from '@oceanprotocol/lib/dist/node/ddo/interfaces/MetadataAlgorithm'
import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/ComputeJob'
import { computeFeedback } from 'utils'

interface UseCompute {
compute: (
Expand All @@ -20,12 +21,6 @@ interface UseCompute {
isLoading: boolean
}

// TODO: customize for compute
export const computeFeedback: { [key in number]: string } = {
0: '1/3 Ordering asset...',
1: '2/3 Transfering data token.',
2: '3/3 Access granted. Starting job...'
}
const rawAlgorithmMeta: MetadataAlgorithm = {
rawcode: `console.log('Hello world'!)`,
format: 'docker-image',
Expand All @@ -38,7 +33,7 @@ const rawAlgorithmMeta: MetadataAlgorithm = {
}

function useCompute(): UseCompute {
const { ocean, account, accountId, config } = useOcean()
const { ocean, account, accountId } = useOcean()
const [computeStep, setComputeStep] = useState<number | undefined>()
const [computeStepText, setComputeStepText] = useState<string | undefined>()
const [computeError, setComputeError] = useState<string | undefined>()
Expand Down
10 changes: 1 addition & 9 deletions src/hooks/useConsume/useConsume.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react'
import { useOcean } from 'providers'
import { feedback } from 'utils'
import { consumeFeedback } from 'utils'
import { DID, Logger, ServiceType } from '@oceanprotocol/lib'

interface UseConsume {
Expand All @@ -16,14 +16,6 @@ interface UseConsume {
isLoading: boolean
}

// TODO: do something with this object,
// consumeStep should probably return one of those strings
// instead of just a number
export const consumeFeedback: { [key in number]: string } = {
...feedback,
3: '3/3 Access granted. Consuming file...'
}

function useConsume(): UseConsume {
const { ocean, account, accountId } = useOcean()
const [isLoading, setIsLoading] = useState(false)
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useMetadata/useMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
MetadataCache
} from '@oceanprotocol/lib'
import { useOcean } from 'providers'
import { getBestDataTokenPrice } from 'utils/dtUtils'
import { isDDO } from 'utils'
import { isDDO, getBestDataTokenPrice } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'

interface UseMetadata {
Expand Down
9 changes: 5 additions & 4 deletions src/hooks/usePricing/usePricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ import { useEffect, useState } from 'react'
import { useOcean } from 'providers'
import { PriceOptions } from './PriceOptions'
import { TransactionReceipt } from 'web3-core'
import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils'
import { Decimal } from 'decimal.js'
import {
getBestDataTokenPrice,
getFirstPool,
getCreatePricingPoolFeedback,
getCreatePricingExchangeFeedback,
getBuyDTFeedback,
getSellDTFeedback
} from './utils'
import { sleep } from 'utils'
getSellDTFeedback,
sleep
} from 'utils'

interface UsePricing {
dtSymbol?: string
Expand Down
10 changes: 3 additions & 7 deletions src/utils/dtUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import Web3 from 'web3'
export async function getCheapestPool(
ocean: Ocean,
dataTokenAddress: string
): Promise<Pool | null> {
if (!ocean || !dataTokenAddress) return null

): Promise<Pool> {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)

if (tokenPools === undefined || tokenPools.length === 0) {
Expand Down Expand Up @@ -46,7 +44,7 @@ export async function getCheapestPool(
export async function getCheapestExchange(
ocean: Ocean,
dataTokenAddress: string
): Promise<Pool | undefined> {
): Promise<Pool> {
try {
const tokenExchanges = await ocean.fixedRateExchange.searchforDT(
dataTokenAddress,
Expand Down Expand Up @@ -88,9 +86,7 @@ export async function getCheapestExchange(
export async function getFirstPool(
ocean: Ocean,
dataTokenAddress: string
): Promise<Pool | null> {
if (!ocean || !dataTokenAddress) return null

): Promise<Pool> {
const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress)

if (tokenPools === undefined || tokenPools.length === 0) {
Expand Down
30 changes: 30 additions & 0 deletions src/hooks/usePricing/utils.ts → src/utils/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
export const feedback: { [key in number]: string } = {
99: 'Decrypting file URL...',
0: '1/3 Looking for data token. Buying if none found...',
1: '2/3 Transfering data token.',
2: '3/3 Payment confirmed. Requesting access...'
}

export const publishFeedback: { [key in number]: string } = {
0: '1/5 Creating datatoken ...',
2: '2/5 Encrypting files ...',
4: '3/5 Storing ddo ...',
6: '4/5 Minting tokens ...',
8: '5/5 Asset published succesfully'
}

// TODO: do something with this object,
// consumeStep should probably return one of those strings
// instead of just a number
export const consumeFeedback: { [key in number]: string } = {
...feedback,
3: '3/3 Access granted. Consuming file...'
}

// TODO: customize for compute
export const computeFeedback: { [key in number]: string } = {
0: '1/3 Ordering asset...',
1: '2/3 Transfering data token.',
2: '3/3 Access granted. Starting job...'
}

export function getCreatePricingPoolFeedback(
dtSymbol: string
): { [key: number]: string } {
Expand Down
30 changes: 30 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { DDO, DID } from '@oceanprotocol/lib'

export function readFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onerror = () => {
reader.abort()
reject(new DOMException('Problem parsing input file.'))
}
reader.onload = () => {
resolve(reader.result as string)
}
reader.readAsText(file)
})
}

export function isDDO(
toBeDetermined: DID | string | DDO
): toBeDetermined is DDO {
if ((toBeDetermined as DDO).id) {
return true
}
return false
}

export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
49 changes: 3 additions & 46 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,4 @@
import { DDO, DID } from '@oceanprotocol/lib'

export function readFileContent(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onerror = () => {
reader.abort()
reject(new DOMException('Problem parsing input file.'))
}
reader.onload = () => {
resolve(reader.result as string)
}
reader.readAsText(file)
})
}

export function isDDO(
toBeDetermined: DID | string | DDO
): toBeDetermined is DDO {
if ((toBeDetermined as DDO).id) {
return true
}
return false
}

export const feedback: { [key in number]: string } = {
99: 'Decrypting file URL...',
0: '1/3 Looking for data token. Buying if none found...',
1: '2/3 Transfering data token.',
2: '3/3 Payment confirmed. Requesting access...'
}

export const publishFeedback: { [key in number]: string } = {
0: '1/5 Creating datatoken ...',
2: '2/5 Encrypting files ...',
4: '3/5 Storing ddo ...',
6: '4/5 Minting tokens ...',
8: '5/5 Asset published succesfully'
}

export * from './dtUtils'
export * from './web3'

export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
export * from './feedback'
export * from './helpers'

0 comments on commit dd7e323

Please sign in to comment.