Skip to content

Commit

Permalink
fix: allowance
Browse files Browse the repository at this point in the history
  • Loading branch information
Majorfi committed Oct 18, 2023
1 parent 8d32478 commit bde2799
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 51 deletions.
29 changes: 19 additions & 10 deletions apps/veyfi/components/ViewStakeUnstakeGauges.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useCallback, useMemo, useState} from 'react';
import Link from 'next/link';
import {erc20ABI, useContractRead} from 'wagmi';
import {useDeepCompareMemo} from '@react-hookz/web';
import {useGauge} from '@veYFI/contexts/useGauge';
import {useOption} from '@veYFI/contexts/useOption';
Expand All @@ -9,7 +10,7 @@ import {SECONDS_PER_YEAR, VEYFI_CHAIN_ID} from '@veYFI/utils/constants';
import {Button} from '@yearn-finance/web-lib/components/Button';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {IconLinkOut} from '@yearn-finance/web-lib/icons/IconLinkOut';
import {allowanceKey, toAddress, truncateHex} from '@yearn-finance/web-lib/utils/address';
import {toAddress, truncateHex} from '@yearn-finance/web-lib/utils/address';
import {formatToNormalizedValue, toBigInt, toNormalizedBN} from '@yearn-finance/web-lib/utils/format.bigNumber';
import {formatAmount, formatPercent} from '@yearn-finance/web-lib/utils/format.number';
import {defaultTxStatus} from '@yearn-finance/web-lib/utils/web3/transaction';
Expand All @@ -34,12 +35,10 @@ type TGaugeData = {
gaugeAPR: number,
gaugeBoost: number,
gaugeStaked: TNormalizedBN,
allowance: TNormalizedBN,
isApproved: boolean,
actions: undefined
}

function StakeUnstakeButtons({isApproved, vaultAddress, gaugeAddress, vaultDeposited, gaugeStaked}: TGaugeData): ReactElement {
function StakeUnstakeButtons({vaultAddress, gaugeAddress, vaultDeposited, gaugeStaked}: TGaugeData): ReactElement {
const {provider, address, isActive} = useWeb3();
const {refresh: refreshGauges} = useGauge();
const {refresh: refreshBalances} = useWallet();
Expand All @@ -49,6 +48,18 @@ function StakeUnstakeButtons({isApproved, vaultAddress, gaugeAddress, vaultDepos
const userAddress = address as TAddress;
const refreshData = useCallback((): unknown => Promise.all([refreshGauges(), refreshBalances()]), [refreshGauges, refreshBalances]);

const {data: allowance, refetch: refreshAllowances} = useContractRead({
address: vaultAddress,
abi: erc20ABI,
chainId: VEYFI_CHAIN_ID,
functionName: 'allowance',
args: [toAddress(address), gaugeAddress]
});

const isApproved = useMemo((): boolean => {
return toBigInt(allowance) >= toBigInt(vaultDeposited?.raw);
}, [allowance, vaultDeposited]);

const onApproveAndStake = useCallback(async (vaultAddress: TAddress, gaugeAddress: TAddress, amount: bigint): Promise<void> => {
const response = await approveAndStake({
connector: provider,
Expand All @@ -60,9 +71,9 @@ function StakeUnstakeButtons({isApproved, vaultAddress, gaugeAddress, vaultDepos
});

if (response.isSuccessful) {
await refreshData();
await Promise.all([refreshData(), refreshAllowances()]);
}
}, [provider, refreshData]);
}, [provider, refreshAllowances, refreshData]);

const onStake = useCallback(async (gaugeAddress: TAddress, amount: bigint): Promise<void> => {
const response = await stake({
Expand Down Expand Up @@ -127,7 +138,7 @@ function StakeUnstakeButtons({isApproved, vaultAddress, gaugeAddress, vaultDepos

export function StakeUnstakeGauges(): ReactElement {
const {isActive, address} = useWeb3();
const {gaugesMap, positionsMap, allowancesMap} = useGauge();
const {gaugesMap, positionsMap} = useGauge();
const {vaults, prices} = useYearn();
const {balances} = useWallet();
const {dYFIPrice} = useOption();
Expand Down Expand Up @@ -164,14 +175,12 @@ export function StakeUnstakeGauges(): ReactElement {
gaugeAPR: APRFor10xBoost,
gaugeBoost: boost,
gaugeStaked: positionsMap[gauge.address]?.deposit ?? toNormalizedBN(0),
allowance: allowancesMap[allowanceKey(VEYFI_CHAIN_ID, vault.address, gauge.address, toAddress(address))],
isApproved: toBigInt(allowancesMap[allowanceKey(VEYFI_CHAIN_ID, vault.address, gauge.address, toAddress(address))]?.raw) >= toBigInt(balances[vault.address]?.raw),
actions: undefined
});
}
set_isLoadingGauges(false);
return data;
}, [gaugesMap, vaults, balances, positionsMap, allowancesMap, address]);
}, [gaugesMap, vaults, balances, positionsMap, address]);

const searchedGaugesData = useMemo((): TGaugeData[] => {
if (!search) {
Expand Down
45 changes: 4 additions & 41 deletions apps/veyfi/contexts/useGauge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import {FixedNumber} from 'ethers';
import {useDeepCompareMemo} from '@react-hookz/web';
import {VEYFI_GAUGE_ABI} from '@veYFI/utils/abi/veYFIGauge.abi';
import {VE_YFI_GAUGES,VEYFI_CHAIN_ID} from '@veYFI/utils/constants';
import {erc20ABI, readContracts} from '@wagmi/core';
import {readContracts} from '@wagmi/core';
import {useWeb3} from '@yearn-finance/web-lib/contexts/useWeb3';
import {allowanceKey, toAddress} from '@yearn-finance/web-lib/utils/address';
import {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 {useAsyncTrigger} from '@common/hooks/useAsyncEffect';
Expand Down Expand Up @@ -35,21 +35,18 @@ export type TGaugePosition = {
export type TGaugeContext = {
gaugesMap: TDict<TGauge | undefined>,
positionsMap: TDict<TGaugePosition | undefined>,
allowancesMap: TDict<TNormalizedBN>,
refresh: () => void,
}
const defaultProps: TGaugeContext = {
gaugesMap: {},
positionsMap: {},
allowancesMap: {},
refresh: (): void => undefined
};

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

const refreshVotingEscrow = useAsyncTrigger(async (): Promise<void> => {
Expand Down Expand Up @@ -87,38 +84,6 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
set_gauges(allGauges);
}, []);

const refreshAllowances = useAsyncTrigger(async (): Promise<void> => {
if (!gauges || !address) {
return;
}
const calls = [];
for (const gauge of Object.values(gauges)) {
calls.push({
address: gauge.vaultAddress,
abi: erc20ABI,
chainId: VEYFI_CHAIN_ID,
functionName: 'allowance',
args: [toAddress(address), gauge.address]
});
calls.push({
address: gauge.vaultAddress,
abi: erc20ABI,
chainId: VEYFI_CHAIN_ID,
functionName: 'decimals'
});

}
const results = await readContracts({contracts: calls});
const _allowancesMap: TDict<TNormalizedBN> = {};
let index = 0;
for (const gauge of Object.values(gauges)) {
const allowance = decodeAsBigInt(results[index++]);
const decimals = Number(decodeAsBigInt(results[index++])) || decodeAsNumber(results[index++]);
_allowancesMap[allowanceKey(VEYFI_CHAIN_ID, gauge.vaultAddress, gauge.address, toAddress(address))] = toNormalizedBN(allowance, decimals);
}
set_allowancesMap(_allowancesMap);
}, [address, gauges]);

const refreshPositions = useAsyncTrigger(async (): Promise<void> => {
if (!gauges || !isActive || !address) {
return;
Expand Down Expand Up @@ -186,15 +151,13 @@ export const GaugeContextApp = memo(function GaugeContextApp({children}: {childr
const refresh = useCallback((): void => {
refreshVotingEscrow();
refreshPositions();
refreshAllowances();
}, [refreshAllowances, refreshPositions, refreshVotingEscrow]);
}, [refreshPositions, refreshVotingEscrow]);

const contextValue = useDeepCompareMemo((): TGaugeContext => ({
gaugesMap: keyBy(gauges, 'address'),
positionsMap: positionsMap,
allowancesMap: allowancesMap ?? {},
refresh
}), [allowancesMap, gauges, positionsMap, refresh]);
}), [gauges, positionsMap, refresh]);

return (
<GaugeContext.Provider value={contextValue}>
Expand Down

1 comment on commit bde2799

@vercel
Copy link

@vercel vercel bot commented on bde2799 Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.