Skip to content

Commit

Permalink
fix: make start time and expiration time uint48
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomariscal committed Dec 16, 2024
1 parent 4b8499f commit 37fe9e1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 57 deletions.
12 changes: 3 additions & 9 deletions l2-contracts/src/ZkCappedMinterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ contract ZkCappedMinterV2 is AccessControl, Pausable {
bool public closed;

/// @notice The timestamp when minting can begin.
uint256 public immutable START_TIME;
uint48 public immutable START_TIME;

/// @notice The timestamp after which minting is no longer allowed (inclusive).
uint256 public immutable EXPIRATION_TIME;
uint48 public immutable EXPIRATION_TIME;

/// @notice Error for when the cap is exceeded.
error ZkCappedMinterV2__CapExceeded(address minter, uint256 amount);
Expand All @@ -52,13 +52,7 @@ contract ZkCappedMinterV2 is AccessControl, Pausable {
/// @param _cap The maximum number of tokens that may be minted by the ZkCappedMinter.
/// @param _startTime The timestamp when minting can begin.
/// @param _expirationTime The timestamp after which minting is no longer allowed (inclusive).
constructor(
IMintableAndDelegatable _token,
address _admin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime
) {
constructor(IMintableAndDelegatable _token, address _admin, uint256 _cap, uint48 _startTime, uint48 _expirationTime) {
if (_startTime > _expirationTime) {
revert ZkCappedMinterV2__InvalidTime();
}
Expand Down
12 changes: 6 additions & 6 deletions l2-contracts/src/ZkCappedMinterV2Factory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ contract ZkCappedMinterV2Factory {
IMintableAndDelegatable token,
address admin,
uint256 cap,
uint256 startTime,
uint256 expirationTime
uint48 startTime,
uint48 expirationTime
);

/// @notice Deploys a new ZkCappedMinterV2 contract using CREATE2.
Expand All @@ -45,8 +45,8 @@ contract ZkCappedMinterV2Factory {
IMintableAndDelegatable _token,
address _admin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) external returns (address minterAddress) {
bytes memory saltArgs = abi.encode(_token, _admin, _cap, _startTime, _expirationTime);
Expand All @@ -69,8 +69,8 @@ contract ZkCappedMinterV2Factory {
IMintableAndDelegatable _token,
address _admin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) external view returns (address addr) {
bytes memory saltArgs = abi.encode(_token, _admin, _cap, _startTime, _expirationTime);
Expand Down
40 changes: 18 additions & 22 deletions l2-contracts/test/ZkCappedMinterV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,35 @@ import {console2} from "forge-std/Test.sol";
contract ZkCappedMinterV2Test is ZkTokenTest {
ZkCappedMinterV2 public cappedMinter;
uint256 constant DEFAULT_CAP = 1000e18;
uint256 DEFAULT_START_TIME;
uint256 DEFAULT_EXPIRATION_TIME;
uint48 DEFAULT_START_TIME;
uint48 DEFAULT_EXPIRATION_TIME;

address cappedMinterAdmin = makeAddr("cappedMinterAdmin");

function setUp() public virtual override {
super.setUp();

DEFAULT_START_TIME = vm.getBlockTimestamp();
DEFAULT_EXPIRATION_TIME = DEFAULT_START_TIME + 100_000_000;
DEFAULT_START_TIME = uint48(vm.getBlockTimestamp());
DEFAULT_EXPIRATION_TIME = uint48(DEFAULT_START_TIME + 100_000_000);

cappedMinter = _createCappedMinter(cappedMinterAdmin, DEFAULT_CAP, DEFAULT_START_TIME, DEFAULT_EXPIRATION_TIME);

vm.prank(admin);
token.grantRole(MINTER_ROLE, address(cappedMinter));
}

function _createCappedMinter(address _admin, uint256 _cap, uint256 _startTime, uint256 _expirationTime)
function _createCappedMinter(address _admin, uint256 _cap, uint48 _startTime, uint48 _expirationTime)
internal
returns (ZkCappedMinterV2)
{
return new ZkCappedMinterV2(IMintableAndDelegatable(address(token)), _admin, _cap, _startTime, _expirationTime);
}

function _boundToValidTimeControls(uint256 _startTime, uint256 _expirationTime)
internal
view
returns (uint256, uint256)
{
function _boundToValidTimeControls(uint48 _startTime, uint48 _expirationTime) internal view returns (uint48, uint48) {
// Using uint32 for time controls to prevent overflows in the ZkToken contract regarding block numbers needing to be
// casted to uint32.
_startTime = bound(_startTime, vm.getBlockTimestamp(), type(uint32).max - 1);
_expirationTime = bound(_expirationTime, _startTime + 1, type(uint32).max);
_startTime = uint48(bound(_startTime, vm.getBlockTimestamp(), type(uint32).max - 1));
_expirationTime = uint48(bound(_expirationTime, _startTime + 1, type(uint32).max));
return (_startTime, _expirationTime);
}

Expand All @@ -67,8 +63,8 @@ contract Constructor is ZkCappedMinterV2Test {
function testFuzz_InitializesTheCappedMinterForAssociationAndFoundation(
address _admin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime
uint48 _startTime,
uint48 _expirationTime
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
vm.warp(_startTime);
Expand All @@ -83,24 +79,24 @@ contract Constructor is ZkCappedMinterV2Test {
function testFuzz_RevertIf_StartTimeAfterExpirationTime(
address _admin,
uint256 _cap,
uint256 _startTime,
uint256 _invalidExpirationTime
uint48 _startTime,
uint48 _invalidExpirationTime
) public {
_startTime = bound(_startTime, 1, type(uint256).max);
uint256 _invalidExpirationTime = bound(_invalidExpirationTime, 0, _startTime - 1);
_startTime = uint48(bound(_startTime, 1, type(uint48).max));
_invalidExpirationTime = uint48(bound(_invalidExpirationTime, 0, _startTime - 1));
vm.expectRevert(ZkCappedMinterV2.ZkCappedMinterV2__InvalidTime.selector);
_createCappedMinter(_admin, _cap, _startTime, _invalidExpirationTime);
}

function testFuzz_RevertIf_StartTimeInPast(address _admin, uint256 _cap, uint256 _startTime, uint256 _expirationTime)
function testFuzz_RevertIf_StartTimeInPast(address _admin, uint256 _cap, uint48 _startTime, uint48 _expirationTime)
public
{
_startTime = bound(_startTime, 1, type(uint256).max);
_startTime = uint48(bound(_startTime, 1, type(uint48).max));
vm.warp(_startTime);

_cap = bound(_cap, 1, DEFAULT_CAP);
uint256 _pastStartTime = _startTime - 1;
_expirationTime = bound(_expirationTime, _pastStartTime + 1, type(uint256).max);
uint48 _pastStartTime = _startTime - 1;
_expirationTime = uint48(bound(_expirationTime, _pastStartTime + 1, type(uint48).max));

vm.expectRevert(ZkCappedMinterV2.ZkCappedMinterV2__InvalidTime.selector);
_createCappedMinter(_admin, _cap, _pastStartTime, _expirationTime);
Expand Down
39 changes: 19 additions & 20 deletions l2-contracts/test/ZkCappedMinterV2Factory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,24 @@ contract ZkCappedMinterV2FactoryTest is ZkTokenTest {
return bound(_cap, 1, MAX_MINT_SUPPLY);
}

function _boundToValidTimeControls(uint256 _startTime, uint256 _expirationTime)
internal
view
returns (uint256, uint256)
{
// Using uint32 for time controls to prevent overflows in the ZkToken contract regarding block numbers needing to be
// casted to uint32.
_startTime = bound(_startTime, vm.getBlockTimestamp() + 1, type(uint32).max - 1);
_expirationTime = bound(_expirationTime, _startTime + 1, type(uint32).max);
return (_startTime, _expirationTime);
function _boundToValidTimeControls(uint48 _startTime, uint48 _expirationTime) internal view returns (uint48, uint48) {
{
// Using uint32 for time controls to prevent overflows in the ZkToken contract regarding block numbers needing to
// be
// casted to uint32.
_startTime = uint48(bound(_startTime, vm.getBlockTimestamp() + 1, type(uint32).max - 1));
_expirationTime = uint48(bound(_expirationTime, _startTime + 1, type(uint32).max));
return (_startTime, _expirationTime);
}
}
}

contract CreateCappedMinter is ZkCappedMinterV2FactoryTest {
function testFuzz_CreatesNewCappedMinter(
address _cappedMinterAdmin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
Expand All @@ -72,8 +71,8 @@ contract CreateCappedMinter is ZkCappedMinterV2FactoryTest {
function testFuzz_EmitsCappedMinterCreatedEvent(
address _cappedMinterAdmin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
Expand Down Expand Up @@ -101,8 +100,8 @@ contract CreateCappedMinter is ZkCappedMinterV2FactoryTest {
function testFuzz_RevertIf_CreatingDuplicateMinter(
address _cappedMinterAdmin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
Expand All @@ -125,8 +124,8 @@ contract GetMinter is ZkCappedMinterV2FactoryTest {
function testFuzz_ReturnsCorrectMinterAddress(
address _cappedMinterAdmin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
Expand All @@ -148,8 +147,8 @@ contract GetMinter is ZkCappedMinterV2FactoryTest {
function testFuzz_GetMinterWithoutDeployment(
address _cappedMinterAdmin,
uint256 _cap,
uint256 _startTime,
uint256 _expirationTime,
uint48 _startTime,
uint48 _expirationTime,
uint256 _saltNonce
) public {
(_startTime, _expirationTime) = _boundToValidTimeControls(_startTime, _expirationTime);
Expand Down

0 comments on commit 37fe9e1

Please sign in to comment.