At a glance
- An AMM is
- A decentralised exchange with no order book. You trade against a shared pool of two tokens, and a formula sets the price
- The formula
- The classic one is constant product:
x * y = k. The reserves multiply to a constant, and every trade moves along that curve - The price
- Is simply the ratio of the two reserves, so a large trade moves it against you: the more you buy, the worse the rate
- Liquidity providers
- Deposit both tokens into the pool and earn a share of the trading fees in return
- The security note
- Because the pool's price is just its current balances, that price can be pushed, which is the root of oracle-manipulation attacks
Trading without a counterparty
A traditional exchange runs an order book: buyers and sellers post prices, and the exchange matches them. On-chain that is slow and expensive, and it falls apart when no one is offering the other side of your trade. The automated market maker throws the order book out. Instead of matching you to a person, it holds a pool of two tokens and lets you trade against the pool itself, with a formula deciding the price. There is always liquidity to trade against, at some price, without anyone posting an order.
The pool and the constant-product curve
A pool holds a reserve of each token, call them x and y. The most common AMM keeps their product constant: x * y = k. When you buy some y, you must add enough x that the product stays equal to k. That single rule produces a sensible price automatically.
// Pool holds x of token X and y of token Y, with x * y = k.
// You put in dx of X. To keep the product at k, the pool gives you dy of Y:
//
// (x + dx) * (y - dy) = k = x * y
//
// Solving for dy:
// dy = y - k / (x + dx)
//
// The bigger your dx, the worse your effective price. That is by design.
The consequence is worth sitting with. The price at any instant is just the ratio of the reserves, y / x. A small trade barely moves the reserves, so you get a rate close to that ratio. A large trade shifts the reserves noticeably, so each additional unit costs more: this is price impact, and it is why a big swap on a thin pool is expensive. The curve, not a market maker, enforces it.
Liquidity providers and fees
The tokens in the pool have to come from somewhere, and they come from liquidity providers. An LP deposits both tokens in the pool's current ratio and receives pool shares that represent their slice of it. In return, every swap pays a small fee that is added to the pool, so LPs earn a passive cut of the trading volume, in proportion to their share. It is not free money: if the two tokens' prices diverge after you deposit, you can end up worse off than if you had simply held them, an effect known as impermanent loss. Providing liquidity is a real position with real risk, not a savings account.
The pool is also a price feed, and that is dangerous
Here is the bridge to the security guides. Because an AMM's price is nothing more than its current reserves, the pool doubles as a convenient on-chain price source, and other contracts are tempted to read it. The problem is that the price is only as trustworthy as it is expensive to move, and with a large trade, or a flash loan, an attacker can move it hard within a single transaction, feed the manipulated number to a contract that trusted it, and profit. The AMM is doing exactly what it was designed to do; the mistake is elsewhere, in trusting a spot price you can shove. The price-manipulation guide follows this thread in full.
The short version. An AMM replaces the order book with a pool of two tokens and a formula, classically x * y = k, so there is always something to trade against. The price is the ratio of the reserves, which means big trades move it against you by design. Liquidity providers supply the tokens and earn fees, at the risk of impermanent loss. And because the pool's price is just its balances, that price can be manipulated, which is why so many DeFi exploits begin at an AMM.
References & further reading
- Uniswap, Pools, liquidity and the constant-product formula. The mechanics of a leading AMM.
- Ethereum community, Decentralised applications. Where AMMs fit among on-chain apps.
- Uniswap, Using a pool as an oracle. Why a spot price is manipulable and how a time-weighted average helps.