Skip to content

Commit

Permalink
fix: yfi addr
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Oct 16, 2023
1 parent a4e5b36 commit eba41ef
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 43 deletions.
18 changes: 18 additions & 0 deletions apps/common/hooks/useAsyncEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {useCallback, useEffect} from 'react';

import type {DependencyList} from 'react';

function useAsyncEffect(effect: () => Promise<void>, deps?: DependencyList): () => Promise<void> {
const asyncEffectInCallback = useCallback(async (): Promise<void> => {
effect();
}, [deps]);

useEffect((): void => {
asyncEffectInCallback();
}, [asyncEffectInCallback]);

return asyncEffectInCallback;
}


export {useAsyncEffect};
57 changes: 19 additions & 38 deletions apps/veyfi/contexts/useGauge.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, {createContext, memo, useCallback, useContext, useEffect, useMemo} from 'react';
import React, {createContext, memo, useCallback, useContext, useEffect, useMemo, useState} from 'react';
import {FixedNumber} from 'ethers';
import {useAsync} from '@react-hookz/web';
import {keyBy} from '@veYFI/utils';
import {VEYFI_GAUGE_ABI} from '@veYFI/utils/abi/veYFIGauge.abi';
import {VEYFI_CHAIN_ID} from '@veYFI/utils/constants';
Expand All @@ -9,6 +8,7 @@ import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {allowanceKey, toAddress} from '@yearn-finance/web-lib/utils/address';
import {decodeAsAddress, decodeAsBigInt, decodeAsNumber, decodeAsString} from '@yearn-finance/web-lib/utils/decoder';
import {toNormalizedBN} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {useAsyncEffect} from '@common/hooks/useAsyncEffect';

import type {ReactElement} from 'react';
import type {TAddress, TDict} from '@yearn-finance/web-lib/types';
Expand Down Expand Up @@ -41,21 +41,22 @@ export type TGaugeContext = {
gaugesMap: TDict<TGauge | undefined>,
positionsMap: TDict<TGaugePosition | undefined>,
allowancesMap: TDict<TNormalizedBN>,
isLoading: boolean,
refresh: () => void,
}
const defaultProps: TGaugeContext = {
gaugeAddresses: [],
gaugesMap: {},
positionsMap: {},
allowancesMap: {},
isLoading: true,
refresh: (): void => undefined
};

const GaugeContext = createContext<TGaugeContext>(defaultProps);
export const GaugeContextApp = memo(function GaugeContextApp({children}: {children: ReactElement}): ReactElement {
const {address: userAddress, isActive} = useWeb3();
const [gauges, set_gauges] = useState<TGauge[]>([]);
const [allowancesMap, set_allowancesMap] = useState<TDict<TNormalizedBN>>({});
const [positions, set_positions] = useState<TGaugePosition[]>([]);

// const {data: vaultAddresses} = useContractRead({
// address: VEYFI_REGISTRY_ADDRESS,
Expand All @@ -70,20 +71,7 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
// toAddress('0x9Cb511D44930c0C3D3114FFAaBedC3e0876D791a')
// ];

const gaugesFetcher = useCallback(async (): Promise<TGauge[]> => {
// if (!isActive || !userAddress) {
// return [];
// }

// const gaugeAddressCalls = vaultAddresses?.map((vaultAddress): TMulticallContract => ({
// address: VEYFI_REGISTRY_ADDRESS,
// abi: VEYFI_REGISTRY_ABI,
// functionName: 'gauges',
// args: [vaultAddress]
// }));
// const gaugeAddressesResults = await multicall({contracts: gaugeAddressCalls ?? [], chainId: VEYFI_CHAIN_ID});
// const gaugeAddresses = gaugeAddressesResults.map(({result}): unknown => result) as TAddress[];

const refreshVotingEscrow = useAsyncEffect(async (): Promise<void> => {
const gaugeAddresses = [
toAddress('0xbADfbF563C6C85F76e086E7a1915A1A46d683810'),
toAddress('0xd5947C01dBaEFeFF05186FE34A976b2E28d90542'),
Expand All @@ -110,14 +98,12 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
totalStaked: toNormalizedBN(decodeAsBigInt(results[4]), decimals)
});
});
return Promise.all(gaugePromises);
}, []);
const [{result: gauges, status: fetchGaugesStatus}, {execute: refreshVotingEscrow}] = useAsync(async (): Promise<TGauge[] | undefined> => gaugesFetcher(), []);

set_gauges(await Promise.all(gaugePromises));
});

const allowancesFetcher = useCallback(async (): Promise<TDict<TNormalizedBN>> => {
const refreshAllowances = useAsyncEffect(async (): Promise<void> => {
if (!gauges || !userAddress) {
return {};
return;
}
const calls = [];
for (const gauge of gauges) {
Expand All @@ -137,21 +123,19 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr

}
const results = await readContracts({contracts: calls});
const allowancesMap: TDict<TNormalizedBN> = {};
const _allowancesMap: TDict<TNormalizedBN> = {};
let index = 0;
for (const gauge of gauges) {
const allowance = decodeAsBigInt(results[index++]);
const decimals = Number(decodeAsBigInt(results[index++])) || decodeAsNumber(results[index++]);
allowancesMap[allowanceKey(VEYFI_CHAIN_ID, gauge.vaultAddress, gauge.address, userAddress)] = toNormalizedBN(allowance, decimals);
_allowancesMap[allowanceKey(VEYFI_CHAIN_ID, gauge.vaultAddress, gauge.address, userAddress)] = toNormalizedBN(allowance, decimals);
}
return allowancesMap;
}, [gauges, isActive, userAddress]);
const [{result: allowancesMap, status: fetchAllowancesMapStatus}, {execute: refreshAllowances}] = useAsync(async (): Promise<TDict<TNormalizedBN> | undefined> => allowancesFetcher(), {});
set_allowancesMap(_allowancesMap);
});


const positionsFetcher = useCallback(async (): Promise<TGaugePosition[]> => {
const refreshPositions = useAsyncEffect(async (): Promise<void> => {
if (!gauges|| !isActive|| !userAddress) {
return [];
return;
}

const positionPromises = gauges.map(async ({address}): Promise<TGaugePosition> => {
Expand Down Expand Up @@ -213,10 +197,8 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
boost
};
});
return Promise.all(positionPromises);
}, [gauges, isActive, userAddress]);
const [{result: positions, status: fetchPositionsStatus}, {execute: refreshPositions}] = useAsync(async (): Promise<TGaugePosition[] | undefined> => positionsFetcher(), []);

set_positions(await Promise.all(positionPromises));
});

const refresh = useCallback((): void => {
refreshVotingEscrow();
Expand All @@ -233,9 +215,8 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
gaugesMap: keyBy(gauges ?? [], 'address'),
positionsMap: keyBy(positions ?? [], 'address'),
allowancesMap: allowancesMap ?? {},
isLoading: fetchGaugesStatus ==='loading' || fetchPositionsStatus === 'loading' || fetchAllowancesMapStatus === 'loading',
refresh
}), [allowancesMap, fetchAllowancesMapStatus, fetchGaugesStatus, fetchPositionsStatus, gauges, positions, refresh]);
}), [allowancesMap, gauges, positions, refresh]);

return (
<GaugeContext.Provider value={contextValue}>
Expand Down
9 changes: 5 additions & 4 deletions apps/veyfi/contexts/useVotingEscrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import {createContext, memo, useCallback, useContext, useMemo} from 'react';
import {erc20ABI, useContractRead, useContractReads} from 'wagmi';
import {VEYFI_ABI} from '@veYFI/utils/abi/veYFI.abi';
import {VEYFI_POSITION_HELPER_ABI} from '@veYFI/utils/abi/veYFIPositionHelper.abi';
import {VEYFI_CHAIN_ID, YFI_ADDRESS} from '@veYFI/utils/constants';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {allowanceKey, isZeroAddress, toAddress} from '@yearn-finance/web-lib/utils/address';
import {VEYFI_ADDRESS, VEYFI_POSITION_HELPER_ADDRESS, YFI_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {VEYFI_ADDRESS, VEYFI_POSITION_HELPER_ADDRESS} from '@yearn-finance/web-lib/utils/constants';
import {decodeAsBigInt, decodeAsNumber, decodeAsString} from '@yearn-finance/web-lib/utils/decoder';
import {toMilliseconds} from '@yearn-finance/web-lib/utils/time';

Expand Down Expand Up @@ -119,9 +120,9 @@ export const VotingEscrowContextApp = memo(function VotingEscrowContextApp({chil
/* 🔵 - Yearn Finance **********************************************************
** Retrieving the user's allowances of YFI for the veYFI contract.
******************************************************************************/
const baseYFIContract = {address: YFI_ADDRESS, abi: erc20ABI};
const {data: allowance, status: allowanceStatus, refetch: refreshAllowance} = useContractRead({
...baseYFIContract,
address: YFI_ADDRESS,
abi: erc20ABI,
functionName: 'allowance',
args: [toAddress(address), VEYFI_ADDRESS],
enabled: isActive && address !== undefined && !isZeroAddress(address)
Expand All @@ -131,7 +132,7 @@ export const VotingEscrowContextApp = memo(function VotingEscrowContextApp({chil
return {};
}
return ({
[allowanceKey(1, YFI_ADDRESS, VEYFI_ADDRESS, address)]: allowance
[allowanceKey(VEYFI_CHAIN_ID, YFI_ADDRESS, VEYFI_ADDRESS, address)]: allowance
});
}, [address, allowance, allowanceStatus]);

Expand Down
3 changes: 2 additions & 1 deletion apps/veyfi/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

import {toAddress} from '@yearn-finance/web-lib/utils/address';
import {YFI_ADDRESS as MAINNET_YFI_ADDRESS} from '@yearn-finance/web-lib/utils/constants';

import type {TAddress} from '@yearn-finance/web-lib/types';
import type {TWeeks} from '@yearn-finance/web-lib/utils/time';

export const VEYFI_CHAIN_ID = 250; //TODO: change it to 1 - veYFI test

export const YFI_ADDRESS = toAddress('0x52Ca0fC251e7a28cf8E67357BCD3d771B105eCa9') || MAINNET_YFI_ADDRESS; // TODO: change to MAINNET_YFI_ADDRESS
export const VEYFI_REGISTRY_ADDRESS: TAddress = toAddress(''); // TODO: update once deployed
export const VEYFI_CLAIM_REWARDS_ZAP_ADDRESS: TAddress = toAddress(''); // TODO: update once deployed
export const VEYFI_OPTIONS_ADDRESS = toAddress('0x16A0cE5957642aDa37F58d5115E38fF3B0C30dc6'); //TODO: change it - veYFI test
Expand Down

0 comments on commit eba41ef

Please sign in to comment.