Borrow Midnight Renewal Callback
BorrowMidnightRenewalCallback migrates a borrow position from a source Morpho Midnight market (for example, one maturing in 2 days) to a target Morpho Midnight market with a later maturity (for example, 30 days), in a single transaction.
Description
Both markets sit on Morpho Midnight. The borrower posts a sell (borrow) offer in the target market (the one maturing in 30 days in the example above) with the callback set as the proceeds receiver. When that sell (borrow) offer is filled, the callback uses the loan-token proceeds to repay the source debt (the position maturing in 2 days) and moves shared collateral from the source market to the target market.
Step-by-step callback execution
Before any take, the borrower posts a renewal sell (borrow) offer on the target market with receiverIfMakerIsSeller set to this callback. The offer carries the callback wiring; the take triggers it.
- Take: A taker fills the borrower's renewal sell (borrow) offer on the target market by calling
MORPHO_MIDNIGHT.take(). The loan-token proceeds (sellerAssets) are sent to the callback because the offer'sreceiverIfMakerIsSellerpoints at it, and Morpho Midnight then invokesonSellon the callback. - Validation: The callback checks that source and target markets share the same
loanToken. Collaterals listed in both markets are auto-detected and will migrate; collaterals listed only on the source are skipped and stay on the source. - Fee: A fee is derived from the offer tick (an effective-price fee applied to the interest portion only). When the callback is routed through the Migration Ratifier, the ratifier sets the fee rate. Used directly, the offer creator encodes the fee themselves.
- Repay source debt: The callback repays the source obligation with
sellerAssets - fee. It reads the borrower's current source debt first and reverts withExcessRepaymentif the encoded units would overpay. It also reverts withZeroAmountif the source debt is already zero. - Move collateral: For each shared collateral token, the callback withdraws pro-rata from the source market (scaled by
repaidUnits / sourceDebtBefore) and supplies it on the target market. On the final fill it moves the full remaining balance to avoid leaving dust. The final fill is detected by exact equality (repaidUnits == sourceDebtBefore); with a nonzero fee, fills sized to the remaining offer capacity may never hit that exact equality, leaving residual debt and collateral on the source market until an exact-repayment fill occurs.
Example
A borrower holds a borrow position on cbBTC/USDC · 2d (the source): they owe 10,000 USDC against 0.25 cbBTC of collateral, and that market matures in 2 days. They want to roll it into cbBTC/USDC · 30d (the target) without unwinding and reopening manually.
- The borrower posts a sell (borrow) offer for 10,000 USDC on
cbBTC/USDC · 30dat6%, withreceiverIfMakerIsSellerset toBorrowMidnightRenewalCallback. - A lender takes the offer. The 10,000 USDC of
sellerAssetsis sent to the callback, and Midnight invokesonSell. - The callback confirms both markets use USDC as
loanTokenand that cbBTC is listed on both, so it migrates. - It repays the 10,000 USDC owed on
cbBTC/USDC · 2dwithsellerAssets − fee(the fee is the interest-portion fee derived from the offer's tick). - It withdraws the 0.25 cbBTC from
cbBTC/USDC · 2dand supplies it oncbBTC/USDC · 30d.
The borrower ends the take owing ~10,000 USDC on cbBTC/USDC · 30d, backed by the same 0.25 cbBTC, with the 2-day position fully closed.
Prerequisites
Before the take can succeed, the borrower must:
- Authorize the callback. A single authorization covers both source and target markets, since they live on the same Midnight instance.
- Post a sell (borrow) offer on the target market with
receiverIfMakerIsSellerset to this callback's address.
When the migrated collateral is ERC-4626 vault shares behind a Vault V2 Allowlist Gate, this callback must also be allowlisted for canReceiveShares. The callback receives the shares transiently while withdrawing the shares from the source market (step 5). Without the entry, the renewal take reverts with CannotReceiveShares. See the allowlist gate page.
Collaterals listed on the source but not on the target are not migrated. After the renewal, the borrower can recover those collaterals by calling withdrawCollateral on the source market.
Routing
The natural taker of a borrower's target sell (borrow) renewal offer is a lender, since taking that offer means providing loan tokens at the offered rate. A keeper can also take the offer, together with an existing target buy (lend) offer, in one transaction if that results in a positive spread. Anyone can run a keeper; it is not a privileged role.
When posting a renewal offer, the borrower prices it as the implied combined rate of closing the source position at par early plus borrowing in the target market, not as a standalone target borrow rate. The X% on the target sell (borrow) offer reflects that combined effective rate.
Taker net rate (lender)
When a lender takes the borrower's target sell (borrow) renewal offer, they lend at the offered rate X%. If the lender already holds a position in the source market, or can enter a source-side lend position at a rate above par (0%) in the same transaction, they also receive source-side interest: the callback repays the borrower's source debt at par, which lets the lender exit the source position atomically.
Example. Borrower's target sell (borrow) offer at X% = 6%. A lender takes the offer and lends at 6% on the target. In the same transaction, the lender also exits a source-side lend position at 5%, atomically unlocked when the callback repays the source debt at par. The lender's net effective rate is 6% on the target plus the source-side interest.
Keeper
A keeper that does not want to hold lend exposure takes both target offers in one transaction when a positive net spread exists.
- Read the borrower's target sell (borrow) offer at
X%. - Find a target buy (lend) offer at
Y%and compute the target spreadX − Y. - Optionally take a source-side sell (borrow) offer at a positive rate
S%in the same transaction: the keeper enters a source-side lend position, and when the callback repays the borrower's source debt at par, that position settles instantly at its full term value, so the keeper receivesS%of source-side interest. - Take both target offers through
MORPHO_MIDNIGHT.take()whenever the target spread plus any source-side interest covers gas.
Even when the target leg alone does not cover the keeper's costs (X − Y ≤ 0 or below gas), a positive source-side leg can cover the shortfall.
In the Migration Ratifier context, the borrower has posted the renewal as their own offer with the ratifier; any keeper can take the offer without further approval from the borrower, together with a buy (lend) offer, once the combined spread exceeds gas.
Example. Borrower's target sell (borrow) offer at X% = 6% and a target buy (lend) offer at Y% = 2%. The 4% gross spread is available to the keeper. The keeper takes both offers atomically: the borrower rolls into the 6% target borrow, the lender lends at 2%, and the keeper keeps whatever remains of the 4% spread after gas. If the target legs alone do not cover gas, the keeper can also take a source-side sell (borrow) offer at, for example, S% = 3%; the callback repays the source debt at par, settling the keeper's source position at full term value and adding 3% of source-side interest to the keeper's proceeds.