3

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) ?)
  • 1
    The schema was definitely wrong and is now fixed. It still doesn't explain how calculations are made though, and contains misleading information. For instance, the 3% fee applies in the contract to the traded amount (amountIn), while the schema implies that the fee should be used to calculate the traded amount. Not a reliable source of info for someone who wants to dive in. – Jean-Baptiste Martin Dec 09 '22 at 16:01
  • @Jean-BaptisteMartin I moved on since then, but thanks for your comment. – Firmin Martin Dec 10 '22 at 00:57

0 Answers0