Chapter 36: Exercises
DeFi Integration and Liquidity Mining
Exercise 1: Composability Basics
Explain the three levels of DeFi composability (morphological, atomic, syntactic) and give one example of each in the context of prediction markets.
Exercise 2: Outcome Token Flow
Diagram the flow of outcome tokens through a three-protocol DeFi stack: prediction market -> DEX -> lending protocol. Label each step with the token transformations that occur.
Exercise 3: LP Token Value Calculation
A prediction market AMM pool contains 50,000 YES tokens and 50,000 NO tokens. The total LP token supply is 10,000. If you hold 500 LP tokens, calculate: (a) Your pool share percentage (b) Your claim on YES and NO tokens (c) The implied YES price (d) The USDC value of your position
Exercise 4: Fee APR Computation
A prediction market pool has $2M TVL, $500K daily trading volume, and a 0.5% fee rate. Calculate: (a) Daily fee revenue for the pool (b) Annualized fee APR (c) Your daily fee income if you hold a 3% pool share (d) How this compares to holding the tokens without providing liquidity
Exercise 5: Impermanent Loss — Basic
An LP enters a YES/NO constant product pool when YES is priced at 0.50. Calculate the impermanent loss if: (a) YES moves to 0.60 (b) YES moves to 0.80 (c) YES moves to 0.95 (d) YES moves back to 0.50
Exercise 6: Impermanent Loss — Advanced
Derive the impermanent loss formula for a prediction market constant product AMM where the LP enters at price $p_0$ and the price moves to $p_1$. Show all steps.
Exercise 7: IL vs. Fee Income
An LP deposits $10,000 into a prediction market pool at YES = 0.50. The fee rate is 1%, daily volume is $100K, TVL is $500K, and the LP holds for 60 days. During this period, YES moves to 0.75. Calculate: (a) Total fee income earned (b) Impermanent loss (c) Net profit/loss (d) How many more days would the LP need to hold (at the same volume) to break even?
Exercise 8: Yield Source Comparison
Compare the following three yield sources for a $50,000 prediction market LP position: - Source A: 15% APR from trading fees, risk score 3 - Source B: 40% APR from liquidity mining (volatile token), risk score 7 - Source C: 8% APR from lending outcome tokens, risk score 2 Calculate the risk-adjusted yield for each and recommend an allocation strategy.
Exercise 9: Stacked Yield Strategy
Design a four-layer stacked yield strategy starting from 100,000 USDC. At each layer, specify: (a) The action taken (b) The yield earned (c) The new risks introduced (d) The total cumulative APR and risk score
Exercise 10: Yield Optimization
Write Python code that takes a list of prediction market pools (each with TVL, daily volume, fee rate, mining APR, and risk score) and outputs the optimal allocation of $100,000 across them to maximize risk-adjusted yield, with a maximum of 30% in any single pool.
Exercise 11: Collateral Factor Design
You are designing a lending protocol that accepts prediction market outcome tokens as collateral. For a YES token currently trading at 0.65: (a) What collateral factor would you set and why? (b) What liquidation threshold would you use? (c) What happens if the market resolves to NO while a loan is outstanding? (d) How would you handle a market that resolves to YES?
Exercise 12: Structured Product Design
Design a "prediction market range token" that pays 1 USDC if a prediction market's YES price stays between 0.40 and 0.60 for the next 30 days, and 0 otherwise. Explain: (a) How to construct this product using existing DeFi primitives (b) How to price it (c) Who would buy it and why
Exercise 13: Options on Outcomes
A YES token is currently trading at 0.55. A call option with strike 0.70 expiring in 30 days is available. Explain: (a) Why Black-Scholes is inappropriate for pricing this option (b) What distribution the underlying follows (c) Sketch the payoff diagram (d) How would you approximate the fair price?
Exercise 14: Flash Loan Arbitrage
YES tokens trade at 0.72 on Platform A and 0.75 on Platform B. There is sufficient liquidity for 100,000 tokens on each platform. Flash loan fee is 0.09%, gas cost is $15. Calculate: (a) The gross arbitrage profit (b) The flash loan cost (c) The net profit (d) The minimum price difference required for the arbitrage to be profitable
Exercise 15: Completeness Arbitrage
On a prediction market, YES trades at 0.52 and NO trades at 0.46. The redemption value of a YES + NO pair is 1.00 USDC. Minting a pair costs 1.00 USDC. (a) Is there an arbitrage opportunity? If so, describe it. (b) Calculate the profit per pair. (c) How much would you need to borrow via flash loan to earn $500 profit (ignoring gas)? (d) What risks exist even in this "risk-free" trade?
Exercise 16: Flash Loan Attack Analysis
Describe step-by-step how an attacker could use a flash loan to manipulate a prediction market that uses a single DEX as its price oracle. What defenses would prevent this attack?
Exercise 17: Sandwich Attack Calculation
A trader submits a transaction to buy 50,000 YES tokens from a pool with 500,000 YES and 500,000 NO tokens (constant product AMM, 0.3% fee). An MEV searcher front-runs with a purchase of 20,000 YES tokens. (a) Calculate the price the victim pays with and without the sandwich. (b) Calculate the searcher's profit from the back-run. (c) Calculate the victim's overpayment. (d) What slippage tolerance would have prevented this attack?
Exercise 18: MEV Protection
Compare and contrast four MEV protection strategies for prediction market traders: (a) Private mempools (Flashbots Protect) (b) Limit orders (c) Batch auctions (d) Encrypted mempools For each, describe the mechanism, effectiveness, trade-offs, and whether it applies to prediction markets specifically.
Exercise 19: Cross-Protocol Strategy Design
Design a cross-protocol strategy that combines: - A prediction market on "Will Protocol X launch token by Q2 2026?" - An Aave lending position - A Uniswap LP position Detail each step, expected yields, risks, and the conditions under which the strategy would lose money.
Exercise 20: Cross-Chain Arbitrage
The same prediction market event is priced differently on Ethereum (YES = 0.60) and Polygon (YES = 0.55). Describe: (a) The arbitrage opportunity (b) The execution steps including bridging (c) The risks unique to cross-chain arbitrage (d) How long the price discrepancy might persist and why
Exercise 21: Cascading Risk Calculation
A strategy involves 4 protocols with individual annual failure probabilities of 1%, 2%, 3%, and 1.5%. (a) Calculate the probability that at least one protocol fails in a year. (b) If a failure causes 80% loss of capital on average, what is the expected annual loss? (c) What minimum yield is required to justify this risk? (d) Would you recommend this strategy for a $1M portfolio? Why or why not?
Exercise 22: Risk Scoring
Apply the risk scoring framework from Section 36.9 to the following protocol: - 2 audits, $50M TVL, 18 months old, Chainlink oracle, token governance with 48-hour timelock, bug bounty active Calculate the overall risk score and estimated annual failure probability.
Exercise 23: Python Risk Assessor
Write a Python function that takes a list of protocol risk profiles and a proposed capital allocation, then outputs: (a) The overall portfolio risk score (b) The maximum drawdown probability (c) A recommendation to accept or reject the strategy
Exercise 24: Governance Token Valuation
A prediction market protocol generates $5M in annual fee revenue and distributes 40% to governance token stakers. The total staked token value is $25M. Calculate: (a) The staking yield (b) If the token is also used for liquidity mining at 20% APR, what is the total implied yield? (c) Is this yield sustainable? What is the "real yield" vs. inflationary yield?
Exercise 25: Vote Escrow Economics
A protocol uses the ve-token model where tokens locked for 4 years receive 1x voting power and proportional fee shares, while tokens locked for 1 year receive 0.25x. If you have 10,000 tokens and annual fee revenue per veToken is $0.50: (a) Calculate your annual income with a 4-year lock vs. 1-year lock. (b) What is the opportunity cost of locking for 4 years? (c) At what token price decline rate does the 1-year lock become preferable?
Exercise 26: Integration Pattern Implementation
Write pseudocode for a "Strategy Vault" smart contract that: (a) Accepts USDC deposits from users (b) Mints prediction market outcome tokens (c) Provides liquidity to an AMM (d) Harvests fees periodically (e) Allows users to withdraw their proportional share
Exercise 27: Full IL Simulation
Write Python code that simulates the impermanent loss for a prediction market LP over the full lifecycle of a market (from creation at p=0.50 to resolution at p=1.0), sampling price paths that gradually trend toward resolution. Plot: (a) IL over time for 10 sample paths (b) The distribution of final IL across 1,000 simulations (c) The breakeven fee rate required to offset IL
Exercise 28: MEV Quantification
Using the Python MEV analysis tools from Section 36.7, write code that: (a) Generates a realistic sequence of 1,000 prediction market transactions (b) Detects sandwich attacks in the sequence (c) Calculates total MEV extracted (d) Compares MEV to total trading volume as a percentage
Exercise 29: Comprehensive Strategy Backtest
Build a Python backtesting framework that simulates a prediction market LP strategy over 90 days with: (a) Daily price movements following a random walk with drift toward resolution (b) Trading volume that increases as resolution approaches (c) Fee income accumulation (d) Impermanent loss tracking (e) Optional liquidity mining rewards Output the final P&L breakdown and Sharpe ratio.
Exercise 30: DeFi Integration Capstone
Design a complete DeFi integration strategy for a $500,000 portfolio that: (a) Allocates across 5 prediction market pools (b) Uses lending for capital efficiency (c) Implements IL hedging (d) Incorporates governance token staking (e) Includes risk limits and rebalancing rules Present your strategy as a one-page specification with all parameters, expected returns, risk metrics, and exit conditions.