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 the offer's callback. 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 this callback set as the offer's 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.
Example
A borrower has a borrow position on a Midnight market with vUSDC (shares of USDC Vault-V2) as collateral: they owe 10,000 USDC and want to exit early. This is the unwind of the Supply Vault Shares example.
- The borrower posts an exit buy (lend) offer for 10,000 USDC, with
MidnightWithdrawVaultSharesCallbackset as the offer'scallbackandcollateralIndexpointing at the vUSDC slot. - A taker fills the offer. Midnight invokes
onBuyon the callback. - The callback validates that vUSDC sits at
collateralIndexwithasset() == USDC, then callspreviewWithdraw(10,000 USDC)to size the vUSDC shares needed. - It pulls those vUSDC shares from the borrower's collateral, redeems them through USDC Vault-V2 for 10,000 USDC, and approves the 10,000 USDC back to Midnight to settle the exit.
The borrower ends the take with the debt cleared. Any vUSDC beyond what the redemption needed stays as collateral 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 the Tenor Router's slippage bounds or a pre-flight simulation on time-sensitive exits.
If the vault's actual share burn on withdraw() is less than what previewWithdraw() predicted, the surplus shares remain stranded in the callback contract.