From c41da3195d6db55713aaea321df57346b07b736e Mon Sep 17 00:00:00 2001 From: Uncle Fatso Date: Mon, 3 Nov 2025 13:41:15 +0300 Subject: [PATCH] concurrent price endpoint added Signed-off-by: Uncle Fatso --- package.json | 2 +- src/constants/addresses.js | 5 ++++- src/hooks/prices/index.js | 28 +++++++++++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index bb9f55d..1246b60 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ghost-dao-interface", "private": true, - "version": "0.3.1", + "version": "0.3.2", "type": "module", "scripts": { "dev": "vite", diff --git a/src/constants/addresses.js b/src/constants/addresses.js index 4d836dc..aba233c 100644 --- a/src/constants/addresses.js +++ b/src/constants/addresses.js @@ -96,5 +96,8 @@ export const UNISWAP_V2_FACTORY = { }; export const CEX_TICKERS = { - [NetworkId.TESTNET_MORDOR]: "ETCUSDT", + [NetworkId.TESTNET_MORDOR]: [ + "https://api.binance.com/api/v3/ticker/price?symbol=ETCUSDT", + "https://api.coinbase.com/v2/prices/ETC-USDT/spot", + ], } diff --git a/src/hooks/prices/index.js b/src/hooks/prices/index.js index 19f476d..a3d54a7 100644 --- a/src/hooks/prices/index.js +++ b/src/hooks/prices/index.js @@ -43,10 +43,10 @@ function callWithCacheTTL(fn, ttlMs = 5000) { export const useDaiPrice = (chainId) => { const [daiPrice, setDaiPrice] = useState(new DecimalBigNumber(1000000000000000000n, 18)); - const cexTicker = CEX_TICKERS[chainId]; + const cexApis = CEX_TICKERS[chainId]; useEffect(() => { - if (!cexTicker) { + if (!cexApis) { setDaiPrice(new DecimalBigNumber(1000000000000000000n, 18)); return; } @@ -54,25 +54,35 @@ export const useDaiPrice = (chainId) => { let getCexPriceCached = cexPriceGetters.get(chainId); if (!getCexPriceCached) { getCexPriceCached = callWithCacheTTL(() => { - return fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cexTicker}`) + const fetchPromises = cexApis.map(url => fetch(url) .then(res => { if (!res.ok) throw new Error(`HTTP ${res.status}`); return res.json(); - }); + }) + ) + return Promise.any(fetchPromises); }, 5000); cexPriceGetters.set(chainId, getCexPriceCached); } getCexPriceCached() - .then(price => { - const coinPrice = Number(price?.price ?? 0.0); - const priceInWei = Math.floor(coinPrice * 1e18) - setDaiPrice(new DecimalBigNumber(BigInt(priceInWei), 18)); + .then(response => { + if ('data' in response) { + const coinPrice = Number(response?.data?.amount ?? 0.0); + const priceInWei = Math.floor(coinPrice * 1e18); + setDaiPrice(new DecimalBigNumber(BigInt(priceInWei), 18)); + } else if ('price' in response) { + const coinPrice = Number(response?.price ?? 0.0); + const priceInWei = Math.floor(coinPrice * 1e18) + setDaiPrice(new DecimalBigNumber(BigInt(priceInWei), 18)); + } else { + throw Error("Unexpected json in response."); + } }) .catch(error => { setDaiPrice(new DecimalBigNumber(0n, 18)); }); - }, [chainId, cexTicker]) + }, [chainId, cexApis]) return daiPrice; };