Automated Market Makers 101
How automated market makers like Uniswap work
Hi friends, welcome back! Today I’m covering one of the most fundamental concepts in DeFi: automated market making. Automated market makers (AMM) are among the most protocols that people use when getting into DeFi. Automated market making is also the mechanism that powers decentralized exchanges like Uniswap and Sushiswap.
How Traditional Exchanges Work
Every one of us has probably used a traditional, centralized exchange. If you’ve ever made an electronic trade of a stock or cryptocurrency, you’ve used a traditional exchange. It may have seemed simple, but the exchange did a lot of work behind the scenes to make the trade happen.
Coinbase, Binance, and Gemini are all examples of traditional exchanges. They have a centralized database (under their control) where all transactions, user data, and account balances are stored.
So how do exchanges like Coinbase work? Well, traditional exchanges operate off something called the order book model. Simply put, these exchanges work by matching buyers and sellers of a coin or token.
On one side of the trade are the buyers. These buyers tell Coinbase what price they’re willing to buy at. On the other side of the trader are sellers. The sellers tell Coinbase what price they’re willing to sell at. Coinbase’s job is to match all of these orders in an efficient way to facilitate the trade between the two parties. Rinse and repeat.
A depth chart, like the one above, is a visual representation of the order book. See that gray line in the middle? That’s the current mid-market price -- the price at which the coin, Bitcoin, is trading right now. To the left, we have the buy orders in green. As we move left on the graph (and get to a cheaper Bitcoin price), we have more people willing to buy, hence the depth of the market increases. On the right, in the red, we have the sell orders. The higher the price per Bitcoin, the more people who are willing to sell (hence the depth of the sellers increases).
The order book model has important implications on the liquidity of assets being traded (i.e. how easy is it to buy or sell an asset). Let’s say you’re trying to buy a thinly traded token. Maybe no token holders want to sell their tokens. What can Coinbase do? Nothing! You’ll just have to wait until someone is willing to sell you their tokens. Automated market makers have a neat way of solving this problem (more on this later).
One benefit of traditional market makers and exchanges is that they offer the buyer and seller more control over the price that they’re willing to trade at. A buyer can tell Coinbase, “hey, I don’t want to sell my Bitcoin at any price under $55,000.” Coinbase will make sure that the seller isn’t matched with a buyer unless the exchange price is more than $55,000. As we’ll see, in (some) AMMs, this isn’t the case! (Note: Uniswap v3 and Sushi Trident aim to solve this issue, but I’m going to leave that out for now in order to keep things simple. If there’s interest, let me know and I’ll do a breakdown in a future Easy DeFi).
Why Don’t Traditional Exchanges Work in DeFi?
The traditional order book model breaks down in DeFi. Why? Today, it’s cost-prohibitive to accept millions of buy and sell orders. Coinbase receives many, many such buy and sell orders each second. Some of these are fresh orders while others are updating prices on existing orders. Regardless, there are tons. Accepting these orders is cheap and easy when you have a centralized database (where it’s essentially free to store data). When dealing with the blockchain, however, we must pay to store data. Storing buy and sell orders on chain would be far too costly to be practical. Also, given the transaction times of Ethereum today, it would likely be too slow to be practical, too. By the time that a buyer and seller could be matched, prices may have shifted or trades may have been front run.
On a more philosophical level, traditional, centralized exchanges also rely on a trusted third party. Relying on trusted third parties goes against the core principles of DeFi. In DeFi, we do things in a decentralized, permissionless manner. Rather than having an account at Coinbase, we have our MetaMask wallet. We also don’t want to rely on Coinbase to match us with buyers and sellers. Coinbase could be hacked or delist assets. Ideally, we’d have a way to access liquidity without needing another party involved.
Because Coinbase is a regulated entity, it also needs to be careful about what it lets on its exchange. Coinbase has been very reluctant to list security tokens on its platform due to potential regulation. Anonymous, decentralized trading protocols are able to avoid this problem and list whichever tokens they please (with the idea being “Who is the SEC going to sue? We’re all anonymous!”).
So traditional order book models are not well suited for implementation on the blockchain. What do we do instead? Enter: automated market makers.
How Automated Market Makers Work
First, automated market makers start by throwing the old order book model out the window.
Instead, they use something called a liquidity pool. Just like everything on Ethereum, liquidity pools are based on smart contracts. The smart contract in this case aggregates tokens from many different holders (called liquidity providers) and locks them for use in trading. Liquidity pools generally consist of 2 different tokens that are traded against each other.
For this article, I’m going to use ETH and USDC as example tokens. In fact, this is the most active pool on Uniswap, with $336 million in Total Value Locked as of October 31, 2021. While I use ETH and USDC to keep things simple here, a pool can consist of any pair of tokens! What’s really cool is that anyone can open up a new pool on Uniswap or Sushiswap due to the fact that these protocols are permissionless (no need to get approval from Uniswap to open a pool).
People like you and me, as well as larger players (like DeFi treasuries), can add tokens to this liquidity pool. In return for doing so, liquidity providers are compensated with a share of the trading fees that are collected by the protocol. On top of that, sometimes liquidity providers earn additional liquidity mining rewards. In short, liquidity mining rewards are used by protocols to reward liquidity providers. When Uniswap V2 launched, for instance, they needed to incentivize people to contribute their tokens to liquidity pools. To do so, Uniswap gave liquidity providers in certain pools $UNI tokens as extra compensation. If the liquidity mining bit didn’t make sense to you, don’t worry about it - it’s not all that important to understanding AMMs (or just shoot me a question 😃).
Okay, so we have a pool full of ETH and USDC tokens. How do we use this pool of ETH and USDC to facilitate trades? To answer this question, we need a bit of math (it’s fairly simple, though, so don’t be scared away!)
Uniswap, the first automated market maker at scale, relies on a formula called the Constant Product Formula:
x * y = k
K here is the product (the product of x and y) - the constant product. x and y are the respective quantities of each token in the pool. To make that concrete, let’s walk through an example:
The pool above has 1,000 USDC (x) and 100 ETH (y). The product, k, is 100,000. Here’s the most important thing: k must always equal 100,000. At least in this example. In real life liquidity pools, k will always be equal to the initial conditions of the pool.
Now that there’s a pool of tokens, how do we use it to facilitate trades? The key, again, is our k value. Let’s say someone comes to the pool and wants to swap their ETH for USDC. This trader has 1 ETH that they’ll be contributing to the pool in exchange for USDC. How do we determine the level of USDC that we’ll give to the trader? It just involves a bit of simple math. Remember, the most important rule is to keep k constant at 100,000.
So the trader swapping 1 ETH against this liquidity pool will receive ~10 USDC (1,000 initial USDC - 990 ending USDC = 10 USDC).
Let’s dig into this trade and make a few observations about the nature of liquidity pools (at least simple liquidity pools).
The buyers and sellers (here, liquidity providers) don’t choose their price. They don’t submit orders but rather accept whatever price that the pool is offering based on how many tokens they’re willing to contribute. Note: this leads to a risk called “impermanent loss” for liquidity provers; it’s outside the scope of this article but there’s a link at the bottom of this post if you want to learn more.
The price a trader gets is dependent upon the ratio of tokens in the pool. Interestingly, this relationship is non-linear. If people all want to trade their USDC for ETH, they’ll drive the price of ETH way up. We can show this relationship between price and token ratio like this:
Notice how, towards the ends of the graph, the curve steepens or flattens. If we continue to add ETH to the pool, swapping it for USDC, we’ll eventually get to a point where the pool is overwhelmingly made up of ETH. Anyone wanting to swap their ETH for USDC will get very few USDC for their ETH. In fact, the pool will never get rid of all its ETH (or USDC) completely - as the pool approaches one of the extremes, the price for 1 ETH will go to 0. At that point, no one will swap their ETH for USD.
Perhaps more intuitively, if we continue to add USDC and take out ETH, the price for 1 ETH will eventually go to infinity (so the pool can never be exhausted of all its ETH). Scalar Capital has a nice table on the relationship between two tokens in a liquidity pool:
It’s also important to notice that the price we get for our ETH depends on how much we’re buying. The more ETH, the worse the price. Usually that doesn’t matter in large liquidity pools, but it can have consequences when trading more illiquid tokens.
Now, let’s think about how the price relates to external markets like Coinbase. If prices are controlled by the ratio of the two tokens, how can markets work efficiently? Some of the tokens traded on Uniswap are available on Coinbase or Sushiswap. How does the token trade at the same price given the AMM mechanism?
In short: arbitrageurs and bots are always there to ensure that prices are consistent across the exchanges. If you can buy ETH more cheaply on Coinbase than Uniswap, someone will buy ETH on Coinbase and deposit it to the Uniswap pool. This will drive the price of ETH up on Uniswap, causing the prices to balance.
Examples of Automated Market Makers
There are lots of automated market makers in crypto-land today. The most popular is Uniswap. With over $7 billion in Total Value Locked, it’s the most frequently used decentralized exchange. Uniswap is governed by the UNI token, which today has a fully diluted valuation of around $24.5 billion (compare that to Coinbase’s valuation of $67 billion).
Sushiswap is another popular DEX. Its origins are actually somewhat controversial. Sushi began as a fork of Uniswap. After forking Uniswap’s code, an anonymous team of developers began working on what we now know as Sushi. Because of its decentralized, anonymous origin, some people in DeFi prefer Sushi over Uniswap. For a full history of Sushi, check out Mario Gabriele’s fantastic piece in The Generalist.
There are also other types of AMMs with slightly different mechanisms, like Curve or Bancor. Changing the Constant Product formula leads to some pretty interesting outcomes for these protocols, which I’ll cover in a future Easy DeFi.