Nexera-FiAdapters & DeFi ProtocolsCompound

CompoundAdapter

Git Source

Extends: BaseAdapter

The CompoundAdapter is designed to interact with the Compound v2 Protocol.

This adapter enables users to harness the power of Compound v2 lending and borrowing capabilities and integrate them into their financial strategies.

Enums

DepositOperation

Defines the actions available with respect to Deposit Operations:

  • MINT: Depositing assets in return for cTokens
  • REPAY_BORROW: Depositing borrowed assets for reducing the outstanding debt (partially of fully)
enum DepositOperation{ MINT, REPAY_BORROW }

WithdrawOperation

Defines the actions available with respect to Withdraw Operations:

  • BORROW: Withdraw assets as a loan
  • REDEEM: Withdraw assets converted from cTokens
  • REDEEM_UNDERLYING: Withdraw underlying assets accrued based on position
enum WithdrawOperation{ BORROW, REDEEM, REDEEM_UNDERLYING }

CollectOperation

Defines the actions for collecting rewards (Collect Operation) from the Compound protocol:

  • CLAIM_COMP: Claim COMP tokens from specific markets as rewards
  • CLAIM_COMP_ALL: Claim all available COMP tokens as rewards
enum CollectOperation{ CLAIM_COMP, CLAIM_COMP_ALL }

OperateOperation

Defines the actions available for interacting with Compound markets:

  • ENTER_MARKETS: Entering one or more Compound markets, which enables supply and borrowing of assets
  • EXIT_MARKETS: Exiting one or more Compound markets, which may introduce restrictions with respect to supplying or borrowing certain assets
enum OperateOperation{ ENTER_MARKETS, EXIT_MARKETS }

Structs

DepositExtraData

Used to indicate the appropriate action of a Deposit Operation.

struct DepositExtraData {
    DepositOperation depositOperation;
}

Members

NameTypeDescription
depositOperationDepositOperationThe action to conduct

WithdrawExtraData

Used to indicate the appropriate action of a Withdraw Operation.

struct WithdrawExtraData {
    WithdrawOperation withdrawOperation;
}

Members

NameTypeDescription
withdrawOperationWithdrawOperationThe action to conduct

CollectExtraData

Used to indicate the appropriate action of a Collect Operation.

struct CollectExtraData {
    CollectOperation collectOperation;
}

Members

NameTypeDescription
collectOperationCollectOperationThe action to conduct

OperateExtraData

Used to indicate the appropriate action of an Operate Operation.

struct OperateExtraData {
    OperateOperation operateOperation;
}

Members

NameTypeDescription
operateOperationOperateOperationThe action to conduct

Layout

Used to store the address of the Compound Comptroller contract in a specific storage slot.

This approach ensures data consistency across different contract versions (in case of upgrades).

struct Layout {
    address comptroller;
}

Members

NameTypeDescription
comptrolleraddressThe address of the Comptroller contract

State Variables

STORAGE_SLOT

Unique identifier for the storage slot where the Layout struct is stored.

bytes32 internal immutable STORAGE_SLOT;

Constructor

Sets the value of the STORAGE_SLOT state variable.

The value is computed as the keccak256 hash of the abi encoding of:

  • The Nexera-FI.CompoundAdapter string literal
  • The address of this contract (CompoundAdapter)
constructor();

Functions

initialize

Initializes the Adapter by setting the address of the Comptroller contract.

function initialize(bytes calldata initData_) external override; 

Parameters

NameTypeDescription
initData_bytesThe ABI encoded Comptroller contract address

_deposit

Internal function for handling Deposit Operations.

This function is used for minting or repaying borrow actions within the Compound protocol.

Called by executeAction function.

function _deposit(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. For both minting and repaying borrow actions, expectedIn should only include the addresses of the cToken markets with an amount equal to 0.
extraDatabytesABI encoded data of type DepositExtraData indicating the action (either MINT or REPAY_BORROW)

_withdraw

Internal function for handling Withdraw Operations.

This function is used for borrowing or redeeming actions within the Compound protocol.

Called by executeAction function.

function _withdraw(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. - In the case of borrowing and redeeming underlying assets, it should contain only the address of the cToken. - In the case of redeeming, it should include the address of the cToken and the amount of cTokens to be provided.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. - For borrowing and redeeming, it should contain the addresses of the assets to be borrowed or redeemed respectively. - For redeeming underlying assets, it should contain the addresses and the amounts of the underlying assets to be redeemed.
extraDatabytesABI encoded data of type WithdrawExtraData indicating the action (BORROW, REDEEM or REDEEM_UNDERLYING)

_collect

Internal function for handling Collect Operations.

This function is used for claiming rewards (COMP Tokens) within the Compound protocol.

Called by executeAction function.

function _collect(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. - In the case of CLAIM_COMP, it should contain the addresses of cToken markets from which to claim COMP. - In the case of CLAIM_COMP_ALL, no TokenAmount arguments are needed.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. For both actions (CLAIM_COMP or CLAIM_COMP_ALL), it should contain the address of the COMP token
extraDatabytesABI encoded data of type CollectExtraData indicating the action (CLAIM_COMP or CLAIM_COMP_ALL)

_operate

Internal function for handling Operate Operations.

This function is used for interacting with markets within the Compound protocol by either entering or exiting them.

Called by executeAction function.

function _operate(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata,
    bytes calldata extraData
) internal override;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. For both actions (ENTER_MARKETS or EXIT_MARKETS), it should contain the cToken addresses of the markets to enter or exit respectively
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. Should be empty as this parameter is not used in this context.
extraDatabytesABI encoded data of type OperateExtraData indicating the action (ENTER_MARKETS or EXIT_MARKETS)

_mint

Internal function for handling Minting actions within a Deposit Operation.

This function is responsible for minting new cTokens when assets (ERC20 or Ether) are supplied into the respective market via interacting with Compound's:

Called by _deposit function.

function _mint(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256 length
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
lengthuint256The length of the out and expectedIn arrays (should have same length as every market cToken is associated with a respective underlying asset)

_repayBorrow

Internal function for handling RepayBorrow actions within a Deposit Operation.

Called by _deposit function.

function _repayBorrow(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256 length
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
lengthuint256The length of the out and expectedIn arrays (should have same length as every market cToken is associated with a respective underlying asset)

_checkRevertsOnDeposit

Internal function for checking potential reverts on Deposit Operations.

Called by _repayBorrow and _mint functions.

function _checkRevertsOnDeposit(
    IAdapter.TokenAmount calldata out,
    IAdapter.TokenAmount calldata expectedIn
) internal view;

Parameters

NameTypeDescription
outIAdapter.TokenAmountA TokenAmount struct defining the token (address & amount) to send
expectedInIAdapter.TokenAmountA TokenAmount struct defining the expected token (address & amount) to receive

_approveOnDeposit

Internal function for approving token allowances on Deposit Operations.

Called by _repayBorrow and _mint functions.

function _approveOnDeposit(
    IAdapter.TokenAmount calldata out,
    IAdapter.TokenAmount calldata expectedIn
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmountA TokenAmount struct defining the token (address & amount) to send
expectedInIAdapter.TokenAmount[]A TokenAmount struct defining the expected token (address & amount) to receive

_borrow

Internal function for handling Borrow actions within a Withdraw Operation.

Called by _withdraw function.

function _borrow(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256 length
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
lengthuint256The length of the out and expectedIn arrays (should have same length as every market cToken is associated with a respective underlying asset)

_redeem

Internal function for handling REDEEM actions within a Withdraw Operation.

Called by _withdraw function.

function _redeem(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256 length
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
lengthuint256The length of the out and expectedIn arrays (should have same length as every market cToken is associated with a respective underlying asset)

_redeem_underlying

Internal function for handling REDEEM_UNDERLYING actions within a Withdraw Operation.

Called by _withdraw function.

function _redeem_underlying(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    uint256 length
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive
lengthuint256The length of the out and expectedIn arrays (should have same length as every market cToken is associated with a respective underlying asset)

_checkRevertsOnWithdrawal

Internal function for checking potential reverts on Withdraw Operations.

Called by _borrow, _redeem and _redeem_underlying functions.

function _checkRevertsOnWithdrawal(
    IAdapter.TokenAmount calldata out,
    IAdapter.TokenAmount calldata expectedIn,
    uint256 amountToCheck
) internal view;

Parameters

NameTypeDescription
outIAdapter.TokenAmountA TokenAmount struct defining the token (address & amount) to send
expectedInIAdapter.TokenAmountA TokenAmount struct defining the expected token (address & amount) to receive
amountToCheckuint256The token amount defined in expectedIn

_claim_comp

Internal function for handling CLAIM_COMP actions within a Collect Operation.

Called by _collect function.

function _claim_comp(IAdapter.TokenAmount[] calldata out, address comptroller) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
comptrolleraddressThe address of the Comptroller contract

_claim_comp_all

Internal function for handling CLAIM_COMP_ALL actions within a Collect Operation.

Called by _collect function.

function _claim_comp_all(address comptroller) internal;

Parameters

NameTypeDescription
comptrolleraddressThe address of the Comptroller contract

_enter_markets

Internal function for handling ENTER_MARKETS actions within a Operate Operation.

Called by _operate function.

function _enter_markets(IAdapter.TokenAmount[] calldata out, address comptroller) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
comptrolleraddressThe address of the Comptroller contract

_exit_markets

Internal function for handling EXIT_MARKETS actions within a Operate Operation.

Called by _operate function.

function _exit_markets(IAdapter.TokenAmount[] calldata out, address comptroller) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send
comptrolleraddressThe address of the Comptroller contract

_checkRevertsOnOperate

Internal function for checking potential reverts on Operate Operations.

Called by _enter_markets and _exit_markets functions.

function _checkRevertsOnOperate(IAdapter.TokenAmount[] calldata out)
    internal
    pure
    returns (uint256 length);

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send

_swap

This function always raises the OPERATION_NOT_SUPPORTED error, as Swap is not an action supported within the Compound protocol.

Called by executeAction function.

function _swap(
    IAdapter.TokenAmount[] calldata,
    IAdapter.TokenAmount[] calldata,
    bytes calldata
) internal virtual override;

layout

Retrieves a reference to the Layout struct stored at a specified storage slot.

function layout() internal view returns (Layout storage l);