Move the maxSupply!=zero check to init function and add test

This commit is contained in:
stubbsta 2025-09-17 15:19:06 +02:00
parent 3b409b4338
commit e1536282b3
No known key found for this signature in database
3 changed files with 18 additions and 7 deletions

View File

@ -14,9 +14,6 @@ contract DeployTokenWithProxy is BaseScript {
// Read desired max supply from env or use default
uint256 defaultMaxSupply = vm.envOr({ name: "MAX_SUPPLY", defaultValue: uint256(1_000_000 * 10 ** 18) });
// Validate value is sensible
require(defaultMaxSupply > 0, "MAX_SUPPLY must be > 0");
// Deploy the initial implementation
address implementation = address(new TestStableToken());

View File

@ -15,6 +15,7 @@ error AccountAlreadyMinter();
error AccountNotInMinterList();
error InsufficientETH();
error ExceedsMaxSupply();
error InvalidMaxSupply(uint256 supplied);
contract TestStableToken is
Initializable,
@ -45,6 +46,7 @@ contract TestStableToken is
__ERC20Permit_init("TestStableToken");
__Ownable_init();
__UUPSUpgradeable_init();
if (_maxSupply == 0) revert InvalidMaxSupply(_maxSupply);
maxSupply = _maxSupply;
}
@ -97,9 +99,6 @@ contract TestStableTokenFactory is BaseScript {
// Read desired max supply from env or use default
uint256 defaultMaxSupply = vm.envOr({ name: "MAX_SUPPLY", defaultValue: uint256(1_000_000 * 10 ** 18) });
// Validate value is sensible
if (defaultMaxSupply == 0) revert("MAX_SUPPLY must be > 0");
// Deploy the implementation
address implementation = address(new TestStableToken());

View File

@ -8,7 +8,8 @@ import {
AccountAlreadyMinter,
AccountNotInMinterList,
InsufficientETH,
ExceedsMaxSupply
ExceedsMaxSupply,
InvalidMaxSupply
} from "./TestStableToken.sol";
import { ERC1967Proxy } from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import { DeployTokenWithProxy } from "../script/DeployTokenWithProxy.s.sol";
@ -336,4 +337,18 @@ contract TestStableTokenTest is Test {
vm.expectRevert("Ownable: caller is not the owner");
token.setMaxSupply(newMaxSupply);
}
function test__InitializeZeroReverts() external {
// Deploy implementation directly
TestStableToken implementation = new TestStableToken();
// Build initializer calldata with zero
bytes memory initData = abi.encodeCall(TestStableToken.initialize, (uint256(0)));
// Expect the InvalidMaxSupply reversion including the supplied value
vm.expectRevert(abi.encodeWithSelector(InvalidMaxSupply.selector, uint256(0)));
// Attempt to deploy proxy with initData - should revert
new ERC1967Proxy(address(implementation), initData);
}
}