As you progress in your journey as a smart contract auditor it's vital to have a deep understanding of DeFi primitives.
Perhaps the most important of these is the AMM (automated-market-maker).
Here's the ultimate guide to the AMM just for you š š§µ
There are many kinds of AMMs:
⢠Constant Mean
⢠Constant Sum
⢠Hybrid Function
⢠Dynamic Automated
But today, we'll deep dive into the most commonly adopted model, the Constant Product AMM.
1ļøā£ What is the Constant Product AMM?
Most famously implemented by Uniswap, the Constant Product AMM is essentially a formula that determines a price based on the supply of each token in a market.
The Constant Product formula is simply the following:
(Amt of Token A) * (Amt of Token B) = K
Where K is a measure of the pool's liquidity.
2ļøā£ Swapping
Let's walk through a swap to see how the formula works.
We begin with a pool containing Ether and Dai.
X - Ether
Y - Dai
There are 5 Ether and 25,000 Dai in the pool.
Therefore the current price of Ether in the pool is 25,000 Dai / 5 Ether = 5,000 Dai / Ether.
The current state of the pool can be visualized on the X * Y = K curve. š
Now I swap 5,000 Dai for Ether.
The change in the y value (dy) = 5,000.
The change in the x value (dx) = The amount of Ether I get out of the swap.
I've shifted the point on the X * Y = K Curve in the following way:
Now we can compute the amount I will get out of the swap by applying dy to our X * Y = K formula.
This results in me receiving roughly .833 Ether from the swap.
This means I experienced ~$1,000 in price slippage!
Here's the price for Ether I experienced during my swap:
3ļøā£ Providing Liquidity
Notice that while I was swapping, the K value was constant and this is what gave the price of Ether I experienced.
The larger the K value, the less a swapper is price impacted on their swap.
The only way the K value can increase is when liquidity providers deposit more of token X & token Y into the pool.
This way the K value is a proxy for how much "liquidity" is in the pool.
Let's walk through the math behind adding liquidity and examine how more liquidity ā less price impact.
We begin with our previous example of a pool containing Ether and Dai.
There are 5 Ether and 25,000 Dai in the pool ā K = 125,000.
Let's say there are 5 LP tokens that track the deposits into the pool so far.
When adding liquidity I can only deposit Ether & Dai at the same ratio as the current pool balance.
Otherwise, my liquidity deposit would change the price of the assets in the pool.
So I add 5 Ether & 25,000 Dai to the pool.
We can compute the # of LP tokens I receive by examining how much I increased the liquidity of either token and translating that to the current LP token supply.
Let's compute my received LP tokens by examining how I increased the liquidity of Dai.
My deposit increased the supply of Dai in the pool by 100%, therefore 100% of the current LP token supply will be minted to me.
Since I doubled both the amount of X tokens and Y tokens, the K value increases by a factor of 4.
The price of Ether in the pool is still 5,000:
50,000 Dai / 10 Ether = 5,000 Dai / Ether
Now when I swap my 5,000 Dai, I'll receive the following amount of Ether:
I got more Ether out of my swap!
Here's the price I experienced during this swap:
Providing liquidity allows swappers to more efficiently trade their tokens without moving so far on the X * Y = K curve.
In return for improving slippage for swappers, LPs collect a fee on every swap.
However, there's one catch that LPs must consider: impermanent loss.
4ļøā£ Impermanent Loss
Impermanent loss refers to the relative losses that LPs realize when the supplies of each token in the pool diverge.
Let's examine exactly how impermanent loss plays out.š
Let's build on our running example of a pool containing Ether and Dai.
There are 10 Ether and 50,000 Dai in the pool.
I've deposited 5 Ether and 25,000 Dai, a total value of $50,000 at the current market price.
I hold 5/10 of the LP tokens for the pool
Now let's consider one large trader (or many individual traders over time) swaps 5 Ether for Dai in the pool.
The resulting pool balance will be:
15 Ether | ~33,333 Dai
Making the price of Ether:
~33,333 Dai / 15 Ether = ~2,222 Dai / Ether
Now when I withdraw my 5 LP tokens (50% of the supply) I receive half of the assets in the pool:
7.5 Ether | ~16,666 Dai
At the current market price, my assets are worth:
However, if I had simply held my 5 Ether and 25,000 Dai rather than depositing them into the pool, here's what my assets would have been worth:
This means I experienced the following impermanent loss:
33,333 ($ from LPing) - 36,110 ($ from holding) = -$2,777
This occurred because I decided to provide liquidity rather than simply hold my assets.
Impermanent loss occurs no matter which direction the price of the pool assets move, it simply arises when the token imbalance increases.
This is because every time a swap is made, the price of the output token is increased & the pool is left with the "losing" input token.
5ļøā£ That's everything
That's all there is to the Constant Product AMM!
Save this thread, go over it a few times, work through the formulas and everything will sink in.