Skip to main content
AlgorithmsCryptocurrencyDeFi

The engine behind decentralized exchanges: A closer look at AMMs and liquidity pools

By May 10, 2023November 19th, 2024No Comments5 min read

AMMs

Cryptocurrencies can be traded on two types of exchanges: centralized exchanges (CEX) and decentralized exchanges (DEX).

CEXs operate like traditional exchanges, based on an order book system. This means that they collect sell offers and match them with buy offers, executing a transaction if both parties’ price expectations align. Some of the most popular CEXs are Binance and Coinbase.

DEXs operate on the principle of Automatic Market Makers (AMM), which are automated platforms for token exchanges. You can think of it as a vending machine that allows you to exchange two tokens at a calculated price at any given moment. As you probably know, blockchain applications are managed by smart contracts. The mechanism is transparent, and in the case of larger exchanges, it has certainly undergone numerous security audits.

Liquidity Pools

When using centralized exchanges to sell a token, we need a buyer on the other side of the transaction. In reality, users trade directly with each other, and the exchange merely acts as an intermediary.

In the case of DEXs, the user interacts with a smart contract, which creates a market situation for them. Hence the name Automatic Market Maker :). How does such a smart contract have the funds to exchange our tokens? How does it know the price to sell them at? Each smart contract on a DEX must operate on a liquidity pool.

A liquidity pool (LP) is the pool of funds used by the exchange protocol to enable users to trade tokens. The smart contract must have the funds to payout exchanged tokens. Where do the funds in the liquidity pool come from? This is one area where decentralization showcases its true beauty. Each of us can deposit funds into such a pool.

Suppose a decentralized exchange allows trading on the ETH/USDC pair, meaning we can freely exchange ETH tokens for USDC and vice versa. For this to be possible, there must be an adequate amount of both tokens in the smart contract.

Liquidity Providers

The greatest strength of DeFi applications is decentralization. Liquidity pools also do not rely on a single central bank or guarantee fund. Any user can become a liquidity provider (LP).

Liquidity providers deposit their tokens into a liquidity pool, which then becomes available for trading by other users. In return for providing liquidity, LPs earn fees generated from the transactions in the pool. The standard fee for exchanging tokens (swap fee) ranges from 0.05% to 1%.

Earnings of liquidity providers depend on the frequency of transactions in a given pool and its size. A small pool generating significant traffic can drive interest rates to several hundred percent annually. However, this scenario exposes us to pool imbalance and unfavorable arbitrage. Typically, in larger, more secure pools, interest rates range from several to several dozen percent per year.

Let’s do some math

Let’s examine how this mechanism works. When creating such a pool, the ratio between the tokens is exactly 50:50. Assume that there are 10 ETH tokens (for calculation purposes, let’s assume that 1 ETH = 1000 USDC) and 10,000 USDC in the pool.

If Marek logs into the exchange and decides to buy 1 ETH for around 1000 USDC, the ratio of tokens in the pool will be disrupted. Suddenly, we have approximately 11,000 USDC and only 9 ETH in the pool. The algorithm managing the liquidity pool calculates the new token prices based on their ratio in the pool. After this transaction, the price of 1 ETH is around 1222 USDC.

This price is much higher than the market price, so nobody will likely buy ETH from this pool anymore. Let’s look at this situation from the perspective of an ETH holder. Due to the imbalance in the pool, they can sell their tokens at a higher price than the market price, shifting the token ratio towards equilibrium. After selling 1 ETH, the AMM will sell the tokens at a dynamic price (the first token will receive a higher price than the second). In the end, the pool will return to relative equilibrium and continue its operation.

In existing decentralized exchanges, liquidity pools are much “deeper” (holding more funds), so disrupting their balance requires a vast number of tokens. When the pool’s price deviates from the market price, traders can take advantage of this difference through a process called arbitrage. They can buy low on one market (e.g., a centralized exchange) and sell high on another (e.g., the DEX with the imbalanced pool), thus profiting from the discrepancy. This mechanism naturally brings pools back to equilibrium.

Examples of Liquidity Pools

Here are some examples of funds deposited (TVL – total value locked) in liquidity pools on the largest exchanges:

  • FRAX / USDC – 508mln USD (Curve finance)
  • DAI / USDC / USDT – 396 mln USD (Curve finance)
  • USDC / ETH – 287 mln USD (Uniswap)
  • WBTC / ETH – 241 mln USD (Uniswap)

In summary, liquidity providers play a crucial role in maintaining decentralized exchanges by supplying the funds necessary for trading. In return, they earn fees generated from the transactions in the pools. This decentralized model ensures that DEXs can function efficiently and securely, enabling users to trade tokens without relying on a centralized authority.