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 k
2newK = (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:

FormulaExampleCharacteristics
Constant Product (x·y=k)Uniswap v2Simple, works for any pair, high slippage
Constant Sum (x+y=k)Zero slippage but easily drained
StableSwapCurveLow slippage for similar assets
Concentrated LiquidityUniswap v3Capital 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,000
2Initial price: 20,000 / 10 = 2,000 USDC per ETH

A trader wants to buy 1 ETH:

1(10 - 1) × (20,000 + Δy) = 200,000
29 × (20,000 + Δy) = 200,000
3Δ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.

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

oog
zealynx

Smart Contract Security Digest

Monthly exploit breakdowns, audit checklists, and DeFi security research — straight to your inbox

© 2026 Zealynx