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 equal to sellerAssets × additionalDepositPercent (a percentage of the lender-funded amount). 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.

Example

A borrower wants to borrow 10,000 USDC on a Midnight market whose collateral slot is vUSDC (the shares of USDC Vault-V2, asset() == USDC). The borrowed USDC itself becomes the collateral, deposited into the vault so it earns yield while backing the position.

  1. The borrower posts a sell (borrow) offer for 10,000 USDC at 6%, with receiverIfMakerIsSeller set to MidnightSupplyVaultSharesCallback and collateralIndex pointing at the vUSDC slot.
  2. A lender takes the offer. The 10,000 USDC of sellerAssets lands on the callback, and Midnight invokes onSell.
  3. The callback validates that vUSDC sits at collateralIndex and that its asset() is USDC.
  4. It deposits the 10,000 USDC into USDC Vault-V2, minting vUSDC to the callback, then supplies those vUSDC shares as collateral on the borrower's behalf.

With additionalDepositPercent = 30%, the callback first pulls an extra 3,000 USDC from the borrower, deposits 13,000 USDC total into the vault, and supplies the resulting vUSDC — over-collateralizing the position to 130% of the borrowed amount.

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().