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 maxLtv parameter reverts the take if the resulting position would exceed a borrower-specified LTV cap.
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 the LTV at execution, so the resulting position cannot exceed the user's preferred LTV 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), andmaxLtv(optional LTV 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 LTV check: If
maxLtv > 0, compute the borrower's account LTV across all collateral slots (using each slot's oracle) and revert withInvalidLtvif it exceeds the cap.
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.maxLtv: optional cap on the borrower's resulting LTV, in WAD precision. 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.
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 InvalidLtv check, or, if maxLtv = 0, completing with worse-than-expected health.