The Constant-Product AMM — Full Derivation

Recommended reading order

See study-guide for the full two-track learning path. This note is Track A, Step 9 (Phase 3: DeFi Mechanics). The DeFi path is:

  1. constant-product-ammyou are here
  2. bonding-curves → deterministic pricing for token launches
  3. impermanent-loss → the cost LPs pay
  4. lp-profitability → when do fees beat IL?
  5. lp-as-short-vol → the LP = short vol capstone
  6. blockchain-transaction-lifecycle → prerequisite for MEV
  7. mev-fundamentalssandwich-attacks → how bots exploit AMM traders
  8. pump-fun-economy → put it all together

Intuition: How an AMM Sets Prices

On a traditional exchange (NYSE, Binance), a human or firm posts a price and waits for someone to trade against it. On a decentralized exchange (DEX), there is no order book and no human market maker. Instead, a smart contract (a program running on a blockchain) holds a pool of two tokens and uses a mathematical formula to decide the price automatically. That program is the Automated Market Maker (AMM).

Here’s the core idea: imagine a pool holding 1,000 ETH and 1,000,000 USDC. The product of these two numbers is 1,000 × 1,000,000 = 1 billion. The rule is: after every trade, this product must stay the same. If someone buys ETH, the pool’s ETH goes down and its USDC goes up — but the product of the two reserves (the amounts the pool holds) stays at 1 billion.

This single rule — keep the product constant — determines the price, the price impact of trades, and how fees accumulate. Everything in this note follows from it.

Notation used in this note

  • , (Greek letter delta) — the change in a quantity. means “the amount of token X added or removed.”
  • (alpha) — a dimensionless ratio (here, trade size as a fraction of the pool).
  • (gamma) — the fee rate (e.g., 0.003 = 0.3%).
  • (lambda) — Kyle’s price impact coefficient (from market microstructure theory).

The constant-product automated market maker is the workhorse of decentralized exchange. Uniswap V2, Raydium V4, and PumpSwap all implement (variants of) the same invariant. This article derives everything from the conservation law .

Setup

A liquidity pool is a smart contract that holds reserves (token balances) of two tokens:

  • — reserve of token X (e.g., ETH)
  • — reserve of token Y (e.g., USDC)

The pool enforces the invariant (a rule that must always hold — no trade is allowed to break it):

where is a constant that changes only when liquidity is added or removed — never during a swap.

Marginal Price (the Quoted Price)

The invariant defines a curve in -space. A swap moves the state along this curve. The marginal price of X in terms of Y — i.e., the current quoted price, what you’d get for a trade so small it’s essentially zero-sized — is the (negative) slope of the curve:

The marginal price (positive, by convention) is therefore:

This is the instantaneous exchange rate: the price of one infinitesimally small unit of X, paid in Y. It depends only on the current reserve ratio. In practice, this is the price the pool is quoting right now. The reason it’s called “marginal” (rather than just “price”) is to distinguish it from the execution price — the average price you actually get for a finite-sized trade, which is always worse (see next section).

Execution Price for a Finite Trade

A trader sends of token X into the pool. The pool must return of token Y such that the invariant holds after the swap:

Solving for :

The execution price (the average price you actually pay across the entire trade — as opposed to the marginal/quoted price, which is the price for an infinitesimally small trade) is:

Note that for any . The trader always receives a worse price than the marginal quote — this is price impact.

Price Impact

Define the relative trade size . Then:

where is the pre-trade marginal price. The price impact is:

Expanding for small :

For small trades (), impact is approximately linear in trade size — just like in Kyle’s model. Kyle’s model (Albert Kyle, 1985) is a foundational result in market microstructure: it says that in a market with one informed trader and many uninformed traders, the price impact of an order of size is simply — perfectly linear. The parameter (Kyle’s lambda) measures how much the price moves per unit of order flow; it depends on how much informed trading there is relative to noise trading. The AMM’s effective Kyle’s lambda is:

which is inversely proportional to pool depth. Deeper pools have lower price impact, exactly as you would expect.

The Impact Function Is Concave (Sub-Linear)

The second derivative of the impact function is:

The negative sign means the impact function is concave (curves downward — like the outside of a bowl). Each additional percent of pool you trade adds less additional impact than the previous percent.

In contrast, Kyle’s linear model assumes , which is linear in : a trade twice as large always costs exactly twice the impact. The AMM is more forgiving:

AMM concave impact vs Kyle’s linear model — the AMM is more forgiving for large trades.

Numerical Comparison

Trade size AMM impact Kyle linear
0.01 (1%)0.99%1.00%
0.05 (5%)4.76%5.00%
0.10 (10%)9.09%10.00%
0.50 (50%)33.3%50.0%
1.00 (100%)50.0%100.0%

At small sizes, the two are nearly identical — both are approximately . At large sizes, the AMM impact is lower than Kyle’s linear extrapolation. The AMM formula approaches 100% asymptotically but never reaches it, while Kyle’s linear model would naively predict 100% impact at .

This is what concavity means in plain English: the impact curve bends below the straight line. A trader using 50% of the pool loses 33.3% of the price, not 50%.

Why You Can Never Drain the Pool

Look at the invariant curve : it’s a hyperbola that approaches both axes but never touches them. If you keep buying token X (moving left along the curve), the amount of Y you need to pay per unit of X shoots upward — the curve gets steeper and steeper. The last units of X are effectively infinitely expensive.

Why does the curve behave this way? The slope is always negative (the curve goes downhill as increases), and near the slope is enormously steep. As grows, the slope becomes less negative — it levels off toward zero. A slope that moves from very negative toward zero is increasing, which means the second derivative is positive (). Positive second derivative = the curve bends upward, like the right side of a bowl that’s still going downhill but flattening out. That upward bend is what makes the curve hug both axes without ever touching them.

This is the pool’s defense against drainage — neither reserve can ever reach zero.

Don’t confuse this with the impact function. Impact (the percentage of price lost on a trade) is a different function with a different shape:

WhatShapePlain English
Price impact Concave (bends below the straight line)AMM is more forgiving than linear for large trades
Invariant curve Hyperbola (bends away from both axes)Last units of either token are infinitely expensive

The Post-Trade Price

After the swap, the new reserves are and . The new marginal price is:

The post-trade price moves quadratically in relative trade size. The ratio is always less than 1 for a buy of token X (which pushes up the price of Y relative to X, i.e., pushes down the price of X in Y terms).

Fee Incorporation

In practice, pools charge a fee (e.g., for Uniswap V2’s 30 bps — basis points, where 1 bp = 0.01%, so 30 bps = 0.30%). Here is exactly what happens, step by step:

Step 1 — The trader sends into the pool. This is the total amount of token X the trader deposits.

Step 2 — The pool skims the fee. The fee is . Only the remainder enters the swap formula:

For example, if you send 1,000 X with a 0.3% fee: the fee is 3 X, and X.

Step 3 — The swap uses (not ). The pool computes how much Y to return using only the post-fee amount:

Step 4 — But the pool keeps ALL of . The new reserves are:

  • X reserve: (the full amount, including the fee)
  • Y reserve: (computed from )

This is the key: the fee portion ( X in our example) sits in the pool’s X reserves but was never “used” by the swap formula.

Why this makes grow. The new invariant is:

Since was computed as if only entered the pool (which would have kept exactly constant), but the X reserve actually grew by the larger , we get . The fee created a small surplus. Every swap ratchets upward.

Over time, the invariant grows, and LP tokens (receipts that liquidity providers — the people who deposited tokens into the pool — hold to track their share of the pool) appreciate against the underlying reserves. This is how fee income accrues to liquidity providers: the pool itself gets deeper, so each LP’s share is worth more.

The fee-adjusted execution price (what the trader actually gets per unit of X deposited) is:

This is worse than the no-fee price by roughly a factor of for small trades — the trader pays the fee on top of the price impact.

Geometric Interpretation

The constant-product curve : a rectangular hyperbola approaching both axes but never touching them.

The constant-product curve is a rectangular hyperbola (a smooth curve that approaches both axes but never touches them) in the first quadrant. A swap is a movement along the curve. Adding liquidity scales the curve outward (higher ). Collecting fees ratchets upward after each trade.

The marginal price at any point is the slope of the tangent line (the line that just barely touches the curve at one point) to the hyperbola. The execution price is the slope of the secant (the straight line connecting two points on the curve) between the pre-trade and post-trade points. The secant always has a shallower slope than the tangent, which is why execution price marginal price — the visual version of price impact.

Implementations

  • Uniswap V2 (Ethereum, 2020): canonical constant-product, 30 bps fee.
  • Raydium V4 (Solana): same invariant, different fee tiers, Solana-native accounts.
  • PumpSwap (Solana, 2025): constant-product pool for graduated tokens from Pump.fun’s bonding curve — see pumpswap.

All three implement the same math. Differences are in fee structure, governance, and how liquidity is bootstrapped.

Connections to Market Microstructure

The constant-product AMM is a market maker — it continuously quotes prices and absorbs inventory. The canonical microstructure models illuminate specific aspects of AMM behavior:

Ho-Stoll (inventory model). A Ho-Stoll market maker adjusts quotes based on inventory — skewing prices to attract offsetting flow. The AMM cannot do this: its pricing is deterministic, dictated by the bonding curve. In Ho-Stoll terms, the AMM has zero inventory-adjustment parameter (). This rigidity is precisely why impermanent loss exists: the AMM keeps quoting the same mid even as it accumulates one-sided inventory. See ho-stoll-inventory-model.

Kyle (price impact). Kyle’s model predicts linear price impact (). The AMM’s price impact is convex — it increases faster than linearly as trade size grows relative to pool reserves. This means AMMs are too generous to small informed trades (low impact) and too harsh on large ones (high impact). Unlike Kyle’s market maker, the AMM cannot modulate based on the information environment. See kyle-lambda.

Spread decomposition. The AMM’s swap fee is a flat spread that conflates all three Huang-Stoll components (adverse selection, inventory risk, order processing). A traditional market maker prices each component separately — widening for informed flow, skewing for inventory. The AMM’s fee is exogenous (set at pool creation), so it cannot adapt to changing market conditions. Uniswap v4’s hook system begins to address this by allowing programmable fee logic. See spread-decomposition.


Companion notebook: notebook — interactive plots of the invariant curve, price impact as a function of trade size, and fee accumulation over a sequence of simulated trades.

Questions to sit with:

  1. The AMM’s effective is . In Kyle’s model, depends on the ratio of informed-to-noise trading. What is the AMM analogue of “informed trading intensity”?
  2. If fees increase monotonically, the pool gets deeper over time even without new LP deposits. Under what conditions does this fee growth offset impermanent-loss?
  3. The constant-product rule treats both tokens symmetrically. What happens if you believe one token is fundamentally more volatile than the other — should the invariant be asymmetric?