1
0
mirror of https://github.com/oceanprotocol/react.git synced 2025-02-14 21:10:38 +01:00

utils consolidation

This commit is contained in:
Matthias Kretschmann 2020-10-22 10:44:36 +02:00
parent 4ae3202f6c
commit dd7e323dad
Signed by: m
GPG Key ID: 606EEEF3C479A91F
8 changed files with 75 additions and 75 deletions

View File

@ -4,6 +4,7 @@ import { ComputeValue } from './ComputeOptions'
import { Logger, ServiceCompute } from '@oceanprotocol/lib' import { Logger, ServiceCompute } from '@oceanprotocol/lib'
import { MetadataAlgorithm } from '@oceanprotocol/lib/dist/node/ddo/interfaces/MetadataAlgorithm' import { MetadataAlgorithm } from '@oceanprotocol/lib/dist/node/ddo/interfaces/MetadataAlgorithm'
import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/ComputeJob' import { ComputeJob } from '@oceanprotocol/lib/dist/node/ocean/interfaces/ComputeJob'
import { computeFeedback } from 'utils'
interface UseCompute { interface UseCompute {
compute: ( compute: (
@ -20,12 +21,6 @@ interface UseCompute {
isLoading: boolean 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 = { const rawAlgorithmMeta: MetadataAlgorithm = {
rawcode: `console.log('Hello world'!)`, rawcode: `console.log('Hello world'!)`,
format: 'docker-image', format: 'docker-image',
@ -38,7 +33,7 @@ const rawAlgorithmMeta: MetadataAlgorithm = {
} }
function useCompute(): UseCompute { function useCompute(): UseCompute {
const { ocean, account, accountId, config } = useOcean() const { ocean, account, accountId } = useOcean()
const [computeStep, setComputeStep] = useState<number | undefined>() const [computeStep, setComputeStep] = useState<number | undefined>()
const [computeStepText, setComputeStepText] = useState<string | undefined>() const [computeStepText, setComputeStepText] = useState<string | undefined>()
const [computeError, setComputeError] = useState<string | undefined>() const [computeError, setComputeError] = useState<string | undefined>()

View File

@ -1,6 +1,6 @@
import { useState } from 'react' import { useState } from 'react'
import { useOcean } from 'providers' import { useOcean } from 'providers'
import { feedback } from 'utils' import { consumeFeedback } from 'utils'
import { DID, Logger, ServiceType } from '@oceanprotocol/lib' import { DID, Logger, ServiceType } from '@oceanprotocol/lib'
interface UseConsume { interface UseConsume {
@ -16,14 +16,6 @@ interface UseConsume {
isLoading: boolean 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 { function useConsume(): UseConsume {
const { ocean, account, accountId } = useOcean() const { ocean, account, accountId } = useOcean()
const [isLoading, setIsLoading] = useState(false) const [isLoading, setIsLoading] = useState(false)

View File

@ -8,8 +8,7 @@ import {
MetadataCache MetadataCache
} from '@oceanprotocol/lib' } from '@oceanprotocol/lib'
import { useOcean } from 'providers' import { useOcean } from 'providers'
import { getBestDataTokenPrice } from 'utils/dtUtils' import { isDDO, getBestDataTokenPrice } from 'utils'
import { isDDO } from 'utils'
import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper' import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper'
interface UseMetadata { interface UseMetadata {

View File

@ -3,15 +3,16 @@ import { useEffect, useState } from 'react'
import { useOcean } from 'providers' import { useOcean } from 'providers'
import { PriceOptions } from './PriceOptions' import { PriceOptions } from './PriceOptions'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt } from 'web3-core'
import { getBestDataTokenPrice, getFirstPool } from 'utils/dtUtils'
import { Decimal } from 'decimal.js' import { Decimal } from 'decimal.js'
import { import {
getBestDataTokenPrice,
getFirstPool,
getCreatePricingPoolFeedback, getCreatePricingPoolFeedback,
getCreatePricingExchangeFeedback, getCreatePricingExchangeFeedback,
getBuyDTFeedback, getBuyDTFeedback,
getSellDTFeedback getSellDTFeedback,
} from './utils' sleep
import { sleep } from 'utils' } from 'utils'
interface UsePricing { interface UsePricing {
dtSymbol?: string dtSymbol?: string

View File

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

View File

@ -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( export function getCreatePricingPoolFeedback(
dtSymbol: string dtSymbol: string
): { [key: number]: string } { ): { [key: number]: string } {

30
src/utils/helpers.ts Normal file
View File

@ -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)
})
}

View File

@ -1,47 +1,4 @@
import { DDO, DID } from '@oceanprotocol/lib' export * from './dtUtils'
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 './web3' export * from './web3'
export * from './feedback'
export function sleep(ms: number): Promise<void> { export * from './helpers'
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}