Commit f5fc5da3 authored by Noah Zinsmeister's avatar Noah Zinsmeister Committed by GitHub

fix erroneous addition of tokensOwned{0,1} (#1533)

only pass tokenId to useV3PositionFees
parent bea5c048
import { useSingleCallResult } from 'state/multicall/hooks' import { useSingleCallResult } from 'state/multicall/hooks'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { PositionDetails } from 'types/position'
import { useV3NFTPositionManagerContract } from './useContract' import { useV3NFTPositionManagerContract } from './useContract'
import { BigNumber } from '@ethersproject/bignumber' import { BigNumber } from '@ethersproject/bignumber'
import { Pool } from '@uniswap/v3-sdk' import { Pool } from '@uniswap/v3-sdk'
...@@ -12,26 +11,24 @@ const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1) ...@@ -12,26 +11,24 @@ const MAX_UINT128 = BigNumber.from(2).pow(128).sub(1)
// compute current + counterfactual fees for a v3 position // compute current + counterfactual fees for a v3 position
export function useV3PositionFees( export function useV3PositionFees(
pool?: Pool, pool?: Pool,
positionDetails?: PositionDetails tokenId?: BigNumber
): [CurrencyAmount<Token>, CurrencyAmount<Token>] | [undefined, undefined] { ): [CurrencyAmount<Token>, CurrencyAmount<Token>] | [undefined, undefined] {
const positionManager = useV3NFTPositionManagerContract(false) const positionManager = useV3NFTPositionManagerContract(false)
const owner = useSingleCallResult(positionDetails?.tokenId ? positionManager : null, 'ownerOf', [ const owner = useSingleCallResult(tokenId ? positionManager : null, 'ownerOf', [tokenId]).result?.[0]
positionDetails?.tokenId,
]).result?.[0]
const tokenId = positionDetails?.tokenId?.toHexString() const tokenIdHexString = tokenId?.toHexString()
const latestBlockNumber = useBlockNumber() const latestBlockNumber = useBlockNumber()
// TODO find a way to get this into multicall // TODO find a way to get this into multicall
// because fees don't ever go down, we don't actually need to clear this state // because these amounts don't ever go down, we don't actually need to clear this state
// latestBlockNumber is included to ensure data stays up-to-date fresh // latestBlockNumber is included to ensure data stays up-to-date every block
const [amounts, setAmounts] = useState<[BigNumber, BigNumber]>() const [amounts, setAmounts] = useState<[BigNumber, BigNumber]>()
useEffect(() => { useEffect(() => {
if (positionManager && tokenId && owner && typeof latestBlockNumber === 'number') { if (positionManager && tokenIdHexString && owner && typeof latestBlockNumber === 'number') {
positionManager.callStatic positionManager.callStatic
.collect( .collect(
{ {
tokenId, tokenId: tokenIdHexString,
recipient: owner, // some tokens might fail if transferred to address(0) recipient: owner, // some tokens might fail if transferred to address(0)
amount0Max: MAX_UINT128, amount0Max: MAX_UINT128,
amount1Max: MAX_UINT128, amount1Max: MAX_UINT128,
...@@ -42,12 +39,12 @@ export function useV3PositionFees( ...@@ -42,12 +39,12 @@ export function useV3PositionFees(
setAmounts([results.amount0, results.amount1]) setAmounts([results.amount0, results.amount1])
}) })
} }
}, [positionManager, tokenId, owner, latestBlockNumber]) }, [positionManager, tokenIdHexString, owner, latestBlockNumber])
if (pool && positionDetails && amounts) { if (pool && amounts) {
return [ return [
CurrencyAmount.fromRawAmount(pool.token0, positionDetails.tokensOwed0.add(amounts[0]).toString()), CurrencyAmount.fromRawAmount(pool.token0, amounts[0].toString()),
CurrencyAmount.fromRawAmount(pool.token1, positionDetails.tokensOwed1.add(amounts[1]).toString()), CurrencyAmount.fromRawAmount(pool.token1, amounts[1].toString()),
] ]
} else { } else {
return [undefined, undefined] return [undefined, undefined]
......
...@@ -243,7 +243,7 @@ export function PositionPage({ ...@@ -243,7 +243,7 @@ export function PositionPage({
}, [inverted, pool, priceLower, priceUpper]) }, [inverted, pool, priceLower, priceUpper])
// fees // fees
const [feeValue0, feeValue1] = useV3PositionFees(pool ?? undefined, positionDetails) const [feeValue0, feeValue1] = useV3PositionFees(pool ?? undefined, positionDetails?.tokenId)
const [collecting, setCollecting] = useState<boolean>(false) const [collecting, setCollecting] = useState<boolean>(false)
const [collectMigrationHash, setCollectMigrationHash] = useState<string | null>(null) const [collectMigrationHash, setCollectMigrationHash] = useState<string | null>(null)
......
...@@ -63,7 +63,7 @@ export function useDerivedV3BurnInfo( ...@@ -63,7 +63,7 @@ export function useDerivedV3BurnInfo(
liquidityPercentage.multiply(positionSDK.amount1.quotient).quotient liquidityPercentage.multiply(positionSDK.amount1.quotient).quotient
) )
const [feeValue0, feeValue1] = useV3PositionFees(pool ?? undefined, position) const [feeValue0, feeValue1] = useV3PositionFees(pool ?? undefined, position?.tokenId)
const outOfRange = const outOfRange =
pool && position ? pool.tickCurrent < position.tickLower || pool.tickCurrent > position.tickUpper : false pool && position ? pool.tickCurrent < position.tickLower || pool.tickCurrent > position.tickUpper : false
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment