Skip to content

Commit

Permalink
feat: Use explorer api bnb prices hook (#9939)
Browse files Browse the repository at this point in the history
<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->


<!-- start pr-codex -->

---

## PR-Codex overview
The focus of this PR is to update the BNB price fetching mechanism in
the `useBnbPrices` hook.

### Detailed summary
- Updated BNB price fetching API from GraphQL to a new Explorer API
- Replaced `infoClient` with `explorerApiClient`
- Removed unused GraphQL queries and related types
- Refactored fetching logic to handle new API response structure

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Jun 11, 2024
1 parent 1cb2d45 commit 0d4025b
Showing 1 changed file with 39 additions and 52 deletions.
91 changes: 39 additions & 52 deletions apps/web/src/views/Info/hooks/useBnbPrices.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<PricesResponse>(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) {
Expand All @@ -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
}

0 comments on commit 0d4025b

Please sign in to comment.