PriceLib
Git Source - Generated with forge doc
Title: PriceLib
Pure arithmetic for zero-coupon bond price computation and rate limit checking.
Functions
computePrice
Returns the zero-coupon bond 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
| Name | Type | Description |
|---|---|---|
isBuy | bool | True when the protected user is the buyer (lend-side), false when the seller. |
ratePerSecond | uint256 | The interest rate per second, in WAD (1e18 = 100% per second). |
durationSeconds | uint256 | The duration in seconds. |
Returns
| Name | Type | Description |
|---|---|---|
<none> | uint256 | price The zero-coupon bond price, 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
| Name | Type | Description |
|---|---|---|
isBuy | bool | True for lend-side, false for borrow-side. |
policyRate | uint256 | The rate from the interest rate policy. |
limitRate | uint256 | The user's configured limit rate. |
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
| Name | Type | Description |
|---|---|---|
isBuy | bool | True for lend-side (floor protection), false for borrow-side (ceiling protection). |
units | uint256 | The number of market units in the take. |
assets | uint256 | The post-fee assets (sellerAssets for borrow, buyerAssets for lend). |
limitRate | uint256 | The user's configured limit rate per second. |
policyRate | uint256 | The rate from the interest rate policy. |
duration | uint256 | The duration in seconds used for price computation. |