Section 4 of 18
CToken: Storage and State
Key takeaway: A Compound V2 cToken contract holds the storage every state-changing operation depends on:
accrualBlockNumber(when interest was last accrued),totalSupply(cToken supply),totalBorrowsandtotalReserves(in the underlying token), andborrowIndex(the global per-market interest accumulator). Per-borrower state (debt plus index snapshot) lives in a separate mapping. Misreads here propagate through every function in the protocol; this is why audit firms re-derive the storage layout independently of the documentation.
What You Are Building
The CTokenStorage contract defines every state variable the CToken needs. It is a pure storage layout with no logic. Separating storage from logic is a deliberate architectural choice: it makes the inheritance hierarchy clear, prevents storage slot collisions when adding features, and lets you audit the complete state of a CToken market by reading a single contract.
This section also defines the interfaces for the Comptroller and InterestRateModel, so the CToken can reference them without circular dependencies. You built the InterestRateModel in section 3. The Comptroller comes later in sections 9-11. For now, you only need the interface.
Your Code
Requirements
Sign up free — keep reading + earn 10 Lynx
Zealynx Academy is free. Track your progress, earn Lynx, and climb the leaderboard.
Sign up free to continueAlready have an account? Log in