The Compound adapter enables the Yieldinator Facet to integrate with Compound's money markets, allowing vaults to earn yield by supplying assets to Compound's lending protocol.
Overview
Compound is a decentralized lending protocol that allows users to supply assets to earn interest and borrow assets against collateral. The Compound adapter facilitates deposits into Compound markets, manages cToken balances, and harvests yield generated from lending interest and COMP token rewards.
Implementation Details
Contract: CompoundAdapter.sol
The adapter implements the standard YieldinatorAdapter interface with Compound-specific functionality:
contractCompoundAdapterisYieldinatorAdapter{// Compound contracts IComptroller public comptroller;addresspublic compToken;// Mapping from underlying asset to cTokenmapping(address=>address)public assetToCToken;// Deposited amounts per tokenmapping(address=>uint256)public depositedAmount;// Constructor and core functions...}
Key Functions
Asset Registration
Before using the adapter for a specific token, the asset must be registered with its corresponding cToken:
Deposit
Deposits tokens into Compound's money markets:
The function:
Transfers tokens from the caller to the adapter
Approves the cToken contract to spend the tokens
Mints cTokens by supplying the underlying asset
Updates the deposited amount tracking
Withdraw
Withdraws tokens from Compound's money markets:
Harvest Yield
Harvests accrued interest and COMP rewards:
The function:
Calculates yield based on the exchange rate between cTokens and the underlying asset
Redeems only the yield portion, leaving the principal intact
Claims COMP token rewards from the Comptroller
Transfers both the yield and COMP rewards to the caller
Emergency Withdraw
Provides emergency withdrawal functionality:
APY Calculation
Returns the current APY for a token in Compound:
The APY is calculated based on Compound's supplyRatePerBlock, converted to an annual percentage with 2 decimal places (scaled by 10000).
Additional Functions
The adapter includes additional Compound-specific functions:
Usage Example
Security Considerations
The adapter uses OpenZeppelin's SafeERC20 for token transfers to prevent common ERC20 vulnerabilities.
Non-reentrancy guards protect against reentrancy attacks during external calls.
Role-based access control ensures only authorized addresses can call sensitive functions.
The adapter checks Compound operation result codes to ensure successful execution.
Gas Optimization
The adapter minimizes the number of external calls to Compound contracts.
Token approvals are set exactly to the amount being deposited to avoid unnecessary approvals.
COMP rewards are claimed in the same transaction as yield harvesting to save gas.
Integration Requirements
To use the Compound adapter, the following components are required:
A Compound Comptroller address for the target network