From e342f5608525fbd9347c0fbf9a2e5e586361f28a Mon Sep 17 00:00:00 2001 From: Majorfi Date: Thu, 5 Oct 2023 09:23:27 +0200 Subject: [PATCH] fix: deposited includes all chains --- apps/common/contexts/useWallet.tsx | 51 +++++++++---------- apps/common/contexts/useYearn.tsx | 14 +++-- apps/common/hooks/useMultichainBalances.ts | 4 +- apps/common/types/types.ts | 2 + apps/vaults/components/list/VaultsListRow.tsx | 10 ++-- 5 files changed, 41 insertions(+), 40 deletions(-) diff --git a/apps/common/contexts/useWallet.tsx b/apps/common/contexts/useWallet.tsx index 6e3906c13..9507461b0 100755 --- a/apps/common/contexts/useWallet.tsx +++ b/apps/common/contexts/useWallet.tsx @@ -1,5 +1,4 @@ import {createContext, memo, useCallback, useContext, useEffect, useMemo} from 'react'; -import {useChainId} from 'wagmi'; import { OPT_YVAGEUR_USDC_STAKING_CONTRACT, OPT_YVALETH_FRXETH_STAKING_CONTRACT, @@ -83,8 +82,10 @@ const defaultToken: TToken = { decimals: 18, chainID: 1, value: 0, + stakingValue: 0, price: toNormalizedBN(0), - balance: toNormalizedBN(0) + balance: toNormalizedBN(0), + stakingBalance: toNormalizedBN(0) }; const defaultProps = { @@ -104,7 +105,6 @@ const defaultProps = { ******************************************************************************/ const WalletContext = createContext(defaultProps); export const WalletContextApp = memo(function WalletContextApp({children}: {children: ReactElement}): ReactElement { - const chain = useChainId(); const {vaults, vaultsMigrations, vaultsRetired, isLoadingVaultList, prices} = useYearn(); const {onLoadStart, onLoadDone} = useUI(); @@ -118,12 +118,11 @@ export const WalletContextApp = memo(function WalletContextApp({children}: {chil const extraTokens: TUseBalancesTokens[] = []; extraTokens.push( ...[ - {chainID: 1, address: toAddress(ETH_TOKEN_ADDRESS)}, - {chainID: 10, address: toAddress(ETH_TOKEN_ADDRESS)}, - {chainID: 137, address: toAddress(ETH_TOKEN_ADDRESS)}, - {chainID: 250, address: toAddress(ETH_TOKEN_ADDRESS)}, - {chainID: 8453, address: toAddress(ETH_TOKEN_ADDRESS)}, - {chainID: 42161, address: toAddress(ETH_TOKEN_ADDRESS)}, + {chainID: 1, address: ETH_TOKEN_ADDRESS}, + {chainID: 10, address: ETH_TOKEN_ADDRESS}, + {chainID: 250, address: ETH_TOKEN_ADDRESS}, + {chainID: 8453, address: ETH_TOKEN_ADDRESS}, + {chainID: 42161, address: ETH_TOKEN_ADDRESS}, {chainID: 1, address: YCRV_TOKEN_ADDRESS}, {chainID: 1, address: LPYCRV_TOKEN_ADDRESS}, {chainID: 1, address: CRV_TOKEN_ADDRESS}, @@ -194,7 +193,7 @@ export const WalletContextApp = memo(function WalletContextApp({children}: {chil } }); return tokens; - }, [isLoadingVaultList, chain, vaults]); + }, [isLoadingVaultList, vaults]); //List all vaults with a possible migration const migratableTokens = useMemo((): TUseBalancesTokens[] => { @@ -233,9 +232,14 @@ export const WalletContextApp = memo(function WalletContextApp({children}: {chil const tokens = useMemo((): TChainTokens => { const _tokens = {...tokensRaw}; - for (const [token] of Object.entries(_tokens)) { - if (STACKING_TO_VAULT[token] && _tokens?.[10]?.[STACKING_TO_VAULT[token]]) { - _tokens[10][token].value = (_tokens[10][token].value || 0) + (_tokens[10][STACKING_TO_VAULT[token]].value || 0); + for (const [chainIDStr, perChain] of Object.entries(_tokens)) { + const chainID = Number(chainIDStr); + for (const [tokenAddress, tokenData] of Object.entries(perChain)) { + if (STACKING_TO_VAULT[tokenAddress] && _tokens?.[chainID]?.[STACKING_TO_VAULT[tokenAddress]]) { + console.warn(tokenAddress, tokenData); + _tokens[chainID][tokenAddress].stakingBalance = _tokens[chainID][STACKING_TO_VAULT[tokenAddress]].balance; + _tokens[chainID][tokenAddress].stakingValue = _tokens[chainID][STACKING_TO_VAULT[tokenAddress]].value; + } } } return _tokens; @@ -243,20 +247,15 @@ export const WalletContextApp = memo(function WalletContextApp({children}: {chil const cumulatedValueInVaults = useMemo((): number => { let cumulatedValueInVaults = 0; - for (const [chainIDStr, perChain] of Object.entries(tokens)) { - const chainID = Number(chainIDStr); - for (const [tokenAddress, balanceData] of Object.entries(perChain)) { - if (vaults?.[toAddress(tokenAddress)] && balanceData.value > 0) { - cumulatedValueInVaults += balanceData.value; - } else if (vaultsMigrations?.[toAddress(tokenAddress)] && balanceData.value > 0) { - cumulatedValueInVaults += balanceData.value; + for (const [, perChain] of Object.entries(tokens)) { + for (const [tokenAddress, tokenData] of Object.entries(perChain)) { + if (tokenData.value + tokenData.stakingValue === 0) { + continue; } - - if (STACKING_TO_VAULT[tokenAddress] && tokens[chainID][STACKING_TO_VAULT[tokenAddress]]) { - const valueInStakingContract = tokens[chainID][STACKING_TO_VAULT[tokenAddress]].value || 0; - if (valueInStakingContract > 0) { - cumulatedValueInVaults += valueInStakingContract; - } + if (vaults?.[toAddress(tokenAddress)]) { + cumulatedValueInVaults += tokenData.value + tokenData.stakingValue; + } else if (vaultsMigrations?.[toAddress(tokenAddress)]) { + cumulatedValueInVaults += tokenData.value + tokenData.stakingValue; } } } diff --git a/apps/common/contexts/useYearn.tsx b/apps/common/contexts/useYearn.tsx index c0deae4e2..eaaa52469 100755 --- a/apps/common/contexts/useYearn.tsx +++ b/apps/common/contexts/useYearn.tsx @@ -108,14 +108,14 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr }); const {data: vaultsMigrations} = useFetch({ - endpoint: `${yDaemonBaseUri}/vaults/all?${new URLSearchParams({ + endpoint: `${yDaemonBaseUriWithoutChain}/vaults/all?${new URLSearchParams({ migratable: 'nodust' })}`, schema: yDaemonVaultsSchema }); const {data: vaultsRetired} = useFetch({ - endpoint: `${yDaemonBaseUri}/vaults/retired`, + endpoint: `${yDaemonBaseUriWithoutChain}/vaults/retired`, schema: yDaemonVaultsSchema }); @@ -167,13 +167,11 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr } } } - if (safeChainID === 10) { - Object.entries(STACKING_TO_VAULT).forEach(([vaultAddress, stackingAddress]): void => { - allPrices[toAddress(stackingAddress)] = allPrices[toAddress(vaultAddress)]; - }); - } + Object.entries(STACKING_TO_VAULT).forEach(([vaultAddress, stackingAddress]): void => { + allPrices[toAddress(stackingAddress)] = allPrices[toAddress(vaultAddress)]; + }); return allPrices; - }, [prices, safeChainID]); + }, [prices]); const toTokens = (obj: TYDaemonTokensChain | undefined): TYDaemonTokens => { if (!obj) { diff --git a/apps/common/hooks/useMultichainBalances.ts b/apps/common/hooks/useMultichainBalances.ts index 961652a78..68122ebe5 100644 --- a/apps/common/hooks/useMultichainBalances.ts +++ b/apps/common/hooks/useMultichainBalances.ts @@ -97,7 +97,9 @@ async function performCall(chainID: number, calls: ContractFunctionConfig[], tok chainID: chainID, balance: toNormalizedBN(balanceOf, decimals), price: toNormalizedBN(rawPrice, 6), - value: toNormalizedValue(balanceOf, decimals) * toNormalizedValue(rawPrice, 6) + value: toNormalizedValue(balanceOf, decimals) * toNormalizedValue(rawPrice, 6), + stakingBalance: toNormalizedBN(0), + stakingValue: 0 }; } return [_data, undefined]; diff --git a/apps/common/types/types.ts b/apps/common/types/types.ts index 3e819ced3..cf656b7eb 100755 --- a/apps/common/types/types.ts +++ b/apps/common/types/types.ts @@ -65,8 +65,10 @@ export type TToken = { chainID: number; logoURI?: string; value: number; + stakingValue: number; price: TNormalizedBN; balance: TNormalizedBN; + stakingBalance: TNormalizedBN; }; export type TChainTokens = TNDict>; diff --git a/apps/vaults/components/list/VaultsListRow.tsx b/apps/vaults/components/list/VaultsListRow.tsx index 8d0b4a210..ea30e742d 100755 --- a/apps/vaults/components/list/VaultsListRow.tsx +++ b/apps/vaults/components/list/VaultsListRow.tsx @@ -9,11 +9,11 @@ import {IconFantomChain} from '@yearn-finance/web-lib/icons/chains/IconFantomCha import {IconOptimismChain} from '@yearn-finance/web-lib/icons/chains/IconOptimismChain'; import {toAddress} from '@yearn-finance/web-lib/utils/address'; import {ETH_TOKEN_ADDRESS, WETH_TOKEN_ADDRESS, WFTM_TOKEN_ADDRESS} from '@yearn-finance/web-lib/utils/constants'; -import {toBigInt} from '@yearn-finance/web-lib/utils/format.bigNumber'; import {formatAmount} from '@yearn-finance/web-lib/utils/format.number'; import {isZero} from '@yearn-finance/web-lib/utils/isZero'; import {ImageWithFallback} from '@common/components/ImageWithFallback'; import {RenderAmount} from '@common/components/RenderAmount'; +import {useWallet} from '@common/contexts/useWallet'; import {useBalance} from '@common/hooks/useBalance'; import {getVaultName} from '@common/utils'; @@ -139,13 +139,13 @@ export function VaultAPR({currentVault}: {currentVault: TYDaemonVault}): ReactEl } export function VaultsListRow({currentVault}: {currentVault: TYDaemonVault}): ReactElement { + const {getToken} = useWallet(); const balanceOfWant = useBalance({chainID: currentVault.chainID, address: currentVault.token.address}); const balanceOfCoin = useBalance({chainID: currentVault.chainID, address: ETH_TOKEN_ADDRESS}); const balanceOfWrappedCoin = useBalance({ chainID: currentVault.chainID, address: toAddress(currentVault.token.address) === WFTM_TOKEN_ADDRESS ? WFTM_TOKEN_ADDRESS : WETH_TOKEN_ADDRESS //TODO: Create a wagmi Chain upgrade to add the chain wrapper token address }); - const deposited = useBalance({chainID: currentVault.chainID, address: currentVault.address}).raw; const vaultName = useMemo((): string => getVaultName(currentVault), [currentVault]); const {stakingRewardsByVault, positionsMap} = useStakingRewards(); @@ -162,10 +162,10 @@ export function VaultsListRow({currentVault}: {currentVault: TYDaemonVault}): Re }, [balanceOfCoin.raw, balanceOfWant.raw, balanceOfWrappedCoin.raw, currentVault.token.address]); const staked = useMemo((): bigint => { - const stakedBalance = toBigInt(positionsMap[toAddress(stakingRewardsByVault[currentVault.address])]?.stake); - const depositedAndStaked = deposited + stakedBalance; + const token = getToken({chainID: currentVault.chainID, address: currentVault.address}); + const depositedAndStaked = token.balance.raw + token.stakingBalance.raw; return depositedAndStaked; - }, [currentVault.address, deposited, positionsMap, stakingRewardsByVault]); + }, [currentVault.address, positionsMap, stakingRewardsByVault]); return (