Skip to content

Commit 83330a6

Browse files
ernestognwAmxxericglauzackrw
authored
Add AccessManager guide (#4691) (#4724)
Co-authored-by: Hadrien Croubois <[email protected]> Co-authored-by: Eric Lau <[email protected]> Co-authored-by: Zack Reneau-Wedeen <[email protected]>
1 parent ab967b8 commit 83330a6

9 files changed

+487
-75
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import {AccessControl} from "../../../access/AccessControl.sol";
5+
import {ERC20} from "../../../token/ERC20/ERC20.sol";
6+
7+
contract AccessControlERC20MintBase is ERC20, AccessControl {
8+
// Create a new role identifier for the minter role
9+
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
10+
11+
error CallerNotMinter(address caller);
12+
13+
constructor(address minter) ERC20("MyToken", "TKN") {
14+
// Grant the minter role to a specified account
15+
_grantRole(MINTER_ROLE, minter);
16+
}
17+
18+
function mint(address to, uint256 amount) public {
19+
// Check that the calling account has the minter role
20+
if (!hasRole(MINTER_ROLE, msg.sender)) {
21+
revert CallerNotMinter(msg.sender);
22+
}
23+
_mint(to, amount);
24+
}
25+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import {AccessControl} from "../../../access/AccessControl.sol";
5+
import {ERC20} from "../../../token/ERC20/ERC20.sol";
6+
7+
contract AccessControlERC20MintMissing is ERC20, AccessControl {
8+
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
9+
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");
10+
11+
constructor() ERC20("MyToken", "TKN") {
12+
// Grant the contract deployer the default admin role: it will be able
13+
// to grant and revoke any roles
14+
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
15+
}
16+
17+
function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
18+
_mint(to, amount);
19+
}
20+
21+
function burn(address from, uint256 amount) public onlyRole(BURNER_ROLE) {
22+
_burn(from, amount);
23+
}
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import {AccessControl} from "../../../access/AccessControl.sol";
5+
import {ERC20} from "../../../token/ERC20/ERC20.sol";
6+
7+
contract AccessControlERC20Mint is ERC20, AccessControl {
8+
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
9+
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");
10+
11+
constructor(address minter, address burner) ERC20("MyToken", "TKN") {
12+
_grantRole(MINTER_ROLE, minter);
13+
_grantRole(BURNER_ROLE, burner);
14+
}
15+
16+
function mint(address to, uint256 amount) public onlyRole(MINTER_ROLE) {
17+
_mint(to, amount);
18+
}
19+
20+
function burn(address from, uint256 amount) public onlyRole(BURNER_ROLE) {
21+
_burn(from, amount);
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
import {AccessManaged} from "../../../access/manager/AccessManaged.sol";
5+
import {ERC20} from "../../../token/ERC20/ERC20.sol";
6+
7+
contract AccessManagedERC20Mint is ERC20, AccessManaged {
8+
constructor(address manager) ERC20("MyToken", "TKN") AccessManaged(manager) {}
9+
10+
// Minting is restricted according to the manager rules for this function.
11+
// The function is identified by its selector: 0x40c10f19.
12+
// Calculated with bytes4(keccak256('mint(address,uint256)'))
13+
function mint(address to, uint256 amount) public restricted {
14+
_mint(to, amount);
15+
}
16+
}

contracts/mocks/docs/MyContractOwnable.sol renamed to contracts/mocks/docs/access-control/MyContractOwnable.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
pragma solidity ^0.8.20;
44

5-
import {Ownable} from "../../access/Ownable.sol";
5+
import {Ownable} from "../../../access/Ownable.sol";
66

77
contract MyContract is Ownable {
88
constructor(address initialOwner) Ownable(initialOwner) {}

docs/modules/ROOT/images/access-control-multiple.svg

Lines changed: 97 additions & 0 deletions
Loading

docs/modules/ROOT/images/access-manager-functions.svg

Lines changed: 47 additions & 0 deletions
Loading

docs/modules/ROOT/images/access-manager.svg

Lines changed: 99 additions & 0 deletions
Loading

docs/modules/ROOT/pages/access-control.adoc

Lines changed: 155 additions & 74 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)