Try   HackMD

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

x,y corresponding to assets X, Y respectively. Suppose these reserves initially satisfy the product-invariant
xy=L2
. When a trader wants to put in
Δx
amount of X tokens and receive
Δy
Y tokens in exchange, here is how we can calculate
Δy
they will receive, taking fees into account. Suppose the fee rate is
1−γ
, e.g.
1−γ=0.003
.

  1. Reduce the amount of X tokens converted, by the fee, so effectively they will convert
    γΔx
    of X
  2. Plug this reduced amount into the invariant formula to get the corresponding
    Δy
    :
    (x+γΔx)(y−Δy)=L2
  3. Track the fee paid as
    (1−γ)Δx
    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:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

As a consequence,

the exact same invariant continues to hold for the virtual reserves in a given liquidity pool.

This is in contrast to v2, where the

(1−γ)Δx is added back to the X reserves, so the new level of X tokens is
x+γΔx+(1−γ)Δx=x+Δx
. But since
Δy
was calculated to satisfy the above equation, the new product of reserves
(x+Δx)(y−Δy)
will exceed
L2
slightly. This is in fact what the v1 whitepaper says (the fee mechanism is identical to v2):

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →