Finance / Automated market makers and liquidity pools

Automated market makers and liquidity pools

How a contract holding two piles of tokens can quote a price and trade with anyone: one swap worked through with the constant-product rule, then slippage, fees and impermanent loss.

Intermediate7 steps~7 min
Alice — a trader swapping one token for another
Bob — a liquidity provider: supplies tokens
Pool — a contract holding TKN and USDx
Arbitrageur — trades until pool and market prices match
// THE SHORT VERSION

In 30 seconds

An automated market maker replaces an exchange’s order book with a pool: a smart contract holding two tokens that anyone can swap against at any time.

Its price comes from one rule. The two amounts multiplied together must stay constant, so buying one token makes it scarcer in the pool and dearer. Large trades against a small pool get a noticeably worse price.

The tokens are supplied by liquidity providers, who share a small fee on every swap. Their risk is impermanent loss: when prices move, their share ends up worth less than simply holding the tokens.

Automated market makers and liquidity pools, step by step

STEPS01 / 07

TKN and USDx are made-up tokens with made-up prices. The swap in step 4 ignores the fee, and the rule shown is the basic constant-product design; newer pools vary it. This is an explanation, not advice.

STEP 01 / 07 Order book or pool
THE MATH
order book: wait for a match pool: trade at once, at a price from a formula
  1. Step 1: Order book or pool. A classic exchange keeps an order book and a trade happens only when a buyer and a seller agree on a price. That needs many active traders. An automated market maker, an AMM, replaces the book with a smart contract holding a pool of two tokens. Anyone can swap against it at any moment.
  2. Step 2: Fill the pool. The tokens come from liquidity providers. Bob adds 10 TKN and 1,000 USDx, equal in value, and others add the rest until the pool holds 100 TKN and 10,000 USDx. Bob now owns 10% of it. The pool’s price is simply the ratio of the two piles: 10,000 ÷ 100 = 100 USDx per TKN.
  3. Step 3: The constant-product rule. The pool follows one rule: the amount of TKN times the amount of USDx must stay the same number, k, here 1,000,000. Every allowed state of the pool sits on this curve. To take TKN out, a trader must put in enough USDx to keep the product the same, so the pool can never be emptied.
  4. Step 4: One swap, worked through. Alice wants TKN and pays in 2,500 USDx. The pool then holds 12,500 USDx, so to keep the product at 1,000,000 it must hold exactly 80 TKN. It started with 100, so the contract hands Alice the other 20. No one set that price: it falls out of the rule. The fee is left out here and added in step 6.
  5. Step 5: Slippage. The pool quoted 100, but Alice paid 2,500 for 20 TKN: 125 each. Her own trade moved the price as it went. This price impact, loosely called slippage, grows with the size of the trade against the pool: 1% for a 100 USDx swap, 25% for hers. Afterwards the pool’s price is 12,500 ÷ 80 = 156.25.
  6. Step 6: Providers and their fees. Why would Bob lend his tokens to the pool? Each swap pays a fee, here 0.3%, that stays in the pool. Alice’s swap leaves 7.5 USDx behind, so she really gets about 19.95 TKN, not 20. The fee belongs to all providers by share: Bob’s 10% earns 0.75 USDx, which he collects when he withdraws.
  7. Step 7: Impermanent loss. Take the starting pool again and let TKN’s market price rise to 400. Arbitrageurs buy the pool’s cheap TKN until it holds 50 TKN and 20,000 USDx. Bob’s 10% is now worth 4,000; had he just held his tokens, 5,000. The gap closes if the price returns and is locked in if he withdraws. Fees may not cover it.
VALUES SO FAR
x····pool
y····pool
price····result
k····rule
in····Alice
out····Alice
paid····result
fee····pool
gap····loss

Spotted a mistake on this page?