1

Denote respectively rA and rB the reserve of Token A and Token B. Let f = 1 - fee (i.e. 0.997) and x be the input amount. Then the price function of Uniswap v2 for a pair (A, B) is

P(x) = (rB * x * f)/(rA + x * f)

This answer explains well how the source code computes it: the computation is based upon the preservation of the "constant product formula": rA * rB = k, where k is a constant. In other word, the product of the new reservers is the same as the old: rA' * rB' = rA * rB = k. But this seems to contradict the documentation of Uniswap v2 which says that

In practice, Uniswap applies a 0.30% fee to trades, which is added to reserves. As a result, each trade actually increases k.

Moreover, the answer seems to suggest that the new reserves are rA' = rA + x * f and rB' = rB - P(x). I was rather confused when I have seen the diagram below from the documentation.

enter image description here

Here are my questions:

  1. If k actually increase at each trade, then why the price is computed in such a way (i.e. based on preservation of k) ?
  2. What should be the value of rA' on the diagram? Let x = 3, rA = 1200, then by the computation above, rA' = rA + x * f < 1203. It looks like that the illustration has multiple errors:
    • It says that the fee is 0.03%, but it should be 0.3%
    • It doesn't agree whether rA' is 1203.03 or 1203.3. But both are wrong either way.
  3. What the "price" in blue on the diagram actually means ? (why it doesn't use P(x) ?)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.