DeFi protocols and their risks · Lesson 2 of 5
Automated market makers and DEX trading
7 min read
Most on-chain trading does not use an order book. It uses automated market makers (AMMs): pools of two tokens that quote prices from a formula. Anyone who trades on a decentralized exchange (DEX), provides liquidity, or values a token by its DEX price needs to understand that formula, the costs it creates for traders and liquidity providers, and the ways other actors extract value from it.
The constant-product formula
The simplest and most widely copied design, introduced by Uniswap, keeps the product of the two reserves constant:
x · y = k
where x and y are the pool's balances of the two tokens. The pool's spot price is the ratio of the reserves (y ÷ x). A trader who takes tokens out of one side must put in enough of the other side to keep k unchanged (before fees).
A worked example. A pool holds 100 ETH and 300,000 USDC, so k = 30,000,000 and the spot price is 300,000 ÷ 100 = 3,000 USDC per ETH. A trader wants to buy 10 ETH.
| Step | ETH in pool | USDC in pool | Calculation |
|---|---|---|---|
| Before the trade | 100 | 300,000 | k = 100 × 300,000 = 30,000,000 |
| After removing 10 ETH | 90 | 333,333.33 | 30,000,000 ÷ 90 |
| Trader pays | 33,333.33 | 333,333.33 − 300,000 | |
| Average price paid | 33,333.33 ÷ 10 = 3,333.33 per ETH | ||
| New spot price | 333,333.33 ÷ 90 = 3,703.70 per ETH |
The trader paid about 11.1% above the starting spot price. This is price impact: the larger the trade relative to the pool, the worse the average price. Buying only 1 ETH from the same pool costs 3,030.30 USDC, about 1% above spot. With a 0.30% fee the trader also pays slightly more input (33,333.33 ÷ 0.997 ≈ 33,433.63 USDC for the 10 ETH), and the fee stays in the pool.
The new spot price (3,703.70) is now far above prices elsewhere. Arbitrageurs sell ETH into the pool until its price matches other markets, which is how AMM prices stay aligned with the wider market. The pool never "knows" the market price; it learns it from arbitrage, and it pays for that lesson, as the next sections show.
Other curve shapes exist. StableSwap designs (popularized by Curve) flatten the curve near a 1:1 price so that assets expected to trade at par, such as two stablecoins, can be swapped in size with little impact. Their weakness is that the curve steepens sharply if one asset loses its peg.
Liquidity providers and fees
The reserves come from liquidity providers (LPs), who deposit both tokens in the current ratio and receive LP tokens (or, in newer designs, a position NFT) representing their share. LPs earn the trading fees: 0.30% per trade in Uniswap v2, and a choice of fee tiers in Uniswap v3 (0.01%, 0.05%, 0.30% and 1%), where stable pairs typically use the lowest tiers and volatile pairs the higher ones.
LPs also carry the risk of every token in the pool. If one asset collapses, arbitrageurs sell it into the pool and take out the healthy asset, so the LPs end up holding mostly the failed token. Many stablecoin pools ended up filled with UST this way when it lost its peg in May 2022.
Fees are only half of the LP's result. The other half is what happens to the deposited assets when prices move.
Impermanent loss
Because arbitrageurs rebalance the pool, an LP automatically sells the token that is rising and buys the one that is falling. Compared with simply holding the two tokens, this produces a shortfall called impermanent loss (IL).
Example: an LP deposits 1 ETH and 3,000 USDC when ETH is 3,000 USDC (total value 6,000). For this share, k = 1 × 3,000 = 3,000. ETH then rises to 12,000 USDC (four times). The share rebalances to the reserves that satisfy x · y = 3,000 at the new price: x = √(3,000 ÷ 12,000) = 0.5 ETH and y = √(3,000 × 12,000) = 6,000 USDC.
- Value in the pool: 0.5 × 12,000 + 6,000 = 12,000 USDC
- Value if simply held: 1 × 12,000 + 3,000 = 15,000 USDC
- Impermanent loss: 12,000 ÷ 15,000 − 1 = −20%
The general formula, for a price ratio r between the new and the starting price, is IL = 2√r ÷ (1 + r) − 1. It depends only on the size of the move, not its direction:
| Price change | r | Impermanent loss vs holding |
|---|---|---|
| +25% | 1.25 | −0.6% |
| +50% | 1.5 | −2.0% |
| +100% (doubles) | 2 | −5.7% |
| +300% | 4 | −20.0% |
| −50% (halves) | 0.5 | −5.7% |
| −75% | 0.25 | −20.0% |
The loss is called "impermanent" because it disappears if the price returns to the starting point. In practice, prices rarely return exactly, and the loss becomes permanent when the LP withdraws. An LP position is profitable only if fees exceed IL. Researchers also measure loss-versus-rebalancing (LVR), the value LPs give away to arbitrageurs who trade against stale pool prices; it is a more precise way of saying the same thing: passive liquidity sells cheap and buys dear.
Concentrated liquidity
Uniswap v3 (May 2021) introduced concentrated liquidity: an LP chooses a price range, for example 2,700–3,300 USDC per ETH, and the capital works only inside it. Within the range the position behaves like a much larger constant-product pool, so it earns more fees per dollar. The trade-offs:
- If the price leaves the range, the position holds 100% of the less valuable token and earns nothing until the price returns or the LP resets the range.
- Impermanent loss inside a narrow range is magnified in proportion to the higher capital efficiency.
- Positions need active management, which has turned liquidity provision into a professional activity.
Uniswap v4 (launched January 2025) added hooks, custom contracts that run at points in a pool's life (before or after a swap, for example). Hooks allow features such as dynamic fees, but each hook is new code whose risk the LP and the trader inherit.
Slippage and MEV
Slippage is the difference between the price a trader expects when submitting a transaction and the price at which it executes. It comes from price impact and from other transactions executing first. Wallets let users set a slippage tolerance (for example 0.5%), which converts into a minimum output: if the trade would return less, it reverts. A deadline does the same for time, so a transaction that sits unconfirmed for an hour cannot execute later at a stale price.
That tolerance is also an invitation. On public blockchains, pending transactions sit in a visible mempool, and the block builder decides their order. Maximal extractable value (MEV) is the profit available from including, excluding or reordering transactions. Some MEV is neutral or useful: arbitrage that realigns prices, or liquidations that keep lending protocols solvent. The harmful form is the sandwich attack:
A trader who buys $100,000 of a token with a 1% tolerance can lose up to about $1,000 to a sandwich in a thin pool. Defenses include tight tolerances, splitting large orders, submitting through private order flow (RPC endpoints that send transactions directly to builders instead of the public mempool), and the designs below.
Aggregators and intents
DEX aggregators search many pools and venues and split an order across them to minimize price impact plus fees. They add a routing contract to the trust chain, but for most trades they improve execution.
Intent-based systems go further. Instead of specifying a route, the user signs a message stating the outcome ("sell 10 ETH for at least 33,000 USDC within five minutes"). Competing solvers (professional market makers or routing engines) fill it from DEX pools, their own inventory or other orders. CoW Swap batches orders and settles them at a uniform clearing price, which removes the ordering that sandwiches depend on; UniswapX uses Dutch auctions among fillers. The trade-off is reliance on off-chain solvers and their competition.
| Execution route | Strength | Main risk |
|---|---|---|
| Direct AMM swap | Simple, fully on-chain | Price impact, sandwiching |
| Aggregator | Best price across pools | Extra contract in the path |
| Private order flow | Hidden from the public mempool | Trust in the builder or relay |
| Intents and batch auctions | MEV protection, possible price improvement | Dependence on solver competition |
Exam focus: the constant-product formula x · y = k and how to compute the output, average price and new spot price of a swap; why price impact grows with trade size; how arbitrage aligns AMM prices; what LPs earn and how impermanent loss arises, including the formula and the −5.7% at 2x and −20% at 4x figures; concentrated liquidity and its out-of-range risk; slippage tolerance, MEV and the sandwich attack; and how aggregators, private order flow and intents reduce execution costs.
Practice questions
Check your understanding. These questions do not count towards the exam.
Sign in to save your progress and continue where you left off. It is free. Sign in