An exchange booth without a clerk
You have euros and need dollars. At an exchange booth, someone takes one currency and hands you the other. What if a machine could do that while the clerk is away?
The problem
A fixed rate is tempting. But if dollars become more valuable elsewhere, people can buy every last dollar from the machine. It needs a rule that offers less of a coin as that coin runs low.
An automated market maker (AMM) is a program that offers trades using a rule. People supply the coins in advance. You trade with that shared supply, called a pool.
1A pool is two piles
Take two kinds of coins, call them X and Y (on a blockchain they are called tokens). Someone put x coins of X and y coins of Y into a box. That is the whole market. The price is nothing but the ratio of the piles: how many Y per one X.
2You trade with the pool
This is what a swap looks like on an exchange like Uniswap. You pay one coin, you receive the other. The other side of your trade, the counterparty, is always the pool. "Price impact" shows how much less you get because your trade moves the price, excluding the fee; step 3 shows where it comes from.
3The one rule
How does the box decide how much to give back? Without a fee, it has one rule: x · y must be the same number before and after. That number is called k. With a fee, the fee stays in the pool, so the product grows.
| x | y | x · y | |
|---|---|---|---|
| before | |||
| after |
4Price over time
Every trade moves the price. Buying X (giving Y) pushes it up, selling X (giving X) pushes it down. Bigger trades move it more; a bigger pool moves less.
price of X in Y after each trade after the swap you are about to make
5Trade log
| # | gave | got | price paid | price before | impact |
|---|---|---|---|---|---|
| No trades yet. | |||||
?Check yourself
New numbers, same rule. A pool holds 100 X and 400 Y, with no fee.
Things to try
- Sell X again and again. Watch the price fall a step at a time. The pool's Y pile shrinks but never hits zero, and each X sells for less than the last.
- Swap 10 X, flip, and swap back the Y you got. Without a fee, an exact reverse trade brings you back where you started. With a fee, you get less back: the pool keeps the fees. The displayed amounts are rounded, so typing them back in may leave a small difference.
- Same trade in a bigger pool. Pick 10 000 / 10 000 and give 10 X. Same price, almost no impact. This resistance to big trades is called depth, and it is what big pools buy you.
- Turn on the fee. After a few trades, k is a bit larger than it started. The pool grew. That growth belongs to whoever filled it.
What this leaves out
Who fills the pool: liquidity providers deposit both coins and own a share of it, which is why the fee goes to them. Why the pool's price matches the rest of the world: arbitrageurs trade against it the moment it drifts, pulling it back. And modern pools let providers concentrate their tokens in a price range so the same deposit gives more depth. All of that is built on the one rule above.