Skip to main content

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.

MidnightSupplyCollateralCallback flow: a taker calls take() on the borrower's sell (borrow) offer on Morpho Midnight; the callback pulls collateral from the borrower pro-rata and supplies it to Morpho Midnight per slot, with an optional LTV check at the end.
  1. Take: A taker fills the borrower's sell (borrow) offer on Morpho Midnight by calling MORPHO_MIDNIGHT.take(). Morpho Midnight invokes onSell on the callback.
  2. Decode: Parse the callback data: amounts[] (per collateral slot), offerSellerAssets (the pro-rata denominator), and maxLtv (optional LTV cap).
  3. Validate: amounts.length must equal obligation.collateralParams.length. offerSellerAssets must be non-zero.
  4. Pull and supply per slot: For each slot i where amounts[i] > 0:
    • Compute the pro-rata supply: amounts[i] * sellerAssets / offerSellerAssets.
    • If it rounds to zero, skip the slot.
    • Otherwise, safeTransferFrom the borrower for that amount and call supplyCollateral on Midnight for slot i on the borrower's behalf.
    • Slots where amounts[i] == 0 are skipped entirely.
  5. Optional LTV check: If maxLtv > 0, compute the borrower's account LTV across all collateral slots (using each slot's oracle) and revert with InvalidLtv if it exceeds the cap.

Parameters

  • amounts[]: the full-fill collateral amount for each slot, indexed in the same order as market.collateralParams. Set an entry to 0 to 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 to 0 to 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.

offerSellerAssets must match the offer's true capacity

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.