Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add yearn harvests schemas #231

Merged
merged 3 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions apps/common/schemas/yDaemonVaultsSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ const yDaemonVaultStrategySchema = z.object({
}).optional() // Optional for migratable
});

export const yDaemonVaultTokenSchema = z.object({
address: addressSchema,
underlyingTokensAddresses: z.array(addressSchema),
name: z.string(),
symbol: z.string(),
type: z.string(),
display_name: z.string(),
display_symbol: z.string(),
description: z.string(),
icon: z.string(),
decimals: z.number()
});

export const yDaemonVaultSchema = z.object({
address: addressSchema,
type: z.literal('Automated').or(z.literal('Standard').or(z.literal('Experimental'))),
Expand All @@ -78,18 +91,7 @@ export const yDaemonVaultSchema = z.object({
riskScore: z.number(),
endorsed: z.boolean(),
emergency_shutdown: z.boolean(),
token: z.object({
address: addressSchema,
underlyingTokensAddresses: z.array(addressSchema),
name: z.string(),
symbol: z.string(),
type: z.string(),
display_name: z.string(),
display_symbol: z.string(),
description: z.string(),
icon: z.string(),
decimals: z.number()
}),
token: yDaemonVaultTokenSchema,
tvl: z.object({
total_assets: z.string(),
total_delegated_assets: z.string(),
Expand Down Expand Up @@ -161,8 +163,8 @@ export const yDaemonVaultSchema = z.object({
export const yDaemonVaultsSchema = z.array(yDaemonVaultSchema);

export const yDaemonVaultHarvestSchema = z.object({
vaultAddress: z.string().optional(),
strategyAddress: z.string().optional(),
vaultAddress: addressSchema.optional(),
strategyAddress: addressSchema.optional(),
txHash:z.string().optional(),
timestamp: z.string(),
profit: z.string(),
Expand All @@ -179,6 +181,8 @@ export type TYDaemonVaultStrategy = z.infer<typeof yDaemonVaultStrategySchema>;

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

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

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

export type TYDaemonVaultHarvestsSchema = z.infer<typeof yDaemonVaultHarvestsSchema>;
export type TYDaemonVaultTokenSchema = z.infer<typeof yDaemonVaultTokenSchema>;
15 changes: 15 additions & 0 deletions apps/common/schemas/ySchemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {z} from 'zod';
import {addressSchema} from '@common/schemas/custom/addressSchema';

export const ySettingsForNetworkSchema = z.object({
rpcURI: z.string().optional(),
yDaemonURI: z.string().optional(),
graphURI: z.string().optional(),
metaURI: z.string().optional(),
apiURI: z.string().optional(),
explorerBaseURI: z.string().optional(),
lensOracleAddress: addressSchema.optional(),
partnerContractAddress: addressSchema.optional()
});

export type TSettingsForNetwork = z.infer<typeof ySettingsForNetworkSchema>;
34 changes: 0 additions & 34 deletions apps/common/types/yearn.tsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {findLatestApr} from '@vaults/components/details/tabs/findLatestApr';
import {GraphForStrategyReports} from '@vaults/components/graphs/GraphForStrategyReports';
import {yDaemonReportsSchema} from '@vaults/schemas/reportsSchema';
import Renderable from '@yearn-finance/web-lib/components/Renderable';
import {useSettings} from '@yearn-finance/web-lib/contexts/useSettings';
import {useChainID} from '@yearn-finance/web-lib/hooks/useChainID';
import IconCopy from '@yearn-finance/web-lib/icons/IconCopy';
import {toAddress} from '@yearn-finance/web-lib/utils/address';
Expand All @@ -16,6 +15,7 @@ import {SearchBar} from '@common/components/SearchBar';
import {Switch} from '@common/components/Switch';
import {useFetch} from '@common/hooks/useFetch';
import IconChevron from '@common/icons/IconChevron';
import {useYDaemonBaseURI} from '@common/utils/getYDaemonBaseURI';

import type {ReactElement} from 'react';
import type {TYDaemonVault, TYDaemonVaultStrategy} from '@common/schemas/yDaemonVaultsSchemas';
Expand Down Expand Up @@ -43,7 +43,7 @@ function RiskScoreElement({label, value}: TRiskScoreElementProps): ReactElement

function VaultDetailsStrategy({currentVault, strategy}: TProps): ReactElement {
const {safeChainID} = useChainID();
const {settings: baseAPISettings} = useSettings();
const {yDaemonBaseUri} = useYDaemonBaseURI({chainID: safeChainID});
const isMounted = useIsMounted();

const riskScoreElementsMap = useMemo((): TRiskScoreElementProps[] => {
Expand All @@ -61,10 +61,8 @@ function VaultDetailsStrategy({currentVault, strategy}: TProps): ReactElement {
]);
}, [strategy]);

const YDAEMON_BASE_URI = `${baseAPISettings.yDaemonBaseURI || process.env.YDAEMON_BASE_URI}/${safeChainID}`;

const {data: reports} = useFetch<TYDaemonReports>({
endpoint: `${YDAEMON_BASE_URI}/reports/${strategy.address}`,
endpoint: `${yDaemonBaseUri}/reports/${strategy.address}`,
schema: yDaemonReportsSchema
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import {assert} from '@common/utils/assert';
import {useYDaemonBaseURI} from '@common/utils/getYDaemonBaseURI';

import type {ReactElement} from 'react';
import type {TYDaemonVault, TYDaemonVaultHarvestsSchema} from '@common/schemas/yDaemonVaultsSchemas';
import type {TSettingsForNetwork} from '@common/types/yearn';
import type {TYDaemonVault, TYDaemonVaultHarvests} from '@common/schemas/yDaemonVaultsSchemas';
import type {TSettingsForNetwork} from '@common/schemas/ySchemas';

type TTabsOptions = {
value: number;
Expand Down Expand Up @@ -176,7 +176,7 @@ function VaultDetailsTabsWrapper({currentVault}: {currentVault: TYDaemonVault}):
}
}

const {data: yDaemonHarvestsData} = useFetch<TYDaemonVaultHarvestsSchema>({
const {data: yDaemonHarvestsData} = useFetch<TYDaemonVaultHarvests>({
endpoint: `${yDaemonBaseUri}/vaults/harvests/${currentVault.address}`,
schema: yDaemonVaultHarvestsSchema
});
Expand Down
8 changes: 4 additions & 4 deletions apps/ybal/components/Harvests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {HarvestListRow} from '@yBal/components/HarvestsListRow';
import {useYBal} from '@yBal/contexts/useYBal';

import type {ReactElement} from 'react';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVaultHarvest, TYDaemonVaultHarvests} from '@common/schemas/yDaemonVaultsSchemas';

function Harvests(): ReactElement {
const {harvests} = useYBal();
const [category, set_category] = useState('all');

const filteredHarvests = useMemo((): TYDaemonHarvests[] => {
const filteredHarvests = useMemo((): TYDaemonVaultHarvests => {
const _harvests = [...(harvests || [])];
if (category === 'st-yBal') {
return _harvests.filter((harvest): boolean => toAddress(harvest.vaultAddress) === STYBAL_TOKEN_ADDRESS);
Expand Down Expand Up @@ -55,12 +55,12 @@ function Harvests(): ReactElement {
<HarvestListHead />
{
(filteredHarvests || [])
.filter((harvest: TYDaemonHarvests): boolean => {
.filter((harvest: TYDaemonVaultHarvest): boolean => {
return (
!isZeroAddress(toAddress(harvest.vaultAddress)) &&
[STYBAL_TOKEN_ADDRESS, LPYBAL_TOKEN_ADDRESS].includes(toAddress(harvest.vaultAddress))
);
}).map((harvest: TYDaemonHarvests, index: number): ReactElement => {
}).map((harvest: TYDaemonVaultHarvest, index: number): ReactElement => {
return (
<HarvestListRow
key={`${harvest.timestamp}_${harvest.vaultAddress}_${index}`}
Expand Down
4 changes: 2 additions & 2 deletions apps/ybal/components/HarvestsListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {formatDate} from '@yearn-finance/web-lib/utils/format.time';
import {ImageWithFallback} from '@common/components/ImageWithFallback';

import type {ReactElement} from 'react';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVaultHarvest} from '@common/schemas/yDaemonVaultsSchemas';

type TRowProps = {
label: string;
Expand All @@ -28,7 +28,7 @@ function Row({label, value, className, valueClassName}: TRowProps): ReactElement
);
}

function HarvestListRow({harvest}: {harvest: TYDaemonHarvests}): ReactElement {
function HarvestListRow({harvest}: {harvest: TYDaemonVaultHarvest}): ReactElement {
const vaultName = toAddress(harvest.vaultAddress) === STYBAL_TOKEN_ADDRESS ? 'st-yBal' : 'lp-yBal';
const gain = formatToNormalizedAmount(toBigInt(harvest.profit) - toBigInt(harvest.loss));
const value = formatUSD(Number(harvest.profitValue) - Number(harvest.lossValue));
Expand Down
29 changes: 11 additions & 18 deletions apps/ybal/contexts/useYBal.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import React, {createContext, useContext, useMemo, useState} from 'react';
import useSWR from 'swr';
import {useSettings} from '@yearn-finance/web-lib/contexts/useSettings';
import {LPYBAL_TOKEN_ADDRESS, STYBAL_TOKEN_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {baseFetcher} from '@yearn-finance/web-lib/utils/fetchers';
import {useFetch} from '@common/hooks/useFetch';
import {yDaemonVaultSchema} from '@common/schemas/yDaemonVaultsSchemas';
import {yDaemonVaultHarvestsSchema, yDaemonVaultSchema} from '@common/schemas/yDaemonVaultsSchemas';
import {useYDaemonBaseURI} from '@common/utils/getYDaemonBaseURI';
import {useAllowances} from '@yBal/contexts/useAllowanceHook';
import {defaultBalHoldings, useHoldings} from '@yBal/contexts/useHoldingsHook';

import type {ReactElement} from 'react';
import type {SWRResponse} from 'swr';
import type {TBalHoldings} from '@yBal/contexts/useHoldingsHook';
import type {TDict} from '@yearn-finance/web-lib/types';
import type {TYDaemonVault} from '@common/schemas/yDaemonVaultsSchemas';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVault, TYDaemonVaultHarvests} from '@common/schemas/yDaemonVaultsSchemas';

type TYBalContext = {
styBalAPY: number,
slippage: number,
allowances: TDict<bigint>,
holdings: TBalHoldings,
harvests: TYDaemonHarvests[],
harvests: TYDaemonVaultHarvests,
set_slippage: (slippage: number) => void,
refetchAllowances: () => void
}
Expand All @@ -41,23 +37,20 @@ const defaultProps = {
const YBalContext = createContext<TYBalContext>(defaultProps);

export const YBalContextApp = ({children}: {children: ReactElement}): ReactElement => {
const {settings: baseAPISettings} = useSettings();
const {yDaemonBaseUri} = useYDaemonBaseURI({chainID: 1});
const [slippage, set_slippage] = useState(0.6);
const holdings = useHoldings();
const allowances = useAllowances();

const YDAEMON_BASE_URI = `${baseAPISettings.yDaemonBaseURI || process.env.YDAEMON_BASE_URI}`;

const {data: styBalVault} = useFetch<TYDaemonVault>({
endpoint: `${YDAEMON_BASE_URI}/1/vaults/${STYBAL_TOKEN_ADDRESS}`,
endpoint: `${yDaemonBaseUri}/vaults/${STYBAL_TOKEN_ADDRESS}`,
schema: yDaemonVaultSchema
});

const {data: yBalHarvests} = useSWR(
`${YDAEMON_BASE_URI}/1/vaults/harvests/${STYBAL_TOKEN_ADDRESS},${LPYBAL_TOKEN_ADDRESS}`,
baseFetcher,
{revalidateOnFocus: false}
) as SWRResponse;
const {data: yBalHarvests} = useFetch<TYDaemonVaultHarvests>({
endpoint: `${yDaemonBaseUri}/vaults/harvests/${STYBAL_TOKEN_ADDRESS},${LPYBAL_TOKEN_ADDRESS}`,
schema: yDaemonVaultHarvestsSchema
});

/* 🔵 - Yearn Finance ******************************************************
** Compute the styBal APY based on the experimental APY and the mega boost.
Expand All @@ -68,7 +61,7 @@ export const YBalContextApp = ({children}: {children: ReactElement}): ReactEleme
** Setup and render the Context provider to use in the app.
***************************************************************************/
const contextValue = useMemo((): TYBalContext => ({
harvests: yBalHarvests,
harvests: yBalHarvests ?? [],
holdings: holdings,
allowances: allowances[0],
refetchAllowances: allowances[1],
Expand Down
6 changes: 3 additions & 3 deletions apps/ycrv/components/Harvests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import {HarvestListRow} from '@yCRV/components/HarvestsListRow';
import {useYCRV} from '@yCRV/contexts/useYCRV';

import type {ReactElement} from 'react';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVaultHarvest, TYDaemonVaultHarvests} from '@common/schemas/yDaemonVaultsSchemas';

function Harvests(): ReactElement {
const {harvests} = useYCRV();
const [category, set_category] = useState('all');

const filteredHarvests = useMemo((): TYDaemonHarvests[] => {
const filteredHarvests = useMemo((): TYDaemonVaultHarvests => {
const _harvests = [...(harvests || [])];
if (category === 'st-yCRV') {
return _harvests.filter((harvest): boolean => toAddress(harvest.vaultAddress) === STYCRV_TOKEN_ADDRESS);
Expand Down Expand Up @@ -53,7 +53,7 @@ function Harvests(): ReactElement {
</div>
<div className={'mt-4 grid w-full grid-cols-1 md:mt-0'}>
<HarvestListHead />
{(filteredHarvests || [])?.map((harvest: TYDaemonHarvests, index: number): ReactElement => {
{(filteredHarvests || [])?.map((harvest: TYDaemonVaultHarvest, index: number): ReactElement => {
return <HarvestListRow key={`${harvest.timestamp}_${harvest.vaultAddress}_${index}`} harvest={harvest} />;
})}
</div>
Expand Down
4 changes: 2 additions & 2 deletions apps/ycrv/components/HarvestsListRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {formatDate} from '@yearn-finance/web-lib/utils/format.time';
import {ImageWithFallback} from '@common/components/ImageWithFallback';

import type {ReactElement} from 'react';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVaultHarvest} from '@common/schemas/yDaemonVaultsSchemas';

function HarvestListRow({harvest}: {harvest: TYDaemonHarvests}): ReactElement {
function HarvestListRow({harvest}: {harvest: TYDaemonVaultHarvest}): ReactElement {
return (
<div className={'yearn--table-wrapper'}>
<div className={'yearn--table-token-section'}>
Expand Down
21 changes: 8 additions & 13 deletions apps/ycrv/contexts/useYCRV.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,25 @@
import React, {createContext, useContext, useMemo, useState} from 'react';
import {formatUnits} from 'viem';
import useSWR from 'swr';
import {LPYCRV_TOKEN_ADDRESS, STYCRV_TOKEN_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {baseFetcher} from '@yearn-finance/web-lib/utils/fetchers';
import {isZero} from '@yearn-finance/web-lib/utils/isZero';
import {useFetch} from '@common/hooks/useFetch';
import {yDaemonVaultSchema} from '@common/schemas/yDaemonVaultsSchemas';
import {yDaemonVaultHarvestsSchema, yDaemonVaultSchema} from '@common/schemas/yDaemonVaultsSchemas';
import {useYDaemonBaseURI} from '@common/utils/getYDaemonBaseURI';
import {useAllowances} from '@yCRV/contexts/useAllowanceHook';
import {defaultHoldings, useHoldings} from '@yCRV/contexts/useHoldingsHook';

import type {ReactElement} from 'react';
import type {SWRResponse} from 'swr';
import type {TCRVHoldings} from '@yCRV/contexts/useHoldingsHook';
import type {TDict} from '@yearn-finance/web-lib/types';
import type {TYDaemonVault} from '@common/schemas/yDaemonVaultsSchemas';
import type {TYDaemonHarvests} from '@common/types/yearn';
import type {TYDaemonVault, TYDaemonVaultHarvests} from '@common/schemas/yDaemonVaultsSchemas';

type TYCRVContext = {
styCRVMegaBoost: number,
styCRVAPY: number,
slippage: number,
allowances: TDict<bigint>,
holdings: TCRVHoldings,
harvests: TYDaemonHarvests[],
harvests: TYDaemonVaultHarvests,
set_slippage: (slippage: number) => void,
refetchAllowances: () => void
}
Expand Down Expand Up @@ -54,11 +50,10 @@ export const YCRVContextApp = ({children}: {children: ReactElement}): ReactEleme
schema: yDaemonVaultSchema
});

const {data: yCRVHarvests} = useSWR(
`${yDaemonBaseUri}/vaults/harvests/${STYCRV_TOKEN_ADDRESS},${LPYCRV_TOKEN_ADDRESS}`,
baseFetcher,
{revalidateOnFocus: false}
) as SWRResponse;
const {data: yCRVHarvests} = useFetch<TYDaemonVaultHarvests>({
endpoint: `${yDaemonBaseUri}/vaults/harvests/${STYCRV_TOKEN_ADDRESS},${LPYCRV_TOKEN_ADDRESS}`,
schema: yDaemonVaultHarvestsSchema
});

/* 🔵 - Yearn Finance ******************************************************
** Compute the mega boost for the staked yCRV. This boost come from the
Expand Down Expand Up @@ -87,7 +82,7 @@ export const YCRVContextApp = ({children}: {children: ReactElement}): ReactEleme
** Setup and render the Context provider to use in the app.
***************************************************************************/
const contextValue = useMemo((): TYCRVContext => ({
harvests: yCRVHarvests,
harvests: yCRVHarvests ?? [],
holdings: holdings,
allowances: allowances[0],
refetchAllowances: allowances[1],
Expand Down