Skip to content

Commit

Permalink
invalidate stale requests in useV3PositionFees
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahZinsmeister committed May 20, 2021
1 parent 5e30a4b commit ffc2015
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/hooks/useV3PositionFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ export function useV3PositionFees(
asWETH = false
): [CurrencyAmount<Currency>, CurrencyAmount<Currency>] | [undefined, undefined] {
const positionManager = useV3NFTPositionManagerContract(false)
const owner = useSingleCallResult(tokenId ? positionManager : null, 'ownerOf', [tokenId]).result?.[0]
const owner: string | undefined = useSingleCallResult(tokenId ? positionManager : null, 'ownerOf', [tokenId])
.result?.[0]

const tokenIdHexString = tokenId?.toHexString()
const latestBlockNumber = useBlockNumber()

// TODO find a way to get this into multicall
// 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 every block
const [amounts, setAmounts] = useState<[BigNumber, BigNumber]>()
useEffect(() => {
let stale = false

if (positionManager && tokenIdHexString && owner && typeof latestBlockNumber === 'number') {
positionManager.callStatic
.collect(
Expand All @@ -38,9 +40,13 @@ export function useV3PositionFees(
{ from: owner } // need to simulate the call as the owner
)
.then((results) => {
setAmounts([results.amount0, results.amount1])
if (!stale) setAmounts([results.amount0, results.amount1])
})
}

return () => {
stale = true
}
}, [positionManager, tokenIdHexString, owner, latestBlockNumber])

if (pool && amounts) {
Expand Down

0 comments on commit ffc2015

Please sign in to comment.