August 14, 2023

Understanding How AMMs Work

TL;DR:

  • AMMs are programs that allow anybody to participate in crypto market making by creating liquidity pools where assets can then be traded instantly by others.
  • AMMs with a constant product formula keep liquidity constant for a pair of assets by increasing or decreasing the price of assets according to their availability in a pool.
  • AMMs were designed to replace order books in cryptocurrency exchanges that wish to remain decentralized.

Working with automated market makers (AMMs) is the bread and butter of crypto market making. They’re one of the most important and unique concepts introduced in DeFi. Understanding them is essential to fully recognize the role of crypto market makers in providing liquidity to the market. 

We’ve already looked into how AMMs have evolved since Uniswap popularized the concept, as well as projecting where AMMs might go next. Now, let’s make sure we understand how they work as well.

What are AMMs, exactly?

As the name implies, automated market makers, or AMMs, are programs that automate the creation of a market for a specific pair (or sometimes more) of assets. AMMs take care of matching buy orders on one side of the market with the sell orders on the other side of the market using a decentralized application instead of relying on a third party. 

Let’s go back to the example of Alice, Bob, and basic market making. When Alice is trading Bob’s asset A for asset B, they’ll often need Charlie to meet them in the middle in order to complete the transaction and close the bid-ask price gap (the gap between the price that Alice is willing to buy for and what Bob is willing to sell for). Unless Charlie steps in to resolve the trade, both Alice and Bob will continue to wait for another party who’s willing to make the deal on each of their respective terms.

AMMs allow anybody to be Charlie. All the individual has to do is deposit however much of asset A and asset B  they’re willing to provide to the market, and the AMM takes care of the rest.

How do AMMs Work?

When a crypto market maker works with an AMM, their assets go into a liquidity pool along with the assets of any other crypto market maker who wants to provide more of assets A and B. Then, when Alice and Bob want to trade, the AMM uses a formula that determines how much asset A can be traded for a given amount of asset B at every moment. 

This formula varies depending on the preferences of a particular exchange. Some of the most popular formulas include:

  • Constant product
  • Constant sum
  • Constant mean

Each of these has its nuances, but the function remains the same: they allow the AMM to set prices mathematically so the market can be automated.

Uniswap, for example, uses a constant product formula:

A x B = C

All this means is that the AMM is making sure that the amount of A times the amount of B  in the pool is always equal to a constant number C. This is how it automatically determines at what price to buy or sell the assets. When there’s less of A in the pool, it charges more for B, and vice versa.

Step by Step

Let’s go through what this looks like in a typical trading situation, taking the constant product formula as an example.

Providing liquidity

As a crypto market maker, you provide an equal value of both asset A and asset B to a liquidity pool. Let’s say they’re worth the same, and you deposit 10 of each. The product of 10 A and 10 B is 100.

Trading

Now, let’s say Bob wants some of asset A and is willing to give up some more of asset B. He tells the AMM how much of A he wants to get. The AMM calculates how much B Bob must provide in order for the trade to keep the total value of A in the pool times the total value of B equal to 100. For example, if he wants to purchase 3 of A, he must provide ~4.286 of B. Every unit of A that Bob gets decreases its supply in the liquidity pool, making A worth more regarding B. Simple supply and demand.

Price determination

After the trade is made, the AMM automatically rebalances the price of A in terms of B one more time, so the next purchase also maintains a total liquidity of 100. If someone else comes along, they’ll find that the price of 1 A is “another step” higher than the price Bob got for his last unit of B.

To be exact, they’ll find that the price of the next unit of A will cost 6.666 B.

(*Play around with these values in a constant product simulator. You’ll find that the greater the amount of total liquidity provided, the less dramatic the price changes between assets A and B become. This is called market depth, and it is an essential part of crypto market makers’ role in reducing volatility.) 

Why Are AMMs So Important in DeFi?

AMMs became popular because they were an efficient way to solve the problem of trading cryptocurrencies in the most decentralized way possible. They’re an alternative for trading assets without the use of an order book, which is the way most traditional exchanges work. 

Using an order book can be tricky. Most exchanges that use this method have to match Alice and Bob off-chain. Meanwhile, market makers working in an order book need to keep an eye on market movements and manually adjust their asset prices. 

In DeFi, this kind of reliance on a centralized third party is avoided at all costs, and AMMs provide a decentralized alternative. Market makers benefit from AMMs because they have a more open and programmatic way to engage with the market. 

AMMs also provide the added advantage of being “composable.” This means that they can be easily combined with other decentralized applications that add additional features to the market.  

Stay in the loop to learn more about fundamental concepts in crypto market making and how Keyrock approaches them by following on Twitter, LinkedIn, or Lens