Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: capped minter v2 close #12

Merged
merged 53 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
9f8ea6e
feat: inherit from AccessControl and update accordingly
marcomariscal Dec 4, 2024
2aecdce
feat: update tests to handle new auth functionality
marcomariscal Dec 4, 2024
e293e2d
feat: add minter role
marcomariscal Dec 4, 2024
b7f4222
feat: test admin can't mint by default
marcomariscal Dec 4, 2024
726d0bf
fix: name
marcomariscal Dec 4, 2024
31d626f
fix: structure
marcomariscal Dec 4, 2024
fb88648
feat: basic pause functionality
marcomariscal Dec 3, 2024
946dc79
feat: pause/unpause tests
marcomariscal Dec 3, 2024
d9b3dba
fix: assume cap is not zero
marcomariscal Dec 3, 2024
1cfbef7
fix: check for pauser role
marcomariscal Dec 3, 2024
3379852
fix: separate role errors
marcomariscal Dec 4, 2024
004c789
feat: _revertIfNotPauser
marcomariscal Dec 4, 2024
4f29f5c
feat: close with tests
marcomariscal Dec 4, 2024
edf05f6
fix: use _revertIfNotPauser
marcomariscal Dec 4, 2024
bceb0f7
chore: better comment for `close`
marcomariscal Dec 4, 2024
e680c31
chore: comment
marcomariscal Dec 10, 2024
6ee3fb1
feat: _grantMinterRole helper
marcomariscal Dec 10, 2024
72ef93c
fix: use _checkRole and remove unused unauth error
marcomariscal Dec 10, 2024
dd15cac
fix: assume cap
marcomariscal Dec 10, 2024
61e7247
fix: toml
marcomariscal Dec 10, 2024
1295f88
chore: format
marcomariscal Dec 10, 2024
54315f5
fix: import
marcomariscal Dec 10, 2024
2558249
fix: compile
marcomariscal Dec 10, 2024
857ebc6
chore: name
marcomariscal Dec 10, 2024
479ef77
fix: import
marcomariscal Dec 10, 2024
007784b
fix: remove unnecessary
marcomariscal Dec 10, 2024
bfbac0e
feat: _revertIfClosed
marcomariscal Dec 10, 2024
8a5f057
Merge branch 'feat/capped-minter-v2-minter-role' into feat/capped-min…
marcomariscal Dec 10, 2024
493c1a6
feat: _formatAccessControlError
marcomariscal Dec 10, 2024
7d84ebe
Merge branch 'feat/capped-minter-v2-pause' into feat/capped-minter-v2…
marcomariscal Dec 10, 2024
6bb7d1a
fix: formatted error
marcomariscal Dec 10, 2024
fca3ffb
fix: remove assume cap greater than 0 in favor of bound
marcomariscal Dec 11, 2024
93e4af3
fix: remove unnecessary assumptions
marcomariscal Dec 11, 2024
225ba92
fix: use grant minter role
marcomariscal Dec 11, 2024
1942a64
fix: remove redundant balance check
marcomariscal Dec 11, 2024
77acf62
feat: refactor to set up capped minter in setup func
marcomariscal Dec 11, 2024
ecb275d
feat: testFuzz_CorrectlyPausesMintsWhenTogglingPause
marcomariscal Dec 11, 2024
43fa666
fix: move func
marcomariscal Dec 11, 2024
7da3772
Merge branch 'feat/capped-minter-v2-pause' into feat/capped-minter-v2…
marcomariscal Dec 12, 2024
57e1f4f
fix: only admin can close, fix mint to zero addr receiver, and format…
marcomariscal Dec 12, 2024
ef8ca96
fix: remove unnecessary revert if closed when unpausing
marcomariscal Dec 12, 2024
3fc7d3a
Update l2-contracts/src/ZkCappedMinterV2.sol
marcomariscal Dec 12, 2024
bb6854b
fix: test
marcomariscal Dec 12, 2024
0b17e88
fix: remove unnecessary pause from close
marcomariscal Dec 12, 2024
7631a29
fix: use global minter
marcomariscal Dec 12, 2024
246efdc
feat: test_CorrectlyChangesClosedVarWhenCalledByAdmin
marcomariscal Dec 16, 2024
91c0ca8
fix: foundry toml revert
marcomariscal Dec 16, 2024
aa47d27
Merge branch 'feat/capped-minter-v2' into feat/capped-minter-v2-close
marcomariscal Dec 16, 2024
48c9ca6
fix: handle check balances before and after
marcomariscal Dec 16, 2024
fe1cac7
fix: assume not capped minter admin
marcomariscal Dec 16, 2024
c7bef58
fix: handle balance check before in test
marcomariscal Dec 17, 2024
010e4c5
fix: rename
marcomariscal Dec 17, 2024
960a751
chore: remove unused
marcomariscal Dec 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion l2-contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[profile.default]
evm_version = "paris"
fs_permissions = [{ access = "read", path = "./zkout" }]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fuzz = { runs = 50 }
optimizer = true
optimizer_runs = 10_000_000
Expand All @@ -8,10 +9,10 @@
"@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
"@openzeppelin/foundry-upgrades/=lib/openzeppelin-foundry-upgrades/src/",
"@murky/=lib/murky/",
"src/=src/",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be removed

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

]
solc_version = "0.8.24"
verbosity = 3
fs_permissions = [{ access = "read", path = "./zkout" }]

[profile.ci]
fuzz = { runs = 5000 }
Expand Down
63 changes: 46 additions & 17 deletions l2-contracts/src/ZkCappedMinterV2.sol
Original file line number Diff line number Diff line change
@@ -1,63 +1,92 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
import {Pausable} from "@openzeppelin/contracts/security/Pausable.sol";
import {IMintableAndDelegatable} from "src/interfaces/IMintableAndDelegatable.sol";

/// @title ZkCappedMinterV2
/// @author [ScopeLift](https://scopelift.co)
/// @notice A contract to allow a permissioned entity to mint ZK tokens up to a given amount (the cap).
/// @custom:security-contact [email protected]
contract ZkCappedMinterV2 {
contract ZkCappedMinterV2 is AccessControl, Pausable {
/// @notice The contract where the tokens will be minted by an authorized minter.
IMintableAndDelegatable public immutable TOKEN;

/// @notice The address that is allowed to mint tokens.
address public immutable ADMIN;

/// @notice The maximum number of tokens that may be minted by the ZkCappedMinter.
uint256 public immutable CAP;

/// @notice The cumulative number of tokens that have been minted by the ZkCappedMinter.
uint256 public minted = 0;

bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");

/// @notice Whether the contract has been permanently closed.
bool public closed;

/// @notice Error for when the cap is exceeded.
error ZkCappedMinterV2__CapExceeded(address minter, uint256 amount);

/// @notice Error for when the caller is not the admin.
error ZkCappedMinterV2__Unauthorized(address account);
/// @notice Thrown when a mint action is taken while the contract is closed.
error ZkCappedMinterV2__ContractClosed();

/// @notice Constructor for a new ZkCappedMinter contract
/// @notice Constructor for a new ZkCappedMinterV2 contract
/// @param _token The token contract where tokens will be minted.
/// @param _admin The address that is allowed to mint tokens.
/// @param _admin The address that will be granted the admin role.
/// @param _cap The maximum number of tokens that may be minted by the ZkCappedMinter.
constructor(IMintableAndDelegatable _token, address _admin, uint256 _cap) {
TOKEN = _token;
ADMIN = _admin;
CAP = _cap;

_grantRole(DEFAULT_ADMIN_ROLE, _admin);
_grantRole(PAUSER_ROLE, _admin);
}

/// @notice Pauses token minting
function pause() external {
_checkRole(PAUSER_ROLE, msg.sender);
_pause();
}

/// @notice Unpauses token minting
function unpause() external {
_checkRole(PAUSER_ROLE, msg.sender);
_unpause();
}

/// @notice Mints a given amount of tokens to a given address, so long as the cap is not exceeded.
/// @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();
_revertIfClosed();
_requireNotPaused();
_checkRole(MINTER_ROLE, msg.sender);
_revertIfCapExceeded(_amount);
minted += _amount;
TOKEN.mint(_to, _amount);
}

/// @notice Reverts if msg.sender is not the contract admin.
function _revertIfUnauthorized() internal view {
if (msg.sender != ADMIN) {
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 {
if (minted + _amount > CAP) {
revert ZkCappedMinterV2__CapExceeded(msg.sender, _amount);
}
}

/// @notice Reverts if the contract is closed.
function _revertIfClosed() internal view {
if (closed) {
revert ZkCappedMinterV2__ContractClosed();
}
}

/// @notice Permanently closes the contract, preventing any future minting.
/// @dev Once closed, the contract cannot be reopened and all minting operations will be permanently blocked.
/// @dev Only callable by the admin.
function close() external {
_checkRole(DEFAULT_ADMIN_ROLE, msg.sender);
closed = true;
}
}
2 changes: 1 addition & 1 deletion l2-contracts/test/ZkCappedMinterFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ contract ZkCappedMinterFactoryTest is ZkTokenTest {

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/ZkCappedMinter.sol/ZkCappedMinter.json");
Expand Down
Loading