Skip to content

Commit

Permalink
fix(Rewards): correct range check (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliXsed authored Aug 20, 2024
1 parent c90ba67 commit 1b1313c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
4 changes: 2 additions & 2 deletions script/DeployRewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ contract DeployRewards is Script {
address internal oracleAddress;
uint256 internal rewardQuotaPerPeriod;
uint256 internal rewardPeriod;
uint256 internal batchSubmitterIncentive; // in percentage of total rewards in a batch. 1 = 1%
uint8 internal batchSubmitterIncentive; // in percentage of total rewards in a batch. 1 = 1%

function setUp() public {
nodlAddress = vm.envOr("N_TOKEN_ADDR", address(0));
oracleAddress = vm.envAddress("N_REWARDS_ORACLE_ADDR");
rewardQuotaPerPeriod = vm.envUint("N_REWARDS_QUOTA");
rewardPeriod = vm.envUint("N_REWARDS_PERIOD");
batchSubmitterIncentive = vm.envUint("N_REWARDS_SUBMITTER_INCENTIVE");
batchSubmitterIncentive = uint8(vm.envUint("N_REWARDS_SUBMITTER_INCENTIVE"));
}

function run() public {
Expand Down
18 changes: 7 additions & 11 deletions src/Rewards.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ contract Rewards is AccessControl, EIP712 {
* This value indicates the cost overhead of minting rewards that the network is happy to take. Though it is set to 2% by default,
* the governance can change it to ensure the network is sustainable.
*/
uint256 public batchSubmitterRewardPercentage;
uint8 public batchSubmitterRewardPercentage;

/**
* @dev Error when the reward quota is exceeded.
Expand Down Expand Up @@ -157,13 +157,9 @@ contract Rewards is AccessControl, EIP712 {
* @param initialPeriod Initial reward period.
* @param oracleAddress Address of the authorized oracle.
*/
constructor(
NODL token,
uint256 initialQuota,
uint256 initialPeriod,
address oracleAddress,
uint256 rewardPercentage
) EIP712(SIGNING_DOMAIN, SIGNATURE_VERSION) {
constructor(NODL token, uint256 initialQuota, uint256 initialPeriod, address oracleAddress, uint8 rewardPercentage)
EIP712(SIGNING_DOMAIN, SIGNATURE_VERSION)
{
// This is to avoid the ongoinb overhead of safe math operations
if (initialPeriod == 0) {
revert ZeroPeriod();
Expand All @@ -173,7 +169,7 @@ contract Rewards is AccessControl, EIP712 {
revert TooLongPeriod();
}

_mustBeLessThan100(batchSubmitterRewardPercentage);
_mustBeLessThan100(rewardPercentage);

_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);

Expand Down Expand Up @@ -249,7 +245,7 @@ contract Rewards is AccessControl, EIP712 {
* - Caller must have the DEFAULT_ADMIN_ROLE.
* - The new reward percentage must be less than 100.
*/
function setBatchSubmitterRewardPercentage(uint256 newPercentage) external {
function setBatchSubmitterRewardPercentage(uint8 newPercentage) external {
_checkRole(DEFAULT_ADMIN_ROLE);
_mustBeLessThan100(newPercentage);
batchSubmitterRewardPercentage = newPercentage;
Expand Down Expand Up @@ -290,7 +286,7 @@ contract Rewards is AccessControl, EIP712 {
* @param percent The percentage value to check.
* @dev Throws an exception if the value is greater than 100.
*/
function _mustBeLessThan100(uint256 percent) internal pure {
function _mustBeLessThan100(uint8 percent) internal pure {
if (percent > 100) {
revert OutOfRangeValue();
}
Expand Down
5 changes: 5 additions & 0 deletions test/Rewards.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,11 @@ contract RewardsTest is Test {
rewards.setBatchSubmitterRewardPercentage(101);
}

function test_deployRewardsWithInvalidSubmitterRewardPercentage() public {
vm.expectRevert(Rewards.OutOfRangeValue.selector);
new Rewards(nodlToken, 1000, RENEWAL_PERIOD, vm.addr(1), 101);
}

function test_changingSubmitterRewardPercentageIsEffective() public {
address alice = address(2);
rewards.grantRole(rewards.DEFAULT_ADMIN_ROLE(), alice);
Expand Down

0 comments on commit 1b1313c

Please sign in to comment.