Exercises: Decentralized Exchanges and Automated Market Makers
Conceptual Questions
Exercise 22.1 — Why Not an On-Chain Order Book?
Explain three specific reasons why running a traditional limit-order-book exchange on Ethereum L1 is impractical. For each reason, identify whether the constraint is fundamental to blockchain architecture or specific to Ethereum's current design, and explain whether Layer 2 solutions or alternative L1 chains (like Solana) address the constraint.
Exercise 22.2 — AMM Invariant Properties
The constant product formula x * y = k satisfies three properties: (1) price reflects supply and demand, (2) the pool never runs out of either token, (3) the function is simple and deterministic. For each of the following alternative invariants, determine which of the three properties it satisfies and which it violates: - (a) x + y = k (constant sum) - (b) x^2 + y^2 = k (constant sum of squares) - (c) x^3 * y = k (asymmetric constant product) - (d) min(x, y) = k
Exercise 22.3 — The Role of Arbitrageurs
Explain why an AMM cannot function correctly without external arbitrageurs. Consider a scenario where no arbitrageurs exist and a major CEX lists ETH at $3,000 while the AMM pool prices ETH at $2,000. What would happen? Who is harmed? Why is the arbitrageur performing a service even though they are extracting profit?
Exercise 22.4 — LP Tokens as Claims
A Uniswap V2 ETH/USDC pool has: - 500 ETH and 1,000,000 USDC in reserves - 10,000 LP tokens outstanding
Alice holds 1,000 LP tokens. Bob deposits 50 ETH and 100,000 USDC.
(a) How many LP tokens does Bob receive? (b) After Bob's deposit, what is Alice's percentage ownership of the pool? (c) If 200 ETH worth of trading fees accumulate (with corresponding USDC fees maintaining the price ratio), what are Alice's and Bob's positions worth when they withdraw?
Exercise 22.5 — Concentrated Liquidity Trade-offs
A liquidity provider has $100,000 and wants to provide ETH/USDC liquidity on Uniswap V3. ETH is currently trading at $2,000.
Compare the following three strategies: - (a) Full range (0 to infinity, equivalent to V2) - (b) Range: $1,500 to $2,500 - (c) Range: $1,950 to $2,050
For each strategy, explain: the approximate capital efficiency relative to V2, the risk if ETH drops to $1,000, and the ideal market conditions for the strategy.
Calculation Problems
Exercise 22.6 — Constant Product Swap Calculation
A pool contains 2,000 ETH and 6,000,000 USDC (k = 12,000,000,000).
(a) What is the current marginal price of ETH? (b) A trader wants to buy ETH with 300,000 USDC (ignore fees). How much ETH do they receive? (c) What is the average execution price of the trade? (d) What is the price impact (percentage difference between marginal price before the trade and average execution price)? (e) After the trade, what is the new marginal price?
Exercise 22.7 — Fee Calculation
Repeat Exercise 22.6 with a 0.3% fee applied to the input amount.
(a) Calculate the effective input amount after fees. (b) Calculate the ETH received. (c) Compare to the no-fee case and calculate the dollar value of fees paid. (d) If this pool processes $10 million in daily volume with a 0.3% fee, what is the annualized fee income for LPs as a percentage of the pool's TVL?
Exercise 22.8 — Impermanent Loss Calculation
Carlos deposits into an ETH/DAI pool: - 10 ETH at $2,500/ETH = $25,000 - 25,000 DAI = $25,000 - Total deposit: $50,000
(a) What is the constant product k? (b) If ETH rises to $3,600, calculate the new pool reserves after arbitrage. (c) Calculate the pool value and the HODL value. (d) Calculate the impermanent loss in dollars and as a percentage. (e) Verify your answer using the IL formula: IL = 2*sqrt(r)/(1+r) - 1. (f) How much in trading fees would Carlos need to have earned to break even?
Exercise 22.9 — Impermanent Loss in a Downturn
Using the same initial conditions as Exercise 22.8, calculate the impermanent loss if ETH drops from $2,500 to $1,000.
(a) Find the new reserves. (b) Calculate pool value vs. HODL value. (c) What is the price ratio r? (Hint: r < 1 for a price decrease.) (d) Compare the IL for this 60% price drop to the IL for a 60% price increase (ETH goes from $2,500 to $4,000). Are they the same? Explain why or why not.
Exercise 22.10 — Sandwich Attack Profitability
A pool has 5,000 ETH and 10,000,000 USDC.
A victim submits a swap of 200,000 USDC for ETH with 0.5% slippage tolerance.
An attacker front-runs with a 500,000 USDC purchase.
(a) Calculate the ETH the attacker receives. (b) Calculate the ETH the victim receives (after the attacker's front-run). (c) Compare the victim's ETH to what they would have received without the attack. (d) Calculate the attacker's back-run: how much USDC do they receive for selling the ETH from step (a)? (e) Calculate the attacker's gross profit. (f) If gas + relay costs are $150, is the attack profitable? What is the net profit? (g) Would the victim's transaction revert given their 0.5% slippage tolerance?
Applied and Design Questions
Exercise 22.11 — Designing an AMM for Stablecoins
Explain why the constant product formula x * y = k is suboptimal for a USDC/USDT pool where both tokens should trade very close to $1. Describe the properties an ideal stablecoin AMM invariant should have and explain how Curve's StableSwap formula achieves these properties. What happens to a Curve pool when one stablecoin depegs significantly?
Exercise 22.12 — DEX Aggregator Routing
You need to swap 500,000 USDC for ETH. Three pools are available:
| Pool | ETH Reserve | USDC Reserve | Fee |
|---|---|---|---|
| Uniswap V3 (0.05%) | 10,000 | 20,000,000 | 0.05% |
| Uniswap V3 (0.3%) | 3,000 | 6,000,000 | 0.30% |
| SushiSwap | 2,000 | 4,000,000 | 0.30% |
(a) Calculate the ETH received from each pool if you route the entire 500,000 USDC to it. (b) Would splitting the trade (e.g., 60% to Pool 1, 40% to Pool 2) yield more ETH than routing 100% to Pool 1? Calculate and compare. (c) Explain why the optimal split is not simply "send everything to the cheapest pool."
Exercise 22.13 — MEV Mitigation Analysis
Evaluate the following MEV mitigation strategies. For each, explain the mechanism, identify the trade-off, and assess effectiveness:
(a) Setting slippage tolerance to 0.1% (b) Using Flashbots Protect for private transaction submission (c) Using CoW Protocol's batch auction system (d) Trading on an L2 with a centralized sequencer (e) Using a limit order instead of a market order
Exercise 22.14 — The V3 LP Decision
You want to provide $50,000 in ETH/USDC liquidity on Uniswap V3. ETH is at $2,000. You expect ETH to trade between $1,700 and $2,300 over the next month. The pool earns approximately 20% APR in fees for a full-range V2-equivalent position.
(a) If you set your range to $1,700-$2,300, approximately how much more fee income do you earn compared to a full-range position? (Estimate the capital efficiency multiplier.) (b) If ETH instead moves to $2,500, describe what happens to your position. (c) Calculate the impermanent loss if ETH ends at $2,300 (the upper bound of your range). (d) Considering fees, IL, and rebalancing gas costs, under what conditions is the concentrated position more profitable than simply holding ETH and USDC?
Exercise 22.15 — Building a Better AMM
You are designing a new AMM. Describe a pricing function that addresses at least two of the following weaknesses of the constant product formula: - High impermanent loss for volatile pairs - Capital inefficiency (liquidity spread across irrelevant price ranges) - Vulnerability to sandwich attacks - No support for limit orders
For your proposed design, explain the invariant function, how swaps work, and what trade-offs your design introduces compared to x * y = k.