In our blog series on understanding decentralized exchanges, today, we’ll dig deeper into the concept of automated market makers i.e., the foundation of decentralized exchanges (DEXs). Automated market makers (AMMs) can be said to be one of the most important instruments in decentralized finance aKa DeFi ecosystem.
Automated market makers have made it possible for everyone to trade digital assets in a decentralized manner without relying on a centralized order book system or traditional interaction of buyers and sellers. First of all, before delving into what is an automated market maker, and how it works? Let’s first figure out the need for automated market makers in the first place.
Why Automated Market Makers?
Well, the answer lies in one word – liquidity. Liquidity refers to the ease of exchanging any asset for another asset or currency. So one might say, a house is not a very liquid asset as it takes time to sell the house to exchange its worth into your choice of fiat currency. On the other hand, gold is more liquid than a house as you can easily sell it to a jewelry maker.
Similarly, we measure the liquidity of a crypto exchange depending on the ease of exchanging any cryptocurrency on that particular exchange. So, when the first set of decentralized exchanges came into existence, all they did was connect buyers and sellers. Thus allowing users to trade cryptocurrencies with each other.
However, at the initial stage of DeFi with a small market, it was difficult to find enough people willing to trade and match them accordingly. There it was – the problem of limited liquidity as it was hard to trade coins on decentralized exchanges. There comes the concept of the automated market maker to solve this problem.
What is an Automated Market Maker?
An automated market maker basically allows traders to buy and sell cryptocurrencies using an algorithm that automatically measures and dictates the price of assets depending on the demand and supply. So with the case of AMMs, users trade directly with a pool of assets rather than with other users. Hence, removing the need for a coincidence of mutual demand while trading crypto assets.
For example, if a user wants to trade a bunch of mangoes for bananas and another user wants to trade bananas for mangoes. In this case, rather than each user waiting for another one to appear, with AMM we create a pool of mangoes and bananas and offer all the users to exchange mangoes with bananas or vice versa through this pool. This pool, here, is called a liquidity pool. Simple, isn’t it?
So, the example mentioned above can be said to be a gross simplification of the concept of AMM. But, how does an AMM actually work and dictate the pricing of assets in a liquidity pool? Well, let’s find out.
How Automated Market Makers Work?
To understand the working of an automated market maker, we first need to get ourselves acquainted with the constant product formula. Using this simple formula, an automated market maker dictates the pricing of assets against each other depending on the demand and supply. So here’s the constant product formula:
x * y = k
x = total number of asset A
y = total number of asset B
k = constant
So, here’s how it works. Let’s take our example of mangoes and bananas. First of all, let’s say one mango and one banana are worth the same i.e., $1. Now, let’s make a pool of 20,000 mangoes and 20,000 bananas.
As per our constant product formula:
x (no. Of bananas) = 20,000
y (no. of mangoes) = 20,000
x * y = k
So our k, the constant is,
k = 20,000 * 20,000
k = 400,000,000
Here we deducted our constant is 400 million. Now, with this formula, all we have to do is make sure the value of k (400 million) stays the same with every trade. Now, let’s say a user comes along wanted to sell 5000 bananas for mangoes. So, in this case, the user deposits the given 5000 bananas in our liquidity pool. Now, with additional 5000 bananas, the pool looks like this:
x = 20,000 + 5000 = 25,000
y = 20,000
Hence, to keep the k constant, we need to change the value of y,
New y = k (constant) / new x
y = 400,000,000 / 25,000
y = 16,000
So, using the formula, we deducted the value of y in the pool should be 16,000. Hence, the difference of earlier y and new y should be transferred to the trader in exchange for her bananas i.e., 4000 mangoes.
Now, after one trade, our pool has 25,000 bananas and 16,000 mangoes while keeping the k constant. Hence we can conclude that the price of mangoes has now increased to $1.5625 (25,000/16,000) whereas the bananas have fallen to $0.64 (16,000/25,000) as per the demand and supply.
As we’ve just witnessed with our example, the algorithm automatically dictates the prices of assets depending on the demand. Here, in this case, the price of mangoes increased with the demand whereas the price of bananas decreased upon fall in demand.
Now, let’s say another user comes along who wants to trade 1000 mangoes for bananas. With our earlier case, we can already see that after this trade, the price of bananas will go back up a little with new demand. Similarly, the price of mangoes will fall down a little. Let’s check it out.
New y (mangoes) = 16,000 + 1000 = 17,000
Finding new x (bananas) = 400,000,000 / 17,000
x = 23529.411
So, the user will get 1471 (25,000 – 23529) bananas in exchange for 1000 mangoes. After this trade, the price of a banana will rise to $0.72, and of mangoes will fall to $1.384. Whereas the k stays the constant of 400,000,000 (23529.411 * 17,000).
I hope you understood a simple explanation of working of automated market maker while following a constant product formula. The above example is one of the dominant AMM models being used by Uniswap. Some other DeFi projects such as Balancer and Curve uses different AMM models we’ll discuss in coming blog posts.