Automated market makers and liquidity pools: how x · y = k works
The formula behind Uniswap-style exchanges, with every step of a real calculation shown.
An automated market maker (AMM) is a decentralized exchange that trades against a pool of two tokens instead of an order book. The classic design keeps the product of the two balances constant (x × y = k), so the price is the ratio of the balances and every trade moves it. Liquidity providers supply the tokens and earn trading fees, but lose out against simply holding when prices move.
Why DEXs use a formula instead of an order book
A traditional exchange matches buyers with sellers in an order book. Running an order book on a blockchain is slow and expensive, because every new order, change and cancellation would be a paid transaction. An automated market maker (AMM) replaces the book with a liquidity pool: a smart contract holding two tokens, and a formula that sets the price from how much of each the pool holds. Anyone can trade against the pool at any time; there's no one on the other side choosing to accept.
The constant product formula: x · y = k
x × y = k
Here x and y are the pool's balances of the two tokens. Uniswap v2, and many DEXs copied from it, require that a trade never lowers the product k (apart from the fee, which raises it). The price of one token in terms of the other is simply the ratio of the balances: a pool with 100 ETH and 300,000 USDC prices ETH at 300,000 ÷ 100 = $3,000.
Worked example
Start with the pool above: x = 100 ETH, y = 300,000 USDC, so k = 30,000,000. You swap 30,000 USDC for ETH, and the pool charges a 0.3% fee ($90), which stays in the pool for its liquidity providers.
- USDC that counts toward the trade: 30,000 × 0.997 = 29,910
- The pool must keep USDC × ETH at k: ETH left = 30,000,000 ÷ (300,000 + 29,910) = 90.9339 ETH
- You receive 100 − 90.9339 = 9.0661 ETH
- Your average price: 30,000 ÷ 9.0661 = $3,309.03 per ETH, 10.3% above the $3,000 starting price
- The pool now holds 90.9339 ETH and 330,000 USDC, so its price is $3,629.01
The gap between $3,000 and $3,309.03 is price impact: your trade was 10% of the pool's USDC, so it moved the price a lot. A 300 USDC trade in the same pool pays an average of $3,012.03, barely more than the fee. Deep pools give better prices for the same trade size.
What brings the price back? Arbitrage. After your trade the pool prices ETH at $3,629 while exchanges elsewhere still say about $3,000, so traders sell ETH into the pool until its price matches the wider market again, pocketing the difference. AMMs don't discover prices on their own; arbitrage keeps them in line.
Liquidity providers
Anyone can add liquidity by depositing both tokens in the current ratio (equal value of each in a 50/50 pool). In return they get a share of the pool, recorded as LP tokens or, on newer designs, a position NFT. Each trade's fee is added to the pool, so providers' shares slowly grow, and they can withdraw their share of both tokens at any time.
The catch is that the pool constantly rebalances: when ETH rises, traders buy ETH from the pool, leaving providers with less ETH and more USDC than they deposited. Compared with simply holding the two tokens, that costs money whenever prices move. That's impermanent loss, and fees have to cover it before providing liquidity pays.
Beyond x · y = k
- Concentrated liquidity (Uniswap v3 and later): providers choose a price range, so their money works harder while the price stays inside it, earns nothing outside it, and suffers larger impermanent loss.
- Stableswap curves (Curve and others): flatter near a 1:1 price, for tokens meant to trade at the same value, such as two dollar stablecoins.
- Different fee tiers and "hooks": newer designs let each pool set its own fee or add custom logic.
Risks for traders and providers
- Slippage and sandwich attacks: bots can see your pending swap, buy just before it and sell just after, so you get the worst price your slippage tolerance allows. Keep the tolerance tight on large trades.
- Fake tokens: anyone can create a pool for any token, including copies of real ones. Check the contract address.
- Contract risk: a bug in the pool or router contract can drain funds.
- Impermanent loss for providers, which fees may not cover.
Related reading: Centralized vs. decentralized exchanges (CEX vs. DEX), and brokers · What is MEV? Transaction ordering explained
Frequently asked questions
What is an automated market maker?
A smart contract that holds a pool of two tokens and prices trades with a formula based on the pool's balances, instead of matching buyers and sellers in an order book.
What does x*y=k mean?
x and y are the pool's balances of its two tokens, and k is their product. Every trade must leave the product at least as big as before (fees make it grow), which fixes how much of one token you get for the other.
Why do big trades get worse prices on a DEX?
Each unit you buy makes the token scarcer in the pool, so the formula raises the price as you go. The bigger the trade compared with the pool, the bigger the price impact.
Who sets the price in a liquidity pool?
The formula does, from the pool's balances. Arbitrage traders keep it close to prices on other exchanges by trading whenever the pool drifts.
How do liquidity providers earn money?
From the trading fee (for example 0.3% per trade on a classic Uniswap v2 pool), shared in proportion to their stake. Impermanent loss can outweigh it.
Where to next
Sources
- How Uniswap works (Uniswap v2 documentation)
- Uniswap v2 core whitepaper, Adams, Zinsmeister and Robinson (2020)
- Concentrated liquidity (Uniswap v3 documentation)
- Decentralized exchanges (ethereum.org)
Written for beginners and checked against the sources listed. Last reviewed November 5, 2026. This page is educational and is not financial, tax or legal advice. How we write and check pages.