Skip to main content

Supply Vault Shares as Collateral

MidnightSupplyVaultSharesCallback wraps the loan-token proceeds of a fixed-rate borrow into ERC-4626 vault shares and supplies them as collateral on Morpho Midnight, atomically in the same take.

Description

The borrower posts a sell (borrow) offer on Morpho Midnight with this callback set as receiverIfMakerIsSeller. When the offer is filled, the loan tokens received from the lender are deposited into an ERC-4626 vault and the resulting shares are supplied as collateral on the borrower's behalf. The borrower never holds the shares directly between transactions.

This is the supply side of the vault-as-collateral pattern. The withdraw side, used on early exit, is the MidnightWithdrawVaultSharesCallback.

Step-by-step callback execution

The borrower posts a sell (borrow) offer on Morpho Midnight with receiverIfMakerIsSeller set to this callback. The offer carries the callback wiring; the take triggers it.

MidnightSupplyVaultSharesCallback flow: a taker calls take() on the borrower's sell (borrow) offer on Morpho Midnight; the callback deposits the loan-token proceeds (plus optional top-up) into the ERC-4626 vault and supplies the resulting shares as collateral on Morpho Midnight.
  1. Take: A taker fills the borrower's sell (borrow) offer on Morpho Midnight by calling MORPHO_MIDNIGHT.take(). The loan-token proceeds (sellerAssets) land on the callback because the offer's receiverIfMakerIsSeller points at it, and Morpho Midnight invokes onSell on the callback.
  2. Validate that the vault is listed at collateralIndex and that asset() == loanToken.
  3. Optional top-up: If additionalDepositPercent > 0, pull additional loan tokens from the borrower, priced from the offer's tick. The borrower can over-collateralize the position this way (for example, deposit 130% of the position's value into the vault even though only 100% came from the lender).
  4. Deposit all loan tokens held by the callback (sellerAssets + top-up) into the vault. Shares are minted to the callback.
  5. Supply collateral: Call supplyCollateral on Morpho Midnight to supply those shares to the market on the borrower's behalf at collateralIndex.

Prerequisites

  • The offer's receiverIfMakerIsSeller must be this callback so that sellerAssets lands here before onSell fires.
  • If additionalDepositPercent > 0, the borrower must have approved the callback for the loan token.
Never transfer loan tokens directly to the callback

The callback holds no state and never refunds. Any loan tokens sent to it outside an active take get swept into the next supply call as if they were part of the fill, and end up in another borrower's position. Always route through Morpho Midnight's take().