Deploying a decentralized lending protocol like Compound involves a series of well-orchestrated steps to set up various smart contract modules. This guide walks through the entire process, from deploying the core COMP token to configuring interest rate models and testing key functionalities. By following this tutorial, you'll gain a deeper understanding of how decentralized finance (DeFi) lending protocols operate under the hood.
Prerequisites and Initial Setup
Before diving into the deployment, ensure you have the necessary tools and environments ready:
- Operating System: macOS or any Unix-based system (Linux/Windows WSL also work)
- Blockchain Network: BSC Testnet (or any Ethereum-compatible testnet)
- Development Tools: Remix IDE and MetaMask (configured for the testnet)
- Contract Source: Compound Protocol GitHub Repository
When compiling and deploying via Remix, enable optimization to reduce gas costs and improve efficiency. The Compound protocol comprises over 30 contracts, which might seem daunting initially. However, breaking down the process into modular steps simplifies the workflow. Understanding the contract architecture is crucial—refer to the widely-shared Compound contract structure diagram to visualize interactions between modules.
COMP Token Module Deployment
The COMP token is the protocol's native incentive asset, distributed to users for supplying or borrowing assets. While you can use a standard ERC-20 contract, Compound's native Comp.sol is recommended for compatibility.
- Deploy
Comp.solwith customized parameters (name, symbol, total supply). - Record the deployed address (e.g.,
0x1fe7FF222D59B6648D640090046661313A1CF0a2). - Update the COMP address in
ComptrollerG7.sol(or your chosen Comptroller contract).
This ensures the Comptroller can correctly distribute incentives.
Comptroller Module Setup
The Comptroller manages risk parameters, liquidity calculations, and market operations. It uses a proxy pattern: Unitroller.sol serves as the proxy, while ComptrollerG7.sol holds the logic.
- Deploy
Unitroller.sol(proxy contract). Note its address. - Deploy
ComptrollerG7.sol(logic contract). Record its address. Bind the proxy to the logic:
- Call
_setPendingImplementationonUnitroller.sol, passing theComptrollerG7.soladdress. - Call
_becomeonComptrollerG7.sol, passing theUnitroller.soladdress.
- Call
After binding, always use the Unitroller address when interacting with the Comptroller.
Price Oracle Configuration
Accurate price feeds are critical for calculating collateral values and loan-to-value ratios. Use SimplePriceOracle.sol for testing:
- Deploy
SimplePriceOracle.soland record its address. - Later, set underlying asset prices (e.g., 1 USD for stablecoins, 2000 USD for ETH).
Interest Rate Model Deployment
Compound uses a jump-rate model for dynamic interest rates. Deploy JumpRateModelV2.sol with parameters:
baseRatePerYear: 0 (minimum rate)multiplierPerYear: 7% (0.07e18)jumpMultiplierPerYear: 3e18 (300%)kink_: 75% (0.75e18, the utilization threshold)
Deploy two instances—one for ERC-20 markets and another for the native ETH market.
CToken Deployment for ERC-20 Assets
CTokens represent deposited assets and accrue interest. For ERC-20 assets:
- Deploy a mock ERC-20 token (e.g., USDT) as the underlying asset.
- Deploy
CErc20Delegate.sol(implementation contract for proxy-based CTokens). Deploy
CErc20Delegator.sol(the actual CToken), specifying:- Underlying ERC-20 address
- Comptroller address
- Interest rate model address
- Initial exchange rate (1e18 for 1:1)
- Name, symbol, decimals
- Admin address (use deployer for testing)
- Implementation contract address
CToken Deployment for Native ETH
For the native currency (ETH), deploy CEther.sol (non-proxy) with similar parameters: Comptroller address, interest rate model, exchange rate, and metadata.
Post-Deployment Configuration
After deploying CTokens, configure the system:
- Set Prices: Use
SimplePriceOracle.setUnderlyingPriceto set prices for each CToken. - Set Reserve Factors: Adjust reserve factors (e.g., 10% for ERC-20, 20% for ETH) via
_setReserveFactor. - Support Markets: Call
_supportMarketin the Comptroller for each CToken. - Set Collateral Factors: Define collateral factors (e.g., 60%) via
_setCollateralFactor.
COMP Reward Distribution
COMP rewards are distributed to suppliers and borrowers based on compSpeed (COMP per block). Use _setCompSpeed to configure rewards for each market. Note:
compSpeedis per market, per block.- The actual distribution doubles this value (suppliers + borrowers).
- By default, it's 0—enable it explicitly.
Users can claim accrued COMP via claimComp methods in the Comptroller.
Testing Core Functionalities
Thoroughly test these key operations:
- Minting: Users supply assets and receive CTokens.
- Borrowing: Users borrow against collateral, subject to liquidity checks.
- Repaying: Users repay borrows, reducing debt.
- Redeeming: Users redeem CTokens for underlying assets.
- Liquidation: Liquidators repay undercollateralized loans seizing collateral.
Test with sample values and verify contract states (e.g., balances, borrow rates).
Frontend Integration
For dApp integration, interact with CToken contracts:
mint(): Supply assetsredeem()/redeemUnderlying(): Withdraw assetsborrow(): Borrow assetsrepayBorrow()/repayBorrowBehalf(): Repay loansenterMarkets(): Enable collateral for multiple assets
Always call enterMarkets before borrowing to register collateral.
👉 Explore advanced deployment strategies
Frequently Asked Questions
What is the role of the Comptroller in Compound?
The Comptroller manages risk parameters, validates user actions (e.g., borrowing), calculates account liquidity, and distributes COMP rewards. It ensures the protocol remains solvent.
How are interest rates determined in Compound?
Interest rates use a jump-rate model, varying with utilization rates. Rates rise slowly until a kink point (e.g., 75% utilization), then jump sharply to incentivize repayments.
What is the difference between CErc20Delegator and CEther?
CErc20Delegator is a proxy contract for ERC-20 assets, enabling upgrades. CEther is for native ETH and doesn't use a proxy, as ETH doesn't conform to ERC-20.
How do liquidations work?
Liquidators repay portions of undercollateralized loans in exchange for collateral CTokens plus a bonus. This restores account health and protects the protocol.
Can I customize the COMP distribution?
Yes, use _setCompSpeed to set COMP per block for each market. Set to 0 to disable rewards.
What oracle does Compound use?
For production, use a robust oracle (e.g., Chainlink). This guide uses SimplePriceOracle for testing, but it's centralized and unsuitable for mainnet.