MIVORA Start learning free

Swapping Without a Middleman: AMMs

No order book, no company — just a pool of two tokens and a formula that prices every trade from how full each side is.

Crypto & Tokenization · Lesson 12 · 9 min read

You want to swap token A for token B, but there’s no company to act as the exchange — and you definitely don’t want to hand your coins to a stranger to “match” your trade. So how can a piece of code, with no buyers or sellers waiting on the other side, give you a fair price instantly, any hour of the day? Hold the question; the answer quietly powers most of decentralized trading.

Swapping without an order book: the liquidity pool

A decentralized exchange (DEX) is just a smart contract. The dominant design, an automated market maker (AMM), throws out the idea of matching buyers and sellers entirely. Instead, a liquidity pool holds a reserve of two tokens, and a formula sets the price from their ratio.

You trade directly against the pool: drop in token A, the formula computes how much B you get, and the contract sends it. There’s always a price and always a counterparty (the pool), at 3am or during a crash, and you never give up custody until the swap executes.

The constant-product formula and why price moves

The classic rule is x × y = k: the pool keeps the product of its two reserves constant. Buy B (remove B, add A) and B gets scarcer while A grows — so each additional B costs more A. Price is just the ratio of the reserves, and every trade moves it against you. That price impact is slippage.

The deeper the pool (more liquidity), the smaller the slippage; a thin pool means even a modest trade moves the price a lot. So pool depth, not a company’s quote, is what determines the rate you get.

Worked example
A swap on x × y = k:
• Pool holds 1,000 A and 1,000 B, so k = 1,000,000. Start price ≈ 1 A per B.
• You add 100 A → the pool now has 1,100 A.
• To keep k constant: new B reserve = 1,000,000 ÷ 1,100 ≈ 909.
• You receive 1,000 − 909 ≈ 91 B for your 100 A — not 100. The ratio shifted, so the price moved against you (slippage). A bigger trade vs. the pool = a worse rate.

Liquidity providers and impermanent loss

Who fills the pool? Liquidity providers (LPs) deposit both tokens and earn a share of the fee on every trade. It sounds like free yield — but there’s a catch called impermanent loss.

Because the formula automatically sells whichever token is rising and buys whichever is falling, if the two prices diverge a lot, an LP ends up holding more of the loser and less of the winner — worth less than if they’d simply held both tokens. Fees may or may not make up the difference. So providing liquidity isn’t free money; it’s a real position with a real risk, and the trust shifts to the contract’s correctness (lesson 8) and the pool’s depth.

An everyday analogy

An AMM is like a self-service exchange machine with two bins — say apples and oranges — that obeys one rule: the number of apples times the number of oranges must stay constant. Take apples out and you must put oranges in, and as apples get scarce in the bin, each one costs more oranges — the machine prices itself purely from how full each bin is, no haggling and no clerk. Anyone can restock the bins (provide liquidity) and earns a fee on every trade. But if apples moon while you’re a stocker, the machine keeps handing your apples out cheaply for oranges, leaving you holding more of the loser — that’s impermanent loss.

Worked example
Why an AMM always has a price when an order book might not:
1. It’s 3am and you want to sell a lesser-known token. On an order-book exchange, there may be no buyer awake — your order just sits there.
2. On an AMM, a liquidity pool for that token already holds reserves, so there’s always something to trade against.
3. You sell into the pool; the constant-product formula instantly quotes a price from the current reserves and executes.
4. The trade nudges the ratio, so you absorb some slippage — worse if the pool is thin.
5. No counterparty had to be found and no company took custody: the standing reserve plus the formula did the whole job.

This is the reading. The interactive version — active-recall quiz, a hands-on experiment you run in your own AI, and an earned mastery check — is free in the app.

Start this lesson free →