Consider the following Solidity contract using OpenZeppelin's AccessControl. What will be the result of calling checkRoleForUser() for address 0x123...?
pragma solidity ^0.8.0; import "@openzeppelin/contracts/access/AccessControl.sol"; contract RoleTest is AccessControl { bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE"); constructor() { _setupRole(ADMIN_ROLE, msg.sender); } function checkRoleForUser(address user) public view returns (bool) { return hasRole(ADMIN_ROLE, user); } }
Remember who is assigned the ADMIN_ROLE in the constructor.
The constructor assigns ADMIN_ROLE only to the deployer (msg.sender). So only that address has the role. Others return false.
Given this contract snippet, what error message or behavior occurs when an address without the MINTER_ROLE calls mint()?
pragma solidity ^0.8.0; import "@openzeppelin/contracts/access/AccessControl.sol"; contract Token is AccessControl { bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); constructor() { _setupRole(DEFAULT_ADMIN_ROLE, msg.sender); } function mint(address to, uint256 amount) public { require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter"); // mint logic here } }
Look at the require statement inside mint().
The mint() function uses a require to check the MINTER_ROLE and reverts with the custom message "Caller is not a minter" if the caller lacks the role.
This contract tries to grant EDITOR_ROLE but fails silently. Identify the bug.
pragma solidity ^0.8.0; import "@openzeppelin/contracts/access/AccessControl.sol"; contract EditorControl is AccessControl { bytes32 public constant EDITOR_ROLE = keccak256("EDITOR_ROLE"); constructor() { _setupRole(DEFAULT_ADMIN_ROLE, msg.sender); } function addEditor(address user) public { grantRole(EDITOR_ROLE, user); } }
Who can call grantRole successfully?
Only accounts with the admin role for a role can call grantRole. If the caller does not have DEFAULT_ADMIN_ROLE, the call reverts.
Fix the syntax error in this role assignment snippet:
_setupRole(EDITOR_ROLE user);
Check the correct syntax for function arguments in Solidity.
Function arguments must be separated by commas. The correct syntax is _setupRole(EDITOR_ROLE, user);
Analyze this contract and determine how many distinct roles exist after deployment.
pragma solidity ^0.8.0; import "@openzeppelin/contracts/access/AccessControl.sol"; contract MultiRole is AccessControl { bytes32 public constant ROLE_A = keccak256("ROLE_A"); bytes32 public constant ROLE_B = keccak256("ROLE_B"); constructor() { _setupRole(DEFAULT_ADMIN_ROLE, msg.sender); _setupRole(ROLE_A, msg.sender); } function assignRoleB(address user) public { grantRole(ROLE_B, user); } }
Consider all roles declared and used in the contract.
The contract defines ROLE_A and ROLE_B explicitly and uses DEFAULT_ADMIN_ROLE from AccessControl. So there are three distinct roles.