Glossary

CFMM (Constant Function Market Maker)

A constant function market maker uses a mathematical invariant to price assets in a liquidity pool without an order book.

Key Takeaways

  • A constant function market maker (CFMM) is a type of automated market maker that uses a mathematical invariant function to determine asset prices. Every valid trade must preserve the value of this function, ensuring that the pool always has liquidity available at some price.
  • The three foundational CFMM variants are the constant product (x · y = k, used by Uniswap), the constant sum (x + y = k, useful for pegged assets), and the constant mean (weighted geometric mean, used by Balancer for multi-asset pools).
  • Innovations like StableSwap (Curve) and concentrated liquidity (Uniswap V3) modify the effective invariant to improve capital efficiency, reducing slippage for traders and boosting returns for liquidity providers.

What Is a Constant Function Market Maker?

A constant function market maker (CFMM) is a class of automated market maker defined by a trading function φ that maps pool reserves to a single value. For any proposed trade, the CFMM smart contract accepts it only if the trading function remains constant after the reserves change. This mathematical constraint replaces the traditional order book model: instead of matching buyers to sellers, the invariant function itself determines prices.

The term “constant function” refers to this core property. Given reserves (x, y) for two tokens, a trading function φ(x, y) must satisfy φ(x, y) = φ(x′, y′) after any valid swap, where (x′, y′) are the new reserves. Different choices of φ produce different pricing behaviors, slippage curves, and capital efficiency profiles. This makes CFMMs a general mathematical framework, with specific protocols like Uniswap, Curve, and Balancer each selecting an invariant function optimized for their use case.

CFMMs power the majority of on-chain trading volume across decentralized finance. Their permissionless nature allows anyone to create a liquidity pool or provide liquidity without requiring approval, and their deterministic pricing means every trade executes transparently on-chain.

How It Works

A CFMM operates as a smart contract holding reserves of two or more tokens. When a trader wants to swap token A for token B, they deposit some amount of A into the pool and withdraw an amount of B. The smart contract calculates the valid withdrawal amount by solving for the value that keeps the invariant function constant.

  1. The pool holds reserves (x, y) of two tokens and defines an invariant function φ(x, y)
  2. A trader proposes depositing Δx of token A
  3. The contract solves for Δy such that φ(x + Δx, y − Δy) = φ(x, y)
  4. If the solution is valid (reserves stay positive, fees are applied), the swap executes
  5. The marginal price of one token in terms of the other shifts based on the new reserve ratio

Because the invariant must be preserved, larger trades cause greater price impact. A trade that represents a large fraction of the pool's reserves pushes the price further along the invariant curve, resulting in worse execution. This self-adjusting property means CFMMs always have liquidity available at some price, though that price may be unfavorable for large orders.

Constant Product (x · y = k)

The constant product formula is the most widely deployed CFMM variant. Uniswap V1 and V2 use this invariant, where the product of the two reserve balances must remain constant after every trade.

// Constant product invariant
// x = reserve of token A, y = reserve of token B
x * y = k

// Example: pool has 100 ETH and 200,000 USDC
// k = 100 * 200,000 = 20,000,000

// Trader deposits 10 ETH, how much USDC do they receive?
// (100 + 10) * (200,000 - Δy) = 20,000,000
// 110 * (200,000 - Δy) = 20,000,000
// 200,000 - Δy = 181,818.18
// Δy = 18,181.82 USDC

// Effective price: 18,181.82 / 10 = 1,818.18 USDC per ETH
// Spot price before trade: 200,000 / 100 = 2,000 USDC per ETH
// Price impact: ~9.1%

The curve defined by x · y = k is a hyperbola that approaches both axes asymptotically but never reaches zero. This means neither reserve can ever be fully depleted, regardless of trade size. However, the price deterioration for large trades is significant, making constant product pools best suited for volatile asset pairs where prices fluctuate widely across the full range.

Constant Sum (x + y = k)

The constant sum invariant maintains a fixed total of reserves: x + y = k. This produces a linear pricing curve with zero slippage, meaning every unit trades at the same price regardless of trade size.

// Constant sum invariant
x + y = k

// Every unit of token A trades at exactly 1 unit of token B
// No price impact, no slippage

While zero slippage sounds ideal, constant sum pools have a critical flaw: they can be completely drained. If the market price deviates from the pool's fixed 1:1 rate, an arbitrageur can buy the underpriced token until one side of the pool is empty. For this reason, pure constant sum pools are not used in practice. However, the constant sum concept is important as a building block in hybrid designs like StableSwap.

Constant Mean (Weighted Geometric Mean)

Constant mean market makers, popularized by Balancer, generalize the constant product formula by introducing weights. For a pool with n tokens and weights w₁, w₂, ..., wₙ (where all weights sum to 1), the invariant is:

// Constant mean invariant (Balancer)
// For two tokens with weights w and (1 - w):
x^w * y^(1-w) = k

// For three tokens with weights w1, w2, w3:
x1^w1 * x2^w2 * x3^w3 = k

// Example: 80/20 ETH/USDC pool
// ETH has weight 0.8, USDC has weight 0.2
// This means the pool holds 80% of value in ETH, 20% in USDC

// When weights are equal (50/50), this reduces to x * y = k
// (the standard constant product formula)

Weighted pools allow liquidity providers to maintain custom portfolio exposures. An 80/20 ETH/USDC pool gives the LP 80% exposure to ETH price movements, compared to the 50/50 split in a standard constant product pool. Balancer supports up to 8 tokens in a single pool, enabling index-fund-like products on-chain.

StableSwap (Curve Hybrid Invariant)

The StableSwap invariant, introduced by Curve Finance in 2019, combines the constant product and constant sum curves using an amplification coefficient A:

// StableSwap invariant (simplified for two assets)
// A = amplification coefficient
// D = total invariant value
// x, y = token reserves

A * (x + y) + D = A * D + D^2 / (4 * x * y)

// When A = 0: behaves like constant product (x * y = k)
// When A → ∞: behaves like constant sum (x + y = k)
// In practice, A is set to values like 100-2000

Near equilibrium (where both reserves are balanced), the StableSwap curve approximates a constant sum, providing extremely low slippage. As reserves become imbalanced, the curve transitions toward constant product behavior, preventing either reserve from being depleted. This makes StableSwap approximately 100x more capital efficient than a standard constant product pool for assets that trade near a 1:1 peg, such as stablecoin pairs (USDC/USDT) or wrapped asset pairs (ETH/stETH).

Concentrated Liquidity

Uniswap V3 introduced concentrated liquidity, which modifies the effective CFMM invariant by allowing liquidity providers to allocate capital within specific price ranges rather than across the entire curve. Within each range, the pool uses “virtual reserves” that behave according to the constant product formula (x · y = L², where L is the concentrated liquidity), but the actual capital required is only a fraction of what a full-range position would need.

This approach creates a piecewise CFMM: each price tick has its own liquidity depth, and the aggregate curve can approximate any shape depending on how LPs distribute their positions. In practice, concentrated liquidity around the current price produces behavior similar to StableSwap for active ranges, with dramatically improved capital efficiency. A position concentrated within a narrow range can achieve hundreds of times the fee revenue compared to a full-range position with the same capital.

Comparing CFMM Variants

VariantInvariantBest ForProtocol
Constant productx · y = kGeneral volatile pairsUniswap V2
Constant sumx + y = kTheoretical only (drainable)None standalone
Constant mean∏ xᵢ^wᵢ = kMulti-asset, weighted portfoliosBalancer
StableSwapHybrid sum/productPegged assets (stablecoins)Curve
Concentrated liquidityRange-bound constant productActive LP managementUniswap V3/V4

Use Cases

  • Decentralized token swaps: CFMMs are the backbone of decentralized exchanges, enabling permissionless trading of any token pair without relying on centralized order matching
  • Stablecoin liquidity: StableSwap CFMMs provide deep, low-slippage liquidity for stablecoin pairs, processing billions in daily volume with minimal price impact
  • Portfolio rebalancing: constant mean pools allow users to create on-chain index funds that automatically rebalance, with arbitrageurs paying fees to maintain target weights
  • Price discovery: CFMMs provide continuous on-chain price feeds that oracles and other protocols can reference, serving as a source of market-driven pricing data
  • Yield generation: liquidity providers earn trading fees proportional to their share of the pool, creating passive income opportunities on idle assets

Why It Matters

CFMMs fundamentally changed how markets operate by replacing human market makers and order books with deterministic mathematical functions. Traditional market making requires sophisticated firms with large balance sheets and low-latency infrastructure. CFMMs democratize this role: anyone with capital can provide liquidity and earn fees.

The design space for CFMMs continues to expand. Protocols experiment with dynamic fee adjustment, oracle-informed pricing, and custom invariant curves tailored to specific asset types. Each new variant explores a different tradeoff between capital efficiency, slippage, and impermanent loss for liquidity providers. As DeFi matures, CFMMs serve as the foundational primitive that most other protocols build on: lending markets use CFMM prices for liquidation triggers, stablecoin arbitrage relies on CFMM pools to restore pegs, and aggregators route through multiple CFMM pools to find optimal execution.

For a deeper exploration of how these trading mechanisms fit into the broader DeFi landscape, see the research on sustainable DeFi revenue models.

Risks and Considerations

Impermanent Loss

All CFMM liquidity providers face impermanent loss: when the relative price of pooled assets changes, the LP's position becomes worth less than if they had simply held the tokens. This occurs because the invariant function mechanically sells the appreciating asset and buys the depreciating one as prices move. The loss is “impermanent” only if prices revert; if they do not, the loss is realized upon withdrawal.

Price Manipulation

Because CFMM prices are determined solely by reserve ratios, they can be manipulated within a single transaction through flash loans. An attacker can borrow a large amount, push the CFMM price to an extreme, exploit another protocol that reads this price, then repay the loan. This oracle manipulation risk is why protocols typically use time-weighted average prices (TWAPs) rather than spot CFMM prices for critical operations.

MEV Extraction

CFMM trades broadcast to a public mempool are vulnerable to maximal extractable value strategies. Sandwich attacks place transactions before and after a victim's swap to profit from the predictable price movement, effectively extracting value from traders. Front-running and back-running also exploit the deterministic nature of CFMM pricing.

Smart Contract Risk

CFMMs rely entirely on smart contract code for custody and execution. Bugs in the invariant logic, rounding errors, or reentrancy vulnerabilities can lead to pool draining. While major protocols undergo extensive auditing and formal verification, the composable nature of DeFi means that interactions between protocols can create emergent risks not present in any single contract.

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.