From 9ffdf85cc533e2b27032e524afca62cd507a353b Mon Sep 17 00:00:00 2001 From: Majorfi Date: Thu, 5 Oct 2023 12:20:31 +0200 Subject: [PATCH] fix: earned --- apps/common/contexts/useWallet.tsx | 3 +-- apps/common/contexts/useYearn.tsx | 13 ++++++---- apps/common/schemas/yDaemonEarnedSchema.ts | 20 +++++++++++++++- apps/common/schemas/yDaemonVaultsSchemas.ts | 16 ++++++------- .../components/details/VaultDetailsHeader.tsx | 24 +++++++++---------- pages/vaults/[chainID]/[address].tsx | 2 +- 6 files changed, 50 insertions(+), 28 deletions(-) diff --git a/apps/common/contexts/useWallet.tsx b/apps/common/contexts/useWallet.tsx index 9507461b0..1d77f0bc9 100755 --- a/apps/common/contexts/useWallet.tsx +++ b/apps/common/contexts/useWallet.tsx @@ -234,9 +234,8 @@ export const WalletContextApp = memo(function WalletContextApp({children}: {chil const _tokens = {...tokensRaw}; for (const [chainIDStr, perChain] of Object.entries(_tokens)) { const chainID = Number(chainIDStr); - for (const [tokenAddress, tokenData] of Object.entries(perChain)) { + for (const [tokenAddress] 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; } diff --git a/apps/common/contexts/useYearn.tsx b/apps/common/contexts/useYearn.tsx index eaaa52469..14b288138 100755 --- a/apps/common/contexts/useYearn.tsx +++ b/apps/common/contexts/useYearn.tsx @@ -67,7 +67,6 @@ const YearnContext = createContext({ export const YearnContextApp = memo(function YearnContextApp({children}: {children: ReactElement}): ReactElement { const {safeChainID} = useChainID(); - const {yDaemonBaseUri} = useYDaemonBaseURI({chainID: safeChainID}); const {yDaemonBaseUri: yDaemonBaseUriWithoutChain} = useYDaemonBaseURI(); const result = useYDaemonStatus({chainID: safeChainID}); const {address, currentPartner} = useWeb3(); @@ -102,8 +101,10 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr orderDirection: 'desc', strategiesDetails: 'withDetails', strategiesRisk: 'withRisk', - strategiesCondition: 'inQueue' - })}&chainIDs=${[1, 10].join(',')}&limit=2500`, + strategiesCondition: 'inQueue', + chainIDs: [1, 10].join(','), + limit: `2500` + })}`, schema: yDaemonVaultsSchema }); @@ -120,7 +121,11 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr }); const {data: earned} = useFetch({ - endpoint: address ? `${yDaemonBaseUri}/earned/${address}` : null, + endpoint: address + ? `${yDaemonBaseUriWithoutChain}/earned/${address}?${new URLSearchParams({ + chainIDs: [1, 10].join(',') + })}` + : null, schema: yDaemonEarnedSchema }); diff --git a/apps/common/schemas/yDaemonEarnedSchema.ts b/apps/common/schemas/yDaemonEarnedSchema.ts index ea8b7282d..e06086762 100644 --- a/apps/common/schemas/yDaemonEarnedSchema.ts +++ b/apps/common/schemas/yDaemonEarnedSchema.ts @@ -1,7 +1,7 @@ import {z} from 'zod'; import {addressSchema} from '@yearn-finance/web-lib/utils/schemas/addressSchema'; -export const yDaemonEarnedSchema = z.object({ +export const yDaemonSingleEarnedSchema = z.object({ earned: z.record( addressSchema, z.object({ @@ -15,4 +15,22 @@ export const yDaemonEarnedSchema = z.object({ totalUnrealizedGainsUSD: z.number() }); +export const yDaemonEarnedSchema = z.object({ + earned: z.record( + z.string(), + z.record( + addressSchema, + z.object({ + realizedGains: z.string(), + realizedGainsUSD: z.number(), + unrealizedGains: z.string(), + unrealizedGainsUSD: z.number() + }) + ) + ), + totalRealizedGainsUSD: z.number(), + totalUnrealizedGainsUSD: z.number() +}); + +export type TYDaemonEarnedSingle = z.infer; export type TYDaemonEarned = z.infer; diff --git a/apps/common/schemas/yDaemonVaultsSchemas.ts b/apps/common/schemas/yDaemonVaultsSchemas.ts index 7c317d0b8..199ad28a9 100644 --- a/apps/common/schemas/yDaemonVaultsSchemas.ts +++ b/apps/common/schemas/yDaemonVaultsSchemas.ts @@ -80,7 +80,13 @@ export const yDaemonVaultTokenSchema = z.object({ export const yDaemonVaultSchema = z.object({ address: addressSchema, - type: z.literal('Automated').or(z.literal('Automated Yearn Vault')).or(z.literal('Experimental')).or(z.literal('Experimental Yearn Vault')).or(z.literal('Standard')), + type: z + .literal('Automated') + .or(z.literal('Automated Yearn Vault')) + .or(z.literal('Experimental')) + .or(z.literal('Experimental Yearn Vault')) + .or(z.literal('Standard')) + .or(z.literal('Yearn Vault')), symbol: z.string(), display_symbol: z.string(), formated_symbol: z.string(), @@ -171,7 +177,6 @@ export const yDaemonVaultSchema = z.object({ }); export const yDaemonVaultsSchema = z.array(yDaemonVaultSchema); - export const yDaemonVaultHarvestSchema = z.object({ vaultAddress: addressSchema.optional(), strategyAddress: addressSchema.optional(), @@ -182,20 +187,15 @@ export const yDaemonVaultHarvestSchema = z.object({ loss: z.string(), lossValue: z.number().optional() }); - export const yDaemonVaultHarvestsSchema = z.array(yDaemonVaultHarvestSchema); export type TYDaemonVault = z.infer; - export type TYDaemonVaultStrategy = z.infer; - export type TYDaemonVaults = z.infer; - export type TYDaemonVaultHarvest = z.infer; - export type TYDaemonVaultHarvests = z.infer; - export type TYDaemonVaultTokenSchema = z.infer; +export const isStandardVault = (vault: TYDaemonVault): boolean => vault.type === 'Standard' || vault.type === 'Yearn Vault'; export const isAutomatedVault = (vault: TYDaemonVault): boolean => vault.type === 'Automated' || vault.type === 'Automated Yearn Vault'; export const isExperimentalVault = (vault: TYDaemonVault): boolean => vault.type === 'Experimental' || vault.type === 'Experimental Yearn Vault'; diff --git a/apps/vaults/components/details/VaultDetailsHeader.tsx b/apps/vaults/components/details/VaultDetailsHeader.tsx index adc702f25..252f39152 100755 --- a/apps/vaults/components/details/VaultDetailsHeader.tsx +++ b/apps/vaults/components/details/VaultDetailsHeader.tsx @@ -10,12 +10,12 @@ import {RenderAmount} from '@common/components/RenderAmount'; import {useBalance} from '@common/hooks/useBalance'; import {useFetch} from '@common/hooks/useFetch'; import {useTokenPrice} from '@common/hooks/useTokenPrice'; -import {yDaemonEarnedSchema} from '@common/schemas/yDaemonEarnedSchema'; +import {yDaemonSingleEarnedSchema} from '@common/schemas/yDaemonEarnedSchema'; import {getVaultName} from '@common/utils'; import {useYDaemonBaseURI} from '@common/utils/getYDaemonBaseURI'; import type {ReactElement} from 'react'; -import type {TYDaemonEarned} from '@common/schemas/yDaemonEarnedSchema'; +import type {TYDaemonEarnedSingle} from '@common/schemas/yDaemonEarnedSchema'; import type {TYDaemonVault} from '@common/schemas/yDaemonVaultsSchemas'; import type {TNormalizedBN} from '@common/types/types'; @@ -43,24 +43,24 @@ function VaultHeaderLineItem({label, children, legend}: TVaultHeaderLineItemProp ); } -export function VaultDetailsHeader({vault}: {vault: TYDaemonVault}): ReactElement { +export function VaultDetailsHeader({currentVault}: {currentVault: TYDaemonVault}): ReactElement { const {address: userAddress} = useWeb3(); - const {yDaemonBaseUri} = useYDaemonBaseURI({chainID: vault.chainID}); - const {address, apy, tvl, decimals, symbol = 'token', token} = vault; - const {data: earned} = useFetch({ - endpoint: address && userAddress ? `${yDaemonBaseUri}/earned/${userAddress}` : null, - schema: yDaemonEarnedSchema + const {yDaemonBaseUri} = useYDaemonBaseURI({chainID: currentVault.chainID}); + const {address, apy, tvl, decimals, symbol = 'token', token} = currentVault; + const {data: earned} = useFetch({ + endpoint: address && userAddress ? `${yDaemonBaseUri}/earned/${userAddress}/${currentVault.address}` : null, + schema: yDaemonSingleEarnedSchema }); const normalizedVaultEarned = useMemo((): TNormalizedBN => { - const {unrealizedGains} = earned?.earned?.[toAddress(address)] || {}; + const {unrealizedGains} = earned?.earned?.[toAddress(currentVault.address)] || {}; const value = toBigInt(unrealizedGains); return toNormalizedBN(value < 0n ? 0n : value); }, [earned?.earned, address]); - const vaultBalance = useBalance({address, chainID: vault.chainID}); - const vaultPrice = useTokenPrice(address) || vault?.tvl?.price || 0; - const vaultName = useMemo((): string => getVaultName(vault), [vault]); + const vaultBalance = useBalance({address, chainID: currentVault.chainID}); + const vaultPrice = useTokenPrice(address) || currentVault?.tvl?.price || 0; + const vaultName = useMemo((): string => getVaultName(currentVault), [currentVault]); const {stakingRewardsByVault, positionsMap} = useStakingRewards(); const stakedBalance = toBigInt(positionsMap[toAddress(stakingRewardsByVault[address])]?.stake); const depositedAndStaked = toNormalizedBN(vaultBalance.raw + stakedBalance, decimals); diff --git a/pages/vaults/[chainID]/[address].tsx b/pages/vaults/[chainID]/[address].tsx index 145c69efe..272937cb9 100755 --- a/pages/vaults/[chainID]/[address].tsx +++ b/pages/vaults/[chainID]/[address].tsx @@ -94,7 +94,7 @@ function Index(): ReactElement | null {
- +