Skip to content

Commit

Permalink
fix: use _checkRole and remove unused unauth error
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomariscal committed Dec 10, 2024
1 parent 6ee3fb1 commit 72ef93c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
12 changes: 1 addition & 11 deletions l2-contracts/src/ZkCappedMinterV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ contract ZkCappedMinterV2 is AccessControl {
/// @notice Error for when the cap is exceeded.
error ZkCappedMinterV2__CapExceeded(address minter, uint256 amount);

/// @notice Error for when the account is unauthorized.
error ZkCappedMinterV2__Unauthorized(address account);

/// @notice Constructor for a new ZkCappedMinterV2 contract
/// @param _token The token contract where tokens will be minted.
/// @param _admin The address that will be granted the admin role.
Expand All @@ -41,19 +38,12 @@ contract ZkCappedMinterV2 is AccessControl {
/// @param _to The address that will receive the new tokens.
/// @param _amount The quantity of tokens, in raw decimals, that will be created.
function mint(address _to, uint256 _amount) external {
_revertIfUnauthorized();
_checkRole(MINTER_ROLE, msg.sender);
_revertIfCapExceeded(_amount);
minted += _amount;
TOKEN.mint(_to, _amount);
}

/// @notice Reverts if the account is unauthorized.
function _revertIfUnauthorized() internal view {
if (!hasRole(MINTER_ROLE, msg.sender)) {
revert ZkCappedMinterV2__Unauthorized(msg.sender);
}
}

/// @notice Reverts if the amount of new tokens will increase the minted tokens beyond the mint cap.
/// @param _amount The quantity of tokens, in raw decimals, that will checked against the cap.
function _revertIfCapExceeded(uint256 _amount) internal view {
Expand Down
23 changes: 21 additions & 2 deletions l2-contracts/test/ZkCappedMinterV2.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {ZkTokenTest} from "test/utils/ZkTokenTest.sol";
import {IMintableAndDelegatable} from "src/interfaces/IMintableAndDelegatable.sol";
import {ZkCappedMinterV2} from "src/ZkCappedMinterV2.sol";
import {console2} from "forge-std/Test.sol";
import {Strings} from "lib/openzeppelin-contracts/contracts/utils/Strings.sol";

contract ZkCappedMinterV2Test is ZkTokenTest {
function setUp() public virtual override {
Expand Down Expand Up @@ -99,7 +100,16 @@ contract Mint is ZkCappedMinterV2Test {
vm.assume(_nonMinter != address(0));
vm.assume(!cappedMinter.hasRole(MINTER_ROLE, _nonMinter));

vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _nonMinter));
vm.expectRevert(
bytes(
string.concat(
"AccessControl: account ",
Strings.toHexString(uint160(_nonMinter), 20),
" is missing role ",
Strings.toHexString(uint256(MINTER_ROLE))
)
)
);
vm.prank(_nonMinter);
cappedMinter.mint(_nonMinter, _cap);
}
Expand Down Expand Up @@ -137,7 +147,16 @@ contract Mint is ZkCappedMinterV2Test {

ZkCappedMinterV2 cappedMinter = createCappedMinter(_admin, _cap);

vm.expectRevert(abi.encodeWithSelector(ZkCappedMinterV2.ZkCappedMinterV2__Unauthorized.selector, _admin));
vm.expectRevert(
bytes(
string.concat(
"AccessControl: account ",
Strings.toHexString(uint160(_admin), 20),
" is missing role ",
Strings.toHexString(uint256(MINTER_ROLE))
)
)
);
vm.prank(_admin);
cappedMinter.mint(_receiver, _amount);
}
Expand Down

0 comments on commit 72ef93c

Please sign in to comment.