Before 2018, decentralized exchanges used order books. They were slow, illiquid, and expensive to run on-chain. Every order was a transaction. Every cancellation was a transaction. The order book was a data structure that required constant updates. On Ethereum, each update cost gas. The gas cost made order book DEXs economically unviable.
In 2018, Vitalik Buterin posted a proposal on Reddit: what if you replaced the order book with a formula? A pool of two tokens. A constant product. xy = k. The product of the quantities of the two tokens in the pool is constant. When you buy token A, you add token B to the pool and remove token A. The price of A, in terms of B, is the ratio of the quantities. The more A you buy, the more expensive A becomes — the curve moves, the ratio changes, you pay more. The formula enforces the price. The liquidity provider, not a counterparty, supplies the tokens. The provider earns fees from trades. The trader trades against the pool, not against another trader.
Hayden Adams implemented the proposal. He called it Uniswap. It launched in November 2018. The initial version had 300 lines of code. The constant product formula was the entire logic. The contract held two tokens. It allowed anyone to deposit tokens (become a liquidity provider), withdraw tokens (redeem their share), and swap tokens (trade against the pool). That was it. Three functions. Three hundred lines. The simplicity was the genius.
The mathematics
The constant product formula: x × y = k, where x is the quantity of token X in the pool, y is the quantity of token Y, and k is a constant. Before a trade, k = x × y. After a trade that adds Δx of token X and removes Δy of token Y, the new quantities are x + Δx and y - Δy. The product must remain constant: (x + Δx)(y - Δy) = k. Solving for Δy: Δy = y - k/(x + Δx). This is the amount of token Y the trader receives for depositing Δx of token X. The price is Δx/Δy, which varies with the size of the trade. Larger trades move the price more. The price impact is the mechanism that prevents the pool from being drained.
The fee is a percentage of the input amount, added to the pool before the trade is executed. In Uniswap V2, the fee is 0.3% — 30 basis points. The fee accrues to liquidity providers in proportion to their share of the pool. The fee is the incentive to provide liquidity. The fee must compensate LPs for the risk of impermanent loss — the opportunity cost of holding tokens in a pool rather than holding them directly. When the price of the tokens changes relative to each other, the LP's position is worth less than if they had held the tokens separately. The difference is impermanent loss. The fee income must exceed the impermanent loss or LPs withdraw. The pool shrinks. Liquidity dries up. The fee must be high enough to retain LPs. The optimal fee is an empirical question. The answer varies by pair, by volatility, by competing pools.
What came after
Uniswap V2 (2020) generalized the formula to any ERC-20 pair. V2 also introduced the price oracle — a time-weighted average price that resists manipulation. The oracle accumulates the price at each block, weighted by the time elapsed since the previous block. The accumulator is immune to flash loan manipulation because it is time-weighted. The oracle is the data feed for lending protocols, derivatives, and any application that needs a manipulation-resistant price.
Uniswap V3 (2021) introduced concentrated liquidity. Instead of providing liquidity across the entire price curve from zero to infinity, LPs choose a price range. Their liquidity is only active within that range. Within the range, they earn fees. Outside the range, they earn nothing. Concentrated liquidity multiplies capital efficiency — LPs can provide the same depth as V2 with a fraction of the capital, or provide far greater depth with the same capital. The trade-off: LPs must actively manage their ranges. Passive liquidity provision no longer works. The LP must monitor the price and adjust ranges. The LP is now a market maker. The market maker's job is the subject of a later post.
Curve (2020) introduced a different formula optimized for stablecoins — assets that trade at roughly 1:1. The Curve formula blends the constant product and constant sum (x + y = k) formulas. Near the peg, it behaves like a constant sum — low slippage, stable price. Far from the peg, it behaves like a constant product — the price diverges, the pool rebalances. The Curve formula enabled efficient stablecoin trading with minimal slippage. Curve became the dominant venue for stablecoin swaps and the foundation of the "Curve Wars" — the competition among protocols to accumulate CRV tokens and direct liquidity incentives.
The AMM as market microstructure
The AMM is not just a product. It is a market microstructure. It replaces the order book with a formula, the counterparty with a pool, the spread with a curve. The AMM is a market where anyone can provide liquidity without permission, anyone can trade without an account, and the rules are enforced by code rather than by an exchange. The AMM is the innovation that made DeFi possible. Without the AMM, decentralized trading required order books that were too expensive to run on-chain. With the AMM, decentralized trading required 300 lines of Solidity. The difference in adoption was the difference in complexity. The simpler system won.
Guillermo Angeris and Tarun Chitra's Improved Price Oracles: Constant Function Market Makers (2020) is the foundational academic treatment of AMM mathematics. The paper formalizes the constant function market maker (CFMM) as a general class, proves properties of the Uniswap and Curve formulas, and analyzes the time-weighted average price as an oracle. The paper is the bridge between the engineering of Uniswap and the theory of market design. The bridge is solid. The theory explains why the engineering works.
References:
- Hayden Adams, Noah Zinsmeister, Dan Robinson, "Uniswap v3 Core," 2021.
- Guillermo Angeris and Tarun Chitra, "Improved Price Oracles: Constant Function Market Makers," 2020.
- Michael Egorov, "StableSwap — Efficient Mechanism for Stablecoin Liquidity," 2019 (Curve whitepaper).
- Related posts: The Order Book, Algorithmic trading in crypto
Trading infrastructure is distributed systems engineering. The order book, the AMM, the matching engine, the relay — each is a component in a latency-critical distributed system. The engineering constraints are the same as any real-time system: throughput, latency, reliability, correctness under concurrency. The domain is finance. The engineering is systems.