# Uniswap AMMs: The Magic Formula Decoded

### Everything you need to know about Uniswap's AMM equation

Kadeem Clarke

March 12, 2023

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?

Let's calculate...

#### πΒ **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