Skip to content

Commit

Permalink
fix(TwabRewards): cast timestamps to uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Jan 11, 2022
1 parent 0fc3538 commit cc4f974
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
26 changes: 13 additions & 13 deletions contracts/TwabRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ contract TwabRewards is ITwabRewards {
*/
event RewardsClaimed(
uint256 indexed promotionId,
uint256[] epochIds,
uint8[] epochIds,
address indexed user,
uint256 amount
);
Expand Down Expand Up @@ -180,7 +180,7 @@ contract TwabRewards is ITwabRewards {
function claimRewards(
address _user,
uint256 _promotionId,
uint256[] calldata _epochIds
uint8[] calldata _epochIds
) external override returns (uint256) {
Promotion memory _promotion = _getPromotion(_promotionId);

Expand All @@ -189,7 +189,7 @@ contract TwabRewards is ITwabRewards {
uint256 _epochIdsLength = _epochIds.length;

for (uint256 index = 0; index < _epochIdsLength; index++) {
uint256 _epochId = _epochIds[index];
uint8 _epochId = _epochIds[index];

require(!_isClaimedEpoch(_userClaimedEpochs, _epochId), "TwabRewards/rewards-claimed");

Expand Down Expand Up @@ -225,7 +225,7 @@ contract TwabRewards is ITwabRewards {
function getRewardsAmount(
address _user,
uint256 _promotionId,
uint256[] calldata _epochIds
uint8[] calldata _epochIds
) external view override returns (uint256[] memory) {
Promotion memory _promotion = _getPromotion(_promotionId);

Expand Down Expand Up @@ -265,7 +265,7 @@ contract TwabRewards is ITwabRewards {
@notice Allow a promotion to be created or extended only by a positive number of epochs.
@param _numberOfEpochs Number of epochs to check
*/
function _requireNumberOfEpochs(uint8 _numberOfEpochs) internal view {
function _requireNumberOfEpochs(uint8 _numberOfEpochs) internal pure {
require(_numberOfEpochs > 0, "TwabRewards/epochs-not-zero");
}

Expand Down Expand Up @@ -314,11 +314,11 @@ contract TwabRewards is ITwabRewards {
function _calculateRewardAmount(
address _user,
Promotion memory _promotion,
uint256 _epochId
uint8 _epochId
) internal view returns (uint256) {
uint256 _epochDuration = _promotion.epochDuration;
uint256 _epochStartTimestamp = _promotion.startTimestamp + (_epochDuration * _epochId);
uint256 _epochEndTimestamp = _epochStartTimestamp + _epochDuration;
uint32 _epochDuration = _promotion.epochDuration;
uint64 _epochStartTimestamp = _promotion.startTimestamp + (_epochDuration * _epochId);
uint64 _epochEndTimestamp = _epochStartTimestamp + _epochDuration;

require(block.timestamp >= _epochEndTimestamp, "TwabRewards/epoch-not-over");

Expand All @@ -331,10 +331,10 @@ contract TwabRewards is ITwabRewards {
);

uint64[] memory _epochStartTimestamps = new uint64[](1);
_epochStartTimestamps[0] = uint64(_epochStartTimestamp);
_epochStartTimestamps[0] = _epochStartTimestamp;

uint64[] memory _epochEndTimestamps = new uint64[](1);
_epochEndTimestamps[0] = uint64(_epochEndTimestamp);
_epochEndTimestamps[0] = _epochEndTimestamp;

uint256[] memory _averageTotalSupplies = _ticket.getAverageTotalSuppliesBetween(
_epochStartTimestamps,
Expand Down Expand Up @@ -372,7 +372,7 @@ contract TwabRewards is ITwabRewards {
@param _epochId Id of the epoch to set the boolean for
@return Tightly packed epoch ids with the newly boolean value set
*/
function _updateClaimedEpoch(uint256 _userClaimedEpochs, uint256 _epochId)
function _updateClaimedEpoch(uint256 _userClaimedEpochs, uint8 _epochId)
internal
pure
returns (uint256)
Expand All @@ -393,7 +393,7 @@ contract TwabRewards is ITwabRewards {
@param _epochId Epoch id to check
@return true if the rewards have already been claimed for the given epoch, false otherwise
*/
function _isClaimedEpoch(uint256 _userClaimedEpochs, uint256 _epochId)
function _isClaimedEpoch(uint256 _userClaimedEpochs, uint8 _epochId)
internal
pure
returns (bool)
Expand Down
4 changes: 2 additions & 2 deletions contracts/interfaces/ITwabRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ interface ITwabRewards {
function claimRewards(
address _user,
uint256 _promotionId,
uint256[] calldata _epochIds
uint8[] calldata _epochIds
) external returns (uint256);

/**
Expand Down Expand Up @@ -121,6 +121,6 @@ interface ITwabRewards {
function getRewardsAmount(
address _user,
uint256 _promotionId,
uint256[] calldata _epochIds
uint8[] calldata _epochIds
) external view returns (uint256[] memory);
}
2 changes: 1 addition & 1 deletion contracts/test/TwabRewardsHarness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract TwabRewardsHarness is TwabRewards {
return _requireTicket(_ticket);
}

function isClaimedEpoch(uint256 _userClaimedEpochs, uint256 _epochId)
function isClaimedEpoch(uint256 _userClaimedEpochs, uint8 _epochId)
external
pure
returns (bool)
Expand Down
14 changes: 7 additions & 7 deletions test/TwabRewards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ describe('TwabRewards', () => {
it('should cancel promotion and still allow users to claim their rewards', async () => {
const promotionId = 1;
const epochNumber = 6;
const epochIds = ['0', '1', '2', '3', '4', '5'];
const epochIds = [0, 1, 2, 3, 4, 5];

const wallet2Amount = toWei('750');
const wallet3Amount = toWei('250');
Expand Down Expand Up @@ -442,7 +442,7 @@ describe('TwabRewards', () => {
describe('getRewardsAmount()', async () => {
it('should get rewards amount for one or more epochs', async () => {
const promotionId = 1;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];

const wallet2Amount = toWei('750');
const wallet3Amount = toWei('250');
Expand Down Expand Up @@ -482,7 +482,7 @@ describe('TwabRewards', () => {

it('should decrease rewards amount if user delegate in the middle of an epoch', async () => {
const promotionId = 1;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];
const halfEpoch = epochDuration / 2;

const wallet2Amount = toWei('750');
Expand Down Expand Up @@ -588,7 +588,7 @@ describe('TwabRewards', () => {
it('should claim rewards for one or more epochs', async () => {
const promotionId = 1;
const epochNumber = 3;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];

const wallet2Amount = toWei('750');
const wallet3Amount = toWei('250');
Expand Down Expand Up @@ -631,7 +631,7 @@ describe('TwabRewards', () => {
it('should decrease rewards amount claimed if user delegate in the middle of an epoch', async () => {
const promotionId = 1;
const epochNumber = 3;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];
const halfEpoch = epochDuration / 2;

const wallet2Amount = toWei('750');
Expand Down Expand Up @@ -685,7 +685,7 @@ describe('TwabRewards', () => {

it('should claim 0 rewards if user has no tickets delegated to him', async () => {
const promotionId = 1;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];
const wallet2Amount = toWei('750');
const zeroAmount = toWei('0');

Expand All @@ -703,7 +703,7 @@ describe('TwabRewards', () => {

it('should return 0 if ticket average total supplies is 0', async () => {
const promotionId = 1;
const epochIds = ['0', '1', '2'];
const epochIds = [0, 1, 2];
const zeroAmount = toWei('0');

await createPromotion();
Expand Down

0 comments on commit cc4f974

Please sign in to comment.