concurrent price endpoint added
Signed-off-by: Uncle Fatso <uncle.fatso@ghostchain.io>
This commit is contained in:
parent
d666a6f559
commit
c41da3195d
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "ghost-dao-interface",
|
||||
"private": true,
|
||||
"version": "0.3.1",
|
||||
"version": "0.3.2",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
|
||||
@ -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",
|
||||
],
|
||||
}
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user