constructor(
uint256 _initialClaimFee,
uint256 _gracePeriod,
uint256 _feeIncreasePercentage,
uint256 _platformFeePercentage
) Ownable(msg.sender) {
require(_feeIncreasePercentage <= 100, "Game: Fee increase percentage must be 0-100.");
require(_platformFeePercentage <= 100, "Game: Platform fee percentage must be 0-100.");
}
The following tests demonstrate that the constructor accepts problematic extreme values:
pragma solidity ^0.8.20;
import {Test} from "forge-std/Test.sol";
import {Game} from "../src/Game.sol";
contract GameConstructorValidationTest is Test {
address public deployer;
uint256 public constant INITIAL_CLAIM_FEE = 0.1 ether;
uint256 public constant GRACE_PERIOD = 1 days;
function setUp() public {
deployer = makeAddr("deployer");
vm.deal(deployer, 10 ether);
}
function testConstructorAllows100PercentFeeIncrease() public {
vm.startPrank(deployer);
Game game = new Game(
INITIAL_CLAIM_FEE,
GRACE_PERIOD,
100,
5
);
vm.stopPrank();
assertEq(game.feeIncreasePercentage(), 100, "Fee increase should be 100%");
}
function testConstructorAllows100PercentPlatformFee() public {
vm.startPrank(deployer);
Game game = new Game(
INITIAL_CLAIM_FEE,
GRACE_PERIOD,
10,
100
);
vm.stopPrank();
assertEq(game.platformFeePercentage(), 100, "Platform fee should be 100%");
}
function testConstructorShouldRejectExtremeValues() public {
vm.startPrank(deployer);
vm.expectRevert();
Game game1 = new Game(INITIAL_CLAIM_FEE, GRACE_PERIOD, 101, 5);
vm.expectRevert();
Game game2 = new Game(INITIAL_CLAIM_FEE, GRACE_PERIOD, 10, 101);
vm.stopPrank();
}
function testReasonableParametersWork() public {
vm.startPrank(deployer);
Game game = new Game(
INITIAL_CLAIM_FEE,
GRACE_PERIOD,
20,
10
);
vm.stopPrank();
assertEq(game.feeIncreasePercentage(), 20, "Should accept reasonable fee increase");
assertEq(game.platformFeePercentage(), 10, "Should accept reasonable platform fee");
}
}
Add proper range validation for percentage parameters to ensure reasonable game economics:
- require(_feeIncreasePercentage <= 100, "Game: Fee increase percentage must be 0-100.");
- require(_platformFeePercentage <= 100, "Game: Platform fee percentage must be 0-100.");
+ require(_feeIncreasePercentage <= 50, "Game: Fee increase percentage must be 0-50.");
+ require(_platformFeePercentage <= 30, "Game: Platform fee percentage must be 0-30.");