Section 1 of 18
Introduction to Lending Protocols
What You Will Learn
You are going to build the Compound V2 lending protocol. Not a simplified version. The real architecture that held over $10 billion in total value locked and spawned an entire category of DeFi. Aave, Venus, Benqi, and dozens of other lending protocols forked or closely modeled their systems on Compound V2. When someone says "money market protocol," this is the design they are referencing.
By the end of this module, you will have written every core function: supplying assets, earning interest, borrowing against collateral, calculating account health, and liquidating undercollateralized positions. You will understand why each design decision was made and what breaks if you change it.
How Lending Protocols Work
A lending protocol connects two sides of a market. Suppliers deposit tokens into a pool and earn interest. Borrowers take tokens out of that pool and pay interest. The protocol sits in between, managing risk by requiring borrowers to post collateral worth more than what they borrow.
This is fundamentally different from peer-to-peer lending. There is no matching between a specific lender and a specific borrower. All suppliers deposit into a shared pool. All borrowers draw from the same pool. Interest rates adjust automatically based on how much of the pool is currently borrowed (utilization). When demand is high, rates rise. When demand is low, rates fall.
The cToken Share Model
When you supply 1,000 DAI to Compound, you do not get an IOU for 1,000 DAI. You get cDAI tokens. These cToken shares represent your proportional claim on the pool, exactly like Uniswap V2 LP tokens represent your share of a liquidity pool.
The key insight: your cToken balance never changes, but each cToken becomes worth more underlying over time. If the exchange rate is 0.02 (meaning 1 cToken = 0.02 DAI) when you deposit, you receive 50,000 cDAI. A year later, the exchange rate might be 0.021. Your 50,000 cDAI are now worth 1,050 DAI. You earned 50 DAI in interest without a single transaction touching your balance.
The exchange rate formula is:
exchangeRate = (totalCash + totalBorrows - totalReserves) / totalSupply
This is the lending equivalent of Uniswap's LP token value formula. totalCash is the underlying tokens sitting in the contract. totalBorrows is what has been lent out (the protocol is owed this). totalReserves is the protocol's revenue cut. As borrowers pay interest, totalBorrows grows, and the exchange rate increases.
The Comptroller: Risk Engine
Compound separates risk management from the token logic. The Comptroller is a standalone contract that acts as the policy engine. Before any risky operation (minting, redeeming, borrowing), the CToken asks the Comptroller for permission.
The Comptroller tracks which markets exist, what collateral factors each market has (how much you can borrow against it), and whether a given user's account is healthy. A collateral factor of 0.75 means you can borrow up to 75% of your collateral's value. If ETH is worth $2,000 and has a 0.75 collateral factor, depositing 1 ETH lets you borrow up to $1,500 worth of other assets.
This is similar to how Uniswap's Factory manages pair creation, but the Comptroller does much more. It enforces economic invariants across the entire system.
Interest Rate Mechanics
Compound uses a Jump Rate Model for interest rates. The model defines a "kink" in the rate curve, typically at 80% utilization. Below the kink, the borrow rate increases linearly but gently. Above the kink, the rate spikes sharply. This creates a strong incentive for the market to stay below the kink: borrowing becomes very expensive above 80% utilization, which encourages repayments and new deposits.
The supply rate is derived from the borrow rate:
supplyRate = borrowRate * utilizationRate * (1 - reserveFactor)
Suppliers earn less than borrowers pay for two reasons. First, not all capital is borrowed (utilization is typically below 100%). Second, the protocol takes a cut (the reserve factor, usually around 10%).
Interest accrues lazily. The protocol does not update every borrower's balance every block. Instead, it maintains a global borrowIndex that grows each time accrueInterest() is called. Each borrower stores a snapshot of the borrowIndex from their last interaction. Their current balance is computed on demand:
currentBorrow = snapshot.principal * currentBorrowIndex / snapshot.interestIndex
This is the same pattern as Uniswap V2's cumulative price oracle: store a running accumulator, compute differences on demand.
Liquidation: The Safety Net
When a borrower's collateral value drops below their borrow value (adjusted by collateral factors), anyone can liquidate them. A liquidator repays a portion of the borrower's debt and receives their collateral at a discount (typically 8%). This discount incentivizes liquidators to keep the system healthy.
Liquidation is the mechanism that prevents bad debt. Without it, a borrower could deposit collateral, borrow against it, watch the collateral lose value, and walk away. The protocol (and its suppliers) would be left holding worthless collateral and unreturned borrows.
The Architecture You Will Build
The module builds the protocol in layers, each section adding one piece:
Part 1 (Core Lending): Fixed-point math, interest rate model, CToken state, exchange rate and interest accrual, supply (mint), withdraw (redeem).
Part 2 (Risk Engine): Price oracle, Comptroller market management, liquidity calculations, policy hooks, borrow, repay.
Part 3 (Liquidation and Integration): Liquidation logic (Comptroller side and CToken side), CErc20 token integration, complete protocol assembly.
Each section builds on the previous one. By the end, every contract connects into a working lending protocol.
Knowledge Check
Answer correctly to earn lynx
What is the core mechanism that allows cToken holders to earn interest without their balance changing?
Two words. Think about what grows over time.