Nexera-FiAdapters & DeFi ProtocolsCompound

Compound v2 Protocol

Overview

Compound v2 Protocol is a decentralized lending and borrowing platform on Ethereum. It employs the concept of money markets (cTokens) and operates with algorithmic interest rates that adjust based on supply and demand. Each money market is implemented as a smart contract that handles an underlying asset (e.g. ETH) and the corresponding token representation (e.g. cETH). Users of the protocol can either supply assets to a market to earn interest in the form of corresponding cTokens, or borrow assets using any other supported asset as collateral.

The Nexera-Fi CompoundAdapter interacts with and utilizes the following three key components of Compound v2:

In the following sections, we'll provide an overview of these contracts and their methods that Nexera-Fi CompoundAdapter interacts with.

Interactions with CompoundAdapter

The CompoundAdapter interacts with the Compound v2 protocol by calling the methods presented in the diagram below. Each method serves a specific purpose within the adapter, enabling strategies to manage asset supplies, borrow funds, optimize account liquidity, and facilitate COMP token claims.

graph LR; style Nexera_Fi fill:#8453ed; style Compound_v2 fill:#11a66f; style CErc20_Instances fill:#5ab3aa; subgraph Nexera_Fi A[<b> CompoundAdapter </b>] end subgraph Compound_v2 B["<b>Comptroller"] subgraph CErc20_Instances C["<b>..."] D["<b>cDAI"] E["<b>cUSDC"] F["<b>cUSDT"] G["<b>..."] end H["<b>CEther"] end A --> |<b>claimComp</b>: <i>Either from all markets or a list of markets</i>| B A --> |<b>enterMarkets</b>: <i>Enter into a specified list of markets</i>| B A --> |<b>exitMarket</b>: <i>Exit a specified market</i>| B A --> |<b>underlying</b>: <i>Retrieve the address of underlying asset</i>| CErc20_Instances A --> |<b>mint</b>: <i>Mint cTokens in exchange for supplying assets</i>| CErc20_Instances A --> |<b>borrow</b>: <i>Borrow assets from markets</i>| CErc20_Instances A --> |<b>repayBorrow</b>: <i>Repay borrows in markets</i>| CErc20_Instances A --> |<b>redeem</b>: <i>Redeem cTokens</i>| CErc20_Instances A --> |<b>redeemUnderlying</b>: <i>Redeem cTokens</i>| CErc20_Instances A --> |<b>mint</b>: <i>Mint cTokens in exchange for supplying ETH</i>| H A --> |<b>borrow</b>: <i>Borrow assets from cETH market</i>| H A --> |<b>repayBorrow</b>: <i>Repay borrows in cETH market</i>| H A --> |<b>redeem</b>: <i>Redeem cTokens</i>| H A --> |<b>redeemUnderlying</b>: <i>Redeem cTokens</i>| H

Comptroller

The Comptroller contract oversees risk management, collateral requirements, and user interactions within Compound. Through its interface, users can enter or exit markets and claim COMP tokens.

In the context of the Nexera-Fi CompoundAdapter, these three methods come into play:

  1. claimComp: Used for claiming COMP tokens earned in Compound markets.
    Overloaded for targeting either a list of specified markets or all markets.
  2. enterMarkets: Used for entering into a specified list of markets.
  3. exitMarket: Used for exiting from a specified market.

Methods

claimComp (all markets)

Claims all the COMP accrued by holder (Adapter) in all markets.

function claimComp(address holder) public;

This method is invoked by the following CompoundAdapter function:

This function invokes the claimComp method targeting all markets, and is executed in the context of the Adapter's _collect function when the operation defined in CollectExtraData is set to CLAIM_COMP_ALL action.


claimComp (specific markets)

Claims all the COMP accrued by holder (Adapter) in the specified markets (cTokens).

function claimComp(address holder, CToken[] memory cTokens) public;

This method is invoked by the following CompoundAdapter function:

This function invokes the claimComp method targeting specific markets, and is executed in the context of the Adapter's _collect function when the operation defined in CollectExtraData is set to CLAIM_COMP action.


enterMarkets

Enters the specified markets corresponding to cTokens (enables supply and borrowing of assets in the markets).

function enterMarkets(address[] memory cTokens) public returns (uint[] memory);

This method is invoked by the following CompoundAdapter function:

This function triggers the enterMarkets method targeting the markets defined in out input argument. It is executed in the context of the Adapter's _operate function when the action defined in OperateExtraData is set to ENTER_MARKETS. If entering any of the specified markets is unsuccessful, the function reverts.


exitMarket

Exits the specified market corresponding to cTokenAddress.

function exitMarket(address cTokenAddress) external returns (uint);

This method is invoked by the following CompoundAdapter function:

This function triggers the exitMarket method targeting the market defined in the out input argument. It is executed in the context of the Adapter's _operate function when the action defined in OperateExtraData is set to EXIT_MARKETS. If exiting the specified market is unsuccessful, the function reverts.

CErc20 & CEther

Compound v2 employs cToken contracts for supported assets, enabling users to earn interest and use cTokens as collateral.

Two types of cTokens exist:

  • CErc20 instances: Each wraps an underlying ERC-20 asset (e.g. cUSDT for USDT etc..)
  • CEther: Wraps Ether itself (i.e. cETH)

Both cToken types follow the EIP-20 standard. However, the core functions that involve transferring assets into the protocol have slightly different interfaces depending on the cToken type.


In the context of the Nexera-Fi CompoundAdapter, these six methods come into play:

  1. underlying: Retrieves the address of the underlying ERC-20 asset.
  2. mint: Mints cTokens in exchange for supplying assets into the market.
  3. borrow: Borrows assets from the respective market.
  4. repayBorrow: Repays the borrow in the respective market.
  5. redeem: Redeems cTokens in exchange for the underlying asset.
  6. redeemUnderlying: Redeems cTokens in exchange for a specified amount of underlying asset.

Methods

underlying

The underlying() getter method is only applicable to CErc20 contracts within Compound v2. It returns the address of the underlying ERC-20 asset represented by the respective CErc20 instance.

address public underlying;

This method is invoked by the following CompoundAdapter functions:

Both these helper functions invoke the underlying getter method to verify that the asset to be deposited or withdrawn respectively is the underlying of the target CErc20 instance.


mint

Mints cTokens in exchange for supplying assets into the market.

For CErc20 type:

function mint(uint mintAmount) external returns (uint);

For CEther type:

function mint() external payable;

This method is invoked by the following CompoundAdapter function:

This function triggers the mint method on the respective market with the associated assets to be supplied, as defined in the out input argument. It is executed in the context of the Adapter's _deposit function when the action defined in DepositExtraData is set to MINT.


borrow

Borrows assets from the respective market.

For both CErc20 & CEther types:

function borrow(uint borrowAmount) external returns (uint);

This method is invoked by the following CompoundAdapter function:

This function triggers the borrow method on the respective market with the associated assets at play, as defined in the out and expectedIn input arguments. It is executed in the context of the Adapter's _withdraw function when the action defined in WithdrawExtraData is set to BORROW.


repayBorrow

Repays the borrow in the respective market

For CErc20 type:

function repayBorrow(uint repayAmount) external returns (uint);

For CEther type:

function repayBorrow() external payable;

This method is invoked by the following CompoundAdapter function:

This function triggers the repayBorrow method on the respective market with the associated assets at play, as defined in the out and expectedIn input arguments. It is executed in the context of the Adapter's _deposit function when the action defined in DepositExtraData is set to REPAY_BORROW.


redeem

Redeems cTokens in exchange for the underlying asset.

For both CErc20 & CEther types:

function redeem(uint redeemTokens) external returns (uint);

This method is invoked by the following CompoundAdapter function:

This function triggers the redeem method on the respective market with the associated assets at play, as defined in the out and expectedIn input arguments. It is executed in the context of the Adapter's _withdraw function when the action defined in WithdrawExtraData is set to REDEEM.


redeemUnderlying

Redeems cTokens in exchange for a specified amount of underlying asset.

For both CErc20 & CEther types:

function redeemUnderlying(uint redeemAmount) external returns (uint);

This method is invoked by the following CompoundAdapter function:

This function triggers the redeemUnderlying method on the respective market with the associated assets at play, as defined in the out and expectedIn input arguments. It is executed in the context of the Adapter's _withdraw function when the action defined in WithdrawExtraData is set to REDEEM_UNDERLYING.


Please note: For detailed information about Compound v2 protocol, consult the official Compound v2 documentation.

On this page