forked from zksync-association/zk-governance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: copy capped minter factory tests for v2
- Loading branch information
1 parent
0c54fe2
commit ca19636
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.24; | ||
|
||
import {ZkTokenTest} from "test/utils/ZkTokenTest.sol"; | ||
import {ZkCappedMinterV2Factory} from "src/ZkCappedMinterV2Factory.sol"; | ||
import {ZkCappedMinterV2} from "src/ZkCappedMinterV2.sol"; | ||
import {IMintableAndDelegatable} from "src/interfaces/IMintableAndDelegatable.sol"; | ||
import {Create2} from "@openzeppelin/contracts/utils/Create2.sol"; | ||
import {console2} from "forge-std/console2.sol"; | ||
import {stdJson} from "forge-std/StdJson.sol"; | ||
|
||
contract ZkCappedMinterV2FactoryTest is ZkTokenTest { | ||
bytes32 bytecodeHash; | ||
ZkCappedMinterV2Factory factory; | ||
|
||
function setUp() public virtual override { | ||
super.setUp(); | ||
|
||
// Read the bytecode hash from the JSON file | ||
string memory root = vm.projectRoot(); | ||
string memory path = string.concat(root, "/zkout/ZkCappedMinterV2.sol/ZkCappedMinterV2.json"); | ||
string memory json = vm.readFile(path); | ||
bytecodeHash = bytes32(stdJson.readBytes(json, ".hash")); | ||
|
||
factory = new ZkCappedMinterV2Factory(bytecodeHash); | ||
} | ||
|
||
function _assumeValidAddress(address _addr) internal view { | ||
vm.assume(_addr != address(0) && _addr != address(factory)); | ||
} | ||
|
||
function _boundToReasonableCap(uint256 _cap) internal view returns (uint256) { | ||
return bound(_cap, 1, MAX_MINT_SUPPLY); | ||
} | ||
} | ||
|
||
contract CreateCappedMinter is ZkCappedMinterV2FactoryTest { | ||
function testFuzz_CreatesNewCappedMinter(address _cappedMinterAdmin, uint256 _cap, uint256 _saltNonce) public { | ||
_assumeValidAddress(_cappedMinterAdmin); | ||
_cap = _boundToReasonableCap(_cap); | ||
|
||
address minterAddress = | ||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
ZkCappedMinterV2 minter = ZkCappedMinterV2(minterAddress); | ||
assertEq(address(minter.TOKEN()), address(token)); | ||
assertEq(minter.ADMIN(), _cappedMinterAdmin); | ||
assertEq(minter.CAP(), _cap); | ||
} | ||
|
||
function testFuzz_EmitsCappedMinterCreatedEvent(address _cappedMinterAdmin, uint256 _cap, uint256 _saltNonce) public { | ||
_assumeValidAddress(_cappedMinterAdmin); | ||
_cap = _boundToReasonableCap(_cap); | ||
|
||
vm.expectEmit(); | ||
emit ZkCappedMinterV2Factory.CappedMinterV2Created( | ||
factory.getMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce), | ||
IMintableAndDelegatable(address(token)), | ||
_cappedMinterAdmin, | ||
_cap | ||
); | ||
|
||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
} | ||
|
||
function testFuzz_RevertIf_CreatingDuplicateMinter(address _cappedMinterAdmin, uint256 _cap, uint256 _saltNonce) | ||
public | ||
{ | ||
_assumeValidAddress(_cappedMinterAdmin); | ||
_cap = _boundToReasonableCap(_cap); | ||
|
||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
vm.expectRevert("Code hash is non-zero"); | ||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
} | ||
} | ||
|
||
contract GetMinter is ZkCappedMinterV2FactoryTest { | ||
function testFuzz_ReturnsCorrectMinterAddress(address _cappedMinterAdmin, uint256 _cap, uint256 _saltNonce) public { | ||
_assumeValidAddress(_cappedMinterAdmin); | ||
_cap = _boundToReasonableCap(_cap); | ||
|
||
address expectedMinterAddress = | ||
factory.getMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
address minterAddress = | ||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
assertEq(minterAddress, expectedMinterAddress); | ||
} | ||
|
||
function testFuzz_GetMinterWithoutDeployment(address _cappedMinterAdmin, uint256 _cap, uint256 _saltNonce) public { | ||
_assumeValidAddress(_cappedMinterAdmin); | ||
_cap = _boundToReasonableCap(_cap); | ||
|
||
address expectedMinterAddress = | ||
factory.getMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
uint256 codeSize; | ||
assembly { | ||
codeSize := extcodesize(expectedMinterAddress) | ||
} | ||
assertEq(codeSize, 0); | ||
|
||
address minterAddress = | ||
factory.createCappedMinter(IMintableAndDelegatable(address(token)), _cappedMinterAdmin, _cap, _saltNonce); | ||
|
||
assembly { | ||
codeSize := extcodesize(expectedMinterAddress) | ||
} | ||
assertGt(codeSize, 0); | ||
assertEq(minterAddress, expectedMinterAddress); | ||
} | ||
} |