[Ethereum] Trying to make sense of Uniswap v3 fees (feeGrowthInside0LastX128, feeGrowthGlobal0X128)

liquidity-providersolidityuniswap

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:

// calculate accumulated fees
  uint128 tokensOwed0 =
     uint128(
        FullMath.mulDiv(
           feeGrowthInside0X128 - _self.feeGrowthInside0LastX128,
           _self.liquidity,
           FixedPoint128.Q128
        )
     );

This would translate to this in javascript, using bignumber.js

const tokensOwed0 = feeGrowthInside0LastX128
    .times(liquidity)
    .div(new bn(2).pow(128));
const tokensOwed1 = feeGrowthInside1LastX128
    .times(liquidity)
    .div(new bn(2).pow(128));

You may need to divide further by the decimals of the underlying ERC20

Related Topic