Skip to content

Commit

Permalink
fix: earned
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Oct 5, 2023
1 parent e342f56 commit 9ffdf85
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
3 changes: 1 addition & 2 deletions apps/common/contexts/useWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 9 additions & 4 deletions apps/common/contexts/useYearn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const YearnContext = createContext<TYearnContext>({

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

Expand All @@ -120,7 +121,11 @@ export const YearnContextApp = memo(function YearnContextApp({children}: {childr
});

const {data: earned} = useFetch<TYDaemonEarned>({
endpoint: address ? `${yDaemonBaseUri}/earned/${address}` : null,
endpoint: address
? `${yDaemonBaseUriWithoutChain}/earned/${address}?${new URLSearchParams({
chainIDs: [1, 10].join(',')
})}`
: null,
schema: yDaemonEarnedSchema
});

Expand Down
20 changes: 19 additions & 1 deletion apps/common/schemas/yDaemonEarnedSchema.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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<typeof yDaemonSingleEarnedSchema>;
export type TYDaemonEarned = z.infer<typeof yDaemonEarnedSchema>;
16 changes: 8 additions & 8 deletions apps/common/schemas/yDaemonVaultsSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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<typeof yDaemonVaultSchema>;

export type TYDaemonVaultStrategy = z.infer<typeof yDaemonVaultStrategySchema>;

export type TYDaemonVaults = z.infer<typeof yDaemonVaultsSchema>;

export type TYDaemonVaultHarvest = z.infer<typeof yDaemonVaultHarvestSchema>;

export type TYDaemonVaultHarvests = z.infer<typeof yDaemonVaultHarvestsSchema>;

export type TYDaemonVaultTokenSchema = z.infer<typeof yDaemonVaultTokenSchema>;

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';
24 changes: 12 additions & 12 deletions apps/vaults/components/details/VaultDetailsHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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<TYDaemonEarned>({
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<TYDaemonEarnedSingle>({
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);
Expand Down
2 changes: 1 addition & 1 deletion pages/vaults/[chainID]/[address].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function Index(): ReactElement | null {
</header>

<section className={'mt-4 grid w-full grid-cols-12 pb-10 md:mt-0'}>
<VaultDetailsHeader vault={currentVault} />
<VaultDetailsHeader currentVault={currentVault} />
<ActionFlowContextApp currentVault={currentVault}>
<WithSolverContextApp>
<VaultActionsTabsWrapper currentVault={currentVault} />
Expand Down

0 comments on commit 9ffdf85

Please sign in to comment.