What Made DeFi Fun← all topics

x · y = k

A market without an order book

Uniswap is one of those ideas that sounds almost stupidly small: put two tokens in a pool and let a formula quote every trade. There is no order book, no waiting for someone to take the other side, and no team deciding what the right price should be. The pool itself is the market. Anyone can trade against it, and anyone can add liquidity to keep it alive.

The whole thing was one equation

Take a pool with 10 ETH and 20,000 USDC. Ignoring fees for a moment, their product is 200,000, so k = 200,000. When someone buys ETH, they remove ETH and add USDC; the pool only releases an amount that keeps the product the same. As ETH becomes scarcer inside the pool, it becomes more expensive. Inventory becomes price, which is basically the magic.

The easiest way to understand it is to mess with the pool. Move either reserve below and watch the other adjust: k stays fixed, but the price doesn't. That's the AMM in motion.

Pool reserves

10 × 20,000 = 200,000

Spot price1 ETH = 2,000 USDC
Constant product reserve curveThe current ETH and USDC reserves move along the x times y equals 200,000 curve as either slider changes. The shaded reserve area shows their relative values using the pool's starting price.10 ETH · 20,000 USDC1 ETH = 2,000 USDCUSDCETHk = 200,00050% ETH · 50% USDC at start

Move either reserve. The other adjusts automatically so their product stays the same.

Something obvious, not so obvious

The obvious question is whether someone can buy every ETH in the pool, even in one transaction. With our 10 ETH and 20,000 USDC pool, k = 200,000. Buying 9 ETH leaves 1 ETH behind, so the pool must end with 200,000 / 1 = 200,000 USDC, which means adding 180,000 USDC. Buying 9.9 ETH leaves 0.1 ETH behind, so the pool needs 2,000,000 USDC instead. At exactly 0 ETH, 200,000 / 0 has no finite answer. You can get ridiculously close, but the final piece is never for sale, and the V2 contract rejects any swap asking for the full reserve.