Constant Product Formula
The mathematical formula (x · y = k) that governs automated market makers, maintaining a constant product of token reserves.
The constant product formula, expressed as x · y = k, is the foundational mathematical principle behind Automated Market Makers (AMMs) like Uniswap v1 and v2. This elegantly simple equation defines how token reserves in a liquidity pool relate to each other and determines the price of every trade. Understanding this formula is essential for anyone building, auditing, or providing liquidity to AMM-based protocols.
How the Formula Works
In a constant product AMM:
- x represents the quantity of one token in the pool (e.g., ETH)
- y represents the quantity of the paired token (e.g., USDC)
- k is the constant product—a value that must remain unchanged during any swap
When a trader wants to exchange tokens, they add one asset and remove the other. The amounts are constrained such that the product of the reserves stays constant:
1(x + Δx) · (y - Δy) = k
This constraint mathematically determines how much output (Δy) a trader receives for their input (Δx).
The Price Curve
The constant product formula creates a hyperbolic bonding curve. This curve has important properties that define AMM behavior:
Infinite liquidity (theoretically): The curve asymptotically approaches both axes but never touches them. Mathematically, the pool can never be fully drained of either asset through normal trading—prices simply become astronomically unfavorable.
Continuous pricing: Every point on the curve represents a valid price. Unlike order book exchanges with discrete price levels, AMMs offer pricing at any trade size.
Non-linear price impact: Larger trades move further along the curve, experiencing progressively worse prices. This creates natural slippage protection for the pool.
Price Determination
The instantaneous price at any moment equals the ratio of reserves:
1Price of X in terms of Y = y / x
After a trade, this ratio changes, which is why larger trades experience more slippage. The marginal price (price for an infinitesimally small trade) differs from the effective price (average price for the actual trade amount).
Fee Accumulation and k Growth
In practice, k doesn't stay perfectly constant—it grows over time due to trading fees. When a 0.3% fee is charged on each swap, that fee is added back to the reserves:
1// Simplified: fee stays in pool, increasing k2newK = (x + amountIn) * (y - amountOut)3// Where amountIn includes the fee portion that stays
This growth in k represents value accruing to liquidity providers. The larger k becomes, the more the pool is worth.
Security Implications
The deterministic nature of the constant product formula creates both strengths and vulnerabilities:
Price manipulation risks: Because price depends solely on reserve ratios, attackers can manipulate prices within a single transaction. Flash loan attacks often exploit this by temporarily skewing reserves to manipulate price-dependent logic.
Not suitable as a spot oracle: The instantaneous price (y/x) can be manipulated atomically. Protocols needing price data should use time-weighted average prices (TWAP) or external oracles like Chainlink instead.
Sandwich attacks: The predictable price impact allows MEV searchers to front-run and back-run large trades, extracting value from traders.
Comparison with Other AMM Formulas
The constant product formula isn't the only approach:
| Formula | Example | Characteristics |
|---|---|---|
| Constant Product (x·y=k) | Uniswap v2 | Simple, works for any pair, high slippage |
| Constant Sum (x+y=k) | — | Zero slippage but easily drained |
| StableSwap | Curve | Low slippage for similar assets |
| Concentrated Liquidity | Uniswap v3 | Capital efficient within price ranges |
Each formula makes different trade-offs between capital efficiency, slippage, and complexity.
Practical Example
Consider a pool with 10 ETH and 20,000 USDC:
1k = 10 × 20,000 = 200,0002Initial price: 20,000 / 10 = 2,000 USDC per ETH
A trader wants to buy 1 ETH:
1(10 - 1) × (20,000 + Δy) = 200,00029 × (20,000 + Δy) = 200,0003Δy = 22,222.22 - 20,000 = 2,222.22 USDC
The trader pays 2,222.22 USDC for 1 ETH—an effective price of 2,222.22 instead of the initial 2,000. This 11% price impact demonstrates how the constant product formula creates natural slippage for larger trades relative to pool size.
The constant product formula's simplicity enabled the DeFi revolution, but its properties—especially around price manipulation—require careful consideration in security audits and protocol design.
Articles Using This Term
Learn more about Constant Product Formula in these articles:

Uniswap V1: How the First AMM Worked - and the Security Gaps It Left Behind
How Uniswap V1 invented the AMM: the x*y=k invariant, ETH-only liquidity pools, and the price manipulation gaps that V2 fixed — a security-first code analysis by Zealynx.

AMM Security Foundations: Master DeFi Trading Risks
Deep dive into the foundations of constant-product AMMs. Learn the math, smart contract building blocks, and core security risks behind decentralized trading protocols.
Related Terms
Automated Market Maker (AMM)
A decentralized exchange protocol that uses mathematical formulas to price assets instead of order books.
Liquidity Provider (LP)
A user who deposits assets into a liquidity pool to facilitate trading, earning fees in return.
Slippage
The difference between the expected price of a trade and the actual execution price, caused by market movement or liquidity constraints.
Impermanent Loss
The temporary loss in value experienced by liquidity providers when the price ratio of deposited assets changes compared to holding them.
Need expert guidance on Constant Product Formula?
Our team at Zealynx has deep expertise in blockchain security and DeFi protocols. Whether you need an audit or consultation, we're here to help.
Get a Quote
