diff --git a/apps/web/src/lib/hooks/useUnderlyingTokenBalanceFromPool.ts b/apps/web/src/lib/hooks/useUnderlyingTokenBalanceFromPool.ts index fe5484d4b5..427484297b 100644 --- a/apps/web/src/lib/hooks/useUnderlyingTokenBalanceFromPool.ts +++ b/apps/web/src/lib/hooks/useUnderlyingTokenBalanceFromPool.ts @@ -1,8 +1,13 @@ 'use client' import { useMemo } from 'react' +import { uniswapV2PairAbi } from 'sushi/abi' +import { SUSHISWAP_V2_FACTORY_ADDRESS, SushiSwapV2ChainId } from 'sushi/config' import { Amount, Token, Type } from 'sushi/currency' import { ZERO } from 'sushi/math' +import { SushiSwapV2Pool } from 'sushi/pool' +import { zeroAddress } from 'viem' +import { useReadContracts } from 'wagmi' interface Params { totalSupply: Amount | undefined | null @@ -17,8 +22,52 @@ type UseUnderlyingTokenBalanceFromPairParams = ( export const useUnderlyingTokenBalanceFromPool: UseUnderlyingTokenBalanceFromPairParams = ({ balance, totalSupply, reserve1, reserve0 }) => { + const { data } = useReadContracts({ + contracts: [ + { + address: totalSupply + ? SUSHISWAP_V2_FACTORY_ADDRESS[ + totalSupply.currency.chainId as SushiSwapV2ChainId + ] + : undefined, + abi: [ + { + inputs: [], + name: 'feeTo', + outputs: [ + { + internalType: 'address', + name: '', + type: 'address', + }, + ], + stateMutability: 'view', + type: 'function', + }, + ], + functionName: 'feeTo', + }, + { + address: totalSupply?.currency.address, + abi: uniswapV2PairAbi, + functionName: 'kLast', + }, + ], + query: { + enabled: Boolean(totalSupply), + }, + }) + return useMemo(() => { - if (!balance || !totalSupply || !reserve0 || !reserve1) { + if ( + !balance || + !totalSupply || + !reserve0 || + !reserve1 || + !data?.every((data) => data.status === 'success') || + // this condition is a short-circuit in the case where balance updates sooner than totalSupply + totalSupply.lessThan(balance) + ) { return [undefined, undefined] } @@ -29,9 +78,33 @@ export const useUnderlyingTokenBalanceFromPool: UseUnderlyingTokenBalanceFromPai ] } + const feeEnabled = data[0].result !== zeroAddress + const kLast = data[1].result + + const _reserve0 = reserve0.currency.isNative + ? Amount.fromRawAmount(reserve0.currency.wrapped, reserve0.quotient) + : (reserve0 as Amount) + const _reserve1 = reserve1.currency.isNative + ? Amount.fromRawAmount(reserve1.currency.wrapped, reserve1.quotient) + : (reserve1 as Amount) + + const pool = new SushiSwapV2Pool(_reserve0, _reserve1) + return [ - reserve0.wrapped.multiply(balance.wrapped.divide(totalSupply)), - reserve1.wrapped.multiply(balance.wrapped.divide(totalSupply)), + pool.getLiquidityValue( + _reserve0.currency, + totalSupply, + balance as Amount, + feeEnabled, + kLast, + ), + pool.getLiquidityValue( + _reserve1.currency, + totalSupply, + balance as Amount, + feeEnabled, + kLast, + ), ] - }, [balance, reserve0, reserve1, totalSupply]) + }, [balance, reserve0, reserve1, totalSupply, data]) } diff --git a/apps/web/src/lib/wagmi/systems/Checker/ApproveERC20WithPermit.tsx b/apps/web/src/lib/wagmi/systems/Checker/ApproveERC20WithPermit.tsx index ce365e0b20..4a2837abfe 100644 --- a/apps/web/src/lib/wagmi/systems/Checker/ApproveERC20WithPermit.tsx +++ b/apps/web/src/lib/wagmi/systems/Checker/ApproveERC20WithPermit.tsx @@ -95,7 +95,7 @@ const _ApproveERC20WithPermit: FC = ({ }) useEffect(() => { - if (bytecode !== null) setApprovalType(ApprovalType.Approve) + if (bytecode) setApprovalType(ApprovalType.Approve) }, [bytecode]) const { setSignature } = useApprovedActions(tag)