MorphoAdapter
Extends: BaseAdapter
Custom Errors
WRONG_OPTIMIZER_ADDRESS
Raised when zero address is provided for Optimizers in the initialize function.
WRONG_TOKEN_COUNT
Raised when the length of expectedIn
& out
parameters, defining token compositions, do not match the expected length required for an operation.
WRONG_TOKEN_ADDRESS
Raised when an invalid address is provided for a token.
WRONG_TOKEN_AMOUNT
Raised when the amount specified in input or output token compositions is wrong with respect to the requested operation.
UNKNOWN_COLLECT_OPERATION
Raised when an enum key not defined in CollectOperation is provided as the operation code for _collect function.
UNKNOWN_OPERATE_OPERATION
Raised when an enum key not defined in OperateOperation is provided as the operation code for _operate function.
UNKNOWN_UNDERLYING_PROTOCOL
Raised when an enum key not defined in UnderlyingProtocol is provided as the target protocol for _optimizerFor function.
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.
Enums
UnderlyingProtocol
Defines the supported underlying protocols in Morpho.
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 MorphoCLAIM_MORPHO_REWARDS
: Claim rewards accrued by using Morpho
OperateOperation
Defines the actions available with respect to Operate Operations:
BORROW
: Borrow funds via MorphoREPAY
: Repay borrowed funds via MorphoLIQUIDATE
: Liquidate a user by repaying their borrowed funds and seizing their collateralSUPPLY_COLLATERAL
: Supply an underlying asset as collateral positionWITHDRAW_COLLATERAL
: Withdraw supplied collateral position
Structs
DepositExtraData
Defines the necessary data required for performing deposit actions.
Members
Name | Type | Description |
---|---|---|
protocol | UnderlyingProtocol | The target underlying protocol |
poolToken | address | The address of the target pool token (i.e. the address of the pool itself) |
WithdrawExtraData
Defines the necessary data required for performing withdraw actions.
Members
Name | Type | Description |
---|---|---|
protocol | UnderlyingProtocol | The target underlying protocol |
poolToken | address | The address of the target pool token (i.e. the address of the pool itself) |
CollectExtraData
Defines the necessary data required for performing collect actions.
Members
Name | Type | Description |
---|---|---|
collectOperation | CollectOperation | The action to conduct |
protocol | UnderlyingProtocol | The target underlying protocol |
collectOperationExtraData | bytes | Additional ABI encoded data related to the operation ( a , b ) |
CollectProtocolRewardsData
Defines an underlying protocol's associated assets to claim rewards from.
Members
Name | Type | Description |
---|---|---|
assets | address[] | 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.
Members
Name | Type | Description |
---|---|---|
rewardsDistributor | address | The address of the target RewardsDistributor |
claimable | uint256 | The amount to claim |
proof | bytes32[] | The Merkle proof that validates the claim |
OperateExtraData
Defines the necessary data required for performing operate actions.
Members
Name | Type | Description |
---|---|---|
operateOperation | OperateOperation | The action to conduct |
protocol | UnderlyingProtocol | The target underlying protocol |
poolToken | address | The address of the target pool token (i.e. the address of the pool itself) |
liquidateExtraData | bytes | Additional 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.
Members
Name | Type | Description |
---|---|---|
poolTokenBorrowed | address | The address of the borrowed pool token to repay |
poolTokenCollateral | address | The address of the collateral pool token to seize |
borrower | address | The 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.
Members
Name | Type | Description |
---|---|---|
borrower | address | The address of the borrower to liquidate |
Layout
Used to store crucial data for the Adapter's functionality in a specific storage slot.
Members
Name | Type | Description |
---|---|---|
compoundOptimizer | IMorphoOptimizer | The address of the Compound V2 Optimizer |
aaveV2Optimizer | IMorphoOptimizer | The address of the Aave V2 Optimizer |
aaveV3Optimizer | IMorphoOptimizer | The 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).
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).
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).
STORAGE_SLOT
Unique identifier for the storage slot where the Layout struct is stored.
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)
Functions
initialize
Initializes the Adapter.
Parameters
Name | Type | Description |
---|---|---|
initData_ | bytes | The 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.
_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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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). |
expectedIn | IAdapter.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) |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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) |
expectedIn | IAdapter.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. |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.TokenAmount[] | Array of TokenAmount structs defining the token compositions to send. Note: - Should be zero-length array as nothing is deposited in this context |
expectedIn | IAdapter.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 |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.TokenAmount[] | Array of TokenAmount structs defining the token compositions to send to the respective Morpho Optimizer. |
expectedIn | IAdapter.TokenAmount[] | Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer |
extraData | bytes | ABI 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.
Parameters
Name | Type | Description |
---|---|---|
data | CollectProtocolRewardsData | Struct 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.
Parameters
Name | Type | Description |
---|---|---|
data | CollectProtocolRewardsData | Struct 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.
Parameters
Name | Type | Description |
---|---|---|
data | CollectMorphoRewardsData | Struct 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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) |
expectedIn | IAdapter.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 |
protocol | UnderlyingProtocol | The target underlying protocol |
poolToken | address | The 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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 |
expectedIn | IAdapter.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) |
protocol | UnderlyingProtocol | The target underlying protocol |
poolToken | address | The 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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. |
expectedIn | IAdapter.TokenAmount[] | Array of TokenAmount structs defining the token compositions to receive from the respective Morpho Optimizer.Note:Should be zero-length array |
protocol | UnderlyingProtocol | The target underlying protocol |
liquidateExtraData | LiquidateExtraDataGeneric | Struct 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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) |
expectedIn | IAdapter.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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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 |
expectedIn | IAdapter.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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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. |
expectedIn | IAdapter.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. |
liquidateExtraData | LiquidateExtraDataAAVEV3 | Struct 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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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 |
expectedIn | IAdapter.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.
Parameters
Name | Type | Description |
---|---|---|
out | IAdapter.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) |
expectedIn | IAdapter.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.
Parameters
Name | Type | Description |
---|---|---|
protocol | UnderlyingProtocol | The target underlying protocol |
Returns
Name | Type | Description |
---|---|---|
<none> | IMorphoOptimizer | The address of the Morpho optimizer for protocol |
layout
Retrieves a reference to the Layout struct stored at the storage slot specified by STORAGE_SLOT.