Compound V3 Book by RareSkills

Compound Finance is the most forked DeFi lending protocol.

By learning the Compound Finance codebase and tokenomics in depth, you will be able to understand dozens of other lending protocols and be able to navigate their codebases more efficiently.

The RareSkills Compound V3 book is a line-by-line examination of the Compound V3 protocol. We explain the tokenomics, architecture, and the code in great detail — down to the number of decimals certain variables have.

Our hope is for you to gain an understanding of the protocol with a depth comparable to if you had written it yourself.

Even if you develop on non-EVM blockchains or layer 2s, what you learn here will still be relevant.

How to read this book

Imagine a protocol engineer sat down with you and walked you through the codebase and design decisions. That is what this book is like.

Pace yourself.

We do not shy away from difficult subjects in our code walkthroughs, so don’t try to cram the information. This resource makes this process as simple as possible, but no matter how you slice it, this is still a big undertaking. Have realistic expectations for how many new ideas you can learn in one session. Compound was groundbreaking when it launched, so mastering the whole thing in 3 days isn’t realistic at all.

On the other hand, if you understand the building blocks of Compound V3, the codebase is not as complex as its line count would suggest. We do not simply provide screenshots of each line and explain the code — we provide a mental framework about how all the high level ideas fit together.

Make sure you are familiar with each of the prerequisites, then read the chapters of the Compound V3 book in order.

About Compound V3
Compound only lends out one kind of asset, which it calls the “base” asset

Unlike Compound V2 (and AAVE) where the borrower can borrow from a menu of assets, in Compound V3, borrowers can only borrow a single asset (and lenders can only lend a single asset) — per “market.” At this time of writing, there is a market for borrowing (and lending) USDC and a market for borrowing and lending ETH.

The available markets are shown on the “Compound V3 markets” page which we provide a screenshot of below. As you can see, it is only possible to borrow USDC or ETH depending on the chain.

Because USDC is the most common asset to borrow and lend, we will refer to the base asset as USDC throughout this series. Collateral does not earn interest in Compound V3.

Comet Nomenclature

Compound V3 refers to itself by the codeword “Comet.” Sometimes, the word “Comet” may refer to the “core lending smart contract” and “Compound V3” referring to the entire system of smart contracts. Other times, “Comet” and “Compound V3” are used interchangeably, but don’t worry, it will be very clear from the context.

Prerequisites
You need to have the following background knowledge to understand Compound V3. Feel free to skip sections you are already familiar with.
Chainlink Price Feeds
Compound V3 uses Chainlink to obtain the price of assets. We assume the reader knows how to integrate with these smart contracts
Read  
Transparent Upgradeable Proxies
Compound V3 uses this proxy pattern to facilitate upgrades via governance. The reader should be familiar with proxies or it won’t be clear how Compound V3’s “immutable” parameters change.
Read  
Fallback-Extension Pattern
The fallback extension pattern is a Solidity design pattern to get around the 24kb smart contract limit minimal complexity. Compound V3 uses this design pattern in the main lending contract.
Read  
DeFi On-Chain Governance
It is common for a privileged actor to configure DeFi applications or change their parameters — but instead of being a multisignature wallet, it is a governance smart contract. This contract initiates transactions on the basis of a programmable democracy. We will refer to “governance” throughout this series — read this article first if you aren’t already familiar.
Read  
MasterChef Staking Algorithm
We assume the reader is familiar with “accumulator accounting” which MasterChef inspired. Compound V3 uses something similar to track interest rate accrual.
Read  
How DeFi Interest Rates Work
Compound inspired the near-universal solution for setting interest rates in DeFi Lending smart contracts, so you should be familiar with how the mechanism works in abstract before looking at code.
Read  
DeFi Liquidations and Collateral
On-chain lending requires collateral to ensure the buyer will pay back the loan. Read this article to familiarize yourself with the common mechanisms for DeFi collateral.
Read  
Aknowledgements

We would like to thank Faybian Byrd, Andrew Chiaramonte, and Walter Cavinaw for their invaluable comments during review of this work.