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 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.

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 borrow-capacity-usage 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 maxBorrowCapacityUsage (optional borrow-capacity-usage 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 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 with InvalidBorrowCapacityUsage if it exceeds the cap. The value is in WAD, where 1e18 means debt equals the full borrowing capacity (the liquidation threshold); a cap ≥ 1e18 never 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.

  1. The borrower posts a sell (borrow) offer for 10,000 USDC on cbBTC/USDC · 30d at 6%, encoding amounts = [0.25 cbBTC] (one entry for the cbBTC slot), offerSellerAssets = 10,000 USDC, and a maxBorrowCapacityUsage cap.
  2. 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 on cbBTC/USDC · 30d.
  3. 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.
  4. After supplying, the callback checks the resulting position against maxBorrowCapacityUsage and 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 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.
  • maxBorrowCapacityUsage: optional cap on the borrower's resulting borrow capacity usage (debt ÷ borrowing capacity), in WAD. 1e18 corresponds to the liquidation threshold; a value ≥ 1e18 never binds. 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.
  • The borrower must also authorize this callback on Morpho Midnight, since the callback calls supplyCollateral on the borrower's behalf. Without that authorization the take reverts.
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 InvalidBorrowCapacityUsage check, or, if maxBorrowCapacityUsage = 0, completing with worse-than-expected health.