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",
|
"name": "ghost-dao-interface",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.3.1",
|
"version": "0.3.2",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite",
|
"dev": "vite",
|
||||||
|
|||||||
@ -96,5 +96,8 @@ export const UNISWAP_V2_FACTORY = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const CEX_TICKERS = {
|
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) => {
|
export const useDaiPrice = (chainId) => {
|
||||||
const [daiPrice, setDaiPrice] = useState(new DecimalBigNumber(1000000000000000000n, 18));
|
const [daiPrice, setDaiPrice] = useState(new DecimalBigNumber(1000000000000000000n, 18));
|
||||||
const cexTicker = CEX_TICKERS[chainId];
|
const cexApis = CEX_TICKERS[chainId];
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!cexTicker) {
|
if (!cexApis) {
|
||||||
setDaiPrice(new DecimalBigNumber(1000000000000000000n, 18));
|
setDaiPrice(new DecimalBigNumber(1000000000000000000n, 18));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -54,25 +54,35 @@ export const useDaiPrice = (chainId) => {
|
|||||||
let getCexPriceCached = cexPriceGetters.get(chainId);
|
let getCexPriceCached = cexPriceGetters.get(chainId);
|
||||||
if (!getCexPriceCached) {
|
if (!getCexPriceCached) {
|
||||||
getCexPriceCached = callWithCacheTTL(() => {
|
getCexPriceCached = callWithCacheTTL(() => {
|
||||||
return fetch(`https://api.binance.com/api/v3/ticker/price?symbol=${cexTicker}`)
|
const fetchPromises = cexApis.map(url => fetch(url)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||||
return res.json();
|
return res.json();
|
||||||
});
|
})
|
||||||
|
)
|
||||||
|
return Promise.any(fetchPromises);
|
||||||
}, 5000);
|
}, 5000);
|
||||||
cexPriceGetters.set(chainId, getCexPriceCached);
|
cexPriceGetters.set(chainId, getCexPriceCached);
|
||||||
}
|
}
|
||||||
|
|
||||||
getCexPriceCached()
|
getCexPriceCached()
|
||||||
.then(price => {
|
.then(response => {
|
||||||
const coinPrice = Number(price?.price ?? 0.0);
|
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)
|
const priceInWei = Math.floor(coinPrice * 1e18)
|
||||||
setDaiPrice(new DecimalBigNumber(BigInt(priceInWei), 18));
|
setDaiPrice(new DecimalBigNumber(BigInt(priceInWei), 18));
|
||||||
|
} else {
|
||||||
|
throw Error("Unexpected json in response.");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
setDaiPrice(new DecimalBigNumber(0n, 18));
|
setDaiPrice(new DecimalBigNumber(0n, 18));
|
||||||
});
|
});
|
||||||
}, [chainId, cexTicker])
|
}, [chainId, cexApis])
|
||||||
|
|
||||||
return daiPrice;
|
return daiPrice;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user