Skip to main content

Lend Midnight Renewal Callback

Rolls a lender's fixed-rate lend position from one Morpho Midnight market into a longer-dated Midnight fixed-rate market.

Description

The lender posts a buy (lend) offer on the target market with this callback set as the receiver. When the offer fills, Midnight invokes onBuy on the callback. The callback withdraws the lender's funds from the source position and uses them to fund the new lend on the target market.

Step-by-step callback execution

The lender posts a buy (lend) offer on the target market with this callback set as the offer's callback. The callback will withdraw the lender's funds from the source market and fund the offer in the target market. The offer carries the callback wiring; the take triggers it.

LendMidnightRenewalCallback flow: a taker calls take() on the target market, which invokes onBuy on the callback; the callback withdraws the lender's funds from the source market and approves them to the target market to settle the new lend.
  1. Take: A taker fills the lender's renewal buy (lend) offer on the target market by calling MORPHO_MIDNIGHT.take(). Morpho Midnight invokes onBuy on the callback. Only Morpho Midnight may call; buyerAssets and units must both be non-zero.
  2. Validation: The source and target obligations must share the same loanToken, otherwise TokenMismatch. The callback also reads the source position with slashing and continuous accrual applied and reverts with ZeroAmount if the post-update credit is zero.
  3. Fee: A fee is derived from the offer tick using the buyer-side effective price and applies to the interest portion of the offer only. When routed through the Migration Ratifier, the ratifier sets the fee rate; used directly, the offer creator encodes it themselves. If fee > 0, it is sent to the fee recipient encoded in the callback data.
  4. Withdraw funds: The callback withdraws buyerAssets + fee from the source market on the lender's behalf, reverting with InsufficientCredit if that exceeds the available post-update source credit.
  5. Transfer funds: The withdrawn buyerAssets funds the new lend on the target market.

Example

A lender holds a lend position of 10,000 USDC on cbBTC/USDC · 2d (the source), maturing in 2 days. They want to keep those funds deployed at a fixed rate by rolling into cbBTC/USDC · 30d (the target).

  1. The lender posts a buy (lend) offer for 10,000 USDC on cbBTC/USDC · 30d at 2%, with LendMidnightRenewalCallback set as the offer's callback.
  2. A borrower takes the offer (or a keeper matches it). Midnight invokes onBuy on the callback.
  3. The callback confirms both markets use USDC as loanToken.
  4. It withdraws 10,000 USDC (plus any fee) from the lender's cbBTC/USDC · 2d position — assuming the source is withdrawable, see Routing.
  5. It uses that 10,000 USDC to fund the new lend on cbBTC/USDC · 30d.

The lender ends the take with a fresh 10,000 USDC fixed-rate lend position on cbBTC/USDC · 30d and no remaining balance on the 2-day market.

Prerequisites

  • The lender must authorize the callback. The callback withdraws from the source position on the lender's behalf.
  • The target buy (lend) offer's receiver field must point at this callback.
  • The source position must be withdrawable at execution time. See Routing for the two cases.
Source must be withdrawable at take time

The source market must already permit withdrawal at execution time. This withdrawable balance can be created just in time by the keeper rolling the lender's position forward.

Routing

The natural taker of a lender's target buy (lend) renewal offer is a borrower, since taking that offer means borrowing at the offered rate. A keeper can also match the offer against an existing target borrow offer in the RFQ if that results in a positive spread (MEV profit).

The callback needs the lender's source position to be withdrawable when the take fires:

  • Source already withdrawable (matured, or a prior borrower repay at par). The take fills with no source-side action.
  • Source not yet withdrawable: Someone (the taker or a keeper) must bundle a source-side borrow + repay in the same transaction to make the source position withdrawable.

Taker net rate (borrower)

When a borrower takes the lender's target buy (lend) renewal offer, they borrow at the offered rate X%. If the lender's source position isn't already withdrawable, the borrower also bears the cost of the source-side bundle (borrow + repay) needed to unlock it. That source-side interest accrues to the lender, so it raises the borrower's effective cost above X%.

Example. Lender's target buy (lend) offer at X% = 2%. A borrower takes the offer and borrows at 2% on the target. If the borrower also bundles a source-side borrow at the source market rate and repays at par to unlock the lender, the accrued source interest is added on top of their 2% target borrow.

Keeper

A third-party keeper doesn't want to hold borrow exposure; they match existing offers when a positive spread exists.

  1. Read the lender's target buy (lend) offer at X%.
  2. Find a target sell (borrow) offer at Y% with Y − X > 0.
  3. If the source isn't already withdrawable, bundle a source-side borrow + repay to unlock the lender's position.
  4. Match the two target offers whenever the Y − X spread covers the source-side cost plus the keeper's transaction cost (gas).

In the Migration Ratifier context, the lender has posted the renewal as their own maker offer with the ratifier; a keeper fills it permissionlessly, matching it against a sell (borrow) offer in the RFQ as soon as the spread exceeds the source-side cost and gas.

Example. Lender's target buy (lend) offer at X% = 2% and a matching target sell (borrow) offer at Y% = 6%. The 4% gross spread is the keeper's profit headroom. The keeper bundles a source-side borrow + repay to unlock the lender; the source-side interest accrues to the lender. The renewal goes through as long as the 4% target spread exceeds the source-side cost plus gas. The lender closes the source position early, rolls into the 2% target lend, and collects the keeper's source-side interest on top, netting a return above 2%.