Borrow Midnight to Blue
BorrowMidnightToBlueCallback moves a borrower from a fixed-rate Midnight position into a variable-rate Blue position.
Description
A borrower holds a fixed-rate position on Morpho Midnight and wants to move into a variable-rate position on Blue, either before or after maturity. They post a buy (lend) offer on Midnight to repay their existing borrow position with this callback attached. When the buy (lend) offer is taken, the callback moves the matching collateral to a Morpho Blue market, borrows variable-rate debt on Blue to settle the Midnight exit, and leaves the borrower with a variable-rate position on Blue.
Step-by-step callback execution
The borrower posts an exit buy (lend) offer on the Midnight source market 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 Midnight by calling
MORPHO_MIDNIGHT.take(). Morpho Midnight invokesonBuyon the callback. - Validate: The Midnight source and the Blue target must share the same
loanToken. The Blue market's single collateral token must appear in the Midnight market's collateral set. - Move collateral: Withdraw the matching collateral from Midnight pro-rata to the debt being closed (or in full if the Midnight debt drops to zero), then supply it to the Blue market.
- Borrow on Blue: Borrow
buyerAssets + feefrom the Blue market on the borrower's behalf. The proceeds land on the callback. - Pay the fee and settle: Transfer the fee to the recipient (zero in practice when orchestrated by the ratifier) and approve
buyerAssetsback to Morpho Midnight to settle the exit. - Crossing guard: Read the borrower's Midnight position after the exit. If any credit balance remains, revert with
PositionCrossing. The borrower must stay a pure borrower throughout; they cannot flip into a lender mid-exit.
Prerequisites
- The borrower must authorize the callback on Morpho Blue. The callback supplies collateral and borrows on the borrower's behalf there.
- The offer must route the Blue borrow proceeds to the callback, so the
receiverIfMakerIsTaker/receiverIfTakerIsSellerfield points at the callback contract. - The destination Blue market must have enough lender supply to fund the
buyerAssets + feeborrow.
Only the collateral matching the Blue target's collateral token migrates. Other collaterals the borrower had on Midnight remain there. After the exit the Midnight position has no debt, so the borrower can withdraw them separately.
This direction uses a flat percentage fee on raw buyerAssets rather than an interest-based fee, because Midnight's rate check happens pre-fee. If the destination Blue market lacks enough spare supply to fund buyerAssets + fee, the MORPHO_BLUE.borrow call reverts and the entire take rolls back.