Lecture 10: Automated Market Makers (AMMs)
Instructor: Yu Feng, UCSB
CS190: Blockchain Programming and Applications
Lecture 10 — Explains how Automated Market Makers (AMMs) replace order books with math-based liquidity pools. It traces the evolution from constant-sum to constant-product to Curve’s StableSwap models, showing how each balances price stability, slippage, and capital efficiency—making AMMs the foundation of modern decentralized exchanges.
The Challenge of On-Chain Liquidity
The foundation of decentralized finance (DeFi) rests on the promise of open, programmable money. While core crypto assets like Ethereum (ETH) and Bitcoin (BTC) are revolutionary, their inherent volatility makes them challenging to use as a stable medium of exchange. Traditional stablecoins address value volatility, but a fundamental challenge remains:
How do we provide efficient, continuous trading liquidity without centralized order books?
This question is critical for the scalability and usability of decentralized exchanges (DEXs) and the broader DeFi ecosystem, leading to the innovation of Automated Market Makers (AMMs).
Automated Market Makers: The Core Innovation
The traditional order book model relies on centralized entities to match trades between buyers and sellers. Implementing this on-chain presents severe challenges: prohibitive gas fees, inefficiency, and fundamental scalability limitations.
Token Reserve Pools
Smart contracts hold reserves of two or more tokens
Mathematical Invariants
Predefined formulas automatically determine prices
Permissionless Trading
Continuous on-chain liquidity without intermediaries
This lecture explores three primary AMM designs, their underlying mathematics, and their respective trade-offs for different use cases in decentralized finance.
Constant-Sum Market Maker (CSMM)
The Linear Invariant
The Constant-Sum Market Maker represents the simplest possible pricing mechanism, maintaining a linear invariant that ensures predictable 1:1 exchanges.
x + y = k
Where x and y represent the reserves of two tokens (X and Y), and k is a constant that remains unchanged when no liquidity is added or removed.
Graphically, this invariant appears as a straight line with a slope of -1, meaning every increase in one token's reserve requires an equal decrease in the other.

Key Property
The linear relationship creates a constant exchange rate regardless of trade size—until reserves are exhausted.
CSMM: The Promise of Zero Slippage
When a trader deposits \Delta x of token X to receive token Y, the new reserves become x' = x + \Delta x and y' = y - \Delta y. To maintain the invariant:
(x + \Delta x) + (y - \Delta y) = k
Since x + y = k, we can simplify this equation by canceling x and y from both sides:
\Delta x - \Delta y = 0 \Rightarrow \Delta y = \Delta x
Zero Price Impact
No matter the trade size, traders receive an equal amount of token Y at a fixed 1:1 exchange rate. The price offered by the pool remains constant.
Perfect Predictability
Trades can be executed at any size with perfectly predictable pricing—right up until one reserve is completely exhausted.
The invariant curve for a Constant-Sum Market Maker (CSMM), showing the linear relationship that provides zero slippage but vulnerability to arbitrage.
The Fatal Flaw: Fragile Liquidity
Vulnerability to Arbitrage
While mathematically elegant, the CSMM model has a critical weakness: its inability to react to external market price changes makes it unsustainable in practice.
Consider a scenario where tokens X and Y initially trade at 1:1 both in the pool and externally. If the external market shifts to 1.1 Y per X, arbitrageurs immediately exploit the fixed pool price.
Price Deviation
External market price shifts away from pool's fixed rate
Arbitrage Attack
Traders continuously swap at favorable fixed rate
Reserve Depletion
One token is completely drained, pool becomes useless
A pure CSMM is almost never used independently due to this massive risk of complete drainage—unless tokens maintain an unbreakable, perfectly synchronized peg with external markets.
Constant-Product Market Maker (CPMM)
The Hyperbolic Invariant
To solve the liquidity depletion problem of CSMM, Uniswap v2 pioneered a more robust model: the Constant-Product Market Maker. The CPMM maintains a fundamentally different invariant:
x \cdot y = k
Unlike CSMM's linear relationship, CPMM maintains a constant product of the two token reserves. The graph of this invariant forms a hyperbola with a crucial characteristic: it never intersects the axes.
Reserve Changes
When a trader sells \Delta x of Token X, the pool's X reserve increases. To maintain the constant product, the pool releases \Delta y of Token Y.
Price Analysis
The spot price (orange tangent) shows instantaneous rate. The effective price (dotted line) shows actual execution rate. The gap between them is price impact.
This visual property directly translates to the model's core design: reserves can never be fully exhausted, providing "infinite range" liquidity.
CPMM: The Mathematics of Infinite Liquidity
The CPMM design cleverly links trade price to the current state of reserves. When a trader adds \Delta x of token X, new reserves become x' = x + \Delta x and y' = y - \Delta y. The invariant requires:
(x + \Delta x)(y - \Delta y) = k = xy
Solving for the output amount \Delta y that the trader receives:
y - \Delta y = \frac{xy}{x + \Delta x} \Rightarrow \Delta y = y - \frac{xy}{x + \Delta x}
Finding a common denominator and simplifying:
\Delta y = \frac{y(x + \Delta x) - xy}{x + \Delta x} = \frac{xy + y\Delta x - xy}{x + \Delta x}
This yields the final output formula:
\Delta y = \frac{\Delta x \cdot y}{x + \Delta x}

The Trade-Off Revealed
This formula shows that output is always less than what the initial price y/x would suggest. As \Delta x increases, the denominator grows larger, causing \Delta y / \Delta x to decrease. This is the inherent price impact of CPMM—the cost of infinite liquidity.
Understanding Slippage and Price Impact
In CPMM, trade price is dynamic rather than constant. The marginal price (instantaneous rate) equals the slope of the hyperbola at any point: P_{X\to Y} = y/x. This ratio changes continuously with every trade.
What is Slippage?
Slippage represents the difference between the average execution price a trader receives and the marginal price at the start of their trade.
Price Impact as Safety
Larger trades create greater price impact, causing bigger deviations between effective and marginal prices. This isn't a bug—it's a crucial safety mechanism.
Arbitrage Alignment
When pool prices deviate from external markets, arbitrage trades become profitable. This continues until the pool's y/x ratio aligns with global market prices.
This built-in feedback loop ensures that CPMM pools automatically adjust their quoted prices to accurately reflect real-time market conditions over time.
CPMM Example: Quantifying Slippage
Let's examine a concrete trade on a Uniswap v2 pool. A trader wants to swap 1 million USDC for DAI in a pool with 50 million of each token.
01
Initial State
x = 50,000,000 USDC, y = 50,000,000 DAI
02
Calculate Invariant
k = x \cdot y = 2.5 \times 10^{15}
03
Post-Trade USDC Reserve
x' = 51,000,000 (trader deposits 1M)
04
Calculate New DAI Reserve
y' = k/x' \approx 49,019,607.84
05
DAI Received
\Delta y = y - y' \approx 980,392.16
06
Effective Price
980,392.16 / 1,000,000 ≈ 0.9804
Fig. 4. Visualization of the trade on the CPMM curve, showing the pool gaining 1M USDC and losing approximately 980K DAI.
1.96%
Price Impact
Deviation from ideal 1:1 rate
980K
DAI Output
Received for 1M USDC input
Compared to CSMM's zero slippage, CPMM introduces significant price impact. However, this "exponentially worse" price curve prevents pool drainage and ensures long-term sustainability.
Hybrid Design: Curve's StableSwap
Combining the Best of Both Worlds
While CPMM is robust and general-purpose, it's inefficient for assets that should trade at fixed ratios—like stablecoins. A 2% slippage on a USDC/DAI trade (which should be 1:1) represents massive inefficiency. Curve Finance's StableSwap solves this problem.
StableSwap is a Constant-Function Market Maker (CFMM) that achieves a hybrid design, combining CSMM's low slippage with CPMM's infinite liquidity. The invariant formula for two tokens:
A(x+y) + D = AD + \frac{D^3}{4xy}
Linear Component
A(x + y) from CSMM provides flat pricing near equilibrium
Product Component
D^3/4xy from CPMM ensures infinite liquidity at extremes
Amplification Coefficient
A is the key tuning parameter that controls the hybrid behavior
Here, x and y are token reserves, D represents total liquidity, and A is the critical amplification coefficient.
The Amplification Coefficient (A)
The amplification coefficient A is the key tuning lever in StableSwap. A higher A value makes the invariant curve much flatter around the equilibrium point (when x \approx y), causing behavior to closely resemble CSMM.
This results in extremely low slippage for most trades when stablecoin prices remain close to 1:1. StableSwap's innovation lies in concentrating the vast majority of liquidity around the expected peg point.
This concentration strategy dramatically boosts capital efficiency, allowing large trades with minimal price impact.
Fig. 5. Comparison of AMM invariants showing how StableSwap (blue) combines the best properties of both CSMM and CPMM.

Dynamic Safety Mechanism
When the pool becomes seriously imbalanced, the curve transitions from its flat, linear-like segment to a CPMM-style hyperbolic shape. This ensures the pool cannot be drained even during market panic, preserving safety while optimizing for the common case.
Case Study: Uniswap v2 vs. Curve
The Problem of General-Purpose Design
This case study highlights the critical differences between general-purpose and specialized AMM designs. While Uniswap v2's CPMM is a DeFi cornerstone—simple, universal, and powerful—its invariant isn't optimized for all asset types.
Volatile Asset Pairs
For pairs like ETH/BTC, CPMM's curve is ideal. It provides liquidity across the entire price range, handling large price swings efficiently without assumption of a fixed ratio.
Stablecoin Pairs
For USDC/DAI where price should be fixed at 1:1, CPMM's generality becomes a liability. The wide liquidity distribution leads to unnecessary inefficiency and high slippage on what should be simple swaps.
Purpose-built solutions like Curve's StableSwap recognize these different asset characteristics and optimize accordingly, demonstrating the evolution from general to specialized AMM design.
Quantitative Comparison: The $1M Swap
Let's re-examine the trade of swapping 1 million USDC for DAI across all three AMM models, assuming initial reserves of 50 million of each token in every pool.
1
CPMM (Uniswap v2)
Output: ~980,392 DAI
Effective Price: ~0.9804
Price Impact: ~1.96% ↓
2
CSMM (Constant-Sum)
Output: 1,000,000 DAI
Effective Price: 1.0000
Price Impact: 0%
⚠️ Unsafe in practice
3
Curve StableSwap
Output: ~999,990 DAI
Effective Price: ~0.99999
Price Impact: ~0.001% ↓
✓ Optimal for stables
The dramatic difference in slippage—from CPMM's 1.96% to StableSwap's 0.001%—demonstrates why specialized AMM designs are crucial for capital efficiency in DeFi.
Performance Comparison Table
196x
Slippage Reduction
StableSwap vs. CPMM for stablecoin swaps
99.9%
Capital Efficiency
Near-perfect execution on $1M trade
100%
Safety Retained
Infinite liquidity protection maintained
Conclusion: The Evolution of AMM Design
The progression from CSMM to CPMM to CFMM represents more than mathematical refinement—it's the maturation of AMM design philosophy from simple to complex, and from general to specialized. Each model embodies distinct trade-offs:
1
Constant-Sum (CSMM)
Pursues perfect, fixed pricing but sacrifices sustainability. Offers zero slippage at the cost of finite, fragile liquidity vulnerable to arbitrage drainage.
2
Constant-Product (CPMM)
Abandons fixed prices to provide liquidity across any price range. Uses predictable slippage as a safety mechanism, making it ideal for volatile assets.
3
Constant-Function (CFMM)
Represents specialization and optimization. Recognizes unique asset properties and concentrates liquidity strategically—near-CSMM efficiency with CPMM security.

References
  1. Hayden Adams, Noah Zinsmeister, and Dan Robinson. 2020. Uniswap v2 Core. https://uniswap.org/whitepaper.pdf
  1. Michael Egorov. 2019. StableSwap—Efficient Mechanism for Stablecoin Liquidity. https://curve.fi/files/stableswap-paper.pdf
Key Takeaways
Automated Market Makers
AMMs revolutionize trading by replacing traditional order books with deterministic mathematical formulas for pricing and facilitating trades.
Constant-Sum (CSMM)
Offers perfect, fixed pricing but lacks liquidity sustainability, making it highly vulnerable to arbitrage and drainage.
Constant-Product (CPMM)
Ensures robust, infinite liquidity across all price ranges, making it suitable for volatile assets, albeit introducing predictable slippage.
Constant-Function (CFMM)
Represents specialized design for specific asset pairs (e.g., stablecoins), optimizing liquidity concentration for enhanced efficiency.
Evolution & Trade-offs
The progression in AMM design highlights a fundamental tension between achieving price stability, ensuring liquidity, and maintaining system resilience.