I'm trying to figure out the uncollected fees for my Uniswap v3 position programmatically.
If I interpret the v3 whitepaper correctly it should be liquidity multiplied by the difference of feeGrowthGlobal0X128 and feeGrowthInside0LastX128.
What math shall I actually do to the feeGrowth* values that Uniswap contracts return to me? They are supposed to be Q128.128 values, so my expectation was I need to divide thm by 2**128 in order to get to actual number. Apparently it's wrong as the results I'm getting are far from what I see on the pool page.
For example, feeGrowthGlobal0X128 for the USDC/ETH v3 pool (0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8) is currently 706909750615834684891740623881648
What do I need to do with this number to come to "fees accumulated per unit of virtual liquidity"?
Best Answer
There is a bunch of functions on the smart contract that explains how to use the liquidity field: In Uniswap V3 core repository, in Position.sol line 60:
This would translate to this in javascript, using bignumber.js
You may need to divide further by the decimals of the underlying ERC20