A Comprehensive Guide to Deploying a Compound-Based Decentralized Lending Protocol

·

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:

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.

  1. Deploy Comp.sol with customized parameters (name, symbol, total supply).
  2. Record the deployed address (e.g., 0x1fe7FF222D59B6648D640090046661313A1CF0a2).
  3. 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.

  1. Deploy Unitroller.sol (proxy contract). Note its address.
  2. Deploy ComptrollerG7.sol (logic contract). Record its address.
  3. Bind the proxy to the logic:

    • Call _setPendingImplementation on Unitroller.sol, passing the ComptrollerG7.sol address.
    • Call _become on ComptrollerG7.sol, passing the Unitroller.sol address.

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:

  1. Deploy SimplePriceOracle.sol and record its address.
  2. 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:

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:

  1. Deploy a mock ERC-20 token (e.g., USDT) as the underlying asset.
  2. Deploy CErc20Delegate.sol (implementation contract for proxy-based CTokens).
  3. 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:

  1. Set Prices: Use SimplePriceOracle.setUnderlyingPrice to set prices for each CToken.
  2. Set Reserve Factors: Adjust reserve factors (e.g., 10% for ERC-20, 20% for ETH) via _setReserveFactor.
  3. Support Markets: Call _supportMarket in the Comptroller for each CToken.
  4. 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:

Users can claim accrued COMP via claimComp methods in the Comptroller.

Testing Core Functionalities

Thoroughly test these key operations:

  1. Minting: Users supply assets and receive CTokens.
  2. Borrowing: Users borrow against collateral, subject to liquidity checks.
  3. Repaying: Users repay borrows, reducing debt.
  4. Redeeming: Users redeem CTokens for underlying assets.
  5. 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:

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.