Skip to content

Commit

Permalink
Merge pull request #1174 from mars-protocol/fallback-pyth-api
Browse files Browse the repository at this point in the history
  • Loading branch information
linkielink authored Oct 13, 2024
2 parents 48050cc + d7e3bdf commit 3da6343
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/api/prices/getPythPrices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,28 @@ import { BN } from 'utils/helpers'

export default async function fetchPythPrices(priceFeedIds: string[], assets: Asset[]) {
const pricesUrl = new URL(`${pythEndpoints.api}/latest_price_feeds`)
const fallbackUrl = new URL(`${pythEndpoints.fallbackApi}/latest_price_feeds`)

try {
priceFeedIds.forEach((id) => pricesUrl.searchParams.append('ids[]', id))
priceFeedIds.forEach((id) => {
pricesUrl.searchParams.append('ids[]', id)
fallbackUrl.searchParams.append('ids[]', id)
})

const pythResponse: PythPriceData[] = await cacheFn(
() => fetchWithTimeout(pricesUrl.toString(), FETCH_TIMEOUT).then((res) => res.json()),
async () => {
try {
return await fetchWithTimeout(pricesUrl.toString(), FETCH_TIMEOUT).then((res) =>
res.json(),
)
} catch (error) {
console.warn('Primary Pyth API failed, falling back to fallback API', error)

return await fetchWithTimeout(fallbackUrl.toString(), FETCH_TIMEOUT).then((res) =>
res.json(),
)
}
},
pythPriceCache,
`pythPrices/${priceFeedIds.flat().join('-')}`,
30,
Expand Down
1 change: 1 addition & 0 deletions src/constants/pyth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const pythEndpoints = {
api: process.env.NEXT_PUBLIC_PYTH_API ?? 'https://hermes.pyth.network/api',
fallbackApi: 'https://hermes.pyth.network/api',
candles: 'https://benchmarks.pyth.network/v1/shims/tradingview',
}

0 comments on commit 3da6343

Please sign in to comment.