Constant Product Formula
The constant product formula (x * y = k) is the mathematical model behind most automated market makers, governing price and liquidity.
Key Takeaways
- The constant product formula (x * y = k) is the core pricing rule used by most automated market makers: it ensures a pool always has liquidity by making the product of two token reserves remain constant after every trade.
- Larger trades cause greater slippage because the formula follows a hyperbolic curve: each additional unit purchased becomes progressively more expensive, protecting the pool from being fully drained.
- Concentrated liquidity (Uniswap v3) evolved the basic model by letting liquidity providers allocate capital within custom price ranges, achieving up to 4,000x greater capital efficiency.
What Is the Constant Product Formula?
The constant product formula is the mathematical invariant that powers most decentralized exchanges. Expressed as x * y = k, where x and y represent the reserves of two tokens in a liquidity pool and k is a constant, this equation determines how prices change as traders swap between assets. When someone buys token X from a pool, the X reserve decreases while the Y reserve increases, but the product of the two must remain equal to k.
The concept was first proposed by Vitalik Buterin in an October 2016 Reddit post exploring on-chain decentralized exchanges, drawing on work by Alan Lu at Gnosis. Hayden Adams built it into a working prototype in 2017, and Uniswap v1 launched on Ethereum mainnet on November 2, 2018. Since then, the constant product formula has become the foundation of decentralized finance, powering billions of dollars in daily trading volume across protocols like Uniswap, SushiSwap, PancakeSwap, and Raydium.
How It Works
A constant product pool holds reserves of two tokens: token X with reserve x, and token Y with reserve y. The pool enforces the invariant x * y = k after every trade, meaning that as one reserve shrinks the other must grow proportionally.
Step-by-Step Trade Example
Consider a pool with 100 ETH and 100,000 USDC, giving k = 10,000,000. The spot price of ETH is 100,000 / 100 = 1,000 USDC. A trader wants to buy ETH by depositing 10,000 USDC:
- The trader sends 10,000 USDC to the pool contract
- New USDC reserve: 100,000 + 10,000 = 110,000
- The pool solves for the new ETH reserve: ETH_new = k / USDC_new = 10,000,000 / 110,000 = 90.909
- ETH output: 100 - 90.909 = 9.091 ETH sent to the trader
- New spot price: 110,000 / 90.909 = 1,210 USDC per ETH
The trader paid an effective price of 10,000 / 9.091 = 1,100 USDC per ETH, which is 10% above the starting spot price. This deviation is slippage: the larger the trade relative to pool reserves, the worse the execution price.
The Output Formula
The exact output for a given input can be derived directly from the invariant:
// Given reserves x, y and input delta_x:
// (x + delta_x) * (y - delta_y) = x * y
// Solving for delta_y (output):
delta_y = y * delta_x / (x + delta_x)
// Spot price at any point:
price = y / xThis formula reveals why slippage is unavoidable: the denominator (x + delta_x) grows with the trade size, reducing the output. For infinitesimally small trades, the effective price approaches the spot price y / x. For large trades, the output diminishes significantly.
How Fees Modify the Formula
In practice, DEX protocols charge a fee on each swap. Uniswap v2 applies a 0.3% fee to the input token before computing the output:
// With 0.3% fee (fee factor = 0.997):
delta_y = y * (0.997 * delta_x) / (x + 0.997 * delta_x)
// Example: swapping 25 ETH in a 100 ETH / 100 USDC pool
// Effective input: 25 * 0.997 = 24.925 ETH
// Output: 100 * 24.925 / (100 + 24.925) = 19.952 USDCIn Uniswap v2, fees are added back to pool reserves, causing k to gradually increase over time as fees compound. This means liquidity providers earn returns simply from the trading activity in their pool.
Why It Creates Infinite Liquidity
The constant product formula traces a hyperbolic curve that asymptotically approaches both axes but never touches them. This has a critical implication: a pool can never be fully drained of either token because each incremental unit becomes infinitely expensive as the reserve approaches zero.
Unlike a traditional order book that can run out of orders at certain prices, a constant product pool always quotes a price for any trade size. The tradeoff is that extremely large trades (relative to pool depth) receive extremely poor execution. This property makes the formula suitable for permissionless, always-available liquidity but creates challenges for traders executing large orders.
Concentrated Liquidity: The Evolution
Standard constant product pools spread liquidity uniformly across all possible prices from zero to infinity. Most of this capital sits idle because trading activity concentrates around the current market price. Uniswap v3, launched on May 5, 2021, introduced concentrated liquidity to solve this inefficiency.
Instead of deploying capital across the entire price curve, liquidity providers in v3 choose a specific price range. Each position acts as a mini constant product curve that is active only within its bounds. The Uniswap v3 whitepaper demonstrated that a position concentrated in the 0.99 to 1.01 range for a stablecoin pair could provide the same market depth as a v2 pool with 4,000 times more capital.
Internally, v3 tracks liquidity (L) and the square root of price (sqrt(P)) rather than raw token reserves. Between any two adjacent price ticks, the pool behaves exactly like a constant product pool with "virtual reserves." The formula still governs local pricing: the innovation is in how capital is allocated across the curve.
Comparing AMM Formulas
| Formula | Invariant | Best For | Tradeoff |
|---|---|---|---|
| Constant Product | x * y = k | General-purpose token pairs | High slippage on large trades |
| Constant Sum | x + y = k | Fixed-rate swaps | Pool can be fully drained |
| StableSwap (Curve) | Hybrid invariant with amplification coefficient | Pegged assets (stablecoins) | Complex; requires tuning parameter A |
| Concentrated (v3) | x * y = k within price ranges | Capital-efficient LP positions | Active management required |
Curve Finance (launched November 2019) designed its StableSwap invariant specifically for stablecoin pairs. When the pool is balanced, it behaves like a constant sum formula (minimal slippage near the peg). As imbalance grows, it transitions to constant product behavior to prevent drainage. An amplification coefficient A controls how tightly the curve hugs the 1:1 exchange rate.
Use Cases
Decentralized Token Trading
The most direct application: anyone can swap between two tokens without intermediaries, order books, or counterparties. The constant product formula guarantees that a price is always available, enabling permissionless 24/7 trading on any ERC-20 token pair.
Liquidity Provision and Yield
Users deposit equal values of two tokens into a pool and earn a share of trading fees proportional to their contribution. This creates a passive income opportunity tied directly to trading volume. The constant product formula ensures that deposits and withdrawals are proportional to existing reserves, maintaining fairness among providers.
Price Discovery
Constant product pools serve as on-chain price references. Arbitrageurs continuously align pool prices with external markets, making AMM prices reliable enough to use as oracle inputs. Uniswap v2 introduced time-weighted average price (TWAP) oracles built directly on pool state, and many DeFi protocols rely on these price feeds.
Bonding Curves and Token Distribution
The constant product formula is a specific type of bonding curve: a mathematical function that determines token price based on supply. Projects use variations of this curve for token launches, governance mechanisms, and algorithmic pricing in specialized markets.
Risks and Considerations
Impermanent Loss
Impermanent loss is a direct mathematical consequence of the constant product formula. When the external price of one token moves relative to the other, arbitrageurs rebalance the pool to match. This leaves liquidity providers with more of the depreciating token and less of the appreciating one, compared to simply holding both tokens.
The loss follows a precise formula: IL = 2 * sqrt(r) / (1 + r) - 1, where r is the ratio of new price to original price. Key reference points:
| Price Change | Impermanent Loss |
|---|---|
| 1.25x (25% increase) | 0.6% |
| 1.5x (50% increase) | 2.0% |
| 2x (100% increase) | 5.7% |
| 3x (200% increase) | 13.4% |
| 5x (400% increase) | 25.5% |
Impermanent loss is symmetric: a 2x price increase causes the same 5.7% loss as a 0.5x decrease. The loss only depends on the magnitude of the price change, not the direction. Liquidity providers must earn enough in trading fees to offset this risk.
Slippage on Large Orders
The constant product formula provides progressively worse prices for larger trades. A trade consuming 10% of a pool's reserves incurs roughly 10% slippage, while a trade consuming 50% of reserves results in extreme price impact. Traders mitigate this by splitting orders across multiple pools via DEX aggregators or using limit orders.
Sandwich Attacks and MEV
The predictable price impact of the constant product formula makes pools vulnerable to sandwich attacks. An attacker observes a pending trade, places a buy order before it (front-running), and a sell order after it (back-running), profiting from the predictable price movement. This form of MEV extraction is a structural consequence of transparent, deterministic pricing.
Oracle Manipulation
Because the spot price in a constant product pool is simply y / x, a single large trade can temporarily distort the price. Protocols using instantaneous AMM prices as oracle inputs are vulnerable to oracle manipulation via flash loans. TWAP oracles mitigate this by averaging prices over multiple blocks, making manipulation more expensive.
The Constant Product Formula Today
Uniswap v4, launched on January 31, 2025, introduced hooks: external smart contracts that plug into the swap lifecycle to customize pool behavior. Hooks enable dynamic fees, automated rebalancing strategies, and custom oracle integrations while still relying on the constant product formula as the underlying pricing mechanism. The core invariant x * y = k remains the foundation, even as the ecosystem builds increasingly sophisticated layers on top of it.
For a deeper exploration of how decentralized exchanges apply these formulas in practice, see the research article on MEV extraction in Layer 2 systems.
This glossary entry is for informational purposes only and does not constitute financial or investment advice. Always do your own research before using any protocol or technology.