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

Fix/v2 amounts #1603

Merged
merged 3 commits into from
Aug 13, 2024
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
81 changes: 77 additions & 4 deletions apps/web/src/lib/hooks/useUnderlyingTokenBalanceFromPool.ts
Original file line number Diff line number Diff line change
@@ -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<Token> | undefined | null
Expand All @@ -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]
}

Expand All @@ -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<Token>)
const _reserve1 = reserve1.currency.isNative
? Amount.fromRawAmount(reserve1.currency.wrapped, reserve1.quotient)
: (reserve1 as Amount<Token>)

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<Token>,
feeEnabled,
kLast,
),
pool.getLiquidityValue(
_reserve1.currency,
totalSupply,
balance as Amount<Token>,
feeEnabled,
kLast,
),
]
}, [balance, reserve0, reserve1, totalSupply])
}, [balance, reserve0, reserve1, totalSupply, data])
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const _ApproveERC20WithPermit: FC<ApproveERC20WithPermitProps> = ({
})

useEffect(() => {
if (bytecode !== null) setApprovalType(ApprovalType.Approve)
if (bytecode) setApprovalType(ApprovalType.Approve)
}, [bytecode])

const { setSignature } = useApprovedActions(tag)
Expand Down
Loading