2022-01-12 21:06:22 +03:00
|
|
|
import { BigNumber } from '@ethersproject/bignumber'
|
2021-12-02 21:35:39 +03:00
|
|
|
import { Currency, CurrencyAmount } from '@uniswap/sdk-core'
|
|
|
|
import { Pool } from '@uniswap/v3-sdk'
|
2022-01-13 19:54:08 +03:00
|
|
|
import { useSingleCallResult } from 'lib/hooks/multicall'
|
2022-01-13 06:36:08 +03:00
|
|
|
import useBlockNumber from 'lib/hooks/useBlockNumber'
|
2021-12-02 21:35:39 +03:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { unwrappedToken } from 'utils/unwrappedToken'
|
|
|
|
|
|
|
|
import { useV3NFTPositionManagerContract } from './useContract'
|
|
|
|
|
|
|
|
const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1)
|
|
|
|
|
|
|
|
// compute current + counterfactual fees for a v3 position
|
|
|
|
export function useV3PositionFees(
|
|
|
|
pool?: Pool,
|
|
|
|
tokenId?: BigNumber,
|
|
|
|
asWETH = false
|
|
|
|
): [CurrencyAmount<Currency>, CurrencyAmount<Currency>] | [undefined, undefined] {
|
|
|
|
const positionManager = useV3NFTPositionManagerContract(false)
|
|
|
|
const owner: string | undefined = useSingleCallResult(tokenId ? positionManager : null, 'ownerOf', [tokenId])
|
|
|
|
.result?.[0]
|
|
|
|
|
|
|
|
const tokenIdHexString = tokenId?.toHexString()
|
|
|
|
const latestBlockNumber = useBlockNumber()
|
|
|
|
|
2022-04-12 18:41:31 +03:00
|
|
|
// we can't use multicall for this because we need to simulate the call from a specific address
|
2021-12-02 21:35:39 +03:00
|
|
|
// latestBlockNumber is included to ensure data stays up-to-date every block
|
2022-04-12 18:41:31 +03:00
|
|
|
const [amounts, setAmounts] = useState<[BigNumber, BigNumber] | undefined>()
|
2021-12-02 21:35:39 +03:00
|
|
|
useEffect(() => {
|
2022-04-12 18:41:31 +03:00
|
|
|
if (positionManager && tokenIdHexString && owner) {
|
2021-12-02 21:35:39 +03:00
|
|
|
positionManager.callStatic
|
|
|
|
.collect(
|
|
|
|
{
|
|
|
|
tokenId: tokenIdHexString,
|
|
|
|
recipient: owner, // some tokens might fail if transferred to address(0)
|
|
|
|
amount0Max: MAX_UINT128,
|
|
|
|
amount1Max: MAX_UINT128,
|
|
|
|
},
|
|
|
|
{ from: owner } // need to simulate the call as the owner
|
|
|
|
)
|
|
|
|
.then((results) => {
|
2022-04-12 18:41:31 +03:00
|
|
|
setAmounts([results.amount0, results.amount1])
|
2021-12-02 21:35:39 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [positionManager, tokenIdHexString, owner, latestBlockNumber])
|
|
|
|
|
|
|
|
if (pool && amounts) {
|
|
|
|
return [
|
2022-04-12 18:41:31 +03:00
|
|
|
CurrencyAmount.fromRawAmount(asWETH ? pool.token0 : unwrappedToken(pool.token0), amounts[0].toString()),
|
|
|
|
CurrencyAmount.fromRawAmount(asWETH ? pool.token1 : unwrappedToken(pool.token1), amounts[1].toString()),
|
2021-12-02 21:35:39 +03:00
|
|
|
]
|
|
|
|
} else {
|
|
|
|
return [undefined, undefined]
|
|
|
|
}
|
|
|
|
}
|