DeFi¶
DeFi (decentralized finance) is what happens when the contract-calling and event-reading patterns from Building Web3 Applications get applied to actual financial primitives: trading, lending, and borrowing, implemented as smart contracts rather than intermediary-operated services. This section covers the mechanics directly, with real, verified math throughout, not conceptual summaries of what these protocols claim to do.
What you need to know first¶
Smart Contracts, Tokens, and Reading Blockchain State. This section assumes you're comfortable with ERC-20 balances, contract calls, and the general account and gas model covered in earlier sections.
Chapters¶
- Stablecoins: why blockchain rails need price stability, and the two structurally different ways to get it
- Collateralized Stablecoins: DAI/USDS, over-collateralized vaults, and the stability mechanisms that hold the peg
- Centralized Stablecoins: USDC/USDT, and the custodial trust question this book keeps returning to
- Decentralized Exchanges: why AMMs beat order books on-chain
- Automated Market Makers: pricing trades from reserves, not from orders
- Constant Product Formula:
x*y=k, worked through with real computed slippage numbers - Liquidity Pools: what a pool contract actually tracks, and how LP tokens work
- Liquidity Providers: the two components of LP return, and why they must be evaluated separately
- Impermanent Loss: derived directly from the formula, verified across eight price scenarios
- Slippage: pool depth, price impact, and the arbitrage that keeps AMM prices honest
- Lending: the pooled model, and interest rates set by utilization
- Borrowing: opening a position, continuous interest accrual, and why there's no fixed term
- Collateral: loan-to-value ratios, liquidation thresholds, and why the two differ
- Liquidations: a complete, verified example, including the liquidator's bonus math
- Flash Loans: uncollateralized borrowing made safe by atomicity alone
- Oracles: why contracts can't query external data directly, verified with a live Chainlink read
- Yield: fees, interest, staking, and emissions, and why they shouldn't be compared as one number
- Staking vs. Lending: separating consensus-layer staking from DeFi products that borrow the word
- Uniswap: v1 through v3, as a case study in how the AMM model actually evolved
- Aave: from peer-to-peer ETHLend to the pooled lending model most protocols converged on
- MakerDAO / Sky: Black Thursday, and a documented liquidation-mechanism failure under stress
- Curve: the StableSwap curve, and a real compiler-level exploit unrelated to its pricing math
Build one¶
examples/defi/ is a runnable project implementing a constant-product AMM pool and a simplified lending market directly from this section's formulas, exercised against real numbers rather than mocked data.
Next¶
Continue to Layer 2, where the scaling techniques Bitcoin's Bitcoin Scaling section covered get Ethereum's own, much larger-scale treatment: rollups, data availability, and the bridges connecting them back to Ethereum mainnet.