Skip to content

Commit

Permalink
feat(Rewards): remember the latest batch digest (#51)
Browse files Browse the repository at this point in the history
* feat(Rewards): remember the latest batch digest

* test(Rewards): latestBatchDetails
  • Loading branch information
aliXsed authored Aug 22, 2024
1 parent 8e49cbe commit 460c9e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Rewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ contract Rewards is AccessControl, EIP712 {
* @dev The sequence number of the batch reward.
*/
uint256 public batchSequence;
/**
* @dev Represents the latest batch reward digest.
*/
bytes32 public latestBatchRewardDigest;
/**
* @dev Determines the amount of rewards to be minted for the submitter of batch as a percentage of total rewards in that batch.
* This value indicates the cost overhead of minting rewards that the network is happy to take. Though it is set to 2% by default,
Expand Down Expand Up @@ -148,7 +152,7 @@ contract Rewards is AccessControl, EIP712 {
* @param batchSum The sum of rewards in the batch.
* @param totalRewardsClaimed The total number of rewards claimed so far.
*/
event BatchMinted(uint256 batchSum, uint256 totalRewardsClaimed);
event BatchMinted(uint256 batchSum, uint256 totalRewardsClaimed, bytes32 digest);

/**
* @dev Initializes the contract with the specified parameters.
Expand Down Expand Up @@ -218,7 +222,10 @@ contract Rewards is AccessControl, EIP712 {
function mintBatchReward(BatchReward memory batch, bytes memory signature) external {
_mustBeValidBatchStructure(batch);
_mustBeExpectedBatchSequence(batch.sequence);
_mustBeFromAuthorizedOracle(digestBatchReward(batch), signature);

bytes32 digest = digestBatchReward(batch);

_mustBeFromAuthorizedOracle(digest, signature);

_checkedUpdateQuota();

Expand All @@ -230,12 +237,14 @@ contract Rewards is AccessControl, EIP712 {
// Safe to increment the sequence after checking this is the expected number (no overflow for the age of universe even with 1000 reward claims per second)
batchSequence = batch.sequence + 1;

latestBatchRewardDigest = digest;

for (uint256 i = 0; i < batch.recipients.length; i++) {
nodl.mint(batch.recipients[i], batch.amounts[i]);
}
nodl.mint(msg.sender, submitterRewardAmount);

emit BatchMinted(batchSum, claimed);
emit BatchMinted(batchSum, claimed, digest);
}

/**
Expand Down Expand Up @@ -369,4 +378,12 @@ contract Rewards is AccessControl, EIP712 {
return
_hashTypedDataV4(keccak256(abi.encode(BATCH_REWARD_TYPE_HASH, receipentsHash, amountsHash, batch.sequence)));
}

/**
* @dev Returns the latest batch details.
* @return The next batch sequence and the latest digest of a successfully submitted batch which must have been for batchSequence - 1.
*/
function latestBatchDetails() external view returns (uint256, bytes32) {
return (batchSequence, latestBatchRewardDigest);
}
}
8 changes: 8 additions & 0 deletions test/Rewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ contract RewardsTest is Test {
assertEq(rewards.sequences(recipients[0]), 0);
assertEq(rewards.sequences(recipients[1]), 0);
assertEq(rewards.batchSequence(), 1);

(uint256 currentSeq, bytes32 batchHash) = rewards.latestBatchDetails();
assertEq(currentSeq, 1);
assertEq(batchHash, rewards.digestBatchReward(rewardsBatch));
}

function test_gasUsed() public {
Expand Down Expand Up @@ -284,6 +288,10 @@ contract RewardsTest is Test {

vm.expectRevert();
rewards.mintBatchReward(Rewards.BatchReward(recipients, amounts, 0), signature);

(uint256 currentSeq, bytes32 batchHash) = rewards.latestBatchDetails();
assertEq(currentSeq, 0);
assertEq(batchHash, bytes32(0));
}

function test_rewardsClaimedResetsOnNewPeriod() public {
Expand Down

0 comments on commit 460c9e6

Please sign in to comment.