From 2d6de296eb844007f8e633d43240615b20a059ea Mon Sep 17 00:00:00 2001 From: Kirill Klimenko Date: Mon, 4 Mar 2024 17:58:25 +0100 Subject: [PATCH] Refactor and cleanup of storing/getting prices --- netlify/functions/tokensPrices.mts | 16 ++++++---------- .../pages/DAOTreasury/hooks/useFormatCoins.tsx | 14 +++++++------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/netlify/functions/tokensPrices.mts b/netlify/functions/tokensPrices.mts index d0980462f4..c40d2f7d6a 100644 --- a/netlify/functions/tokensPrices.mts +++ b/netlify/functions/tokensPrices.mts @@ -17,15 +17,11 @@ export default async function getTokenprices(request: Request) { const cachedPrices = await Promise.all( tokens.map(tokenAddress => store.getWithMetadata(tokenAddress, { type: 'json' })) ); - const expiredPrices: string[] = []; const cachedUnexpiredPrices = cachedPrices - .filter(tokenPrice => { - if (tokenPrice && (tokenPrice?.metadata as any as TokenPriceMetadata).expiration <= now) { - expiredPrices.push(tokenPrice.data.tokenAddress); - return false; - } - return tokenPrice; - }) + .filter( + tokenPrice => + tokenPrice && (tokenPrice?.metadata as any as TokenPriceMetadata).expiration <= now + ) .map(tokenPrice => ({ tokenAddress: tokenPrice?.data.tokenAddress, price: tokenPrice?.data.price, @@ -54,7 +50,7 @@ export default async function getTokenprices(request: Request) { const tokenPricesResponseJson = await tokenPricesResponse.json(); const tokenPriceMetadata = { metadata: { expiration: now + 1000 * 60 * 30 } }; Object.keys(tokenPricesResponseJson).forEach(tokenAddress => { - const price = tokenPricesResponseJson[tokenAddress]; + const price = tokenPricesResponseJson[tokenAddress].usd; responseBody[tokenAddress] = price; store.setJSON(tokenAddress, { tokenAddress, price }, tokenPriceMetadata); }); @@ -69,7 +65,7 @@ export default async function getTokenprices(request: Request) { const ethPriceResponseJson = await ethPriceResponse.json(); store.setJSON( 'ethereum', - { tokenAddress: 'ethereum', price: ethPriceResponseJson.ethereum }, + { tokenAddress: 'ethereum', price: ethPriceResponseJson.ethereum.usd }, tokenPriceMetadata ); responseBody.ethereum = ethPriceResponseJson.ethereum; diff --git a/src/components/pages/DAOTreasury/hooks/useFormatCoins.tsx b/src/components/pages/DAOTreasury/hooks/useFormatCoins.tsx index df02f84c12..d05060cd19 100644 --- a/src/components/pages/DAOTreasury/hooks/useFormatCoins.tsx +++ b/src/components/pages/DAOTreasury/hooks/useFormatCoins.tsx @@ -1,7 +1,7 @@ import { SafeBalanceUsdResponse } from '@safe-global/safe-service-client'; import { BigNumber, ethers } from 'ethers'; import { useEffect, useState } from 'react'; -import useCoinGeckoAPI from '../../../../providers/App/hooks/usePriceAPI'; +import usePriceAPI from '../../../../providers/App/hooks/usePriceAPI'; import { useNetworkConfig } from '../../../../providers/NetworkConfig/NetworkConfigProvider'; import { formatCoin, formatUSD } from '../../../../utils/numberFormats'; @@ -21,7 +21,7 @@ export function useFormatCoins(assets: SafeBalanceUsdResponse[]) { const { nativeTokenSymbol, nativeTokenIcon } = useNetworkConfig(); const [totalFiatValue, setTotalFiatValue] = useState(0); const [displayData, setDisplayData] = useState([]); - const { getTokenPrices } = useCoinGeckoAPI(); + const { getTokenPrices } = usePriceAPI(); useEffect(() => { async function loadDisplayData() { @@ -32,17 +32,17 @@ export function useFormatCoins(assets: SafeBalanceUsdResponse[]) { let asset = assets[i]; if (asset.balance === '0') continue; const tokenPrice = asset.tokenAddress - ? tokenPrices[asset.tokenAddress.toLowerCase()]?.usd - : tokenPrices.ethereum?.usd; + ? tokenPrices[asset.tokenAddress.toLowerCase()] + : tokenPrices.ethereum; let tokenFiatBalance = 0; if (tokenPrice && asset.balance) { - const numerator = 1000000000; + const multiplicator = 1000000000; tokenFiatBalance = BigNumber.from(asset.balance) - .mul(Math.round(tokenPrice * numerator)) // We'll be loosing precision with super small prices like for meme coins. But that shouldn't be awfully off - maybe up to 1% + .mul(Math.round(tokenPrice * multiplicator)) // We'll be loosing precision with super small prices like for meme coins. But that shouldn't be awfully off - maybe up to 1% .div(BigNumber.from(10).pow(asset.token?.decimals || 18)) - .toNumber() / numerator; + .toNumber() / multiplicator; newTotalFiatValue += tokenFiatBalance; }