Prices and fees
AMM protocol is based on the “Constant Product” formula:
xy=kx * y = k
where
xx
and
y y
are reserve balances, and k is the invariant that remains unchanged during the trading operation. This formula underlies how the exchange price is calculated.
For example, the case where the initial reserves are
xx
of token AAA and
yy
of token BBB and the user sells aaa of token AAA. If no fees are charged, then the received amount of
bb
is:
{xy=k(x+a)(yb)=kxy=(x+a)(yb)xy=xy+ayb(x+a)b(x+a)=ayb=ay/(x+a)\begin{cases} x*y=k \\ (x+a)*(y-b)=k \end{cases}\\ x*y=(x+a)*(y-b) \\ x*y=x*y+a*y-b*(x+a) \\ b*(x+a)=a*y \\ b=a*y/(x+a)
Then the price is
p=(x+a)/yp=(x+a)/y
tokens AAA per token BBB. With fee charge things will be slightly different:
{xy=k(x+0.997a)(yb)=kxy=(x+0.997a)(yb)xy=xy+0.997ayb(x+0.997a)b(x+0.997a)=0.997ayb=0.997ay/(x+0.997a)\begin{cases} x*y=k \\ (x+0.997*a)*(y-b)=k \end{cases}\\ x*y=(x+0.997*a)*(y-b) \\ x*y=x*y+0.997*a*y-b*(x+0.997*a) \\ b*(x+0.997*a)=0.997*a*y \\ b=0.997*a*y/(x+0.997*a)
The price is then
p=(x+0.997a)/(0.997y)p=(x+0.997a)/(0.997y)
tokens AAA per token BBB. The fee goes to the pool, increases the reserves, and thereby increases the
k k
invariant. It entails the growth of the value underlying each share and becomes the source of LP providers' gains.
If price altering results in significant differences between internal and external market prices, arbitrage opportunities will push the price back to the rational value.
Last modified 5mo ago
Copy link