Skip to content

Commit

Permalink
fix: deposited includes all chains
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Oct 5, 2023
1 parent 70dcd38 commit e342f56
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
51 changes: 25 additions & 26 deletions apps/common/contexts/useWallet.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 = {
Expand All @@ -104,7 +105,6 @@ const defaultProps = {
******************************************************************************/
const WalletContext = createContext<TWalletContext>(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();

Expand All @@ -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},
Expand Down Expand Up @@ -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[] => {
Expand Down Expand Up @@ -233,30 +232,30 @@ 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;
}, [tokensRaw]);

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;
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions apps/common/contexts/useYearn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr
});

const {data: vaultsMigrations} = useFetch<TYDaemonVaults>({
endpoint: `${yDaemonBaseUri}/vaults/all?${new URLSearchParams({
endpoint: `${yDaemonBaseUriWithoutChain}/vaults/all?${new URLSearchParams({
migratable: 'nodust'
})}`,
schema: yDaemonVaultsSchema
});

const {data: vaultsRetired} = useFetch<TYDaemonVaults>({
endpoint: `${yDaemonBaseUri}/vaults/retired`,
endpoint: `${yDaemonBaseUriWithoutChain}/vaults/retired`,
schema: yDaemonVaultsSchema
});

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion apps/common/hooks/useMultichainBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions apps/common/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TDict<TToken>>;

Expand Down
10 changes: 5 additions & 5 deletions apps/vaults/components/list/VaultsListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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();

Expand All @@ -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 (
<Link
Expand Down

0 comments on commit e342f56

Please sign in to comment.