Sign in

The problem: trading without an order book

Centralized exchanges work with order books. Buyers post bids, sellers post asks, the exchange matches them. On chain, this is hard. Every post and every cancel is a transaction that costs gas. A market maker who wants to update quotes every block goes bankrupt fast.

Uniswap V2 takes a different approach. Instead of matching orders, it holds a pool of both tokens and uses a formula to set the price. Anyone can swap one token for the other against the pool. Anyone can add tokens to the pool to make it deeper.

The simplest formula would be a fixed price, say 1 token A for 1 token B. It works only while every other market trades at the same price. If token A starts trading at 2 token B elsewhere, traders buy A from the pool for 1 until the pool has no A left. So the price has to rise as the reserve shrinks, and each next token A has to cost more than the one before.

The pool and its rule

A V2 pool, also called a pair, is a contract that holds two tokens, and every pair of tokens has its own contract. A separate Factory contract deploys new pairs deterministically as people want new markets. Both tokens must be ERC-20s, which gives the pool one code path for moving tokens. Native ETH is not an ERC-20, so anyone trading ETH first wraps it into WETH (the ERC-20 wrapper for ETH).

We'll call the two tokens token A and token B. The contract enforces one rule, under which each next token A costs more than the one before:

x * y = k

x is how much of token B the pool holds. y is how much of token A the pool holds. k is the product, and the pool keeps k constant across every swap.

Suppose the pool currently holds 100 of token A and 100 of token B. Then x = 100, y = 100, and k = 10,000. After any swap, the product of the two reserves must still equal 10,000.

A swap pays out whatever keeps the product constant

A trader wants to swap token B for token A. They put 10 of token B into the pool. What does the contract give them back?

After the trader's deposit, the pool's reserve of token B has grown by 10.

x_new = 100 + 10 = 110

To keep k = 10,000, the pool's reserve of token A must shrink to whatever value satisfies 110 * y_new = 10,000.

y_new = 10,000 / 110 = 90.91

The pool used to have 100 of token A. Now it must have 90.91. The difference goes to the trader.

trader receives = 100 - 90.91 = 9.09 of token A

The trader put in 10 of token B and got 9.09 of token A. There was no price quote and no order matching. The output was whatever amount keeps the product equal to 10,000.

Written as a formula in general terms, with Δx for the input and Δy for the output:

Δy = y - k / (x + Δx)

The new reserve of token A must equal k divided by the new reserve of token B, and the trader gets whatever the old reserve was minus that. After simplification:

Δy = (y * Δx) / (x + Δx)

That's the swap formula. It's the invariant x * y = k rearranged to solve for the output.

AMM swap curve x·y = k moving from point (X, Y) to (X + Δx, Y − Δy)The graph plots reserve of token B (x) against reserve of token A (y) on a curve where x times y equals k. A swap moves the pool from a before point (X, Y) to an after point (X + Δx, Y − Δy), with Δx going in and Δy coming out, while the product x·y stays the same. The pool's reserves always sit on the curve x * y = k reserve of token B (x) reserve of token A (y) x · y = k before (X, Y) X Y after (X + Δx, Y − Δy) X + Δx Y − Δy Δx in Δy out A swap pushes the pool from one point on the curve to another. The product x · y stays the same.

The curve x * y = k is a hyperbola. The pool's current state is always one point on this curve. The trader's input pushes the pool to the right along the curve, and the contract gives back enough token A to make the pool's new position satisfy the invariant.

The price changes after every swap

The pool's reserves change after every swap, which means the next trader doing the same swap gets a different result.

After the first trader's swap, the pool is at (110, 90.91). A second trader wants to do the same thing, putting in another 10 of token B.

x_new = 110 + 10 = 120
y_new = 10,000 / 120 = 83.33
second trader receives = 90.91 - 83.33 = 7.58 of token A

A third trader puts in the same 10 of token B.

x_new = 120 + 10 = 130
y_new = 10,000 / 130 = 76.92
third trader receives = 83.33 - 76.92 = 6.41 of token A

9.09, then 7.58, then 6.41. Each trader gets less than the one before, even though they all put in the same amount. The pool has been giving away token A and receiving token B, so token A is becoming scarcer in the pool, and the pool charges more for it.

AMM price curve x·y=k: three trades of Δx=30 give shrinking A output (23.08, 14.42, 9.87)A curve plots reserve of token A (y) against reserve of token B (x), always keeping x times y equal to k. Three traders each swap in the same amount of token B (Δx = 30) starting from a 100/100 pool, and each one gets less token A than the last: 23.08, then 14.42, then 9.87. Three traders, same input. Each one gets less output than the last. reserve of token B (x) reserve of token A (y) x · y = k start 100 / 100 23.08 14.42 9.87 trade 1 trade 2 trade 3 Each trader puts in the same Δx = 30 of token B: trade 1: pool was 100 / 100 gets 23.08 of A trade 2: pool was 130 / 76.92 gets 14.42 of A trade 3: pool was 160 / 62.50 gets 9.87 of A

The diagram shows the same 100 / 100 pool with larger trades, 30 of token B each. The outputs shrink the same way: 23.08, then 14.42, then 9.87. The curve gets flatter as the pool moves to the right, and a flatter curve means less token A given out per token B taken in.

The ratio of the reserves is the pool's spot price, the rate a trade gets when it is much smaller than the reserves. At 100 / 100, one token A costs 1 token B. After the three swaps of 10, at 130 / 76.92, it costs 130 / 76.92 = 1.69 token B.

This is the price discovery mechanism. The pool doesn't ask anyone what the price should be. It just defends the invariant, and the invariant produces prices that respond to supply and demand. If token A is in high demand (lots of traders putting B in to get A out), the reserve of A shrinks fast, and the price of A in B rises fast. If demand fades, no swaps happen, and the price stays put.

The contract does not know the price on other markets. Traders called arbitrageurs keep the pool in line with them. When token A is cheaper in the pool than elsewhere, they buy it from the pool and sell it elsewhere, and every purchase raises the pool's price until it matches the market. When it is more expensive in the pool, they do the opposite. Their profit comes out of the pool's reserves. The section on impermanent loss below counts it.

Why bigger trades face worse rates

What if a single trader put in 30 of token B in one swap instead of three swaps of 10? The pool goes from 100 / 100 straight to x = 130 and y = 10,000 / 130 = 76.92, so the trader receives 100 - 76.92 = 23.08 of token A. That is the sum of 9.09 + 7.58 + 6.41, and it is the first trade in the diagram. The pool ends at the same point whether you arrive in one big swap or three small ones, so the total output is identical.

The single 30-token trade averaged a rate of 23.08 / 30 = 0.77 token A per token B. The first 10-token trade by itself got 9.09 / 10 = 0.91 token A per token B. The bigger your trade relative to the pool, the worse your average rate, because you push deeper into the unfavorable part of the curve in one move.

This effect is called price impact. A trader doing a small swap against a deep pool barely moves the price. A trader doing a swap that's a sizable fraction of the pool gets a noticeably worse rate. There's nothing the contract can do about this: the rate is what the invariant produces.

Where the reserves come from: liquidity providers

The pool needs reserves before anyone can swap against it. Liquidity providers (LPs) supply them.

An LP deposits both tokens. They must deposit them in the same ratio the pool currently holds. If the pool is at 100 of token A and 200 of token B, an LP depositing 1 of token A must also deposit 2 of token B. Depositing out of ratio would change the pool's spot price away from the market price, which would invite arbitrage and effectively let the LP donate value to whoever shows up next.

In exchange for the deposit, the LP receives LP tokens. LP tokens are themselves an ERC-20 issued by the pool contract. They represent a share of the pool. If an LP owns 10% of the LP token supply, they own a claim on 10% of the pool's reserves. To take their money out, they burn their LP tokens and the contract sends them their share.

The first deposit into a fresh pool is special, because there's no existing ratio. The first LP sets it. The LP tokens minted to them are computed as sqrt(x_deposit * y_deposit). With the square root, doubling both deposited amounts doubles the LP tokens. The plain product x_deposit * y_deposit would quadruple them.

For every later deposit, LP tokens are minted in proportion. If an LP adds 5% to the pool's reserves (in the current ratio), the contract mints 5% of the current LP supply to them.

How LPs get paid: the fee

LPs take real risk by providing liquidity. The composition of the pool changes as traders swap, which means an LP can end up holding more of whichever token has fallen in value and less of whichever has risen. To compensate for that risk, the pool charges traders a fee on every swap.

The fee is 0.3% of the input amount, and it is not sent anywhere. The trader puts in Δx, the contract treats only 99.7% of Δx as effective input when computing the output, and the remaining 0.3% stays in the reserves.

The swap formula with the fee becomes:

Δy = (y * Δx * 997) / (x * 1000 + Δx * 997)

That's the same formula as before, but with Δx replaced by Δx * 997 / 1000. The * 1000 factor in the denominator is the standard way to handle the 0.3% in integer arithmetic without using floating point.

No new LP tokens are minted to cover the fee, so the total LP supply stays the same while the reserves grow, and each LP token claims a slightly bigger slice of the pool. LPs don't have to claim anything. The contract has no per-LP fee tracker and no claimFees function.

LP fee accrual across three stages: deposit, fee growth, withdrawalAlice deposits 100 USDC and 0.1 ETH for 3.16 LP tokens, then swaps grow the pool's reserves to 108 USDC and 0.108 ETH while the LP token supply stays at 3.16, so she later withdraws the full 108 USDC and 0.108 ETH. A side panel explains why: fees stay in the pool and no new LP tokens are minted for them, so value per LP token equals reserves divided by total LP supply. Fees accrue to LPs: pool grows, LP supply doesn't Stage 1: Alice provides initial liquidity Pool reserves: 100 USDC, 0.1 ETH LP token total supply: 3.16 (= sqrt(100 * 0.1)) Alice owns 100% of LP tokens many swaps over time Stage 2: Time passes, swaps generate fees Pool reserves grew: 108 USDC, 0.108 ETH LP token total supply unchanged: 3.16 Each LP token is worth more now Alice burns LP tokens Stage 3: Alice withdraws Alice receives 108 USDC, 0.108 ETH Why LP tokens become more valuable Every swap collects a 0.3% fee. The fee stays in the pool. No LP tokens are minted for fees. Reserves grow, supply stays flat. Each LP token's share of the pool stays the same percentage, but that percentage of a bigger pool means more underlying tokens. value per LP token = reserves / total LP supply

Impermanent loss: the cost LPs pay

The risk that the fee pays for is called impermanent loss (IL). In an ETH/USDC pool, if ETH appreciates on the market, arbitrageurs buy ETH from the pool, so the pool loses ETH and gains USDC. By the time price has doubled, the pool holds less ETH than at entry. An LP who owns a fixed share of the pool holds proportionally less ETH too. They missed part of the ETH appreciation.

The same happens in reverse. If ETH falls, the pool buys ETH from sellers and ends up with more of it. Either direction, the pool sells the token that is rising and buys the one that is falling, so holding the original tokens would have done better.

Let's count it for one LP, leaving fees aside. Alice is the only LP in the pool. She deposits 1 ETH and 4,000 USDC when ETH costs $4,000, so her deposit is worth $8,000. Then ETH jumps to $8,000 in one step.

If Alice had kept the tokens in her wallet, she would have 1 ETH worth $8,000 plus 4,000 USDC, $12,000 in total.

In the pool, the product of the reserves is still 1 * 4,000 = 4,000, and arbitrageurs have traded until the ratio of the reserves is 8,000 USDC per ETH. Multiplying the product by the ratio cancels the ETH reserve and leaves the USDC reserve squared, so the pool holds sqrt(4,000 * 8,000) = 5,657 USDC and 5,657 / 8,000 = 0.707 ETH. Alice withdraws them, and at the new price they are worth 0.707 * 8,000 + 5,657 = $11,313. That is $687 less than holding, a loss of 5.7%.

The $687 went to the arbitrageurs. They took 0.293 ETH out of the pool, worth $2,344 at the new price, and paid 1,657 USDC for it.

The same count works for any price move. Let r be the new price divided by the entry price. Holding is worth 4,000 * r + 4,000. In the pool, the USDC reserve is sqrt(4,000 * 4,000 * r) = 4,000 * sqrt(r), and the ETH reserve is worth the same amount, because the ratio of the two is the price. So the position is worth 2 * 4,000 * sqrt(r). Dividing it by the holding value and subtracting 1 gives the loss relative to holding:

IL = 2·√r / (1 + r) − 1

For r = 2 this is 2 * 1.414 / 3 - 1 = -5.7%, the number from Alice's example.

Impermanent loss chart and ETH LP-vs-HODL worked exampleA line chart plots LP loss versus HODL value as the price ratio moves from 0.25x to 4x, showing no loss at the 1x entry price and losses up to −20% at the extremes (for example 2x gives −5.7%, 4x gives −20%). Below it, a worked example compares HODLing versus providing liquidity when ETH price doubles from $4,000 to $8,000: HODL ends at $12,000 total, while the LP position ends at $11,313, a $687 loss from rebalancing. Impermanent loss: any price move makes the LP worse off than HODLing LP value vs HODL value, as a function of price change 0% −5% −10% −15% −20% LP loss vs HODL 0.25x 0.5x 1x (entry) 2x 4x price ratio (current price / entry price) no loss 2x → −5.7% 0.5x → −5.7% 3x → −13.4% 4x → −20% 0.25x → −20% Worked example: ETH doubles from $4,000 to $8,000 At entry (ETH = $4,000): Alice has 1 ETH + 4,000 USDC, total value = $8,000. She splits her capital evenly. Path A: HODL She keeps her tokens in her wallet. When ETH hits $8,000: 1 ETH × $8,000 = $8,000 + 4,000 USDC = $4,000 total = $12,000 Path B: LP in V2 pool Pool rebalances as price rises. When ETH hits $8,000, she now holds: 0.707 ETH × $8,000 = $5,657 + 5,657 USDC = $5,657 total = $11,313 Difference: $687 lost to rebalancing (the −5.7% in the chart above)

The chart plots this formula. The loss is symmetric: a 2x move and a 0.5x move both produce 5.7% loss, and a 4x move and a 0.25x move both produce 20%.

Why "impermanent"

The loss is only impermanent in one specific sense: if price returns to entry before you withdraw, the loss disappears. The pool has rebalanced back to its original composition and you get out what you put in.

If you withdraw at any other price, the loss is realized and permanent. Most LP exits happen at prices different from entry, so the loss is best understood as the cost of providing liquidity, paid in full when you exit.

Fees vs IL

An LP position beats holding only if the earned fees exceed the impermanent loss. In Alice's example, her fees over the period would have to be more than $687.

For stablecoin pairs the price ratio barely moves, so IL stays negligible and small fee income wins. For volatile pairs the math is closer. ETH/USDC on a 0.3% pool needs enough trading volume to pay back the IL accumulated from price movement. Pools where trading volume is high relative to the pool's total assets earn back impermanent loss and more, and quiet pools do not.

The first deposit locks 1000 wei of LP tokens

The first LP into a fresh pool gets sqrt(x_deposit * y_deposit) LP tokens. V2 mints them slightly less than that, and burns a small amount (1000 wei, the smallest units of the LP token) by sending them to address(0), where nobody can ever spend them. This is the minimum liquidity lock.

Without it, the first LP can make one wei of LP so expensive that the next depositor's share is rounded away.

  1. The attacker is the first LP. They deposit 1 wei of each token and get sqrt(1 * 1) = 1 wei of LP tokens.
  2. The attacker transfers 1,000 tokens of each kind directly into the pool contract, calling transfer on the underlying tokens and bypassing the add-liquidity function. The pool's reserves are now 1,000 of each token, but the LP supply is still 1 wei.
  3. A victim deposits 1,500 of each token, which is 150% of the reserves, so their proportional share is 1.5 wei of LP. The contract works in integers and rounds down, so they get 1 wei.
  4. The pool holds 2,500 of each token against 2 wei of LP. The attacker burns their 1 wei and withdraws half, 1,250 of each token. They put in 1,000, and the extra 250 is the victim's.

A deposit below 1,000 tokens does worse. Its proportional share rounds down to zero LP tokens, and the depositor loses the whole deposit to the attacker.

Now repeat the attack with the lock. To end up with 1 wei of LP the attacker has to deposit 1,001 wei of each token: sqrt(1,001 * 1,001) = 1,001, of which 1,000 are burned. After the same donation the LP supply is 1,001 wei, and the attacker owns 1 of them. The victim's deposit is again 150% of the reserves, and 150% of 1,001 wei is 1,501.5, rounded down to 1,501. Their 1,501 wei out of 2,502 redeem for 1,499.8 of the 2,500 tokens of each kind, so rounding cost them 0.2 of a token instead of 250. The attacker's 1 wei redeems for about 1 token of each kind, and the rest of the 1,000 they donated backs the burned wei, which nobody can redeem.

Burning 1000 wei of LP tokens at the first deposit means there's always a baseline supply that nobody owns and nobody can manipulate around. This caps how much an attacker can inflate the per-share value. The ERC-4626 lesson shows the same attack on vault shares and a second defense against it.

When you build your own version, check it with one question: if the first LP holds 1 wei of LP and then transfers tokens straight to the pool, how many LP tokens does the next depositor get?