diff --git a/package-lock.json b/package-lock.json index 7873199..20bc39a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1521,9 +1521,9 @@ "integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg==" }, "@oceanprotocol/lib": { - "version": "0.9.7", - "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.7.tgz", - "integrity": "sha512-C47Bv1d9IEvWkI2cOAYUE115jc6iN87ddDdl/BTCAiHKVllhl3TvoSMD+kdwfTivnsiW6HytpqL56IGtA/F4zQ==", + "version": "0.9.8", + "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.8.tgz", + "integrity": "sha512-jV7lobTXskqqmv23Vl3iQ4SZk+0FVFxLSVkXniOKOOFP0pJ0vllSNnM+wHkG4inF3HWHHyeh8AyFNTSxOUuirA==", "requires": { "@ethereum-navigator/navigator": "^0.5.0", "@oceanprotocol/contracts": "^0.5.7", diff --git a/src/hooks/useMetadata/useMetadata.ts b/src/hooks/useMetadata/useMetadata.ts index 0dc2113..c70117d 100644 --- a/src/hooks/useMetadata/useMetadata.ts +++ b/src/hooks/useMetadata/useMetadata.ts @@ -8,7 +8,7 @@ import { MetadataCache } from '@oceanprotocol/lib' import { useOcean } from 'providers' -import { isDDO, getBestDataTokenPrice } from 'utils' +import { isDDO, getBestDataTokenPrice, getDataTokenPrice } from 'utils' import { ConfigHelperConfig } from '@oceanprotocol/lib/dist/node/utils/ConfigHelper' interface UseMetadata { @@ -18,7 +18,6 @@ interface UseMetadata { title: string | undefined price: BestPrice | undefined isLoaded: boolean - getPrice: (dataTokenAddress: string) => Promise refreshPrice: () => void } @@ -42,20 +41,24 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata { [config.metadataCacheUri] ) - const getPrice = useCallback( - async ( - dataTokenAddress: string, - poolAddress?: string - ): Promise => { - const price = await getBestDataTokenPrice( - ocean, - dataTokenAddress, - poolAddress - ) - return price - }, - [ocean] - ) + const getPrice = useCallback(async (): Promise => { + if (!internalDdo) + return { + type: '', + address: '', + value: 0, + ocean: 0, + datatoken: 0 + } as BestPrice + + const price = await getDataTokenPrice( + ocean, + internalDdo.dataToken, + internalDdo?.price?.type, + internalDdo.price.pools[0] + ) + return price + }, [ocean, internalDdo]) const getMetadata = useCallback(async (ddo: DDO): Promise => { const metadata = ddo.findServiceByType('metadata') @@ -83,11 +86,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata { init() }, [asset, getDDO]) async function refreshPrice(): Promise { - if (!internalDdo) return - const livePrice = await getPrice( - internalDdo.dataToken, - internalDdo.price.pools[0] - ) + const livePrice = await getPrice() setPrice(livePrice) } // @@ -113,10 +112,7 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata { return // Set price again, but from chain - const priceLive = await getPrice( - internalDdo.dataToken, - internalDdo.price.pools[0] - ) + const priceLive = await getPrice() priceLive && internalDdo.price !== priceLive && setPrice(priceLive) } init() @@ -145,7 +141,6 @@ function useMetadata(asset?: DID | string | DDO): UseMetadata { title, price, isLoaded, - getPrice, refreshPrice } } diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index 591d6da..873dee9 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -107,12 +107,19 @@ export async function getFirstPool( } } - const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') + let firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress) const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress) + if (firstPoolPrice) { + const priceChars = firstPoolPrice.split('.') + const numberOfCharsOfPrice = priceChars[0].length + + if (numberOfCharsOfPrice > 8) firstPoolPrice = '0' + } + return { address: firstPoolAddress, price: Number(firstPoolPrice), @@ -121,6 +128,44 @@ export async function getFirstPool( } } +export async function getDataTokenPrice( + ocean: Ocean, + dataTokenAddress: string, + type?: string, + poolAddress?: string +) { + switch (type) { + case 'pool': { + const cheapestPool = await getFirstPool( + ocean, + dataTokenAddress, + poolAddress + ) + return { + type: 'pool', + address: cheapestPool?.address, + value: cheapestPool?.price, + ocean: cheapestPool?.ocean, + datatoken: cheapestPool?.datatoken + } as BestPrice + } + case 'exchange': { + const cheapestExchange = await getCheapestExchange( + ocean, + dataTokenAddress + ) + return { + type: 'exchange', + address: cheapestExchange?.address, + value: Number(cheapestExchange?.price) + } as BestPrice + } + default: { + return await getBestDataTokenPrice(ocean, dataTokenAddress, poolAddress) + } + } +} + export async function getBestDataTokenPrice( ocean: Ocean, dataTokenAddress: string,