Skip to main content

Borrow Blue to Midnight

BorrowBlueToMidnightCallback migrates a borrower from a variable-rate position on Morpho Blue into a fixed-rate position on Morpho Midnight in a single take.

Description

The borrower has a variable-rate position on Blue and posts a sell (borrow) offer on Midnight with this callback as the receiver. When the sell (borrow) offer is taken, the Midnight proceeds (sellerAssets) land on the callback. The callback uses them to repay the Blue debt, withdraws the Blue collateral, and supplies it to Midnight on the borrower's behalf, so the borrower exits the take with a fixed-rate borrow position on Midnight.

When used through the Migration Ratifier, the renewal window is anchored by the borrower's renewalCadence (for example, FourWeekCadence) since Blue positions lack maturity timestamps. The callback itself does not enforce cadence.

Step-by-step callback execution

The borrower posts a sell (borrow) offer on the Midnight target market with receiverIfMakerIsSeller set to this callback. The offer carries the callback wiring; the take triggers it.

BorrowBlueToMidnightCallback flow: a taker calls take() on the Midnight market, the proceeds (sellerAssets) repay the Blue debt, and the Blue collateral is moved to Midnight.
  1. Take: A taker fills the borrower's sell (borrow) offer on the Midnight target market by calling MORPHO_MIDNIGHT.take(). The Midnight proceeds (sellerAssets) land on the callback because the offer's receiverIfMakerIsSeller points at it, and Morpho Midnight invokes onSell on the callback.
  2. Validate: The Blue source market and the Midnight target market must share the same loanToken. The Blue market's single collateral token must appear somewhere in the Midnight market's collateral set; the matching slot is resolved automatically by the callback.
  3. Calculate and transfer fees: An effective-price fee is derived from the offer's tick and applied to the interest portion only, then transferred to the protocol fee recipient.
  4. Repay Blue: The callback repays the borrower's variable-rate debt with sellerAssets - fee. It reverts with ExcessRepayment if the repay budget exceeds the Blue market debt. On the final fill the position is closed in shares (exactly zero); on partial fills the debt is repaid pro-rata in assets.
  5. Withdraw collateral: The matching amount of collateral is withdrawn from Blue (pro-rata on partial fills, in full on the final fill).
  6. Supply collateral: The withdrawn collateral is supplied to the matching slot on the Midnight market on the borrower's behalf.

Example

A borrower holds a variable-rate borrow position on cbBTC/USDC · Blue (the source): they owe 10,000 USDC against 0.25 cbBTC. They want a predictable cost, so they move into the fixed-rate cbBTC/USDC · 30d (the target).

  1. The borrower posts a sell (borrow) offer for 10,000 USDC on cbBTC/USDC · 30d at 6%, with receiverIfMakerIsSeller set to BorrowBlueToMidnightCallback.
  2. A lender takes the offer. The 10,000 USDC of sellerAssets lands on the callback, and Midnight invokes onSell.
  3. The callback confirms cbBTC/USDC · Blue and cbBTC/USDC · 30d share USDC as loanToken, and that cbBTC is a collateral slot on the Midnight market.
  4. It repays the 10,000 USDC of variable-rate debt on cbBTC/USDC · Blue with sellerAssets − fee.
  5. It withdraws the 0.25 cbBTC from Blue and supplies it to the matching cbBTC slot on cbBTC/USDC · 30d.

The borrower ends the take with a fixed-rate borrow of ~10,000 USDC on cbBTC/USDC · 30d, backed by the same 0.25 cbBTC, and no remaining position on Blue.

Prerequisites

  • The borrower must authorize the callback on Morpho Blue. Blue's authorization surface is distinct from Midnight's, so granting Midnight authorization is not sufficient.
  • The offer's receiverIfMakerIsSeller must point at this callback so sellerAssets lands here before onSell runs.
Single-collateral migration

Only the Blue market's single collateral token is migrated. If the Midnight target lists additional collateral slots, the borrower must supply those separately (for example, via Supply Collateral). They are not pulled by this callback.