Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update(TwabLib): uniform uint types for calculations #170

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions contracts/libraries/ExtendedSafeCastLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,19 @@ library ExtendedSafeCastLib {
require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
return uint208(value);
}

/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
}
12 changes: 7 additions & 5 deletions contracts/libraries/TwabLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ library TwabLib {
ObservationLib.Observation[MAX_CARDINALITY] storage _twabs,
AccountDetails memory _accountDetails
) internal view returns (uint24 index, ObservationLib.Observation memory twab) {
index = uint24(RingBufferLib.mostRecentIndex(_accountDetails.nextTwabIndex, MAX_CARDINALITY));
index = uint24(
RingBufferLib.mostRecentIndex(_accountDetails.nextTwabIndex, MAX_CARDINALITY)
);
twab = _twabs[index];
}

Expand Down Expand Up @@ -311,15 +313,15 @@ library TwabLib {
/// @return New TWAB that was recorded.
function _computeNextTwab(
ObservationLib.Observation memory _currentTwab,
uint256 _currentBalance,
uint224 _currentBalance,
uint32 _time
) private pure returns (ObservationLib.Observation memory) {
// New twab amount = last twab amount (or zero) + (current amount * elapsed seconds)
return
ObservationLib.Observation({
amount: (uint256(_currentTwab.amount) +
(_currentBalance * (_time.checkedSub(_currentTwab.timestamp, _time))))
.toUint208(),
amount: _currentTwab.amount +
_currentBalance *
(_time.checkedSub(_currentTwab.timestamp, _time)),
timestamp: _time
});
}
Expand Down
2 changes: 1 addition & 1 deletion test/DrawCalculator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ describe('DrawCalculator', () => {
const userRandomNumber =
'0x369ddb959b07c1d22a9bada1f3420961d0e0252f73c0f5b2173d7f7c6fe12b70'; // intentionally same as winning random number

const prizeDistributionIndex: BigNumber =
const prizeDistributionIndex: BigNumber =
await drawCalculator.calculateDistributionIndex(
userRandomNumber,
winningRandomNumber,
Expand Down
30 changes: 25 additions & 5 deletions test/libraries/OverflowSafeComparator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ describe('overflowSafeComparatorLib', () => {
const timestampB = currentTimestamp - 100;

expect(
await overflowSafeComparatorLib.lteHarness(timestampA, timestampB, currentTimestamp),
await overflowSafeComparatorLib.lteHarness(
timestampA,
timestampB,
currentTimestamp,
),
).to.equal(true);
});

Expand All @@ -79,7 +83,11 @@ describe('overflowSafeComparatorLib', () => {
const timestampB = timestampA;

expect(
await overflowSafeComparatorLib.lteHarness(timestampA, timestampB, currentTimestamp),
await overflowSafeComparatorLib.lteHarness(
timestampA,
timestampB,
currentTimestamp,
),
).to.equal(true);
});

Expand All @@ -88,7 +96,11 @@ describe('overflowSafeComparatorLib', () => {
const timestampB = currentTimestamp + 1000;

expect(
await overflowSafeComparatorLib.lteHarness(timestampA, timestampB, currentTimestamp),
await overflowSafeComparatorLib.lteHarness(
timestampA,
timestampB,
currentTimestamp,
),
).to.equal(false);
});

Expand All @@ -97,7 +109,11 @@ describe('overflowSafeComparatorLib', () => {
const timestampB = currentTimestamp - 1000;

expect(
await overflowSafeComparatorLib.lteHarness(timestampA, timestampB, currentTimestamp),
await overflowSafeComparatorLib.lteHarness(
timestampA,
timestampB,
currentTimestamp,
),
).to.equal(true);
});

Expand All @@ -106,7 +122,11 @@ describe('overflowSafeComparatorLib', () => {
const timestampB = timestampA;

expect(
await overflowSafeComparatorLib.lteHarness(timestampA, timestampB, currentTimestamp),
await overflowSafeComparatorLib.lteHarness(
timestampA,
timestampB,
currentTimestamp,
),
).to.equal(true);
});
});
Expand Down