← simple-learning

How a crypto swap pool works

AMM stands for automated market maker: how a market works when there is nobody to trade with.

Needs division and the idea of a price. Leads to liquidity providers, arbitrage, Uniswap.

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.

Order bookYou trade with another person. No seller, no trade.
AMMYou trade with a pool of coins. It never says no, it only changes its price.

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.

X in pool
Y in pool

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.

You payBalance:
X
You receiveBalance:
Y
Rate
Price impact
Fee
your wallet
X
Y
pool
X
Y

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.

xyx · 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

Why did the price move? Nobody changed their mind.The pool has no opinion and reads no news. It knows one thing: the two piles. A trade changes the piles, so it changes the price. That is also why a big pool moves less: the same trade is a smaller change to its piles.

5Trade log

#gavegotprice paidprice beforeimpact
No trades yet.

?Check yourself

New numbers, same rule. A pool holds 100 X and 400 Y, with no fee.

Things to try

  1. 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.
  2. 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.
  3. 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.
  4. 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.