Understanding Constant Product AMM Theory

It’s important to understand how most AMM work today so let’s have a look at that. Let’s take the example of the most known example, Uniswap V2.

1) Swapping a token on Uniswap

The steps are basically the same as a traditional exchange : you put an amount of token that you own and want to sell and the desired token you would like to have in exchange. You don’t need a buyer to accept your offer hence the alias AMM (automated market maker).

So how is the price of the asset decided and who are you exchanging with?

2) Providing liquidity on Uniswap

Uniswap is double-sided: you “swap” tokens for another token with which it is paired. Most of the tokens are paired with ETH, but they can also be paired with any token. If the pair doesn’t exist, the protocol will find a way for you. For instance, if you want to swap LINK with EYE but the pair doesn’t exist, the protocol routes the transaction for you and makes the following swaps : LINK -> ETH and ETH -> EYE.

When you want to provide liquidity, you will put in a pool a pair of tokens that you own (for ex. ETH and EYE) in the same proportion. So if ETH is worth 2000$ and EYE 2$ the ratio of what you will have to put is 1000:1 EYE/ETH. So for ex 1 ETH and 1000 EYE, or .5 ETH and 500 EYE.

These pools of liquidity are what people swap their assets with and they also determine the price of the asset with respect to the other. If the EYE/ETH pool ratio is 1000:1, 1 ETH = 1000 EYE.

What changes the ratio (and the price)? Swapping. Here is the process: if the pool is made of 1 ETH and 1000 EYE and you want to swap 10 EYE for ETH your 10 EYE are going to be added to the 1000 EYE and some of the pooled ETH (~0.01 ETH here) will be given to you. The pool will be now constituted by 1010 EYE and approx. 0.99 ETH (neglecting fees).

How do you calculate how many tokens you will get when swapping? Uniswap uses a constant product in the number of tokens in the pool to calculate it. So basically, if the pool is constituted by 1 ETH and 1000 EYE, the product is currently 1 x 1000 = 1000. When you want to swap 10 EYE for ETH, you add those 10 EYE to the previous 1000 EYE. You get 1010. To find how much eth will be left in the pool, you have to search for the number N that will satisfy 1010 x N = 1000 (the constant product) which is 1000/1010 = ~0.99. The pool will be constituted of 1010 ETH and 0.99 ETH, you get the remaining ETH so 1–0.99 = 0.01 ETH. The ratio in the pool is now 1010:0.99 so ETH is now more expensive in EYE terms (you get 1010 EYE for 0.99 ETH).

3) Fees, Impermanent Loss (IL), Price Impact and Liquidity

Fees

Why would you provide liquidity? Because you earn fees for swaps that happen in the liquidity pool you’re contributing to. When you put your tokens in the pool you get Liquidity provider tokens (LP token) which represent your share of the pool. When you want to get back your original tokens, you give back your LP token to Uniswap (Uni) and it gives you back your part of the pool.

For any transaction in Uni, you will be charged a 0.3% fee. This fee is directly added to the pool so accrued to your LP token. If you own 1% of the pool for a day, you will get 0.3% x 1% of all the transactions that have been made during the day on the pair. So it can be rewarding BUT you have to own both tokens to pool PLUS you can lose money vs just holding the assets because of impermanent loss.

Impermanent Loss (IL)

Providing liquidity can be risky in particular if one of the tokens takes a lot of value in comparison to the other. Let’s see why. Let’s imagine you own 1% of a pool containing 1 ETH and 1000 EYE so you provided 0.010 ETH and 10 EYE. The price of EYE is 1$ and the price of ETH is 1000$.

Now imagine after some swaps the price changed and EYE is now worth 2$. ETH price didn’t change and is still 1000$ but 1 ETH is now worth 500 EYE. The pool, (with arbitrage) will reflect the price change and the ratio should now be 2:1000 = 1:500 but keeping the constant product of the number of tokens of 1000. So the pool will be constituted of ~1.41 ETH and ~709 EYE because 1.41 x 709 = 1000 (constant product) and 709/1.41 = 500 (ratio of the pool).

If you want your token back you will get 1% of the pool which is (neglecting fees) 0.014 ETH and 7.09 EYE. In dollar value, your holding is now worth 0.014 x 1000 + 7.09 x 2 =28.18$. If you had held without pooling, you would have had 0.01 x 1000 + 10 x 2 = 30$ so the loss relatively to hold is 1.82$ (~6%).

The fees you earn are there to mitigate the loss but in some situations, it is not enough.

Now let’s see why liquidity is important.

Price Impact and Liquidity

Liquidity is the ease to sell an asset at its right intrinsic value. The more liquidity you have in a pool, the less price impact there is on a trade. Whether you are buying or selling, a large price impact in a low liquidity pool is not optimal because you end up selling for cheaper or buying for a higher price. The price impact is less affected by large buys and sells in higher liquidity pools.

Here is an example: I want to swap 5 ETH for some EYE. In pool A you have 10 ETH and 10000 EYE and in pool B you have 100 ETH and 100 000 EYE so same ratio (and price) in pool A and B: 1:1000. With my 5 ETH, if I swap in pool A, I will get ~3333 EYE (=10 000 -(100 000/15)). If I swap in pool B I will get ~4761 EYE (= 100 000 — (10 000 000/105)).

The ratio in pool A is now 15:6666,66 = 1:444,44 so 1 ETH = 444,44 EYE when the ratio in pool B is 105:95 238 = 1:907 -> 1 ETH = 907 EYE so it works out to be a huge difference in price!

This is the end of this short thread on constant product AMM. Thanks for reading and don’t forget to clap !

Twitter account : @crypto_pao10

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Crypto Cheat Sheet

Crypto Cheat Sheet

Prendre du temps pour lire et comprendre fait gagner du temps