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.
- Take: A taker fills the borrower's exit buy (lend) offer on Morpho Midnight by calling
MORPHO_MIDNIGHT.take(). Morpho Midnight invokesonBuyon the callback. - Validate that the vault is listed at
collateralIndexand thatasset() == loanToken. - Compute shares needed to produce
buyerAssetsloan tokens:previewWithdraw(buyerAssets). - Withdraw collateral: Call
withdrawCollateralon Morpho Midnight to pull those shares from the borrower's collateral into the callback. - Redeem the shares via
IERC4626.withdraw, producingbuyerAssetsloan tokens. - Settle: Approve
buyerAssetsback to Morpho Midnight to settle the exit. - 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.
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.