Uniswap AMMs: The Magic Formula Decoded
Everything you need to know about Uniswap's AMM equation
Curious about the math behind AMMs like @Uniswap?
Here's everything you need to know about it 👇
1️⃣ The Magical Equation 🧝
Let's look at the equation behind the Uniswap AMM
X * Y = K also known as the Constant Product Formula
• X = Initial Quantity of Token 1 ($aTKN) in the pool
• Y = Initial quantity of Token 2 ($bTKN) in the pool
• K = Constant value
2️⃣ Let’s look at an example 📊
Let's look at an example to understand this.
📌 Suppose at the initial state the pool has:
X = 100 $aTKN
Y = 100 $bTKN
So, 100 * 100 = 10000 = K(constant)
This constant is fixed and can't be altered.
📌 Suppose you want to buy 20 $bTKN in exchange for $aTKN
So how many $aTKN do you need to deposit into the pool?
📌 Remember the constant product formula:
X * Y = K
i.e → $aTKN * $bTKN = K
Change (△) = | Initial state - Final state |
📌 At the final state of the pool,
△ $bTKN = 100 - 80 = 20
→ (100 - 20) * $aTKN = 10000
→ $aTKN = 10000/80 = 125
So there needs to be 125 $aTKN in the final state of the pool to maintain the value of K
△ aTKN = 125 - 100 = 25
So you need to deposit 25 $aTKN to get 20 $bTKN.
3️⃣ Final Step : Determine token price
Now let's determine the price of the tokens w.r.t each other at the final stage.
This is the graph of the above equation
And the SLOPE of this graph will give you the PRICE of the X and Y.
So what's a Slope? 👇️
And as you can see in this video as you deposit more and more token X, the slope (or price) falls very sharply.
The Red line is the slope. https://t.co/W1MHdHHuHZ
— The Crypto Illuminati (@0x_illuminati)
Jul 29, 2022