@0x/contracts-staking
: Remove IStructs.CumulativeRewardInfo
, etc.
`@0x/contracts-staking`: Convert all rewards to WETH. `@0x/contracts-staking`: Style changes. `@0x/contracts-staking`: Address misc. review comments. `@0x/contracts-staking`: Make `LibFractions` scaling a separate step.
This commit is contained in:
@@ -7,17 +7,14 @@ library LibFractions {
|
||||
|
||||
using LibSafeMath for uint256;
|
||||
|
||||
/// @dev Maximum value for addition result components.
|
||||
uint256 constant internal RESCALE_THRESHOLD = 10 ** 27;
|
||||
|
||||
/// @dev Safely adds two fractions `n1/d1 + n2/d2`
|
||||
/// @param n1 numerator of `1`
|
||||
/// @param d1 denominator of `1`
|
||||
/// @param n2 numerator of `2`
|
||||
/// @param d2 denominator of `2`
|
||||
/// @return numerator of sum
|
||||
/// @return denominator of sum
|
||||
function addFractions(
|
||||
/// @return numerator Numerator of sum
|
||||
/// @return denominator Denominator of sum
|
||||
function add(
|
||||
uint256 n1,
|
||||
uint256 d1,
|
||||
uint256 n2,
|
||||
@@ -40,15 +37,61 @@ library LibFractions {
|
||||
.safeMul(d2)
|
||||
.safeAdd(n2.safeMul(d1));
|
||||
denominator = d1.safeMul(d2);
|
||||
return (numerator, denominator);
|
||||
}
|
||||
|
||||
// If either the numerator or the denominator are > RESCALE_THRESHOLD,
|
||||
// re-scale them to prevent overflows in future operations.
|
||||
if (numerator > RESCALE_THRESHOLD || denominator > RESCALE_THRESHOLD) {
|
||||
/// @dev Rescales a fraction to prevent overflows during addition if either
|
||||
/// the numerator or the denominator are > `maxValue`.
|
||||
/// @param numerator The numerator.
|
||||
/// @param denominator The denominator.
|
||||
/// @param maxValue The maximum value allowed for both the numerator and
|
||||
/// denominator.
|
||||
/// @return scaledNumerator The rescaled numerator.
|
||||
/// @return scaledDenominator The rescaled denominator.
|
||||
function normalize(
|
||||
uint256 numerator,
|
||||
uint256 denominator,
|
||||
uint256 maxValue
|
||||
)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
uint256 scaledNumerator,
|
||||
uint256 scaledDenominator
|
||||
)
|
||||
{
|
||||
// If either the numerator or the denominator are > `maxValue`,
|
||||
// re-scale them by `maxValue` to prevent overflows in future operations.
|
||||
if (numerator > maxValue || denominator > maxValue) {
|
||||
uint256 rescaleBase = numerator >= denominator ? numerator : denominator;
|
||||
rescaleBase /= RESCALE_THRESHOLD;
|
||||
numerator = numerator.safeDiv(rescaleBase);
|
||||
denominator = denominator.safeDiv(rescaleBase);
|
||||
rescaleBase /= maxValue;
|
||||
scaledNumerator = numerator.safeDiv(rescaleBase);
|
||||
scaledDenominator = denominator.safeDiv(rescaleBase);
|
||||
} else {
|
||||
scaledNumerator = numerator;
|
||||
scaledDenominator = denominator;
|
||||
}
|
||||
return (scaledNumerator, scaledDenominator);
|
||||
}
|
||||
|
||||
/// @dev Rescales a fraction to prevent overflows during addition if either
|
||||
/// the numerator or the denominator are > 2 ** 127.
|
||||
/// @param numerator The numerator.
|
||||
/// @param denominator The denominator.
|
||||
/// @return scaledNumerator The rescaled numerator.
|
||||
/// @return scaledDenominator The rescaled denominator.
|
||||
function normalize(
|
||||
uint256 numerator,
|
||||
uint256 denominator
|
||||
)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
uint256 scaledNumerator,
|
||||
uint256 scaledDenominator
|
||||
)
|
||||
{
|
||||
return normalize(numerator, denominator, 2 ** 127);
|
||||
}
|
||||
|
||||
/// @dev Safely scales the difference between two fractions.
|
||||
@@ -57,8 +100,8 @@ library LibFractions {
|
||||
/// @param n2 numerator of `2`
|
||||
/// @param d2 denominator of `2`
|
||||
/// @param s scalar to multiply by difference.
|
||||
/// @return result = `s * (n1/d1 - n2/d2)`.
|
||||
function scaleFractionalDifference(
|
||||
/// @return result `s * (n1/d1 - n2/d2)`.
|
||||
function scaleDifference(
|
||||
uint256 n1,
|
||||
uint256 d1,
|
||||
uint256 n2,
|
||||
@@ -81,7 +124,7 @@ library LibFractions {
|
||||
.safeMul(d2)
|
||||
.safeSub(n2.safeMul(d1));
|
||||
uint256 tmp = numerator.safeDiv(d2);
|
||||
result = s
|
||||
return s
|
||||
.safeMul(tmp)
|
||||
.safeDiv(d1);
|
||||
}
|
||||
|
Reference in New Issue
Block a user