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:
- Comptroller contract
- CErc20 contracts
- CEther contract
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.
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:
claimComp
: Used for claiming COMP tokens earned in Compound markets.
Overloaded for targeting either a list of specified markets or all markets.enterMarkets
: Used for entering into a specified list of markets.exitMarket
: Used for exiting from a specified market.
Methods
claimComp
(all markets)
Claims all the COMP accrued by holder (Adapter) in all markets.
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).
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).
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.
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:
underlying
: Retrieves the address of the underlying ERC-20 asset.mint
: Mints cTokens in exchange for supplying assets into the market.borrow
: Borrows assets from the respective market.repayBorrow
: Repays the borrow in the respective market.redeem
: Redeems cTokens in exchange for the underlying asset.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.
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:
For CEther type:
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:
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:
For CEther type:
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:
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:
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.