Skip to content

Commit

Permalink
feat: update tests to handle new auth functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomariscal committed Dec 4, 2024
1 parent 9f8ea6e commit 2aecdce
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
53 changes: 40 additions & 13 deletions l2-contracts/test/ZkCappedMinterV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ contract Constructor is ZkCappedMinterV2Test {
_cap = bound(_cap, 0, MAX_MINT_SUPPLY);
ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap);
assertEq(address(cappedMinter.TOKEN()), address(token));
assertEq(cappedMinter.ADMIN(), _cappedMinterAdmin);
assertEq(cappedMinter.hasRole(DEFAULT_ADMIN_ROLE, _cappedMinterAdmin), true);
assertEq(cappedMinter.CAP(), _cap);
}
}

contract Mint is ZkCappedMinterV2Test {
function testFuzz_MintsNewTokensWhenTheAmountRequestedIsBelowTheCap(
address _cappedMinterAdmin,
address _minter,
address _receiver,
uint256 _cap,
uint256 _amount
Expand All @@ -42,14 +43,21 @@ contract Mint is ZkCappedMinterV2Test {
_amount = bound(_amount, 1, MAX_MINT_SUPPLY);
vm.assume(_cap > _amount);
vm.assume(_receiver != address(0) && _receiver != initMintReceiver);
vm.assume(_minter != address(0));

ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap);

vm.prank(_cappedMinterAdmin);
cappedMinter.grantRole(MINTER_ROLE, _minter);

vm.prank(_minter);
cappedMinter.mint(_receiver, _amount);
assertEq(token.balanceOf(_receiver), _amount);
}

function testFuzz_MintsNewTokensInSuccessionToDifferentAccountsWhileRemainingBelowCap(
address _cappedMinterAdmin,
address _minter,
address _receiver1,
address _receiver2,
uint256 _cap,
Expand All @@ -63,38 +71,57 @@ contract Mint is ZkCappedMinterV2Test {
vm.assume(_receiver1 != address(0) && _receiver1 != initMintReceiver);
vm.assume(_receiver2 != address(0) && _receiver2 != initMintReceiver);
vm.assume(_receiver1 != _receiver2);
vm.assume(_minter != address(0));

ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap);
vm.startPrank(_cappedMinterAdmin);

vm.prank(_cappedMinterAdmin);
cappedMinter.grantRole(MINTER_ROLE, _minter);

vm.startPrank(_minter);
cappedMinter.mint(_receiver1, _amount1);
cappedMinter.mint(_receiver2, _amount2);
vm.stopPrank();

assertEq(token.balanceOf(_receiver1), _amount1);
assertEq(token.balanceOf(_receiver2), _amount2);
}

function testFuzz_RevertIf_MintAttemptedByNonAdmin(address _cappedMinterAdmin, uint256 _cap, address _nonAdmin)
function testFuzz_RevertIf_MintAttemptedByNonMinter(address _cappedMinterAdmin, address _nonMinter, uint256 _cap)
public
{
_cap = bound(_cap, 0, MAX_MINT_SUPPLY);
vm.assume(_nonAdmin != _cappedMinterAdmin);

ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap);
vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _nonAdmin));
vm.startPrank(_nonAdmin);
cappedMinter.mint(_nonAdmin, _cap);

vm.assume(_nonMinter != address(0));
vm.assume(!cappedMinter.hasRole(MINTER_ROLE, _nonMinter));

vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _nonMinter));
vm.prank(_nonMinter);
cappedMinter.mint(_nonMinter, _cap);
}

function testFuzz_RevertIf_CapExceededOnMint(address _cappedMinterAdmin, address _receiver, uint256 _cap) public {
function testFuzz_RevertIf_CapExceededOnMint(
address _cappedMinterAdmin,
address _minter,
address _receiver,
uint256 _cap
) public {
_cap = bound(_cap, 4, MAX_MINT_SUPPLY);
vm.assume(_receiver != address(0) && _receiver != initMintReceiver);
vm.assume(_minter != address(0));

ZkCappedMinterV2 cappedMinter = createCappedMinter(_cappedMinterAdmin, _cap);

vm.prank(_cappedMinterAdmin);
cappedMinter.grantRole(MINTER_ROLE, _minter);

vm.prank(_minter);
cappedMinter.mint(_receiver, _cap);
assertEq(token.balanceOf(_receiver), _cap);
vm.expectRevert(
abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__CapExceeded.selector, _cappedMinterAdmin, _cap)
);
vm.prank(_cappedMinterAdmin);

vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__CapExceeded.selector, _minter, _cap));
vm.prank(_minter);
cappedMinter.mint(_receiver, _cap);
}
}
2 changes: 1 addition & 1 deletion l2-contracts/test/ZkCappedMinterV2Factory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ contract CreateCappedMinter is ZkCappedMinterV2FactoryTest {

ZkCappedMinterV2 minter = ZkCappedMinterV2(minterAddress);
assertEq(address(minter.TOKEN()), address(token));
assertEq(minter.ADMIN(), _cappedMinterAdmin);
assertEq(minter.hasRole(DEFAULT_ADMIN_ROLE, _cappedMinterAdmin), true);
assertEq(minter.CAP(), _cap);
}

Expand Down

0 comments on commit 2aecdce

Please sign in to comment.