PerShareLibrary
rateMul
Multiply a PerShare rate by a Shares to obtain a FPUnsigned numerator
function rateMul(PerShare a, Shares b) internal pure returns (FPUnsigned qty)
Parameters
| Name | Type | Description |
|---|---|---|
a | PerShare | a PerShares rate |
b | Shares | a number of shares |
Return Values
| Name | Type | Description |
|---|---|---|
qty | FPUnsigned | a * b; numerator units of a |
ZeroRateDiv
x / rate is undefined for rates of 0
error ZeroRateDiv(uint256 b)
divRate
Divide a FPUnsigned numerator by a PerShare rate to obtain a Shares quantity
function divRate(PerShare a, uint256 b) internal pure returns (Shares shares)
Parameters
| Name | Type | Description |
|---|---|---|
a | PerShare | a PerShares rate |
b | uint256 | a quantity (same units as a's numerator) |
Return Values
| Name | Type | Description |
|---|---|---|
shares | Shares | b / a |
perShareEq
Whether a is equal to b.
function perShareEq(PerShare a, PerShare b) internal pure returns (bool)
perShareNeq
Whether a is not equal to b.
function perShareNeq(PerShare a, PerShare b) internal pure returns (bool)
perShareAdd
Add two PerShare rates.
function perShareAdd(PerShare a, PerShare b) internal pure returns (PerShare)
Dev note
Possible precision loss here, because we must invert the stored ratios in order to add them and the intermediate values may be small:
However, this method is only called when a user lets two requests expire in a row, so it's better than incurring precision loss from small ratios every time we create or apply a penalty rate.