Decentralized Finance (DeFi)
Chapters 1 through 3 gave you the real tools smart contracts are built from. This chapter applies all of it to DeFi — "decentralized finance": real trading, lending, and borrowing services built entirely from smart contracts, with no bank, exchange, or broker sitting in the middle.
Automated Market Makers: Trading Without an Order Book
A traditional exchange matches buyers and sellers through an order book. DeFi's dominant real alternative is the Automated Market Maker (AMM), pioneered by Uniswap, created by Hayden Adams and launched on 2 November 2018, directly inspired by an earlier blog post from Ethereum co-founder Vitalik Buterin (Course 1, Chapter 6).
Instead of matching individual buy and sell orders, an AMM holds a shared pool of two tokens and
prices trades using a real, deterministic formula: the constant product formula,
x × y = k, where x and y are the pool's current
quantities of each token, and k must stay exactly constant across every trade.
Notice what just happened to the real price: before the trade, 1 ETH was worth 2,000 USDC (200,000 ÷ 100). After it, the pool implies roughly 1,653 USDC per ETH (181,818 ÷ 110) — the trade itself moved the price, purely as a mechanical consequence of the formula, with no separate "order book" or human market maker involved at all.
Liquidity Pools and Liquidity Providers
The tokens sitting in that pool don't come from Uniswap itself — they're deposited by ordinary users called liquidity providers (LPs), who contribute a matched pair of tokens to the pool and, in exchange, earn a real share of the trading fees every subsequent swap generates.
Lending and Borrowing: Real Protocols, No Credit Check
Real DeFi lending protocols — Aave and Compound are two of the most established — let users deposit crypto assets to earn interest, and let other users borrow against posted collateral, with interest rates that adjust automatically based on real-time supply and demand for each asset.
There's a genuine structural reason DeFi lending looks different from a bank loan: over-collateralization. Since there's no credit check, no identity verification, and no legal recourse against a pseudonymous blockchain address, these protocols require borrowers to post more collateral value than they're actually allowed to borrow — the collateral itself is the entire basis of trust, replacing everything a credit score or a bank's own underwriting process would normally provide.
Flash Loans: A Genuinely Novel DeFi Primitive
DeFi introduced a real financial instrument with no direct traditional-finance equivalent: the flash loan — an uncollateralized loan of any size, borrowed and fully repaid within a single blockchain transaction, or the entire transaction reverts as though it never happened.
This is only possible because of a real property Chapter 2 already established: a single
Ethereum transaction is atomic — if execution fails to complete for any
reason, every state change it attempted gets reverted, exactly like Chapter 2's own out-of-gas
behavior, just triggered here by a deliberate require check instead of running out
of gas. Because the loan and its repayment are forced into that same atomic unit, the lender
needs zero trust in the borrower at all: either the money genuinely comes back by the end of the
transaction, or, from the chain's own perspective, the loan simply never happened.
| DeFi Primitive | Real Purpose | Real Risk |
|---|---|---|
| AMM / liquidity pool | Token trading without an order book | Impermanent loss for liquidity providers |
| Over-collateralized lending | Borrowing without a credit check | Automatic liquidation if collateral value falls |
| Flash loan | Trust-free, uncollateralized borrowing within one transaction | A real, documented tool in several price-manipulation exploits |
Hands-On Exercises
Three exercises applying AMM mechanics, over-collateralization, and flash loan atomicity before Chapter 5 turns to the broader dApp and Web3 stack these protocols live inside.
Quick Reference
- AMM — prices trades via the constant product formula (
x × y = k) instead of an order book; pioneered by Uniswap (Hayden Adams, 2 November 2018). - Liquidity pool / LP — users deposit paired tokens, earn trading fees, but risk impermanent loss if the price ratio shifts.
- Over-collateralization — DeFi lending requires more collateral than borrowed, replacing a credit check.
- Liquidation — automatic, code-enforced collateral sale if its value falls too close to the borrowed amount.
- Flash loan — an uncollateralized loan that must be repaid within the same atomic transaction, or the whole thing reverts.