diff --git a/src/utils/dtUtils.ts b/src/utils/dtUtils.ts index c875f9b..9c8b638 100644 --- a/src/utils/dtUtils.ts +++ b/src/utils/dtUtils.ts @@ -86,11 +86,38 @@ export async function getCheapestExchange( } } +export async function getFirstPool( + ocean: Ocean, + dataTokenAddress: string +): Promise { + if (!ocean || !dataTokenAddress) return null + + const tokenPools = await ocean.pool.searchPoolforDT(dataTokenAddress) + + if (tokenPools === undefined || tokenPools.length === 0) { + return { + address: '', + price: 0 + } + } + const firstPoolAddress = tokenPools[0] + const firstPoolPrice = await ocean.pool.getOceanNeeded(firstPoolAddress, '1') + const oceanReserve = await ocean.pool.getOceanReserve(firstPoolAddress) + const dtReserve = await ocean.pool.getDTReserve(firstPoolAddress) + + return { + address: firstPoolAddress, + price: Number(firstPoolPrice), + ocean: Number(oceanReserve), + datatoken: Number(dtReserve) + } +} + export async function getBestDataTokenPrice( ocean: Ocean, dataTokenAddress: string ): Promise { - const cheapestPool = await getCheapestPool(ocean, dataTokenAddress) + const cheapestPool = await getFirstPool(ocean, dataTokenAddress) const cheapestExchange = await getCheapestExchange(ocean, dataTokenAddress) Decimal.set({ precision: 5 })