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 vault). They post a buy (lend) offer on Midnight with this callback as the receiver. 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 capital sits 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, non-managed 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 receiverIfMakerIsBuyer set to this 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.
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 receiver field must point at this callback so that
onBuylands here on fill.
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.