There is no commentSelect some text and then click Comment, or simply add a comment to this page from below to start a discussion.
Fee calculation vs invariant in Uniswap V3
In v3, fee tokens are held/tracked separately from the liquidity pool. This means that, unlike in v2, the invariant product does not change after a trade, as we see below.
Consider a specific pool with a given range-position, and suppose it has virtual reserves corresponding to assets X, Y respectively. Suppose these reserves initially satisfy the product-invariant . When a trader wants to put in amount of X tokens and receive Y tokens in exchange, here is how we can calculate they will receive, taking fees into account. Suppose the fee rate is , e.g. .
Reduce the amount of X tokens converted, by the fee, so effectively they will convert of X
Plug this reduced amount into the invariant formula to get the corresponding :
Track the fee paid as amount of X tokens
Note that due to the non-fungibility of liquidity pools in v3, the fees are kept separate from the pool. Here are some relevant highlights from the v3 whitepaper:
the exact same invariant continues to hold for the virtual reserves in a given liquidity pool.
This is in contrast to v2, where the is added back to the X reserves, so the new level of X tokens is . But since was calculated to satisfy the above equation, the new product of reserves will exceed slightly. This is in fact what the v1 whitepaper says (the fee mechanism is identical to v2):