Nexera-FiAdapters & DeFi ProtocolsMorpho

Morpho Protocol

Overview

The Morpho protocol supports DeFi lending and borrowing by serving as an on-chain peer-to-peer layer atop protocols like Aave and Compound. By leveraging a matching process akin to an on-chain order book, Morpho optimizes rates for lenders and borrowers while maintaining underlying liquidity and risk parameters. In essence, Morpho acts as a lending pool optimizer, streamlining and enhancing the efficiency of DeFi operations.

The Nexera-Fi MorphoAdapter interacts with and utilizes the following four components of Morpho:

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

Interactions with MorphoAdapter

The MorphoAdapter interacts with Morpho protocol by calling the methods presented in the diagram below. Each method serves a specific purpose within the Adapter, enabling strategies to seamlessly integrate liquidity provision, liquidity withdrawal, and borrowing/lending functionality via Morpho.


graph LR; style Nexera_Fi fill:#8453ed style Morpho fill:#5429f0; style Optimizers fill:#57deca; style RewardsDistributor fill:#68a6f2; subgraph Nexera_Fi A[<b> MorphoAdapter </b>] end subgraph Morpho subgraph RewardsDistributor end subgraph Optimizers B["<b>Compound V2 Optimizer</b>"] C["<b>Aave V2 Optimizer</b>"] D["<b>Aave V3 Optimizer</b>"] end end A --> |<b>supply</b>: <i>Add Liquidity</i>| Optimizers A --> |<b>withdraw</b>: <i>Remove Liquidity</i>| Optimizers A --> |<b>claimRewards</b>: <i>Claim Underlying Protocol Rewards</i>| Optimizers A --> |<b>borrow</b>: <i>Borrow Assets</i>| Optimizers A --> |<b>repay</b>: <i>Repay Debt</i>| Optimizers A --> |<b>liquidate</b>: <i>Liquidate Position</i>| Optimizers A --> |<b>supplyCollateral</b>: <i>Supply Collateral to Aave V3</i>| D A --> |<b>withdrawCollateral</b>: <i>Withdraw Collateral from Aave V3</i>| D A --> |<b>claim</b>: <i>Claim Morpho Rewards</i>| RewardsDistributor

Aave V2 & Compound V2 Optimizers

Aave V2 and Compound V2 Morpho Optimizers enhance the capital efficiency of positions within established lending pools of the said underlying protocols. They form a peer-to-peer layer facilitating peer-to-peer matches between lenders and borrowers.

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

  1. supply
  2. withdraw
  3. borrow
  4. repay
  5. liquidate
  6. claimRewards (Deprecated for Aave V2)

Methods

supply

Supplies underlying tokens to a specific market.

function supply(address _poolToken, uint256 _amount) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the supply method of the Morpho Optimizer for either Aave V2 or Compound V2, based on the underlying protocol specified in the encoded DepositExtraData.


withdraw

Withdraws underlying tokens from a specific market.

function withdraw(address _poolToken, uint256 _amount) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the withdraw method of the Morpho Optimizer for either Aave V2 or Compound V2, based on the underlying protocol specified in the encoded WithdrawExtraData.


borrow

Borrows underlying tokens from a specific market.

function borrow(address _poolToken, uint256 _amount) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the borrow method of the Morpho Optimizer for either Aave V2 or Compound V2, based on the underlying protocol specified in the encoded OperateExtraData. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as BORROW.


repay

Repays the debt of the sender, up to the amount provided.

function repay(address _poolToken, uint256 _amount) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the repay method of the Morpho Optimizer for either Aave V2 or Compound V2, based on the underlying protocol specified in the encoded OperateExtraData. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as REPAY.


liquidate

Liquidates a position.

function liquidate(address _borrowed, address _collateral, address _borrower, uint256 _amount) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the liquidate method of the Morpho Optimizer for either Aave V2 or Compound V2, based on the underlying protocol specified in the encoded OperateExtraData. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as LIQUIDATE.


claimRewards

Claims rewards for the given assets.

Notes:

  • Deprecated for Aave V2
  • The incentives vault will never be implemented, thus second parameter of this function becomes useless
function claimRewards(address[] calldata _cTokenAddresses, bool) external returns (uint256 claimedAmount);

This method is invoked by the following MorphoAdapter function:

This function invokes the claimRewards method of the Compound V2 Morpho Optimizer when the underlying protocol specified in the encoded OperateExtraData is Compound V2. It is triggered within the context of the Adapter's _collect function when CollectOperation is configured as CLAIM_PROTOCOL_REWARDS.

Aave V3 Optimizer

Aave V3 Morpho Optimizer enhances the capital efficiency of positions within established Aave V3 lending pools by facilitating peer-to-peer matches between lenders and borrowers.

In the context of the Nexera-Fi MorphoAdapter, these eight methods come into play:

  1. supply
  2. withdraw
  3. borrow
  4. repay
  5. liquidate
  6. claimRewards
  7. supplyCollateral
  8. withdrawCollateral

Methods

supply

Supplies amount of underlying on behalf of onBehalf. The supplied amount cannot be used as collateral but is eligible for the peer-to-peer matching.

function supply(address underlying, uint256 amount, address onBehalf, uint256 maxIterations)
    external
    returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the supply method of the Aave V3 Morpho Optimizer when the underlying protocol specified in the encoded DepositExtraData is Aave V3.

The Adapter's address is provided as onBehalf parameter and the AAVE_V3_SUPPLY_MAX_ITERATIONS value is provided as maxIterations.


withdraw

Withdraws amount of underlying on behalf of onBehalf.

function withdraw(address underlying, uint256 amount, address onBehalf, address receiver, uint256 maxIterations)
    external
    returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the withdraw method of the Aave V3 Morpho Optimizer when the underlying protocol specified in the encoded WithdrawExtraData is Aave V3.

The Adapter's address is provided as onBehalf and receiver parameters and the AAVE_V3_WITHDRAW_MAX_ITERATIONS value is provided as maxIterations.


borrow

Borrows amount of underlying on behalf of onBehalf.

function borrow(address underlying, uint256 amount, address onBehalf, address receiver, uint256 maxIterations)
    external
    returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the borrow method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as BORROW, and the underlying protocol specified in the encoded OperateExtraData is Aave V3.

The Adapter's address is provided as onBehalf and receiver parameters and the AAVE_V3_BORROW_MAX_ITERATIONS value is provided as maxIterations.


repay

Repays amount of underlying on behalf of onBehalf.

function repay(address underlying, uint256 amount, address onBehalf) external returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the repay method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as REPAY, and the underlying protocol specified in the encoded OperateExtraData is Aave V3.

The Adapter's address is provided as onBehalf parameter.

liquidate

Liquidates user.

function liquidate(address underlyingBorrowed, address underlyingCollateral, address user, uint256 amount)
    external
    returns (uint256, uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the liquidate method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as LIQUIDATE, and the underlying protocol specified in the encoded OperateExtraData is Aave V3.


claimRewards

Claims rewards for the given assets.

function claimRewards(address[] calldata assets, address onBehalf)
    external
    returns (address[] memory rewardTokens, uint256[] memory claimedAmounts);

This method is invoked by the following MorphoAdapter function:

This function invokes the claimRewards method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _collect function when CollectOperation is configured as CLAIM_PROTOCOL_REWARDS, and the underlying protocol specified in the encoded CollectExtraData is Aave V3.


supplyCollateral

Supplies amount of underlying collateral to the pool on behalf of onBehalf. The supplied amount cannot be matched peer-to-peer but can be used as collateral.

function supplyCollateral(address underlying, uint256 amount, address onBehalf) external returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the supplyCollateral method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as SUPPLY_COLLATERAL, and the underlying protocol specified in the encoded OperateExtraData is Aave V3.


withdrawCollateral

Withdraws amount of underlying collateral on behalf of onBehalf

function withdrawCollateral(address underlying, uint256 amount, address onBehalf, address receiver)
    external
    returns (uint256);

This method is invoked by the following MorphoAdapter function:

This function invokes the withdrawCollateral method of the Aave V3 Morpho Optimizer. It is triggered within the context of the Adapter's _operate function when OperateOperation is configured as WITHDRAW_COLLATERAL, and the underlying protocol specified in the encoded OperateExtraData is Aave V3.

RewardsDistributor

This contract allows Morpho users to claim their rewards. The rewards are in the form of MORPHO governance token.

Methods

claim

Claims MORPHO rewards for _account, involving an overall _claimable amount and the inclusion of a merkle proof validating the claim.

function claim(address _account, uint256 _claimable, bytes32[] calldata _proof) external;

This method is invoked by the following MorphoAdapter function:

This function invokes the claim method of the Morpho RewardsDistributor contract. It is triggered within the context of the Adapter's _collect function when CollectOperation is configured as CLAIM_MORPHO_REWARDS.


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