Constant Product Formula
The x·y=k invariant that sets AMM prices: buying one side makes it scarcer and more expensive.
Basically
The formula x * y = k multiplies the amounts of two tokens in a pool. It keeps the result the same after each trade and sets the price you get.
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 keeping the product of two token reserves constant after every trade.
- Larger trades cause greater slippage because the formula follows a hyperbolic curve. Each additional unit purchased becomes progressively more expensive, which protects the pool from being fully drained.
- Concentrated liquidity in Uniswap v3 developed the basic model by letting liquidity providers allocate capital within custom price ranges. This can achieve 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. It is expressed as x * y = k, where x and y represent the reserves of two tokens in a liquidity pool and k is a constant, and it 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.
Vitalik Buterin first proposed the concept 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. The constant product formula has since 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. One reserve must grow proportionally as the other shrinks.
Step-by-Step Trade Example
A pool with 100 ETH and 100,000 USDC gives 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 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 invariant directly provides the exact output for a given input. You can derive it from x * y = k.
The formula shows 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
DEX protocols charge a fee on each swap in practice. Uniswap v2 applies a 0.3% fee to the input token before computing the output.
Uniswap v2 adds fees back to pool reserves, causing k to gradually increase over time as fees compound. Liquidity providers therefore 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. A pool can therefore never be fully drained of either token because each incremental unit becomes infinitely expensive as the reserve approaches zero.
A constant product pool always quotes a price for any trade size, unlike a traditional order book that can run out of orders at certain prices. 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.
Liquidity providers in v3 choose a specific price range instead of deploying capital across the entire price curve. 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.
Uniswap v3 internally 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, and the innovation is in how capital is allocated across the curve.
Comparing AMM Formulas
Curve Finance, launched in November 2019, designed its StableSwap invariant specifically for stablecoin pairs. When the pool is balanced, it behaves like a constant sum formula with minimal slippage near the peg, but as imbalance grows, it transitions to constant product behaviour to prevent drainage. An amplification coefficient A controls how tightly the curve hugs the 1:1 exchange rate.
Decentralized Token Trading
The most direct application lets anyone swap between two tokens without intermediaries, order books, or counterparties. The constant product formula guarantees that a price is always available. This enables 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 specialised markets.
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. Liquidity providers are left 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 show the loss at different price changes.
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 through front-running, and a sell order after it through back-running, profiting from the predictable price movement. This form of MEV extraction is a structural consequence of transparent, deterministic pricing.
Oracle Manipulation
The spot price in a constant product pool is simply y / x, so 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 customise pool behaviour. 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.
The research article on MEV extraction in Layer 2 systems offers a deeper exploration of how decentralized exchanges apply these formulas in practice. It examines their practical application in greater detail.
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.