AMM Math Deep Dive
Every Uniswap pool runs the same mathematical invariant: x y = k. Understanding this formula
unlocks how price impact, slippage, fee revenue, and impermanent loss all flow from one simple idea.
Constant Product Explorer
Adjust the two token reserves and the trade amount to see how xy=k determines the output.
Notice how large trades push the pool far from its pre-trade price.
Slippage Calculator
For a given pool depth and trade size, compute the expected slippage across fee tiers. Deep pools with high liquidity have lower slippage; thin pools can move 5-10% on a single large trade.
| Fee Tier | Effective Depth | Output Amount | Slippage | Fee Revenue |
|---|
Constant Product vs StableSwap
The same xy=k formula behaves very differently for pegged vs. volatile assets. Curve's StableSwap flattens the curve near the peg so that USDC/USDT trades feel almost frictionless. See the comparison:
The Math: Swaps, k, and Fee Math
How xy=k produces a market
The constant product invariant is deceptively simple. A pool starting with 1,000 ETH and 2,000,000 USDC has k = 2,000,000,000. The pre-trade spot price is simply y/x = 2,000 USDC per ETH. When a trader submits a swap for ?x ETH, the protocol first deducts the fee (e.g. 0.30% = 0.003 ?x), then computes the new reserves as x' = x + (?x ? fee) and y' = k / x'. The trader receives y ? y' USDC.
The critical insight is that every unit of ?x pushed into the pool moves the price - and larger pushes move it more. For the first 10 ETH swapped into the 1,000 ETH / 2M USDC pool, the output is approximately 19,802 USDC (about 1% below the spot price). For the next 10 ETH, the output drops to about 19,604 USDC - the execution price for the second tranche is worse than the first. This is slippage: the average price paid per unit is between the spot price and the execution price.
The fee is what makes LP economically viable. Every swap pays a fee that is distributed pro-rata to all existing LPs, so even though the AMM's constant rebalancing causes impermanent loss, the fee revenue from sufficient trading volume more than compensates. For a pool doing $10M daily volume at 0.30% fee, LPs earn $30,000 per day - enough to offset IL from moderate price moves within days.
Key concepts
- Invariant k
- The product of the two token reserves. In V2, k is constant for the life of the pool unless an LP adds or removes liquidity. In V3, the concept of "liquidity" (??) is separated from k, but the swap formula still reads as if the pool has full-range reserves when active - this is what makes V3 swaps equivalent to constant-product within the active range.
- Spot price
- The ratio of reserves: y/x. This is also the price at which an infinitesimally small trade would execute. Any real trade, no matter how small, executes at a worse price due to the discrete nature of the EVM math and the rounding that happens at the bytecode level.
- Execution price
- The average price paid: (USDC paid) / (ETH received). For a trade split into multiple tranches (if routed through multiple pools), the execution price is the volume-weighted average of each tranche's execution price.
- Price impact
- The percentage difference between the execution price and the pre-trade spot price. A 1% price impact means the trade moves the market 1%. Price impact is non-linear: it grows faster as the trade size approaches a meaningful fraction of the pool depth.
- Slippage tolerance
- The maximum acceptable price impact set by the trader as a guard against market movement between transaction signing and settlement. A 0.5% slippage tolerance means the swap reverts if the execution price is more than 0.5% worse than the quoted spot price - protecting against MEV sandwiching where an attacker floods the mempool with a large trade to move the price before the victim's trade executes.
- Fee revenue model
- At 0.30% fee, each swap splits: 0.06% goes to protocol (if fee switch is on), 0.24% goes to LPs. On a $1M swap, LPs earn $2,400. The APY for LPs in a pool depends on daily volume / TVL ratio: $10M daily volume in a $100M pool implies 36.5% annualized fee return (before IL).