Skip to content

Commit

Permalink
Refactor and cleanup of storing/getting prices
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrila committed Mar 4, 2024
1 parent e1f77e1 commit 2d6de29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
16 changes: 6 additions & 10 deletions netlify/functions/tokensPrices.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand All @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/components/pages/DAOTreasury/hooks/useFormatCoins.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -21,7 +21,7 @@ export function useFormatCoins(assets: SafeBalanceUsdResponse[]) {
const { nativeTokenSymbol, nativeTokenIcon } = useNetworkConfig();
const [totalFiatValue, setTotalFiatValue] = useState(0);
const [displayData, setDisplayData] = useState<TokenDisplayData[]>([]);
const { getTokenPrices } = useCoinGeckoAPI();
const { getTokenPrices } = usePriceAPI();

useEffect(() => {
async function loadDisplayData() {
Expand All @@ -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;
}

Expand Down

0 comments on commit 2d6de29

Please sign in to comment.