Skip to main content

TakeMathLib

Git Source - Generated with forge doc

Title: TakeMathLib

Shared unit/price math for sizing Midnight takes.

Sizers never revert: degenerate or non-binding constraints (zero price, overflow) saturate to uint128.max; Midnight enforces take validity at take time.

Functions

getOfferRemaining

Returns the offer's remaining fillable capacity in market units, reading consumption from Morpho Midnight.

offer.maxAssets is the maker-side cap: buyerAssets for BUY offers, sellerAssets for SELL offers.

For asset-denominated offers, computes the tight inverse of Midnight's forward rounding: BUY uses mulDivDownInverse (forward = mulDivDown, largest u where floor(u * p / WAD) <= R) and SELL uses mulDivUpInverse (forward = mulDivUp, largest u where ceil(u * p / WAD) <= R).

This guarantees the returned units never cause consumed to overshoot the offer's max.

A zero-priced asset cap does not bound unit exposure: the returned capacity is type(uint128).max even though offer.maxAssets is finite.

function getOfferRemaining(IMidnight morphoMidnight, Offer calldata offer, bytes32 marketId)
internal
view
returns (uint256 remainingUnits);

Parameters

NameTypeDescription
morphoMidnightIMidnight
offerOffer
marketIdbytes32The pre-computed market ID (avoids a redundant toId call for asset-denominated offers).

sellerPrice

Returns the fee-inclusive seller price for an offer: offerPrice - settlementFee (zero-floored) for BUY offers (seller is taker) and offerPrice for SELL offers (seller is maker).

function sellerPrice(IMidnight morphoMidnight, bytes32 marketId, Offer calldata offer)
internal
view
returns (uint256);

buyerPrice

Returns the fee-inclusive buyer price for an offer: offerPrice for BUY offers (buyer is maker) and offerPrice + settlementFee for SELL offers (buyer is taker).

function buyerPrice(IMidnight morphoMidnight, bytes32 marketId, Offer calldata offer)
internal
view
returns (uint256);

assetsToSellerUnits

Returns the maximum units whose seller receipt does not exceed assets.

Tight inverse of Midnight's seller-receipt forward (buy => mulDivDown, sell => mulDivUp).

function assetsToSellerUnits(IMidnight morphoMidnight, bytes32 marketId, Offer calldata offer, uint256 assets)
internal
view
returns (uint256 units);

maxUnitsForSellerBudget

Returns the maximum units where the seller's net budget (sellerAssets - fee) <= maxBudget.

SELL offers: seller is maker, no settlement fee. BUY offers: seller is taker, settlement fee deducted.

function maxUnitsForSellerBudget(
IMidnight morphoMidnight,
bytes32 marketId,
Offer calldata offer,
uint256 feeRate,
uint256 maxBudget
) internal view returns (uint256);

maxUnitsForBuyerBudget

Returns the maximum units where the buyer's net budget (buyerAssets + fee) <= maxBudget.

BUY offers: buyer is maker, no settlement fee. SELL offers: buyer is taker, settlement fee added.

For SELL offers with feeRate > 0, takes the min of settlement-fee-adjusted and Tenor-fee-adjusted bounds.

function maxUnitsForBuyerBudget(
IMidnight morphoMidnight,
bytes32 marketId,
Offer calldata offer,
uint256 feeRate,
uint256 maxBudget
) internal view returns (uint256);

mulDivDownInverse

Returns the largest x with x.mulDivDown(num, den) <= target, i.e. inverts floor(x * num / den) <= target.

Closed-form: x <= ((target + 1) * den - 1) / num.

When num is 0 every x satisfies the constraint, so there is no upper bound: return type(uint256).max instead of dividing by zero.

function mulDivDownInverse(uint256 target, uint256 den, uint256 num) internal pure returns (uint256);

mulDivUpInverse

Returns the largest x with x.mulDivUp(num, den) <= target, i.e. inverts ceil(x * num / den) <= target.

Since ceil(x * num / den) <= target is equivalent to x * num / den <= target for integer target, the answer is floor(target * den / num).

When num is 0 every x satisfies the constraint, so there is no upper bound: return type(uint256).max instead of dividing by zero.

function mulDivUpInverse(uint256 target, uint256 den, uint256 num) internal pure returns (uint256);

available

Returns the maximum amount of token that spender can pull from owner: min(balance, allowance), the common constraint for any ERC-20 transfer.

function available(address token, address owner, address spender) internal view returns (uint256);

capReduceOnly

Caps maxUnits by the maker's existing position when the offer is reduceOnly; no-op otherwise.

BUY offers: caps by the buyer's debt (prevents crossing to credit).

SELL offers: caps by the seller's credit (prevents crossing to debt).

function capReduceOnly(IMidnight morphoMidnight, bytes32 marketId, Offer calldata offer, uint256 maxUnits)
internal
view
returns (uint256);

min

Returns the minimum of three uint256 values.

function min(uint256 a, uint256 b, uint256 c) internal pure returns (uint256);