What is AMM and how does it work? A basic beginner’s guide

Decentralized exchanges, lending platforms, pharming in liquidity pools and other DeFi tools have made the cryptocurrency market more diverse and users’ lives easier. Today you can easily exchange tokens without KYC, swap profits into stabelcoins, or earn passive income by providing liquidity. But all these opportunities wouldn’t exist without automated market makers (AMMs). Let’s break down what AMMs are, how they work, and where they are used.

What is AMM

Cryptocurrency is a convenient tool for transferring value on the Internet, but the peculiarity of the first cryptocurrencies was that users could not get them before the blockchain and block producers (miners) were launched.

«Pre-mining or instant-mining with subsequent drops was simply not popular in the early crypto market, and even now networks claiming the status of “cryptocurrency” do not use this method of token distribution.»

Only after the blockchain is up and running, transactions are processed, and new coins are issued as rewards for mined blocks can they get to users. Moreover, initially they had only 2 ways to get cryptocurrency:

– Buy equipment and mine new coins by mining.

– To buy already mined coins from miners on any of the trading platforms.

Now it does not seem like a serious problem, but in the crypto market before the DeFi era, users could only buy cryptocurrency on centralized exchanges (CEX). These exchanges used order books to create the market, bringing together orders from users and market makers, and providing quotes for buy and sell orders.

«At the same time, the centralized exchanges themselves receive income from arbitrage and the spread gap (the difference between the buy and sell prices)»

That doesn’t seem like a problem either, because CEXs are convenient, accessible, and safe, right? In fact, centralized exchanges are subject to risks that can affect not only the site itself, but also the users:

– CEXs store all user assets, so they often attract the attention of hackers. The market is safer now than it was in its early days, and CEX security tools have evolved, yet periodic hacks do occur.

– The marketplace can freeze or seize assets from the owner at its discretion or at the request of law enforcement. This negates one of the major advantages of blockchain and cryptocurrency: resistance to censorship.

– The Coinbase Q2 2022 10-Q report also notes that since user cryptocurrency is formally considered an exchange’s cryptocurrency, these assets could also be used to meet obligations to creditors in the event of bankruptcy.

These problems gave rise to the search for a new method of value transfer that would allow owners to store and exchange cryptocurrency without exposure to the risks of the counterparty (the exchange).

NXT and Counterparty were developing the first peer-to-peer exchanges back in 2014, but they did not even remotely resemble today’s DEX. Early decentralized exchanges used the same order books, but tried to integrate them with smart contracts to do away with a centralized operator. It worked like this:

«The buyer would send a limit order to the smart contract to buy the asset, and when that order matched with a corresponding sell order from another user, they were automatically matched by the smart contract.»

But the side effects of this decision were an increase in the cost of gas, an increased burden on the blockchain and a high competition of orders in profitable price segments. Because of this, transactions were carried out with failures and delays.

«For example, on the EtherDelta platform, based on an automated order book, spreads were up to 10%, which made market making very expensive and reduced liquidity.»

But, in 2016, Vitalik Buterin first introduced the concept of automatic market maker (AMM), which in 2017 was described in detail in “On Path Independence”. According to Buterin’s vision:

AMM is a set of smart contracts that shape the price of an asset based on the ratio of assets in a liquidity pool.

The pools act as market makers, but since the exchange rate in them is set algorithmically by a smart contract, the counterparty risk is much lower than when working with centralized venues.

How the automated market maker works

The constant product is the way in which AMMs set the exchange rate between assets in the liquidity pool. The exchange rate through the constant product is calculated using the formula x * y = k, where:

– x is the inventory of the first asset in the pool;

– y – stocks of the second asset in the pool;

– k – the constant product, the value of which does not change.

The price curve, which is based on the above formula and determines the ratio of one asset to another, looks like this

AMM1

The exchange of tokens using the price curve works like this:

  1. Token A and token B are stored on a certain smart contract (liquidity pool).
  2. The smart contract contains the familiar formula x * y = k.

«The value of k can remain unchanged if it is a closed liquidity pool to which no extraneous assets can be added or change if new assets are added to the pool. But k ALWAYS remains unchanged during the exchange.»

3. When a user wants to exchange tokens through a pool, he simply adds a certain number of A tokens to the pool and receives a certain number of B tokens (or vice versa) according to the calculated exchange rate

Suppose we have a pool with 100 ETH (token A) and 100 USDT (token B). The exchange rate in the primary market is 1:1, and A*B (constant product) = 10,000. Under these conditions, the pool must maintain the A to B ratio so that A*B is always equal to 10,000. Now:

«If the user buys 1 ETH, the pool will hold 99 ETH, so to keep the constant of 10 000, the pool must have not 100 but 101,01 USDT (10 000/99 = 101,01). That is, to pay for the purchase of 1 ETH, you need to transfer ~1.01 USDT into the pool.»

This price is close to the spot price, because the user buys just 1 ETH, which causes a slight price distortion – slippage. But, as the transaction volume increases, the slippage will also increase. Let’s say that the same pool needs to buy 20 ETH:

«If a user buys 20 ETH, there will be 80 ETH left in the pool, the pool will need to replenish 25 USDT (10,000/80=125) to maintain a constant product of 10,000.»

In this case the value of 1 ETH will be 1.25 USDT, which is very different from the spot price. This slippage makes the transaction unprofitable for the trader, but the problem has two solutions:

1. Building up liquidity in the pool. The larger the stock of tokens in the pool, the less individual trades will affect the price and the lower the slippage will be.

  1. Arbitrage. If the value of 1 ETH in the pool is 1.25 USDT, but you can buy 1 ETH for 1 USDT on CEX, the traders simply conduct arbitrage: they buy on the spot and sell in the pool, thereby returning the balance of assets and the price to its initial level.

This is how most modern DEXes work. And although AMM mechanisms have evolved since their inception, the description above will help grasp the basic mechanics of the exchange.

Fundamentals of AMM markets and liquidity pools

To interact with cryptoservices running on AMM, the user must know the basic elements and principles on which such platforms are based:

– Liquidity is the most important indicator of any market. Liquidity determines how easy it is to exchange an asset for another. Liquid assets are cash fiat, because it is easy to exchange it for any desired commodity. Real estate is already less liquid because it is not so easy to determine a fair price to exchange.

«On a fundamental level, cryptocurrency market liquidity depends on supply and demand for a token. It reflects how many tokens holders are willing to sell, at what price, at a given point in time. To solve the liquidity problem, AMMs use liquidity pools.»

– A liquidity pool is a smart contract that holds a stock of two or more tokens and is the main mechanism of DEX and DeFi. The pool does not require a counterparty to make an exchange: the user simply transfers one token to the pool and immediately receives the second, and there are simply no price spreads.

«Most pools require you to deposit two tokens from a liquid pair at once in equal price terms. For example, in an ETH/USDT pool, you need to deposit 50% of the desired amount in ETH and 50% in USDT.»

– Slippage is the difference between the expected and final transaction price. Slippage occurs in traditional markets and order book trading, but in AMMs, the potential magnitude of slippage is greater. If you are trying to execute a large trade in a small pool of liquidity, the final price may be different from the expected or spot price. In the previous section, we have discussed why this is the case.

«Most DEX displays the expected slippage and allows you to set a limit on the allowable slippage, but the lower the allowable slippage, the longer it will take to execute the trade.»

– Liquidity providers – decentralized platforms do not pour their own funds into liquidity pools, instead, users themselves place assets in the pool. For providing liquidity, they receive part of the commission from transactions in the pool and additional rewards

«When a liquidity provider deposits tokens into the pool, he receives LP tokens representing his share in the pool. If he wants to withdraw his funds, he simply returns the LPs, receiving the provided assets in return.»

– Non-permanent loss is when you provide cryptocurrency to the liquidity pool, and the price of tokens has changed from the original price, so the number of tokens contributed will also change and you will incur unrealized losses. If the price of both tokens from the pool goes up – you will earn, but keeping those tokens not in the pool, but just in your wallet, the earnings would be higher. The more the price differs from the original price, the greater the unrealized loss.

«These losses are called impermanent because as long as the assets remain in the pool, they are not fixed, but as soon as the supplier takes the liquidity, the losses turn from impermanent to permanent.»

– Arbitrage is a way to make money on the difference in the value of an asset on different markets. If in the DEX pool, ETH is cheaper than on the spot market of the centralized exchange, the trader buys the asset cheaply in the pool and sells it at a higher price on the CEX spot.

«Since each liquidity pool is a separate market with its own balance of supply and demand, arbitrage is considered the primary mechanism for leveling price gaps between different markets. In theory, one can become an arbitrage trader and make money on price differences, but in practice these transactions are conducted by bots, which find price gaps faster than a human»

Popular AMM sites

Uniswap

Uniswap is one of the leading DEX and also the first AMM on the market. Uniswap hosts hundreds (if not thousands) of liquidity pools for various ERC-20 tokens, which creates enormous revenue opportunities on liquidity provision. Since its inception, Uniswap has gone through several updates:

– Uniswap V1 allowed you to create a liquidity pool for any ETH base pair token. This was not very convenient, because if a user wanted to exchange, for example, UNI to AAVE, he had to change UNI to ETH and then ETH to AAVE.

– Uniswap V2 made it possible to create liquidity pools for a specific pair without using ETH as the underlying exchange asset. This lowered fees and made exchanges faster, which raised the popularity of Uniswap as a result.

– Uniswap V3 introduced a concentrated liquidity feature. It allows liquidity providers to choose which exchange rate they are willing to provide liquidity for. This significantly improved capital efficiency and pool profitability.

The Uniswap protocol is owned and operated by UNI token holders, so in theory it is decentralized.

Balancer

The Balancer protocol is also an open-source AMM, but unlike most other venues that use a 50/50 asset split to create liquidity pools, Balancer allows you to create pools of up to eight different tokens. These pools are automatically balanced according to smart contracts, which reduces the risk of volatile losses.

Users can also provide liquidity to only one asset in the pool without having to buy 2 tokens, as required by pools on Uniswap.

For providing liquidity, providers receive a percentage of fees in BAL management tokens, which are used to vote on proposals to improve Balancer.

SushiSwap

SushiSwap was founded in 2020 by a certain Chef Nomi and two anonymous cofounders named “Sushiswap” and “0xMaki”. It is a fork of Uniswap, with some additional features and tools.

The main innovation of the site was the $SUSHI native token, as it allowed to create a decentralized management system, launch incentive programs for liquidity providers, and share the income from commissions among all $SUSHI holders via stacking.

In practice, for most users, SushiSwap differs from Uniswap only in the large number of “exotic” tokens that are not traded on CEX and other DEX.

Conclusions

It is impossible to imagine today’s crypto market without DeFi – it is a whole segment that gives users the opportunity to exchange cryptocurrencies without centralized platforms and earn on their assets. But DeFi venues would never exist without AMM.

The most important elements of AMM are pools and liquidity providers, with which the concepts of volatile losses and arbitrage are associated. If you are considering liquidity provision as a way to make money on cryptocurrency, then knowing all of the above is a must – it is the base of DeFi and AMM.

Kotov Dmitry
CEO and Founder of
Cryptoconsulting

Related Articles

Latest Articles