Understanding AMMs - The Basics
A breakdown of these magical primitives with simple, understandable numbers.
Automated market makers. The most talked about thing in crypto, while simultaneously being one of the least understood primitives. This purpose of this article is to remove some of the mystique around how they work and then a subsequent article will explain how various AMMs differ.
AMMs trace their origins to a Reddit post by Vitalik a few years ago over here: https://www.reddit.com/r/ethereum/comments/55m04x/lets_run_onchain_decentralized_exchanges_the_way/
From there, we had Bancor being the first AMM live on Ethereum. However due to the incorrect use of their native BNT token, it failed to get much traction... Until Uniswap came along and delivered on the simple promise of a properly functioning automated market maker with no extras. Since Uniswap got traction in the past year, the AMM space is truly starting to take off with many players now entering the space.
Order-books 101
Before we go into explaining what are AMMs, let's maybe talk about what problem they solve. So in the traditional/normal world, an exchange is usually compromised of an order book. The way an order book works is someone will place an order to BUY a certain amount of tokens at a certain price with a certain currency and another party will place an order to SELL a certain amount of tokens for a certain currency.
If we break it down there's three variables at play:
The price they want their offers to go through at
The amount of tokens they're willing to buy/sell
A token pair that they will accept in exchange for the token they're buying/selling
This means that you'll have a two groups and only when do they overlap will trades get settled
In some sorts you can see the order book as a constant negotiation between buyers and sellers. If there's more demand to buy then people will outbid other buyers in order to have their orders settled first. You'll also have traders wanting to cancel orders and change prices. As you can see that this will involve a fair bit of actions which isn't great in a blockchain context.
DEXs like 0x solve this issue by having the matching of orders on a server where speed is no problem and then treating the blockchain as a settlement layer. This semi-works although has a few major drawbacks:
Order cancellation can be messy since it's a race between the settlement traction versus when you tell the blockchain not to honour the trade
Smart contracts can't automatically just buy tokens, they require off-chain orders passed to them in order to buy or sell tokens
Bootstrapping liquidity for markets is hard because price discovery isn't easy with a few buyers and sellers
What if instead we had a kind of exchange where:
There's only one price and the smart contract determines what it is
You don't need to match orders, there's simply one price and either you take it or leave it
Anyone can bootstrap a market and provide liquidity for it
Conceptual AMM
Lets imagine we have a AMM that exchanges between token A and token B. Now say the AMM currently has 20 of token A in its reserve, and 40 of token B.
This means that the Product is 20*40 = 800. This is the number that needs to stay constant (hence the name constant product market maker). Now imagine Alice comes along and wants to sell the AMM 5 of token A, in exchange for some amount of token B.
How much of B will she get? Let's see!
If she's given 5 of token A to the AMM, its reserve must have increased to 20 + 5 = 25.
Since the market maker needs to keep that product constant at 800, the reserve of B needs to change to 32 (since 25 * 32 = 800).
So the AMM's reserve of token B has decreased by 8. But where did those tokens go? Well to Alice of course!
So this means that Alice gave the AMM 5 of token A, and received 8 of token B in return. This means that Alice got an effective price of 5/8 = 0.625.
Notice what happens if Alice had tried to sell 10 of token B instead. The reserve of B would have increased from 40 + 10 = 50.
Since the market maker needs to keep that product constant at 800, the reserve of A needs to change to 16 (since 16 * 50 = 800). This means that Alice got an effective price of 2.5.
AMM with Realistic Math
Turn on your concentration, because this is where we get more math-y about how this plays out.
x * y = k
where x = the quantity of one type of token
where y = the quantity of a second type of token
where k = a fixed amount that never ever changes
Let's make this less abstract. To create a Uniswap market you need to provide two tokens in equal amounts.
ETH = $100.
DAI = $1.
x = 10 ETH = $100 * 10 = 1,000
y = 1,000 DAI = $1 * 1,000 = 1,000 (I used 1,000 because we need to provide the same amount of ETH)
x * y = k
ETH * DAI = constant
($100 * 10) * ($1 * 1,000) = (1000 * 1000)
1,000 * 1,000 = 1,000,000
So if we just saw this equation, we could determine the ratio of ETH/DAI = 100,000/1,000 = 100 (which is the price of Ethereum)!
"Well duh, that's because we input it". Hold up, I haven't finished.
Now let's say someone would like to purchase $100 worth of ETH from the pool. What they're essentially doing is increasing y by 100 (depositing DAI) and decreasing x = 1 (withdrawing ETH)
Our new equation will look like this:
(ETH price * (10 -1)) * ((DAI Price * 1000) + 100) = 1,000,000
(ETH price * 9) * ((1 * 1000) + 100) = 1,000,000
(ETH price * 9) * (1000 + 100) = 1,000,000
(ETH price * 9) * 1100 = 1,000,000
(ETH price * 9) = 1,000,000 / 1100
ETH Price = (1,000,000 / 1100) / 9
ETH Price = $101.01
"Wait, but the price of ETH has changed from $100" you may be asking yourself. That's correct. It's a feature, not a bug.
Creating high quality content on a consistent basis is demanding. Read more about why DeFi Weekly is switching to a Freemium model:
https://defiweekly.substack.com/p/defi-weekly-premium