Prices and fees
AMM protocol is based on the “Constant Product” formula:
$x * y = k$
where
$x$
and
$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
$x$
of token AAA and
$y$
of token BBB and the user sells aaa of token AAA. If no fees are charged, then the received amount of
$b$
is:
$\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)/y$
tokens AAA per token BBB. With fee charge things will be slightly different:
$\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)$
tokens AAA per token BBB. The fee goes to the pool, increases the reserves, and thereby increases the
$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.