Automated Market Makers - The backbone of DeFi
Decentralized Exchanges, or otherwise known as Dex are one of the most popular entities in DeFi. These facilitate the exchange and trading of various coins or tokens. While traditional finance relies on the order book model for pricing, DeFi Dexes use AMMs for the same purpose. Although these might seem like blackboxes, they are actually quite simple and an ingenious way to ensure demand and supply in DeFi.
Happy Reading!
Liquidity
This is a term you’ll hear a lot in DeFi. But what does it actually mean? Let’s break it down in a way that’s easy to understand.
Liquidity, in its simplest form, is how easily an asset can be bought or sold without causing a big change in its price. Think of it like this: If you own something (say a rare coin) and it’s hard to find a buyer, then that coin isn’t very liquid. But if you have cash, you can quickly exchange it for just about anything—cash is super liquid. In DeFi, liquidity refers to how easily you can trade cryptocurrencies without impacting their prices too much.
Liquidity Pools: Where Liquidity Comes From
In traditional finance, liquidity is provided by big institutions like banks. In DeFi, it’s a little different. Liquidity comes from what we call liquidity pools. These pools are essentially large collections of tokens locked into smart contracts, ready to be traded by users.
Imagine a pool as a bucket filled with two different tokens, like ETH and USDC. This pool allows people to swap ETH for USDC (and vice versa) whenever they need. The smart contract behind the pool which is generally referred to as the pair contract automatically handles the trade based on a mathematical formula that ensures there’s always enough of each token in the pool. This mathematical formula and the contract that enforces it, is the main focus of this article - AMM.
Liquidity Providers (LPs): The Heroes Behind the Scenes
So, where do these tokens in the liquidity pools come from? This is where liquidity providers, or LPs, come in. LPs are regular users who contribute their tokens to a pool to help provide liquidity. In return, they earn rewards—usually a cut of the trading fees that the pool generates.
It’s like LPs are lending their assets to the pool for others to trade with, and in exchange, they get a share of the profits. The more liquidity they provide, the more they can potentially earn. But, to become an LP, you must put in the two types of tokens present in the pool to ensure balance between both. The reason behind this will be more clear in the later sections.
Demand and Supply in DeFi - The advent of AMMs
Demand and supply is probably the oldest and most important concept of economics. This is what determines the price of anything in the real world. But how is it possible to realise this concept in DeFi in a fair manner? This is where AMMs come in. Automated Market Makers, otherwise known as AMMs use mathematical formulas to maintain a balance between the two tokens of a liquidity pool and determine pricing based on demand and supply. In Traditional finance, this is achieved by the order book model. Often, big fund houses who play the role of market makers, buy stocks from sellers and sell the same to buyers. They keep the difference between the bid price and the ask price which is known as the “bid-ask spread” as fees. In DeFi, this is handled automatically by a smart contract hence the name ‘Automated Market Maker’. There are various types of AMMs, Constant Product AMMs being the most popular one. Let us go over some of these one by one -
Constant Product AMMs
Constant Product AMMs (CPAMMs) use a simple mathematical formula :
$x * y = k$.
Here $k$ is a constant which should always remain same irrespective of the change in $x$ and $y$. In the liquidity pool, $x$ and $y$ represent the quantities of two tokens.
So, let’s say a pool holds USDC and ETH and initially, the value of 1 ETH is $1000. Since USDC is a stable coin, it maintains a 1:1 ratio with US Dollars. This means if there is 100 ETH initially in the pool, there should be 100,000 USDC in the pool as well. So, according to the CPAMM formula,
$x * y = k$
$100 * 100,000 = 10,000,000$
This value $10,000,000$ must always be constant. So, if Adam comes and wants to withdraw 5 ETH from the pool, let’s see how much he should have to pay for it in USDC.
$(100-5) * (100,000 + price) = 10,000,000$
$95 * (100,000 + price) = 10,000,000$
$(100,000 + price) = 10,000,000 / 95$
$(100,000 + price) = 105,263$
$price = 105,263 - 100,000$
$price = 5,263$
So, after the trade is complete, the liquidity pool will have 95 ETH and 105,263 USDC. And this means the price of ETH (in terms of USDC) will increase and the price of USDC (in terms of ETH) will decrease due to simple demand and supply.
Present price of ETH (in USDC): $105,263/95 = 1,108$
This increased from $1000.
Constant Sum AMMs
These are very similar to CPAMMs, but work on a different mathematical formula involving the sum of the token pair.
$x + y = k$
Here, $x$ and $y$ represent the same quantities as CPAMMs and as before, $k$ must be constant at any cost. However, these AMMs are avoided since they don’t provide infinite liquidity and one of the two tokens can be drained.
AMMs are not perfect
Although an integral part of the DeFi ecosystem, AMMs have still got a long way to go. Currently there are a multitude of problems involved with their use if done without caution. Problems like impermanent loss, price manipulation, slippage, etc. are very regular and often leave liquidity providers with heavy losses. While there are ways of mitigating these risks, they are not perfect and will definitely evolve with time. ___
Conclusion
The math behind AMMs can seems a bit daunting at first but this is a very important and interesting part of DeFi. In this article I have discussed only two types of AMMs but there are more, and in the future others might be created too. The risks and security concerns involved with the usage of AMMs definitely demands a seperate article.