Skip to content

Instantly share code, notes, and snippets.

@coffiasd
Created July 26, 2023 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coffiasd/340241d63980dc9d423e4ece2f9b20db to your computer and use it in GitHub Desktop.
Save coffiasd/340241d63980dc9d423e4ece2f9b20db to your computer and use it in GitHub Desktop.
Role-Based Access Control
pragma solidity ^0.8.13;
import "forge-std/Test.sol";
import {MyToken2} from "../src/Access2.sol";
contract Access2Test is Test {
MyToken2 public myToken;
address owner = address(0x100);
address arbitrary = address(0x101);
function setUp() public {
//create the contract.
myToken = new MyToken2(owner);
}
function testAccessControl() public {
//use arbitrary address invoke mint() function.
vm.prank(arbitrary);
vm.expectRevert("Caller is not a minter");
myToken.mint(address(this), 1);
//switch to owner.
vm.prank(owner);
myToken.mint(address(this), 1);
vm.prank(owner);
myToken.grantRole(keccak256("MINTER_ROLE"), arbitrary);
vm.prank(arbitrary);
myToken.mint(address(this), 1);
vm.prank(arbitrary);
vm.expectRevert();
myToken.grantRole(keccak256("MINTER_ROLE"), address(0x1001));
vm.prank(owner);
myToken.revokeRole(keccak256("MINTER_ROLE"), arbitrary);
vm.prank(arbitrary);
vm.expectRevert("Caller is not a minter");
myToken.mint(address(this), 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment