Section 6 of 18
CToken: Supply (mint)
Key takeaway: Compound V2's
mint()function deposits underlying tokens and issues cTokens at the current exchange rate, computed as(cash + totalBorrows - totalReserves) / totalSupply. The first depositor sets the rate, which is why Compound V2 forks with no minimum-supply defense have been drained by donation attacks (Hundred Finance lost approximately $7M to this exact pattern).mint()also callsaccrueInterestfirst, ensuring all subsequent accounting uses the up-to-date borrowIndex rather than stale state.
What You Are Building
The mint function is how assets enter the lending protocol. When a user supplies underlying tokens (like DAI), they receive cTokens (like cDAI) in return. These cTokens represent their share of the pool and grow in value over time as borrowers pay interest.
This is the first user-facing operation you build. Everything from the previous sections (fixed-point math, interest rates, state variables, exchange rates, and interest accrual) converges here. The mint flow exercises the Comptroller hook, the exchange rate calculation, and the state update pattern that every subsequent operation will follow.
Your Code
Requirements
Sign up free — keep reading + earn 15 Lynx
Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.
Sign up free to continueAlready have an account? Log in