Skip to main content

PriceLib

Git Source - Generated with forge doc

Title: PriceLib

Pure arithmetic for unit price computation and rate limit checking.

Functions​

computePrice​

Returns the unit price WAD^2 / (WAD + ratePerSecond * durationSeconds).

Returns WAD (par, 0% discount) when ratePerSecond == 0 or durationSeconds == 0.

The price lies in [0, WAD]: the buy branch (mulDivDown) can floor to 0 for very large ratePerSecond * durationSeconds, while the sell branch (mulDivUp) stays >= 1.

Rounds in the protected user's favor: down when the user buys (lower ceiling on what they pay), up when the user sells (higher floor on what they receive).

Input bound (ceil-branch mulDivUp headroom): with the seller-side rate capped by the uint40 limit rate, overflow would need durations exceeding ~1e65 seconds.

function computePrice(bool isBuy, uint256 ratePerSecond, uint256 durationSeconds) internal pure returns (uint256);

Parameters

NameTypeDescription
isBuyboolTrue when the protected user is the buyer (lend-side), false when the seller.
ratePerSeconduint256The interest rate per second, in WAD (1e18 = 100% per second).
durationSecondsuint256The duration in seconds.

Returns

NameTypeDescription
<none>uint256price The unit price (assets per unit), in WAD.

computeEffectiveRate​

Returns the effective rate for the position side: max(policyRate, limitRate) for lenders (isBuy == true, floor protection) and min(policyRate, limitRate) for borrowers (isBuy == false, ceiling protection).

function computeEffectiveRate(bool isBuy, uint256 policyRate, uint256 limitRate) internal pure returns (uint256);

Parameters

NameTypeDescription
isBuyboolTrue for lend-side, false for borrow-side.
policyRateuint256The rate from the interest rate policy.
limitRateuint256The user's configured limit rate.

Returns

NameTypeDescription
<none>uint256

satisfiesRateLimit​

Returns true if the executed price satisfies the user's rate limit constraint.

For lenders (isBuy == true): assets * WAD <= units * price (floor).

For borrowers (isBuy == false): assets * WAD >= units * price (ceiling).

Any fee must be folded into assets by the caller (e.g. via RouterLib.netBuyerPrice / netSellerPrice in BaseMigrationRatifier._ratifyRate).

Overflow safety: units and assets are uint128 from Midnight and price <= WAD (1e18), so both sides of the comparison fit in uint256.

function satisfiesRateLimit(
bool isBuy,
uint256 units,
uint256 assets,
uint256 limitRate,
uint256 policyRate,
uint256 duration
) internal pure returns (bool);

Parameters

NameTypeDescription
isBuyboolTrue for lend-side (floor protection), false for borrow-side (ceiling protection).
unitsuint256The number of market units in the take.
assetsuint256The post-fee assets (sellerAssets for borrow, buyerAssets for lend).
limitRateuint256The user's configured limit rate per second.
policyRateuint256The rate from the interest rate policy.
durationuint256The duration in seconds used for price computation.

Returns

NameTypeDescription
<none>bool