Skip to main content

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

NameTypeDescription
aPerSharea PerShares rate
bSharesa number of shares

Return Values

NameTypeDescription
qtyFPUnsigneda * 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

NameTypeDescription
aPerSharea PerShares rate
buint256a quantity (same units as a's numerator)

Return Values

NameTypeDescription
sharesSharesb / 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 {a}{b}\frac\{a\}\{b\} values may be small:

{1}{{1}{{b1}{a1}}+{1}{{b2}{a2}}}\cfrac\{1\}\{\cfrac\{1\}\{\frac\{b_1\}\{a_1\}\} + \cfrac\{1\}\{\frac\{b_2\}\{a_2\}\}\}

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 {a}{b}\frac\{a\}\{b\} ratios every time we create or apply a penalty rate.