From ef1dd3783707037fb8644bfd33f53b001c8299bf Mon Sep 17 00:00:00 2001 From: mihaisc Date: Thu, 20 Aug 2020 12:23:09 +0300 Subject: [PATCH] added pool, refactor --- example/src/MetadataExample.tsx | 4 ++-- src/hooks/useMetadata/Pool.ts | 4 ++++ src/hooks/useMetadata/index.ts | 1 + src/hooks/useMetadata/useMetadata.ts | 36 +++++++++++++++------------- src/utils/dtUtils.ts | 14 ++++++----- 5 files changed, 34 insertions(+), 25 deletions(-) create mode 100644 src/hooks/useMetadata/Pool.ts diff --git a/example/src/MetadataExample.tsx b/example/src/MetadataExample.tsx index 0b4adbc..b270083 100644 --- a/example/src/MetadataExample.tsx +++ b/example/src/MetadataExample.tsx @@ -2,12 +2,12 @@ import React, { useEffect, useState } from 'react' import { useMetadata } from '@oceanprotocol/react' export function MetadataExample({ did }: { did: string }) { - const { title, bestPrice } = useMetadata(did) + const { title, pool } = useMetadata(did) return ( <>
- {title} - {bestPrice} + {title} - {pool && pool.price + ' - ' + pool.address}
) diff --git a/src/hooks/useMetadata/Pool.ts b/src/hooks/useMetadata/Pool.ts new file mode 100644 index 0000000..e17afe0 --- /dev/null +++ b/src/hooks/useMetadata/Pool.ts @@ -0,0 +1,4 @@ +export default interface Pool { + address: string + price: string +} diff --git a/src/hooks/useMetadata/index.ts b/src/hooks/useMetadata/index.ts index 9c67533..d513773 100644 --- a/src/hooks/useMetadata/index.ts +++ b/src/hooks/useMetadata/index.ts @@ -1 +1,2 @@ export * from './useMetadata' +export * from './Pool' diff --git a/src/hooks/useMetadata/useMetadata.ts b/src/hooks/useMetadata/useMetadata.ts index 71ae5ad..d93d73a 100644 --- a/src/hooks/useMetadata/useMetadata.ts +++ b/src/hooks/useMetadata/useMetadata.ts @@ -3,18 +3,18 @@ import { DID, DDO, Metadata, Logger } from '@oceanprotocol/lib' import { useOcean } from '../../providers' import ProviderStatus from '../../providers/OceanProvider/ProviderStatus' import { getBestDataTokenPrice, getCheapestPool } from '../../utils/dtUtils' +import Pool from './Pool' interface UseMetadata { ddo: DDO did: DID | string metadata: Metadata title: string - bestPrice: string + price: string + pool: Pool isLoaded: boolean - getBestPrice: (dataTokenAddress?: string) => Promise - getBestPool: ( - dataTokenAddress?: string - ) => Promise<{ poolAddress: string; poolPrice: string }> + getPrice: (dataTokenAddress?: string) => Promise + getPool: (dataTokenAddress?: string) => Promise } function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata { @@ -24,7 +24,8 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata { const [metadata, setMetadata] = useState() const [title, setTitle] = useState() const [isLoaded, setIsLoaded] = useState(false) - const [bestPrice, setBestPrice] = useState() + const [price, setPrice] = useState() + const [pool, setPool] = useState() async function getDDO(did: DID | string): Promise { if (status === ProviderStatus.CONNECTED) { @@ -33,13 +34,11 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata { } } - async function getBestPrice(dataTokenAddress?: string): Promise { + async function getPrice(dataTokenAddress?: string): Promise { if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken return await getBestDataTokenPrice(ocean, accountId, dataTokenAddress) } - async function getBestPool( - dataTokenAddress: string - ): Promise<{ poolAddress: string; poolPrice: string }> { + async function getPool(dataTokenAddress?: string): Promise { if (!dataTokenAddress) dataTokenAddress = internalDdo.dataToken return await getCheapestPool(ocean, accountId, dataTokenAddress) } @@ -81,16 +80,18 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata { const metadata = await getMetadata() setMetadata(metadata) setTitle(metadata.main.name) - const price = await getBestPrice() - setBestPrice(price) + const pool = await getPool() + setPool(pool) + setPrice(pool.price) setIsLoaded(true) } } init() const interval = setInterval(async () => { - const price = await getBestPrice() - setBestPrice(price) + const pool = await getPool() + setPool(pool) + setPrice(pool.price) }, 10000) return () => clearInterval(interval) }, [internalDdo]) @@ -100,10 +101,11 @@ function useMetadata(did?: DID | string, ddo?: DDO): UseMetadata { did: internalDid, metadata, title, - bestPrice, + price, + pool, isLoaded, - getBestPrice, - getBestPool + getPrice, + getPool } } diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index 912016f..12fb126 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -1,10 +1,12 @@ import { Logger, Ocean, Account } from '@oceanprotocol/lib' import { Decimal } from 'decimal.js' +import Pool from '../hooks/useMetadata/Pool' + export async function getCheapestPool( ocean: Ocean, accountId: string, dataTokenAddress: string -): Promise<{ poolAddress: string; poolPrice: string }> { +): Promise { if (!ocean || !accountId || !dataTokenAddress) return const tokenPools = await ocean.pool.searchPoolforDT( @@ -14,8 +16,8 @@ export async function getCheapestPool( Logger.log('DT Pool found', tokenPools) if (tokenPools === undefined || tokenPools.length === 0) { return { - poolAddress: '', - poolPrice: '' + address: '', + price: '' } } let cheapestPoolAddress @@ -38,8 +40,8 @@ export async function getCheapestPool( } return { - poolAddress: cheapestPoolAddress, - poolPrice: cheapestPoolPrice.toString() + address: cheapestPoolAddress, + price: cheapestPoolPrice.toString() } } @@ -50,7 +52,7 @@ export async function getBestDataTokenPrice( ): Promise { const bestPool = await getCheapestPool(ocean, accountId, dataTokenAddress) - return bestPool.poolPrice + return bestPool.price } export async function checkAndBuyDT(