Skip to content

Commit

Permalink
update(TwabLib): uniform uint types for calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
kamescg committed Oct 5, 2021
1 parent 4c9f635 commit 82dcb57
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
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);
}
}
14 changes: 9 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,17 @@ 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: uint256(
_currentTwab.amount +
_currentBalance *
(_time.checkedSub(_currentTwab.timestamp, _time))
).toUint224(),
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

0 comments on commit 82dcb57

Please sign in to comment.