Uniswap AMM Core Mechanics
Uniswap's Automated Market Maker (AMM) is the engine that powers decentralized token swaps. At its heart is the constant-product formula x*y=k — a deceptively simple equation that creates an elegant pricing curve without any order book or centralized matching. Understanding how it works is essential for any DeFi user, liquidity provider, or protocol developer.
? Swap Simulator
Configure a swap and see how price impact, fees, and output amount change with pool size and trade size.
The Constant Product Formula: x ? y = k
Uniswap's pricing mechanism is governed by one equation: the product of the two token reserves must remain constant after every swap. This single invariant creates the entire pricing curve without any external price feed or matching engine.
- x = reserve of token0 (e.g., ETH)
- y = reserve of token1 (e.g., USDC)
- k = constant (never changes)
= USDC_reserve / ETH_reserve
price = 2,000,000 / 1,000 = 2,000 USDC/ETH
? Visual: The AMM Price Curve
The constant-product curve is a hyperbola. Near the middle (1:1 ratio), the curve is relatively flat — small swaps have minimal price impact. As you move away from center, the curve steepens — each additional unit costs exponentially more.
How Swaps Execute: Step by Step
When you execute a swap, the AMM follows a precise sequence of calculations. The fee is taken first, then the constant-product invariant is applied to compute output.
Output: ~1,994 USDC
Price impact: ~0.10%
Near-perfect execution
Output: ~97,561 USDC
Price impact: ~2.4%
Notable slippage
Output: ~363,636 USDC
Price impact: ~9.1%
Severe slippage
Fee Tiers: Why They Exist
Uniswap supports three fee tiers (0.05%, 0.30%, 1.00%) — each optimized for different volatility profiles. The fee tier is set when the pool is created and cannot be changed without deploying a new pool.
| Fee Tier | Best For | LP Annualized Revenue | IL Risk | Typical Pairs |
|---|---|---|---|---|
| 0.05% | Stablecoin pairs (1:1 ratio) | Low per trade, high volume | Minimal (correlated assets) | USDC/USDT, DAI/USDC |
| 0.30% | General ETH/major tokens | Moderate per trade | Moderate | ETH/USDC, BTC/USDC |
| 1.00% | Volatile/meme/exotic tokens | High per trade, lower volume | High | MEME/ETH, PEPE/USDC |
Liquidity Provision: How to Earn Fees
Liquidity providers (LPs) deposit token pairs into pools and earn a share of the trading fees. In return for providing liquidity, you receive LP tokens that represent your proportional share of the pool.
Providing Liquidity Steps
- Select pool — Choose token pair + fee tier
- Deposit both tokens — In proportion to current ratio (e.g., 50% ETH + 50% USDC by value)
- Receive LP tokens — Proportional to your share of the pool
- Fees accumulate — Every swap distributes 0.30% to all LPs
- Claim fees — Redeem accrued fees via LP token
- Withdraw — Burn LP tokens to get back tokens + fees
LP Token Mechanics
- ERC-20 tokens — Standard fungible tokens
- 1:1 value backing — Each LP token = fractional pool share
- Fee accumulation — Pool grows in value, LP tokens appreciate
- No lockup — Withdraw anytime (but you lose accumulated fees if you exit early)
- Composable — Can be used in other DeFi (LP tokens as collateral, etc.)
Virtual Reserves & V3 Concentrated Positions
In Uniswap V3, positions use virtual reserves — the token amounts in a hypothetical full-range V2 pool that would produce the same swap behavior as the concentrated position. The swap formula xy=k still applies, but to virtual reserves.
Capital deployed: $10,000
Virtual reserves: $10,000
Fee revenue: 0.30% of trade value
IL risk: distributed across all prices
Capital deployed: $200
Virtual reserves: $10,000
Fee revenue: Same as V2 full-range!
IL risk: Amplified 50x within the range
? Price Impact Curve
As you increase trade size, price impact grows non-linearly. This chart shows how price impact compounds for the same ETH/USDC pool at different trade sizes.
How the AMM Actually Works
Uniswap's AMM is built on a single invariant: x * y = k. When you deposit liquidity into a pool, you're committing tokens to a shared reserve. The AMM uses these reserves to facilitate trades, charging a fee on every swap and distributing that fee proportionally to all LP token holders.
The beauty of the constant-product formula is that it creates a self-regulating market without any external control. If someone buys a lot of ETH, the ETH reserve decreases and the USDC reserve increases. The new price (USDC/ETH) is automatically higher — because the pool now has less ETH relative to USDC, each unit of ETH costs more USDC. This is the market's way of discouraging further buys and encouraging sells.
The 0.30% fee (in V2 and standard V3 pools) is taken at the input side — before the swap math is applied. So when you swap 10 ETH, only 9.97 ETH goes into the constant-product calculation, and the remaining 0.03 ETH becomes the first piece of LP fee revenue. This fee is taken upfront so that the pool's k is computed correctly with the net input amount.
Liquidity Provision Math
When you provide liquidity, you deposit both tokens in the proportion that matches the current pool ratio. If the pool has 1,000 ETH and 2,000,000 USDC, the ratio is 2,000 USDC per ETH. If you want to add $5,000 of liquidity, you'd deposit 2.5 ETH and 5,000 USDC — maintaining the ratio.
You receive LP tokens proportional to your share. If the total pool is worth $2,000,000 and you deposit $10,000, you own 0.5% of the pool and receive LP tokens representing that 0.5% share. Every time someone trades, 0.30% of the trade value goes to the pool, increasing the value of all LP tokens proportionally.
To withdraw, you burn your LP tokens and receive your proportional share of the current pool — which now includes accumulated fees. If the pool grew from $2,000,000 to $2,100,000 due to trading fees, your 0.5% share is worth $10,500, and you receive that plus your initial principal back in the form of the token pair.
Impermanent Loss: The Hidden Cost
Impermanent loss (IL) is the difference between holding tokens in the AMM vs holding them in a wallet. It occurs because the AMM automatically sells your appreciating asset as the price rises — leaving you with less of it than if you'd just held. IL is "impermanent" in the sense that it becomes real only when you withdraw; if prices return to the original ratio, the loss disappears.
The IL formula for a price ratio change of r (where r = new_price / old_price) is: IL = 2*?r / (1+r) - 1. For a 2x price increase (r=2), IL = 2*?2/3 - 1 = -5.72%. For a 5x increase (r=5), IL = -25.5%. This means even if the pool earns 20% in fees, a 5x price move would leave the LP underwater compared to just holding.
V3 concentrated positions amplify IL geometrically. A 2% width position experiences ~50x the IL of a full-range V2 position for the same price move — because the same capital is concentrated into a tighter price window where each price step has a proportionally larger effect.
Multi-Hop Swaps
Uniswap's router can find paths through multiple pools. If you want to swap USDC for WBTC but there's no direct WBTC/USDC pool, the router might find: USDC → ETH (ETH/USDC pool) → WBTC (WBTC/ETH pool). Each hop adds a fee, so multi-hop swaps are more expensive than direct swaps.
The router uses a "split search" algorithm — it checks all possible paths up to a maximum number of hops (typically 3), computes the output for each path, and returns the best one. For same-chain swaps, paths with more hops are generally worse due to compounding fees, but they may be necessary for illiquid pairs.
Key Concepts
- Constant product invariant (x*y=k)
- The mathematical foundation of Uniswap's pricing. The product of the two token reserves must remain constant after every swap. This creates a hyperbolic price curve where buying one token increases its price exponentially as you consume more liquidity.
- Trading fee (0.30% standard)
- Charged on the input amount of every swap. For a 10 ETH swap at 0.30% tier, 0.03 ETH goes to LPs and 9.97 ETH goes into the constant-product calculation. The fee is built into the swap math, not added afterward.
- LP tokens
- ERC-20 tokens issued when you provide liquidity. They represent your proportional share of a pool's reserves plus accumulated fees. Each pool has its own LP token (e.g., ETH/USDC LP token is different from ETH/WBTC LP token).
- Price impact
- The difference between the mid price before your swap and your actual average fill price. It's caused by walking up the AMM curve — each unit of the asset you buy costs more than the previous unit. Price impact grows with swap size relative to pool liquidity.
- Virtual reserves
- In V3 concentrated positions, the token amounts in a hypothetical full-range V2 pool that would produce the same swap behavior. The constant product formula applies to virtual reserves, enabling the same depth with a fraction of the capital.
- Slippage tolerance
- Your specified maximum acceptable price deviation from the quoted price. If the execution price moves beyond your slippage tolerance, the transaction reverts. Setting a higher tolerance protects against failed transactions but increases MEV exposure.
- Minimum output
- The minimum amount of output token you'll accept from a swap. Calculated by the router based on the quoted price minus your slippage tolerance. If the swap would produce less than this, it reverts and you pay no gas.