Nexera-FiAdapters & DeFi ProtocolsMorpho

MorphoAdapter

Git Source

Extends: BaseAdapter

Custom Errors

WRONG_OPTIMIZER_ADDRESS

Raised when zero address is provided for Optimizers in the initialize function.

error WRONG_OPTIMIZER_ADDRESS();

WRONG_TOKEN_COUNT

Raised when the length of expectedIn & out parameters, defining token compositions, do not match the expected length required for an operation.

error WRONG_TOKEN_COUNT();

WRONG_TOKEN_ADDRESS

Raised when an invalid address is provided for a token.

error WRONG_TOKEN_ADDRESS();

WRONG_TOKEN_AMOUNT

Raised when the amount specified in input or output token compositions is wrong with respect to the requested operation.

error WRONG_TOKEN_AMOUNT();

UNKNOWN_COLLECT_OPERATION

Raised when an enum key not defined in CollectOperation is provided as the operation code for _collect function.

error UNKNOWN_COLLECT_OPERATION();

UNKNOWN_OPERATE_OPERATION

Raised when an enum key not defined in OperateOperation is provided as the operation code for _operate function.

error UNKNOWN_OPERATE_OPERATION();

UNKNOWN_UNDERLYING_PROTOCOL

Raised when an enum key not defined in UnderlyingProtocol is provided as the target protocol for _optimizerFor function.

error UNKNOWN_UNDERLYING_PROTOCOL();

UNSUPPORTED_PROTOCOL_REWARDS

Raised when an enum key not defined in UnderlyingProtocol, or the enum key UnderlyingProtocol.AAVE_V2 is provided as the target protocol for _collect function.

Note: Currently, AAVE V2 rewards are not supported.

error UNSUPPORTED_PROTOCOL_REWARDS();

Enums

UnderlyingProtocol

Defines the supported underlying protocols in Morpho.

enum UnderlyingProtocol {
    COMPOUND,
    AAVE_V2,
    AAVE_V3
}

CollectOperation

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

  • CLAIM_PROTOCOL_REWARDS: Claim rewards from a supported underlying protocol accrued through Morpho
  • CLAIM_MORPHO_REWARDS: Claim rewards accrued by using Morpho
enum CollectOperation {
    CLAIM_PROTOCOL_REWARDS,
    CLAIM_MORPHO_REWARDS
}

OperateOperation

Defines the actions available with respect to Operate Operations:

  • BORROW: Borrow funds via Morpho
  • REPAY: Repay borrowed funds via Morpho
  • LIQUIDATE: Liquidate a user by repaying their borrowed funds and seizing their collateral
  • SUPPLY_COLLATERAL: Supply an underlying asset as collateral position
  • WITHDRAW_COLLATERAL: Withdraw supplied collateral position
enum OperateOperation {
    BORROW,
    REPAY,
    LIQUIDATE,
    SUPPLY_COLLATERAL,
    WITHDRAW_COLLATERAL
}

Structs

DepositExtraData

Defines the necessary data required for performing deposit actions.

struct DepositExtraData {
    UnderlyingProtocol protocol;
    address poolToken;
}

Members

NameTypeDescription
protocolUnderlyingProtocolThe target underlying protocol
poolTokenaddressThe address of the target pool token (i.e. the address of the pool itself)

WithdrawExtraData

Defines the necessary data required for performing withdraw actions.

struct WithdrawExtraData {
    UnderlyingProtocol protocol;
    address poolToken;
}

Members

NameTypeDescription
protocolUnderlyingProtocolThe target underlying protocol
poolTokenaddressThe address of the target pool token (i.e. the address of the pool itself)

CollectExtraData

Defines the necessary data required for performing collect actions.

struct CollectExtraData {
    CollectOperation collectOperation;
    UnderlyingProtocol protocol;
    bytes collectOperationExtraData;
}

Members

NameTypeDescription
collectOperationCollectOperationThe action to conduct
protocolUnderlyingProtocolThe target underlying protocol
collectOperationExtraDatabytesAdditional ABI encoded data related to the operation ( a , b )

CollectProtocolRewardsData

Defines an underlying protocol's associated assets to claim rewards from.

struct CollectProtocolRewardsData {
    address[] assets;
}

Members

NameTypeDescription
assetsaddress[]Array with assets' addresses to claim rewards from. The specific assets depend on the underlying protocol: - For Compound: cTokens - For Aave V3: aTokens and/or debtTokens Note: Currently, AAVE V2 rewards are not supported.

CollectMorphoRewardsData

Defines the necessary data for collecting rewards accrued through Morpho.

struct CollectMorphoRewardsData {
    address rewardsDistributor;
    uint256 claimable;
    bytes32[] proof;
}

Members

NameTypeDescription
rewardsDistributoraddressThe address of the target RewardsDistributor
claimableuint256The amount to claim
proofbytes32[]The Merkle proof that validates the claim

OperateExtraData

Defines the necessary data required for performing operate actions.

struct OperateExtraData {
    OperateOperation operateOperation;
    UnderlyingProtocol protocol;
    address poolToken;
    bytes liquidateExtraData;
}

Members

NameTypeDescription
operateOperationOperateOperationThe action to conduct
protocolUnderlyingProtocolThe target underlying protocol
poolTokenaddressThe address of the target pool token (i.e. the address of the pool itself)
liquidateExtraDatabytesAdditional ABI encoded data related to the operation ( a , b )Note: Only applies when the operate related action is set to OperateOperation.LIQUIDATE. Should be empty for any other operate action

LiquidateExtraDataGeneric

Defines the necessary data required for invoking the liquidate() function of the respective Morpho Optimizer contract associated with a target underlying protocol. It encapsulates the necessary data for repaying a user's borrowed position and seizing their collateral.

struct LiquidateExtraDataGeneric {
    address poolTokenBorrowed;
    address poolTokenCollateral;
    address borrower;
}

Members

NameTypeDescription
poolTokenBorrowedaddressThe address of the borrowed pool token to repay
poolTokenCollateraladdressThe address of the collateral pool token to seize
borroweraddressThe address of the borrower to liquidate

LiquidateExtraDataAAVEV3

Defines the necessary data required for invoking the liquidate() function of the Aave V3 Morpho Optimizer contract. It encapsulates the necessary data for repaying a user's borrowed position and seizing their collateral.

struct LiquidateExtraDataAAVEV3 {
    address borrower;
}

Members

NameTypeDescription
borroweraddressThe address of the borrower to liquidate

Layout

Used to store crucial data for the Adapter's functionality in a specific storage slot.

struct Layout {
    IMorphoOptimizer compoundOptimizer;
    IMorphoOptimizer aaveV2Optimizer;
    IMorphoOptimizer aaveV3Optimizer;
}

Members

NameTypeDescription
compoundOptimizerIMorphoOptimizerThe address of the Compound V2 Optimizer
aaveV2OptimizerIMorphoOptimizerThe address of the Aave V2 Optimizer
aaveV3OptimizerIMorphoOptimizerThe address of the Aave V3 Optimizer

State Variables

AAVE_V3_SUPPLY_MAX_ITERATIONS

The maximum number of iterations allowed during the peer-to-peer matching process when supplying assets to Aave V3 via the associated Morpho Optimizer. This value is suggested by Morpho (see here).

uint256 internal constant AAVE_V3_SUPPLY_MAX_ITERATIONS = 4;

AAVE_V3_BORROW_MAX_ITERATIONS

The maximum number of iterations allowed during the peer-to-peer matching process when borrowing assets from Aave V3 via the associated Morpho Optimizer. This value is suggested by Morpho (see here).

uint256 internal constant AAVE_V3_BORROW_MAX_ITERATIONS = 4;

AAVE_V3_WITHDRAW_MAX_ITERATIONS

The maximum number of iterations allowed during the peer-to-peer matching process when withdrawing assets from Aave V3 via the associated Morpho Optimizer. This value is used to fallback to the default number of iterations for withdrawals (see here).

uint256 internal constant AAVE_V3_WITHDRAW_MAX_ITERATIONS = 0;

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.MorphoAdapter string literal
  • The address of this contract (MorphoAdapter)
constructor();

Functions

initialize

Initializes the Adapter.

function initialize(bytes calldata initData_) external override;

Parameters

NameTypeDescription
initData_bytesThe ABI encoded Layout data

_swap

This function always raises the OPERATION_NOT_SUPPORTED error, as SWAP is not an action supported within Morpho's Optimizers.

Called by executeAction function.

function _swap(
    IAdapter.TokenAmount[] calldata /*out*/,
    IAdapter.TokenAmount[] calldata /*expectedIn*/,
    bytes calldata /*extraData*/
) internal virtual override;

_deposit

Internal function for handling Deposit Operations.

This function is responsible for depositing assets to an underlying protocol via invoking the supply method of the associated Morpho Optimizer.

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. Notes: - The length of the array must equal one (no batch deposits supported in Morpho). - The token field should match the underlying token of the poolToken defined in the provided extraData parameter (e.g. DAI for cDAI pool in Compound).
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. Note: - Should be zero-length array (Morpho does not return any tokens on supply)
extraDatabytesABI encoded data of type DepositExtraData indicating the underlying protocol, and the target pool token (i.e. the address of the pool itself)

_withdraw

Internal function for handling Withdraw Operations.

This function is responsible for withdrawing deposited funds from an underlying protocol via invoking the withdraw method of the associated Morpho Optimizer.

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. Note: - Should be zero-length array (Morpho does not consume any tokens on withdraw)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive. Notes: - The length of the array must equal one (no batch withdrawals supported in Morpho). - The token field should match the underlying token of the poolToken defined in the provided extraData parameter (e.g. USDT for cUSDT pool in Compound). - If amount field holds a value greater than available balance, the available balance is withdrawn. Thus, type(uint256).max can be used to fully withdraw.
extraDatabytesABI encoded data of type WithdrawExtraData indicating the underlying protocol, and the target pool token (i.e. the address of the pool itself)

_collect

Internal function for handling Collect Operations.

This function is employed to retrieve rewards from underlying protocols or from Morpho itself.

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send. Note: - Should be zero-length array as nothing is deposited in this context
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the expected token compositions to receive as rewards. Note: - Can be a zero-length array in case the tokens to be received are not known
extraDatabytesABI encoded data of type CollectExtraData Notes: - For collectOperation == CLAIM_PROTOCOL_REWARDS: see CollectProtocolRewardsData - For collectOperation == CLAIM_MORPHO_REWARDS: see CollectMorphoRewardsData

_operate

Internal function for handling Operate Operations.

This function is used for:

  • Borrowing funds from underlying protocols via Morpho
  • Repaying borrowed funds via Morpho
  • Liquidating borrow positions via Morpho
  • Supplying underlying assets as collateral via Morpho
  • Withdrawing supplied collateral positions

Called by executeAction function.

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

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the respective Morpho Optimizer.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer
extraDatabytesABI encoded data of type OperateExtraData

_claimCompoundRewards

Internal helper function for claiming Compound rewards, by invoking Morpho's Compound V2 Optimizer claimRewards function.

Called by _collect function.

function _claimCompoundRewards(CollectProtocolRewardsData memory data) internal;

Parameters

NameTypeDescription
dataCollectProtocolRewardsDataStruct defining the associated assets to claim rewards from.Note:-data.assets: List of cTokens to claim rewards from

_claimAaveV3Rewards

Internal helper function for claiming Aave V3 rewards, by invoking Morpho's Aave V3 Optimizer claimRewards function.

Called by _collect function.

function _claimAaveV3Rewards(CollectProtocolRewardsData memory data) internal;

Parameters

NameTypeDescription
dataCollectProtocolRewardsDataStruct defining the associated assets to claim rewards from.Note:-data.assets: List of aTokens and/or debtTokens to claim rewards from

_claimMorphoRewards

Internal helper function for claiming Morpho rewards, by invoking Morpho's RewardsDistributor claim function.

Called by _collect function.

function _claimMorphoRewards(CollectMorphoRewardsData memory data) internal;

Parameters

NameTypeDescription
dataCollectMorphoRewardsDataStruct defining the necessary data to claim rewards from Morpho.Note:-data.rewardsDistributor: Address of the RewardsDistributor contract-data.claimable: Amount to claim-data.proof: Merkle proof that validates the claim

_borrowGeneric

Internal helper function for borrowing funds via Morpho Protocol, by invoking the borrow function of the associated Morpho Optimizer.

Called by _operate function.

function _borrowGeneric(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    UnderlyingProtocol protocol,
    address poolToken
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the respective Morpho Optimizer.Note:Should be zero-length array (Morpho does not consume any tokens on borrow)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:-token field should hold the underlying token address of the underlying protocol (e.g. USDT for cUSDT pool in Compound)-amount field should hold the amount to borrow
protocolUnderlyingProtocolThe target underlying protocol
poolTokenaddressThe address of the target pool (e.g. cUSDT for Compound USDT pool)

_repayGeneric

Internal helper function for repaying borrowed funds via Morpho Protocol, by invoking the repay function of the associated Morpho Optimizer.

Called by _operate function.

function _repayGeneric(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    UnderlyingProtocol protocol,
    address poolToken        
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the respective Morpho Optimizer.Note:-token field should hold the underlying token address of the underlying protocol (e.g. USDT for cUSDT pool in Compound)-amount field should hold the amount to repay
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:Should be zero-length array (Morpho does not return any tokens on repay)
protocolUnderlyingProtocolThe target underlying protocol
poolTokenaddressThe address of the target pool (e.g. cUSDT for Compound USDT pool)

_liquidateGeneric

Internal helper function for liquidating borrowed positions via Morpho Protocol, by invoking the liquidate function of the associated Morpho Optimizer.

Called by _operate function.

function _liquidateGeneric(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    UnderlyingProtocol protocol,
    LiquidateExtraDataGeneric memory liquidateExtraData
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the respective Morpho Optimizer.Note:- The length of the array must equal one.-token field should hold the underlying borrowed token address to be repaid (i.e. should match the liquidateExtraData.poolTokenBorrowed).-amount field should hold the amount to repay.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:Should be zero-length array
protocolUnderlyingProtocolThe target underlying protocol
liquidateExtraDataLiquidateExtraDataGenericStruct defining the necessary data for liquidating borrowed positions

_borrowAAVEV3

Internal helper function for borrowing funds from Aave V3 via Morpho Protocol, by invoking the borrow function of the Aave V3 Morpho Optimizer.

Called by _operate function.

function _borrowAAVEV3(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the Aave V3 Morpho Optimizer.Note:Should be zero-length array (Morpho does not consume any tokens on borrow)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:-token field should hold the underlying token address of the underlying protocol (e.g. WETH)-amount field should hold the amount to borrow

_repayAAVEV3

Internal helper function for repaying borrowed funds to Aave V3 via Morpho Protocol, by invoking the repay function of the Aave V3 Morpho Optimizer.

Called by _operate function.

function _repayAAVEV3(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the Aave V3 Morpho Optimizer.Note:-token field should hold the underlying token address of the underlying protocol (e.g. WETH)-amount field should hold the amount to repay
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:Should be zero-length array (Morpho does not return any tokens on repay)

_liquidateAAVEV3

Internal helper function for liquidating borrowed positions in Aave V3 via Morpho Protocol, by invoking the liquidate function of the Aave V3 Morpho Optimizer.

Called by _operate function.

function _liquidateAAVEV3(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn,
    LiquidateExtraDataAAVEV3 memory liquidateExtraData        
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the Aave V3 Morpho Optimizer.Note:- The length of the array must equal one.-token field should hold the underlying borrowed token address to be repaid.-amount field should hold the amount to repay.
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:- The length of the array must equal one.-token field should hold the collateral address.
liquidateExtraDataLiquidateExtraDataAAVEV3Struct containing the address of the borrower to liquidate

_supplyCollateralAAVEV3

Internal helper function for supplying underlying collateral to Aave V3 via Morpho Protocol, by invoking the supplyCollateral function of the Aave V3 Morpho Optimizer.

Called by _operate function.

function _supplyCollateralAAVEV3(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn     
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the Aave V3 Morpho Optimizer.Note:-token field should hold the underlying token address (e.g. WETH)-amount field should hold the amount to supply
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the Aave V3 Morpho Optimizer.Note:Should be zero-length array (Morpho does not return any tokens on supplyCollateral)

_withdrawCollateralAAVEV3

Internal helper function for withdrawing underlying collateral from Aave V3 via Morpho Protocol, by invoking the withdrawCollateral function of the Aave V3 Morpho Optimizer.

Called by _operate function.

function _withdrawCollateralAAVEV3(
    IAdapter.TokenAmount[] calldata out,
    IAdapter.TokenAmount[] calldata expectedIn   
) internal;

Parameters

NameTypeDescription
outIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to send to the Aave V3 Morpho Optimizer.Note:Should be zero-length array (Morpho does not consume any token on withdrawCollateral)
expectedInIAdapter.TokenAmount[]Array of TokenAmount structs defining the token compositions to receive from the Aave V3 Morpho Optimizer.Note:-token field should hold the underlying token address (e.g. wstETH)-amount field should hold the amount to withdraw

_optimizerFor

Returns the address of the Morpho protocol Optimizer.

function _optimizerFor(UnderlyingProtocol protocol) internal view returns(IMorphoOptimizer);

Parameters

NameTypeDescription
protocolUnderlyingProtocolThe target underlying protocol

Returns

NameTypeDescription
<none>IMorphoOptimizerThe address of the Morpho optimizer for protocol

layout

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

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