diff --git a/package-lock.json b/package-lock.json index 059e763..c648ee2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1521,9 +1521,9 @@ "integrity": "sha512-p0oOHXr60hXZuLNsQ/PsOQtCfia79thm7MjPxTrnnBvD+csJoHzARYMB0IFj/KTw6U5vLXODgjJAn8x6QksLwg==" }, "@oceanprotocol/lib": { - "version": "0.9.16", - "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.16.tgz", - "integrity": "sha512-7uHnVaykabT3cEPUGRq3ITpiw9+mhS29Pq6rDTi5mtW4dD8DX7jdA7ddkxyHsZe/uQY9I4FLEuCmeTPcaFXPJw==", + "version": "0.9.18", + "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.9.18.tgz", + "integrity": "sha512-RsP4CjAnauI2kDH0923LOO3NhdKNB1y8WwpAviVwIwz9sYqsIIcac6MKXIm5oDeLNhmCIhJXbwvQehf17wRL5Q==", "requires": { "@ethereum-navigator/navigator": "^0.5.0", "@oceanprotocol/contracts": "^0.5.7", @@ -5011,9 +5011,9 @@ } }, "file-saver": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.2.tgz", - "integrity": "sha512-Wz3c3XQ5xroCxd1G8b7yL0Ehkf0TC9oYC6buPFkNnU9EnaPlifeAFCyCh+iewXTyFRcg0a6j3J7FmJsIhlhBdw==" + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/file-saver/-/file-saver-2.0.5.tgz", + "integrity": "sha512-P9bmyZ3h/PRG+Nzga+rbdI4OEpNDzAVyy74uVO9ATgzLK6VtAsYybF/+TOCvrc0MO793d6+42lLyZTw7/ArVzA==" }, "filesize": { "version": "6.1.0", diff --git a/package.json b/package.json index fa2bafc..0ddd385 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "dist/" ], "dependencies": { - "@oceanprotocol/lib": "^0.9.14", + "@oceanprotocol/lib": "^0.9.18", "axios": "^0.21.0", "decimal.js": "^10.2.1", "web3": "^1.3.0", diff --git a/src/hooks/usePricing/usePricing.ts b/src/hooks/usePricing/usePricing.ts index 0fc0586..7cd5ec1 100644 --- a/src/hooks/usePricing/usePricing.ts +++ b/src/hooks/usePricing/usePricing.ts @@ -6,7 +6,7 @@ import { TransactionReceipt } from 'web3-core' import { Decimal } from 'decimal.js' import { getBestDataTokenPrice, - getFirstPool, + getFirstPoolPrice, getCreatePricingPoolFeedback, getCreatePricingExchangeFeedback, getBuyDTFeedback, @@ -185,9 +185,9 @@ function usePricing(ddo: DDO): UsePricing { setPricingIsLoading(true) setPricingError(undefined) setStep(1, 'sell') - const pool = await getFirstPool(ocean, dataToken) - if (!pool || pool.price === 0) return - const price = new Decimal(pool.price).times(0.95).toString() + const pool = await getFirstPoolPrice(ocean, dataToken) + if (!pool || pool.value === 0) return + const price = new Decimal(pool.value).times(0.95).toString() setStep(2, 'sell') Logger.log('Selling token to pool', pool, accountId, price) const tx = await ocean.pool.sellDT( diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index e6b23ee..e00db3b 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -1,17 +1,20 @@ import { Ocean, BestPrice, Logger } from '@oceanprotocol/lib' import { Decimal } from 'decimal.js' -import Pool from 'hooks/useMetadata/Pool' -export async function getCheapestPool( +export async function getCheapestPoolPrice( ocean: Ocean, dataTokenAddress: string -): Promise { +): Promise { const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) if (tokenPools === undefined || tokenPools.length === 0) { return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } let cheapestPoolAddress = tokenPools[0] @@ -19,7 +22,12 @@ export async function getCheapestPool( if (tokenPools) { for (let i = 0; i < tokenPools.length; i++) { - const poolPrice = await ocean.pool.getOceanNeeded(tokenPools[i], '1') + const poolPrice = await ocean.pool.calcInGivenOut( + tokenPools[i], + dataTokenAddress, + ocean.pool.oceanAddress, + '1' + ) const decimalPoolPrice = new Decimal(poolPrice) if (decimalPoolPrice < cheapestPoolPrice) { @@ -29,21 +37,25 @@ export async function getCheapestPool( } } + const usePrice = await ocean.pool.getOceanNeeded(cheapestPoolAddress, '1') const oceanReserve = await ocean.pool.getOceanReserve(cheapestPoolAddress) const dtReserve = await ocean.pool.getDTReserve(cheapestPoolAddress) return { + type: 'pool', + pools: [cheapestPoolAddress], address: cheapestPoolAddress, - price: Number(cheapestPoolPrice), + value: Number(cheapestPoolPrice), ocean: Number(oceanReserve), - datatoken: Number(dtReserve) + datatoken: Number(dtReserve), + isConsumable: Number(usePrice) > 0 ? 'true' : 'false' } } -export async function getCheapestExchange( +export async function getCheapestExchangePrice( ocean: Ocean, dataTokenAddress: string -): Promise { +): Promise { try { const tokenExchanges = await ocean.fixedRateExchange.searchforDT( dataTokenAddress, @@ -51,8 +63,12 @@ export async function getCheapestExchange( ) if (tokenExchanges === undefined || tokenExchanges.length === 0) { return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } let cheapestExchangeAddress = tokenExchanges[0].exchangeID @@ -67,20 +83,35 @@ export async function getCheapestExchange( } } + const dtReserve = cheapestExchangeAddress + ? await ocean.fixedRateExchange.getSupply(cheapestExchangeAddress) + : '0' return { + type: 'pool', + pools: [], address: cheapestExchangeAddress || '', - price: Number(cheapestExchangePrice) + value: Number(cheapestExchangePrice), + ocean: 0, + datatoken: Number(dtReserve), + isConsumable: Number(dtReserve) > 0 ? 'true' : 'false' } } catch (err) { Logger.log(err) return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } } -export async function getFirstExchange(ocean: Ocean, dataTokenAddress: string) { +export async function getFirstExchangePrice( + ocean: Ocean, + dataTokenAddress: string +): Promise { try { const tokenExchanges = await ocean.fixedRateExchange.searchforDT( dataTokenAddress, @@ -88,103 +119,116 @@ export async function getFirstExchange(ocean: Ocean, dataTokenAddress: string) { ) if (tokenExchanges === undefined || tokenExchanges.length === 0) { return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } + const [tokenExchange] = tokenExchanges return { + type: 'pool', + pools: [], address: tokenExchange.exchangeID || '', - price: Number(tokenExchange.fixedRate) + value: Number(tokenExchange.fixedRate), + ocean: 0, + datatoken: Number(tokenExchange.supply), + isConsumable: Number(tokenExchange.supply) > 0 ? 'true' : 'false' } } catch (err) { Logger.log(err) return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } } -export async function getFirstPool( +export async function getFirstPoolPrice( ocean: Ocean, dataTokenAddress: string, poolAddress?: string -): Promise { +): Promise { let firstPoolAddress = poolAddress if (!poolAddress) { const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) if (tokenPools === undefined || tokenPools.length === 0) { return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } ;[firstPoolAddress] = tokenPools } if (!firstPoolAddress) { return { + type: '', address: '', - price: 0 + pools: [], + datatoken: 0, + value: 0, + isConsumable: '' } } - let firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') - + let firstPoolPrice = await ocean.pool.calcInGivenOut( + firstPoolAddress, + dataTokenAddress, + ocean.pool.oceanAddress, + '1' + ) + const usePrice = 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 { + type: 'pool', + pools: [firstPoolAddress], address: firstPoolAddress, - price: Number(firstPoolPrice), + value: Number(firstPoolPrice), ocean: Number(oceanReserve), - datatoken: Number(dtReserve) + datatoken: Number(dtReserve), + isConsumable: Number(usePrice) > 0 ? 'true' : 'false' } } export async function getBestDataTokenPrice( ocean: Ocean, - dataTokenAddress: string, - poolAddress?: string + dataTokenAddress: string ): Promise { - const cheapestPool = await getFirstPool(ocean, dataTokenAddress, poolAddress) - const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress) + const cheapestPool = await getCheapestPoolPrice(ocean, dataTokenAddress) + const cheapestExchange = await getCheapestExchangePrice( + ocean, + dataTokenAddress + ) Decimal.set({ precision: 5 }) const cheapestPoolPrice = new Decimal( - cheapestPool && cheapestPool.price !== 0 ? cheapestPool.price : 999999999999 + cheapestPool && cheapestPool.value !== 0 ? cheapestPool.value : 999999999999 ) const cheapestExchangePrice = new Decimal( - cheapestExchange && cheapestExchange?.price !== 0 - ? cheapestExchange.price + cheapestExchange && cheapestExchange?.value !== 0 + ? cheapestExchange.value : 999999999999 ) - if (cheapestPoolPrice < cheapestExchangePrice) { - return { - type: 'pool', - address: cheapestPool?.address, - value: cheapestPool?.price, - ocean: cheapestPool?.ocean, - datatoken: cheapestPool?.datatoken - } as BestPrice - } else { - return { - type: 'exchange', - address: cheapestExchange?.address, - value: Number(cheapestExchange?.price) - } as BestPrice - } + return cheapestPoolPrice < cheapestExchangePrice + ? cheapestPool + : cheapestExchange } export async function getDataTokenPrice( @@ -192,32 +236,16 @@ export async function getDataTokenPrice( dataTokenAddress: string, type?: string, poolAddress?: string -) { +): Promise { 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 + return await getFirstPoolPrice(ocean, dataTokenAddress, poolAddress) } case 'exchange': { - const cheapestExchange = await getFirstExchange(ocean, dataTokenAddress) - return { - type: 'exchange', - address: cheapestExchange?.address, - value: Number(cheapestExchange?.price) - } as BestPrice + return await getFirstExchangePrice(ocean, dataTokenAddress) } default: { - return await getBestDataTokenPrice(ocean, dataTokenAddress, poolAddress) + return await getBestDataTokenPrice(ocean, dataTokenAddress) } } }