AMM (Liquidity Pools Algorithm)

0xWeiss
3 min readJun 7, 2022

As I promised in the DEFI III episode, we are going to see what AMM(automated market maker) is.

It is an algorithm that allows traders to buy and sell certain coins. Basically, it dictates how much expensive some token should be based on how much of it there is.

Imagine hundreds of years ago when they were trading food between villages. Your village is very rich with apples and a nearby village is rich in oranges, and you both want to trade them. Let’s say every village gives the other one 1000 units of its autochthon fruit, and they are kept in a magic box that uses an algorithm that is called “Constant Product Automated Market Maker” with the formula X * Y = K.

So this formula means that will take both quantities of fruits(X and Y) and will be multiplied so that they always give the same result, in this case, 1 million(1000apples*1000oranges).

This way if 1 year there are fewer oranges, the oranges will be more expensive to trade with/to buy.

It’s everything based on supply and demand. If you want to buy apples, and another village also wants apples and so on, the apples will be far more expensive. Imagine that now, you add 500 oranges more to the box, that will add to 1500 total oranges and 1000 apples.

So if the total multiplied value is 1million(K), the apples should go down to 667, because 667 times 1500 is 1 million. So you will be given in exchange for this 500 oranges, 333 apples. Because 1000–667 is 333.

Let’s talk with the same example, converting this mystery box in a liquidity pool, and the quantity of 1 unit of each fruit is 1 dollar. So if the 1000 apples are a thousand dollars, and the price of the entire set of oranges should be also 1000 dollars. But we have 1500 oranges so the price for each orange will obviously be lower. In this case more or less 0.67 cents for each orange.

Therefore, as you saw in the examples, always it will remain the same total price because you will continue having 1k worth of apples and 1k worth of oranges. So that’s how the liquidity pool algorithm works.

An example for you to understand:

Let’s say that there are now 1230 oranges and 813 apples, and you, the person who is reading this, wants to add 150 apples in exchange for some oranges. How many oranges will you get back?

The solution will be below, don’t look at it.

X*Y = Z

You are trying to find the number of oranges that will remain in the pool, in order for you to find how many you get from the trade.

First, we have to divide the 1million in total, because it always will remain the same, between the number of apples (963). This gives us the remaining number of oranges that should stay in the liquidity pool.

For the formula to make sense, in this case, it adds up to 1038 oranges. So basically the oranges that you will get are: 1230–1038 = 192 oranges.

--

--