How to Deploy Compound: Build Your Own Decentralized Lending Bank

Β·

Decentralized Finance (DeFi) has revolutionized traditional financial systems by enabling permissionless, transparent, and trustless financial services. Among the leading DeFi protocols, Compound stands out as a robust lending and borrowing platform built on blockchain technology. This comprehensive guide walks you through deploying a simplified version of the Compound protocol on the BSC Testnet using Remix IDE and MetaMask, offering hands-on experience with core DeFi mechanisms.

Whether you're a developer exploring smart contract architecture or a blockchain enthusiast diving into DeFi, this tutorial delivers practical insights into building a decentralized lending bank from scratch.

πŸ‘‰ Discover how to interact with decentralized financial protocols securely and efficiently.


Core Keywords


Understanding the Compound Architecture

Before deployment, it’s essential to understand Compound’s modular design. The protocol consists of several interconnected smart contracts that manage lending, borrowing, interest accrual, price feeds, and governance incentives.

Despite its complexity β€” with over 30 source files β€” breaking down the system into functional modules makes it approachable. Below is a structured walkthrough of each critical component.

Pro Tip: While Compound's codebase may seem daunting at first, studying it reveals elegant design patterns and security-conscious engineering. With patience, even beginners can grasp its inner workings.

Module 1: COMP Token – Incentive Mechanism

The COMP token serves as the governance and reward token within the Compound ecosystem. Although optional for test environments, deploying a mock COMP token helps simulate real-world incentive mechanics.

Deployment Steps:

  1. Use Comp.sol from the official repository.
  2. Customize parameters:

    • Name: MyCOMP
    • Symbol: MCOMP
    • Total Supply: 10,000,000 MCOMP
  3. Deploy via Remix IDE using Account 1.

After deployment, note the contract address:

0x1fe7FF222D59B6648D640090046661313A1CF0a2

Update this address in the ComptrollerG7.sol contract using the getCompAddress() function to ensure compatibility.

πŸ‘‰ Learn how top-tier platforms handle token economics and user incentives.


Module 2: Comptroller – Core Logic Controller

The Comptroller acts as the central risk management and market coordination engine. It uses a proxy pattern (Unitroller) for upgradability.

2.1 Deploy Unitroller (Proxy Contract)

2.2 Deploy ComptrollerG7 (Logic Contract)

Proxy Binding Process

To activate the proxy:

  1. Call _setPendingImplementation(newImplementation) on Unitroller, passing the ComptrollerG7 address.
  2. Call _become(unitroller) on ComptrollerG7.

This two-step process prevents accidental upgrades to invalid logic contracts.

Note: Always expose the Unitroller address externally β€” it proxies all calls to the live logic contract.

Module 3: Price Oracle – Market Valuation System

Accurate asset pricing is vital for maintaining loan health and preventing under-collateralization.

Deploy SimplePriceOracle

Later, set prices for underlying assets like USDT and ETH to reflect their USD values.


Module 4: Configuration & Risk Parameters

Now configure global risk controls through the Comptroller interface.

4.2 Set Close Factor

Defines the maximum portion of a loan that can be repaid during liquidation.

4.3 Set Liquidation Incentive

Rewards liquidators for maintaining system solvency.

4.4 Assign Price Oracle

Link the oracle to Comptroller:


Module 5: Interest Rate Models

Interest rates dynamically adjust based on asset utilization.

Deploy JumpRateModelV2

Used for both ERC-20 and native ETH markets.

Parameters:

Deploy two instances:

  1. For ERC20-based cTokens β†’ Address:
    0x8A517DA790929D2aC3527210f9472E2822424180
  2. For ETH-based cETH β†’ Address:
    0x0cca4ccD1ED542B5D7F3Ebbcf49D92DCB0a8D04e

These models allow flexible rate adjustments post-deployment.


Module 6: cToken for ERC-20 Assets

cTokens represent tokenized deposits and accrue interest over time.

6.1 Deploy Mock ERC-20 (e.g., USDT)

Create a test stablecoin:

6.2 Deploy CErc20Delegate

Shared logic contract for all ERC-20-backed cTokens.

6.3 Deploy CErc20Delegator (cUSD)

The actual cToken representing USDT deposits.

Constructor Arguments:

ParameterValue
UnderlyingtUSDT address
ComptrollerUnitroller address
Interest Rate ModelFirst JumpRateModelV2
Initial Exchange Rate1e18 (1:1)
Name"COMPOUND USD"
Symbol"cUSD"
Decimals18
Adminmsg.sender
ImplementationCErc20Delegate address
Data0x

Deployed at:
0x209C9b6a0Ec37b91d0758514070A8439B14B9B3c


Module 7: cToken for Native ETH (cETH)

For native cryptocurrency support.

Deploy CEther.sol

Parameters:

Deployed at:
0xf3feeab27E8B8b71ED92040be19E5aA80baf9B01


Module 8: Set Market Prices

Update asset valuations in the oracle.

8.1 cUSD Price

8.2 cETH Price

These values enable accurate collateral calculations.


Module 9: Final cToken Configuration

9.1 Set Reserve Factor

Portion of interest reserved for protocol reserves.

Call _setReserveFactor(newReserveFactorMantissa) on each cToken.

9.2 Add Markets

Enable markets in Comptroller:

9.3 Set Collateral Factor

Determines how much users can borrow against deposited assets.

Only supported markets with sufficient price data can be used as collateral.


Module 10: COMP Rewards & Distribution

Users earn COMP tokens when supplying or borrowing in enabled markets.

Enable COMP Speed

Use _setCompSpeed(cToken, compSpeed) where:

Note: COMP is not distributed by default. Admins must explicitly enable rewards per market.

Calculate daily emissions:

const compSpeedPerBlock = await comptroller.methods.compSpeeds(cTokenAddr).call();
const compPerDay = (compSpeedPerBlock / 1e18) * 4 * 60 * 24;

Module 11: Claiming COMP Rewards

Users claim accumulated rewards via:

function claimComp(address holder);
function claimComp(address holder, CToken[] memory cTokens);
function claimComp(address[] memory holders, CToken[] memory cTokens, bool borrowers, bool suppliers);

Example Web3 call:

await comptroller.methods.claimComp("your-wallet-address").send({ from: sender });

Rewards accumulate per block while active in markets.


Testing Core User Functions

Validate functionality with five key operations:

1. Deposit (Mint)

User supplies USDT β†’ receives cUSD at current exchange rate.

await usdt.approve(cUSD.address, amount);
await cUSD.mint(amount);

Check balances and liquidity:

const liquidity = await comptroller.getAccountLiquidity(user);

2. Borrow

User borrows against collateral after entering markets.

await comptroller.enterMarkets([cUSD.address, cETH.address]);
await cUSD.borrow(loanAmount);

System checks liquidity pre-borrow.

3. Repay Loan

Repay debt directly or on behalf of others.

await usdt.approve(cUSD.address, repayment);
await cUSD.repayBorrow(repayment);

Interest accrues up to repayment block.

4. Withdraw (Redeem)

Convert cTokens back to underlying assets.

await cUSD.redeem(cTokenAmount);
// OR redeem specific amount of underlying
await cUSD.redeemUnderlying(usdtAmount);

Subject to liquidity and solvency checks.

5. Liquidation

Occurs when collateral value drops below threshold.

Critical for protocol stability.


Frequently Asked Questions (FAQ)

Q: Can I deploy Compound on mainnet?
A: Yes, but ensure thorough testing on testnets first. Mainnet deployment requires robust security audits and risk parameter tuning.

Q: What is the role of Unitroller?
A: Unitroller is a proxy contract that delegates calls to the Comptroller logic contract, enabling future upgrades without disrupting user positions.

Q: Why use two JumpRateModelV2 instances?
A: Each cToken requires its own interest rate model instance due to independent configuration needs for different asset types (ERC-20 vs ETH).

Q: How are COMP rewards calculated?
A: Rewards are distributed per block based on supply/borrow activity in enabled markets. The _setCompSpeed() function controls emission rates.

Q: Is manual price feeding secure?
A: For production, use decentralized oracles like Chainlink. SimplePriceOracle is suitable only for testing and development.

Q: Can I customize collateral factors?
A: Yes, admins can set collateral factors between 0% and ~90%, depending on asset volatility and risk tolerance.


πŸ‘‰ Explore secure wallet integrations and advanced DeFi interactions today.

By following this guide, you’ve successfully deployed a functional decentralized lending platform inspired by Compound. This foundation enables further experimentation with governance, flash loans, cross-chain integration, and automated yield strategies β€” all core components of modern DeFi ecosystems.