Lend Vault to Midnight
LendVaultToMidnightCallback moves a lender's position from an ERC-4626 compliant vault into a fixed-rate position on Morpho Midnight in a single take.
Description
The lender holds shares in an ERC-4626 vault (typically a Morpho Vault-V2). They post a buy (lend) offer on Midnight with this callback set as the offer's callback. When the offer is taken, the callback redeems enough vault shares to fund the fixed-rate lend offer on Midnight.
This is the onchain mechanism behind Earn while you wait: the lender's idle funds sit in a variable-rate vault until a borrower fills their fixed-rate lend offer. For Earn while you wait on Tenor, the vault is typically an immutable, unmanaged Morpho Vault-V2 that allocates into a Morpho Blue market with the same parameters as the Morpho Midnight target market (same collateral, oracle, and LLTV).
Why an ERC-4626 vault, not Morpho Blue directly?
The simplest source is a Morpho Vault-V2 that allocates 1:1 into a single Morpho Blue market: an unmanaged, immutable vault that mirrors one variable-rate market. That is what Earn while you wait uses.
Sourcing from an ERC-4626 vault rather than a Morpho Blue market directly also keeps the callback generic: the same path supports the Money Market product, curated/managed vaults, and any third-party ERC-4626 strategy whose asset() matches the Midnight market's loan token.
Step-by-step callback execution
Before any take, the lender posts a buy (lend) offer on the Midnight target market with this callback set as the offer's callback. The offer carries the callback wiring; the take triggers it.
- Take: A taker fills the lender's buy (lend) offer on the Midnight target market by calling
MORPHO_MIDNIGHT.take(). Morpho Midnight invokesonBuyon the callback. - Validate: The vault's
asset()must be the Midnight market'sloanToken. - Compute the fee: The fee is derived from the offer tick using
CallbackLib.buyerFeeFromTick. It is an effective-price fee applied to the interest portion only. Zero when no fee is configured. - Withdraw from the vault: The callback calls
IERC4626.withdraw(buyerAssets + fee, address(this), buyer), which pulls vault shares from the lender and produces loan tokens on the callback. - Settle: If the fee is non-zero, it is transferred to the recipient.
buyerAssetsis approved back to Morpho Midnight so it can settle the new lend.
Example
A lender holds vUSDC shares in USDC Vault-V2 worth 10,000 USDC, earning the vault's variable rate while they wait. They want to lock in a fixed rate on cbBTC/USDC · 30d when a borrower shows up.
- The lender posts a buy (lend) offer for 10,000 USDC on
cbBTC/USDC · 30dat2%, withLendVaultToMidnightCallbackset as the offer'scallback. - A borrower takes the offer. Midnight invokes
onBuyon the callback. - The callback confirms
USDC Vault-V2'sasset()is USDC, the market'sloanToken. - It calls
withdraw(10,000 USDC + fee, ...)on the vault, which burns the lender's vUSDC shares and produces 10,000 USDC on the callback. - It approves the 10,000 USDC back to Midnight to settle the new lend.
The lender ends the take holding fewer (or zero) vUSDC shares and a 10,000 USDC fixed-rate lend position on cbBTC/USDC · 30d. This is the Earn while you wait flow.
Prerequisites
- The lender must grant an ERC-20 allowance on the vault's share token to this callback. The vault's
withdrawcall pulls shares from the lender via that allowance. - The buy (lend) offer's
callbackfield must point at this callback so thatonBuylands here on fill (buy offers carry no receiver; the ratifier requiresreceiverIfMakerIsSellerto be zero).
The required allowance is on the vault's share token, not on the underlying loan token. A loan-token allowance is not sufficient and the migration will revert at withdraw.