diff --git a/apps/web/src/views/Info/hooks/useBnbPrices.ts b/apps/web/src/views/Info/hooks/useBnbPrices.ts index 135283ff8de3d..abffdd21c610e 100644 --- a/apps/web/src/views/Info/hooks/useBnbPrices.ts +++ b/apps/web/src/views/Info/hooks/useBnbPrices.ts @@ -1,8 +1,9 @@ -import { gql } from 'graphql-request' import { useEffect, useState } from 'react' import { getDeltaTimestamps } from 'utils/getDeltaTimestamps' -import { infoClient } from 'utils/graphql' -import { useBlocksFromTimestamps } from 'views/Info/hooks/useBlocksFromTimestamps' +import { explorerApiClient } from 'state/info/api/client' +import { ChainId } from '@pancakeswap/chains' +import { WNATIVE } from '@pancakeswap/sdk' +import dayjs from 'dayjs' export interface BnbPrices { current: number @@ -11,56 +12,44 @@ export interface BnbPrices { week: number } -const BNB_PRICES = gql` - query prices($block24: Int!, $block48: Int!, $blockWeek: Int!) { - current: bundle(id: "1") { - bnbPrice - } - oneDay: bundle(id: "1", block: { number: $block24 }) { - bnbPrice - } - twoDay: bundle(id: "1", block: { number: $block48 }) { - bnbPrice - } - oneWeek: bundle(id: "1", block: { number: $blockWeek }) { - bnbPrice - } - } -` - -interface PricesResponse { - current: { - bnbPrice: string - } - oneDay: { - bnbPrice: string - } - twoDay: { - bnbPrice: string - } - oneWeek: { - bnbPrice: string - } -} - const fetchBnbPrices = async ( - block24: number, - block48: number, - blockWeek: number, + t24: number, + t48: number, + tWeek: number, ): Promise<{ bnbPrices: BnbPrices | undefined; error: boolean }> => { try { - const data = await infoClient.request(BNB_PRICES, { - block24, - block48, - blockWeek, - }) + const wNative = WNATIVE[ChainId.BSC] + const currentTime = dayjs().unix() + + const times = [currentTime, t24, t48, tWeek] + + const [currentPrice, t24Price, t48Price, tWeekPrice] = await Promise.all( + times.map((t) => + explorerApiClient + .GET('/cached/tokens/{chainName}/{address}/price', { + signal: null, + params: { + path: { + chainName: 'bsc', + address: wNative.address, + }, + query: { + timestamp: t, + protocols: ['v3', 'v2', 'stable'], + }, + }, + }) + .then((res) => res.data?.priceUSD), + ), + ) + return { error: false, bnbPrices: { - current: parseFloat(data.current?.bnbPrice ?? '0'), - oneDay: parseFloat(data.oneDay?.bnbPrice ?? '0'), - twoDay: parseFloat(data.twoDay?.bnbPrice ?? '0'), - week: parseFloat(data.oneWeek?.bnbPrice ?? '0'), + current: parseFloat(currentPrice ?? '0'), + oneDay: parseFloat(t24Price ?? '0'), + twoDay: parseFloat(t48Price ?? '0'), + week: parseFloat(tWeekPrice ?? '0'), }, } } catch (error) { @@ -80,22 +69,20 @@ export const useBnbPrices = (): BnbPrices | undefined => { const [error, setError] = useState(false) const [t24, t48, tWeek] = getDeltaTimestamps() - const { blocks, error: blockError } = useBlocksFromTimestamps([t24, t48, tWeek]) useEffect(() => { const fetch = async () => { - const [block24, block48, blockWeek] = blocks ?? [] - const { bnbPrices, error: fetchError } = await fetchBnbPrices(block24.number, block48.number, blockWeek.number) + const { bnbPrices, error: fetchError } = await fetchBnbPrices(t24, t48, tWeek) if (fetchError) { setError(true) } else { setPrices(bnbPrices) } } - if (!prices && !error && blocks && !blockError) { + if (!prices && !error) { fetch() } - }, [error, prices, blocks, blockError]) + }, [error, prices, t24, t48, tWeek]) return prices }