Supply Collateral
MidnightSupplyCollateralCallback lets a borrower post a sell (borrow) offer on Morpho Midnight at a specific rate without locking collateral upfront. Collateral is only pulled and supplied to Midnight when the offer fills, in proportion to the fill amount; partial fills pull proportionally less collateral.
Description
The borrower posts a sell (borrow) offer on Morpho Midnight with this callback encoded in the offer data. When the offer is filled (fully or partially) the callback pulls collateral from the borrower in proportion to the fill and supplies it to Midnight in the same transaction. An optional maxBorrowCapacityUsage parameter reverts the take if the resulting position would exceed a borrower-specified cap on borrow capacity usage (debt as a fraction of the position's borrowing capacity).
This pattern lets a borrower post a borrow offer that:
- executes only at the rate specified in the offer,
- commits no collateral while the offer is available, and
- bounds borrow capacity usage at execution, so the resulting position cannot exceed the user's preferred fraction of borrowing capacity if the collateral value changes while the offer is pending.
Step-by-step callback execution
The borrower posts a sell (borrow) offer on Morpho Midnight with this callback encoded in the offer's callback data. The offer carries the callback wiring; the take triggers it.
- Take: A taker fills the borrower's sell (borrow) offer on Morpho Midnight by calling
MORPHO_MIDNIGHT.take(). Morpho Midnight invokesonSellon the callback. - Decode: Parse the callback data:
amounts[](per collateral slot),offerSellerAssets(the pro-rata denominator), andmaxBorrowCapacityUsage(optional borrow-capacity-usage cap). - Validate:
amounts.lengthmust equalobligation.collateralParams.length.offerSellerAssetsmust be non-zero. - Pull and supply per slot: For each slot
iwhereamounts[i] > 0:- Compute the pro-rata supply:
amounts[i] * sellerAssets / offerSellerAssets. - If it rounds to zero, skip the slot.
- Otherwise,
safeTransferFromthe borrower for that amount and callsupplyCollateralon Midnight for slotion the borrower's behalf. - Slots where
amounts[i] == 0are skipped entirely.
- Compute the pro-rata supply:
- Optional borrow-capacity-usage check: If
maxBorrowCapacityUsage > 0, compute the borrower's debt as a fraction of its borrowing capacity across all collateral slots (using each slot's oracle and LLTV) and revert withInvalidBorrowCapacityUsageif it exceeds the cap. The value is in WAD, where1e18means debt equals the full borrowing capacity (the liquidation threshold); a cap≥ 1e18never binds.
Example
A borrower wants to borrow 10,000 USDC on cbBTC/USDC · 30d at 6%, backed by 0.25 cbBTC, but only wants to commit the cbBTC if the offer actually fills at their rate.
- The borrower posts a sell (borrow) offer for 10,000 USDC on
cbBTC/USDC · 30dat6%, encodingamounts = [0.25 cbBTC](one entry for the cbBTC slot),offerSellerAssets = 10,000 USDC, and amaxBorrowCapacityUsagecap. - A lender takes the full offer. Midnight invokes
onSell. The callback pulls 0.25 cbBTC from the borrower (0.25 × 10,000 / 10,000) and supplies it to the cbBTC slot oncbBTC/USDC · 30d. - If instead the lender takes 40% of the offer (
4,000 USDC), the callback pulls only 0.10 cbBTC (0.25 × 4,000 / 10,000) and supplies that, in proportion to the fill. - After supplying, the callback checks the resulting position against
maxBorrowCapacityUsageand reverts the take if it exceeds the cap.
No cbBTC leaves the borrower's wallet until an offer fills, and never more than the fill's pro-rata share.
Parameters
amounts[]: the full-fill collateral amount for each slot, indexed in the same order asmarket.collateralParams. Set an entry to0to skip a slot.offerSellerAssets: the denominator used to scale collateral on partial fills. Should equal the offer's full seller-assets capacity. The contract only enforces that it is non-zero.maxBorrowCapacityUsage: optional cap on the borrower's resulting borrow capacity usage (debt ÷ borrowing capacity), in WAD.1e18corresponds to the liquidation threshold; a value≥ 1e18never binds. Set to0to skip the check.
Prerequisites
- The borrower must approve this callback contract for each collateral token to be supplied. Missing approvals revert the take on
transferFrom. - The borrower must also authorize this callback on Morpho Midnight, since the callback calls
supplyCollateralon the borrower's behalf. Without that authorization the take reverts.
If it is set lower than the offer's full capacity, each fill pulls more collateral than intended and the take can revert when the borrower's balance is exceeded. If it is set higher, each fill pulls less collateral than intended, likely reverting on the InvalidBorrowCapacityUsage check, or, if maxBorrowCapacityUsage = 0, completing with worse-than-expected health.