Compound v2 Documentation

·

The Compound v2 protocol is a foundational decentralized finance (DeFi) platform that enables users and developers to supply, borrow, and earn interest on digital assets in a trustless, blockchain-based environment. Built on Ethereum and guided by the principles outlined in the Compound Whitepaper (2019), this open-source protocol has become a benchmark for algorithmic money markets. This comprehensive guide explores the technical architecture, mathematical models, and practical implementation details of Compound v2—ideal for developers, analysts, and DeFi enthusiasts.

Whether you're integrating with the protocol, analyzing interest accrual mechanics, or building a front-end interface, understanding the core mechanisms of Compound v2 is essential. Let’s dive into its structure, functionality, and real-world applications.

Getting Started with Compound v2

Compound v2 operates through a suite of smart contracts deployed across multiple Ethereum-compatible networks. The entire codebase is publicly available on GitHub under the compound-finance organization, ensuring transparency and community-driven development. Similarly, the official user interface at app.compound.finance is open-source, allowing developers to audit, fork, or contribute improvements.

👉 Discover how to interact with DeFi protocols like Compound using powerful trading tools.

For real-time support and collaboration, the Compound community maintains an active presence on Discord. Joining the #development channel provides direct access to core contributors from Compound Labs and experienced community builders who can assist with integration challenges, governance queries, or debugging smart contract interactions.

Developer Guides and Resources

To help developers get up and running quickly, Compound offers a series of technical guides covering key aspects of protocol interaction:

These resources form a solid foundation for anyone looking to build on top of or extend the functionality of Compound v2.

Supported Networks

Compound v2 is deployed across several blockchain networks to maximize accessibility and reduce transaction costs. While Ethereum Mainnet remains the primary deployment environment, additional Layer 2 solutions and sidechains may also host versions of the protocol. Full contract addresses for each network are maintained in the official compound-config repository.

Developers should verify network-specific contract addresses before deploying integrations to ensure compatibility and security.

Understanding Protocol Math in Compound v2

One of the most critical aspects of working with Compound v2 is mastering its internal mathematical model. The protocol uses high-precision fixed-point arithmetic implemented in ExponentialNoError.sol to avoid floating-point inaccuracies common in smart contracts.

cToken and Underlying Asset Decimals

All cTokens are ERC-20 compliant tokens with exactly 8 decimal places. However, their underlying assets vary—for example:

This variation affects how exchange rates and balances are interpreted.

cTokencToken DecimalsUnderlyingUnderlying Decimals
cETH8ETH18
cUSDC8USDC6
cWBTC8WBTC8
Note: Always retrieve the decimals() value from the underlying token contract dynamically rather than hardcoding it.

Interpreting Exchange Rates

The exchangeRateCurrent function returns the current rate at which cTokens can be converted into their underlying asset. Because of differing decimal precision, the formula adjusts accordingly:

oneCTokenInUnderlying = exchangeRateCurrent / (1 * 10 ^ (18 + underlyingDecimals - cTokenDecimals))

Here’s how to calculate this in JavaScript using Web3.js:

const cTokenDecimals = 8;
const underlying = new web3.eth.Contract(erc20Abi, batAddress);
const cToken = new web3.eth.Contract(cTokenAbi, cBatAddress);
const underlyingDecimals = await underlying.methods.decimals().call();
const exchangeRateCurrent = await cToken.methods.exchangeRateCurrent().call();
const mantissa = 18 + parseInt(underlyingDecimals) - cTokenDecimals;
const oneCTokenInUnderlying = exchangeRateCurrent / Math.pow(10, mantissa);
console.log('1 cBAT can be redeemed for', oneCTokenInUnderlying, 'BAT');

To find total redeemable underlying tokens:
underlyingTokens = cTokenAmount * oneCTokenInUnderlying

👉 Explore real-time DeFi analytics and monitor lending markets efficiently.

Calculating Accrued Interest

Interest accrues dynamically based on utilization rates (borrowed/supplied ratio) and is updated whenever someone calls mint, redeem, borrow, or repay. These actions trigger the accrueInterest() method, which computes accumulated interest for all participants since the last accrual.

For example:

Interest compounds only when these functions are called—meaning inactive markets may lag in interest updates.

Calculating APY from Rate Per Block

Annual Percentage Yield (APY) reflects compounded returns over a year. Use this formula:

const ethMantissa = 1e18;
const blocksPerDay = 7200; // ~12 seconds per block
const daysPerYear = 365;

const supplyApy = (Math.pow((supplyRatePerBlock / ethMantissa * blocksPerDay) + 1, daysPerYear) - 1) * 100;
const borrowApy = (Math.pow((borrowRatePerBlock / ethMantissa * blocksPerDay) + 1, daysPerYear) - 1) * 100;

Calculating APR from Rate Per Block

Annual Percentage Rate (APR) represents simple interest without compounding:

const blocksPerYear = 7200 * 365;
const supplyApr = (supplyRatePerBlock / ethMantissa) * blocksPerYear * 100;

APR is useful for comparing base yields across assets before compounding effects.

Frequently Asked Questions

Q: What is the difference between APY and APR in Compound v2?
A: APR reflects simple annual interest without compounding, while APY includes the effect of compounding interest over time. In active markets where transactions occur frequently, APY will be higher than APR.

Q: Why do some cTokens have different decimal places than their underlying assets?
A: All cTokens standardize on 8 decimals for consistency in internal calculations. The underlying asset decimals vary by token design (e.g., USDC uses 6 decimals), so conversions must account for this difference.

Q: When does interest accrue in Compound v2?
A: Interest accrues only when certain functions (mint, redeem, borrow, repay) are called. If no one interacts with a market for several blocks, interest accumulates retroactively once an action occurs.

Q: How can I get historical interest rate data?
A: Use the MarketHistoryService API documented in the official Compound v2 API reference to retrieve past interest rates and utilization metrics.

Q: Can I delegate my COMP tokens without transferring ownership?
A: Yes. You can delegate voting power via EIP-712 signatures, allowing participation in governance without moving your tokens.

Q: Is Compound v2 safe for long-term deposits?
A: While the protocol has undergone extensive audits and has a strong security track record, always assess risks such as smart contract vulnerabilities, oracle failures, and market volatility before supplying assets.

👉 Stay ahead in DeFi with advanced portfolio tracking and market insights.

Core Keywords

By mastering these concepts, developers and users alike can fully leverage the power of one of DeFi’s most influential lending platforms.