mirror of
https://github.com/oceanprotocol/react.git
synced 2024-12-23 01:29:49 +01:00
utils consolidation
This commit is contained in:
parent
4ae3202f6c
commit
dd7e323dad
@ -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: (
|
||||
@ -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',
|
||||
@ -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>()
|
||||
|
@ -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 {
|
||||
@ -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)
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
@ -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,
|
||||
@ -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) {
|
||||
|
@ -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 } {
|
30
src/utils/helpers.ts
Normal file
30
src/utils/helpers.ts
Normal 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)
|
||||
})
|
||||
}
|
@ -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'
|
||||
|
Loading…
Reference in New Issue
Block a user