Skip to main content

CallbackLib

Git Source - Generated with forge doc

Title: CallbackLib

Shared utilities for Morpho Midnight callback contracts.

Midnight trading fees take priority over Tenor fees: sellerFeeFromTick and buyerFeeFromTick are computed on trading-fee-adjusted assets and zero-floored, so a nonzero feeRate can settle as zero.

State Variables​

MAX_PERCENTAGE_FEE_RATE​

Maximum fee rate for flat percentage fees (1%).

uint256 internal constant MAX_PERCENTAGE_FEE_RATE = 0.01e18

Functions​

validateVaultCollateral​

Reverts unless the vault's underlying asset matches the loan token and the vault is listed at collateralIndex in the market's collaterals array.

function validateVaultCollateral(Market memory market, address vault, address loanToken, uint256 collateralIndex)
internal
view;

findCollateral​

Returns whether token is listed in the market's collaterals and, if so, its index.

Assumes collaterals are sorted ascending by token address, a Morpho Midnight invariant.

function findCollateral(Market memory market, address token) internal pure returns (bool found, uint256 index);

percentageFee​

Returns the flat percentage fee assets * feeRate / WAD, rounded down.

Reverts if feeRate > MAX_PERCENTAGE_FEE_RATE (1%).

function percentageFee(uint256 assets, uint256 feeRate) internal pure returns (uint256 fee);

_interestFeeComponent​

Returns the fraction of the offer interest taken as fee, (WAD - price) * feeRate / WAD, rounded down.

Reverts if feeRate > WAD.

The caller must handle feeRate == 0 before calling.

function _interestFeeComponent(uint256 price, uint256 feeRate) private pure returns (uint256);

sellerEffectivePrice​

Returns the seller-side effective price, price * WAD / (WAD + feeShareOfInterest), rounded up.

sellerBudget = units * sellerEffPrice / WAD, so the seller receives assets - fee.

function sellerEffectivePrice(uint256 price, uint256 feeRate) internal pure returns (uint256 effPrice);

Parameters

NameTypeDescription
priceuint256The offer price (tickToPrice(tick)), must be <= WAD.
feeRateuint256The fee rate, in WAD (0 = no fee, WAD = 100% of interest).

buyerEffectivePrice​

Returns the buyer-side effective price, price * WAD / (WAD - feeShareOfInterest), rounded down.

buyerBudget = units * buyerEffPrice / WAD, so the buyer pays assets + fee.

The effective prices yield lender-APR = offerAPR * (1 - feeRate) and seller-APR = offerAPR * (1 + feeRate).

function buyerEffectivePrice(uint256 price, uint256 feeRate) internal pure returns (uint256 effPrice);

Parameters

NameTypeDescription
priceuint256The offer price (tickToPrice(tick)), must be <= WAD and > 0 when feeRate == WAD.
feeRateuint256The fee rate, in WAD (0 = no fee, WAD = 100% of interest).

sellerFeeFromTick​

Returns the seller-side fee from a tick-priced market, assets - sellerBudget, zero-floored, with sellerBudget = units * sellerEffPrice / WAD rounded up.

assets is Midnight's settlement amount, already reduced by Midnight's trading fee, while sellerBudget is derived from the raw tick price. The callback fee is therefore lowered by the Midnight trading fee, settling at zero when that fee exceeds the callback fee.

Returns 0 when feeRate == 0.

function sellerFeeFromTick(uint256 tick, uint256 feeRate, uint256 units, uint256 assets)
internal
pure
returns (uint256);

buyerFeeFromTick​

Returns the buyer-side fee from a tick-priced market, buyerBudget - assets, zero-floored, with buyerBudget = units * buyerEffPrice / WAD rounded down.

assets is Midnight's settlement amount, already increased by Midnight's trading fee, while buyerBudget is derived from the raw tick price. The callback fee is therefore lowered by the Midnight trading fee, settling at zero when that fee exceeds the callback fee.

Returns 0 when feeRate == 0.

function buyerFeeFromTick(uint256 tick, uint256 feeRate, uint256 units, uint256 assets)
internal
pure
returns (uint256);

Errors​

OnlyMidnight​

error OnlyMidnight();

InvalidReceiver​

error InvalidReceiver();

ZeroAmount​

error ZeroAmount();

InvalidFeeConfig​

error InvalidFeeConfig();

TokenMismatch​

error TokenMismatch();

SameMarket​

error SameMarket();

InsufficientCredit​

error InsufficientCredit();

ExcessRepayment​

error ExcessRepayment();

PositionCrossing​

error PositionCrossing();

InvalidCollateral​

error InvalidCollateral();

InvalidBorrowCapacityUsage​

error InvalidBorrowCapacityUsage();