Skip to main content

Withdraw Vault Shares as collateral

MidnightWithdrawVaultSharesCallback redeems ERC-4626 vault-share collateral back into loan tokens to settle a borrower's exit buy (lend) offer on Morpho Midnight, atomically in the same take.

Description

The borrower posts an exit buy (lend) offer on Morpho Midnight with this callback set as receiverIfMakerIsBuyer. When the offer is filled, the callback pulls vault-share collateral from the borrower's Midnight position, redeems it for loan tokens via the vault, and uses the proceeds to settle the exit. The borrower never holds the shares directly between transactions.

This is the early-exit side of the vault-as-collateral pattern. The supply side, used when the position opens, is the MidnightSupplyVaultSharesCallback.

Step-by-step callback execution

The borrower posts an exit buy (lend) offer on Morpho Midnight with receiverIfMakerIsBuyer set to this callback. The offer carries the callback wiring; the take triggers it.

MidnightWithdrawVaultSharesCallback flow: a taker calls take() on the borrower's exit buy (lend) offer on Morpho Midnight; the callback pulls vault-share collateral from Morpho Midnight, redeems it to loan tokens via the vault, and approves the proceeds back to Morpho Midnight to settle the exit.
  1. Take: A taker fills the borrower's exit buy (lend) offer on Morpho Midnight by calling MORPHO_MIDNIGHT.take(). Morpho Midnight invokes onBuy on the callback.
  2. Validate that the vault is listed at collateralIndex and that asset() == loanToken.
  3. Compute shares needed to produce buyerAssets loan tokens: previewWithdraw(buyerAssets).
  4. Withdraw collateral: Call withdrawCollateral on Morpho Midnight to pull those shares from the borrower's collateral into the callback.
  5. Redeem the shares via IERC4626.withdraw, producing buyerAssets loan tokens.
  6. Settle: Approve buyerAssets back to Morpho Midnight to settle the exit.
  7. Remainder stays: Any remaining collateral (and any other collateral the borrower had on the market) stays in place and can be withdrawn separately.

Prerequisites

The borrower must have authorized this callback on Morpho Midnight before the exit. The callback calls withdrawCollateral on the borrower's behalf, and the take reverts without that authorization.

Withdraw cap drift

Shares are sized by previewWithdraw(buyerAssets) at execution time. If the vault's exchange rate moves between submission and inclusion (a fee event, a yield distribution, etc.), the realized collateral cost shifts. Use Tenor Router's slippage bounds or a pre-flight simulation on time-sensitive exits.