From e84b0e20b36ee22619aa2fe9e82259125cff0745 Mon Sep 17 00:00:00 2001 From: Noisekit <28145325+noisekit@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:32:13 +0800 Subject: [PATCH] Hotfixes (#388) * Account for missing data * Add missing enabled query check * Fix collateral prices --- liquidity/lib/useAccounts/useAccounts.ts | 3 +- .../useCollateralPriceUpdates.ts | 36 +++++++++---------- .../components/Pools/PoolCards/PoolCard.tsx | 22 +++++++----- 3 files changed, 32 insertions(+), 29 deletions(-) diff --git a/liquidity/lib/useAccounts/useAccounts.ts b/liquidity/lib/useAccounts/useAccounts.ts index c6dfd571a..97a42f845 100644 --- a/liquidity/lib/useAccounts/useAccounts.ts +++ b/liquidity/lib/useAccounts/useAccounts.ts @@ -16,8 +16,9 @@ export function useAccounts() { 'Accounts', { accountAddress: activeWallet?.address, AccountProxy: AccountProxy?.address }, ], + enabled: Boolean(AccountProxy && activeWallet?.address), queryFn: async function () { - if (!AccountProxy || !activeWallet?.address) throw new Error('Should be disabled'); + if (!(AccountProxy && activeWallet?.address)) throw new Error('Should be disabled'); const numberOfAccountTokens = await AccountProxy.balanceOf(activeWallet.address); diff --git a/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts b/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts index 3bd958c18..167526773 100644 --- a/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts +++ b/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts @@ -25,7 +25,11 @@ async function getPythFeedIds(network: Network) { return getAllPriceIdsEntries(extras).map(([_key, value]) => value); } -async function getPythFeedIdsFromCollateralList(collateralList: string[]) { +async function getPythFeedIdsFromCollateralList( + collateralList: { + symbol: string; + }[] +) { const extras = await Promise.all( networksOffline.map((network) => importExtras(network.id, network.preset)) ); @@ -56,13 +60,10 @@ async function getPythFeedIdsFromCollateralList(collateralList: string[]) { // Find the corresponding price feed id for each symbol return collateralList.map((collateral) => { - let symbol = collateral; - if (collateral === 'WETH') { - symbol = 'ETH'; - } + const symbol = collateral.symbol === 'WETH' ? 'ETH' : collateral.symbol; const id = deduped.find((x) => x.symbol?.toUpperCase() === symbol.toUpperCase()); return { - collateral, + ...collateral, priceId: id?.priceId, }; }); @@ -154,23 +155,20 @@ export const useOfflinePrices = (collaterals?: Collaterals[]) => { return returnData; } - const pythIds = await getPythFeedIdsFromCollateralList( - filteredCollaterals.map((collateral) => collateral.symbol) - ); - + const collateralsWithPriceId = await getPythFeedIdsFromCollateralList(filteredCollaterals); const prices = await priceService.getLatestPriceFeeds( - pythIds.map((x) => x.priceId) as string[] + collateralsWithPriceId.map((x) => x.priceId) as string[] ); - - prices?.forEach((item, index) => { + prices?.forEach((item) => { + const col = collateralsWithPriceId.find(({ priceId }) => priceId === `0x${item.id}`); const price = item.getPriceUnchecked(); - - returnData.push({ - symbol: filteredCollaterals[index].symbol, - price: parseUnits(price.price, 18 + price.expo), - }); + if (col) { + returnData.push({ + symbol: col.symbol, + price: parseUnits(price.price, 18 + price.expo), + }); + } }); - return returnData; }, refetchInterval: 60000, diff --git a/liquidity/ui/src/components/Pools/PoolCards/PoolCard.tsx b/liquidity/ui/src/components/Pools/PoolCards/PoolCard.tsx index de676261d..e9231e34b 100644 --- a/liquidity/ui/src/components/Pools/PoolCards/PoolCard.tsx +++ b/liquidity/ui/src/components/Pools/PoolCards/PoolCard.tsx @@ -77,7 +77,8 @@ export const PoolCard = ({ const { connect } = useWallet(); const vaultTVL = collateralTypes?.reduce((acc, type) => { - const price = wei(collateralPrices?.find((price) => price.symbol === type.symbol)?.price || 0); + const collateralWithPrice = collateralPrices?.find((price) => price.symbol === type.symbol); + const price = collateralWithPrice ? wei(collateralWithPrice.price) : ZEROWEI; const amount = wei(type.collateralDeposited, Number(type.decimals), true); const value = price.mul(amount); return acc.add(value); @@ -298,22 +299,25 @@ export const PoolCard = ({ ); }) .map((type, index) => { - const price = wei( - collateralPrices?.find( - (price) => price.symbol.toUpperCase() === type.symbol.toUpperCase() - )?.price + const collateralWithPrice = collateralPrices?.find( + (price) => price.symbol.toUpperCase() === type.symbol.toUpperCase() ); - + const price = collateralWithPrice ? wei(collateralWithPrice.price) : ZEROWEI; const collateralApr = apr.collateralAprs.find( - (apr) => apr.collateralType === type.tokenAddress.toLowerCase() - ); + (apr) => + `${apr.collateralType}`.toLowerCase() === `${type.tokenAddress}`.toLowerCase() + ) || { + apr28d: 0, + apr28dRewards: 0, + apr28dPnl: 0, + }; const { apr28d, apr28dRewards, apr28dPnl } = collateralApr; const onClick = async () => { try { if (!currentNetwork) { - connect(); + await connect(); return; }