0
0
Blockchain / Solidityprogramming~20 mins

Access control with OpenZeppelin in Blockchain / Solidity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
OpenZeppelin Access Control Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this contract call?

Consider the following Solidity contract using OpenZeppelin's AccessControl. What will be the result of calling checkRoleForUser() for address 0x123...?

Blockchain / Solidity
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);
    }
}
Atrue if user is deployer, false otherwise
Bcompilation error due to missing constructor visibility
Cfalse for any address
Dtrue for any address
Attempts:
2 left
💡 Hint

Remember who is assigned the ADMIN_ROLE in the constructor.

Predict Output
intermediate
2:00remaining
What error occurs when calling a restricted function without role?

Given this contract snippet, what error message or behavior occurs when an address without the MINTER_ROLE calls mint()?

Blockchain / Solidity
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
    }
}
ACompilation error due to missing override
BTransaction succeeds but does nothing
CTransaction reverts with "AccessControl: account is missing role"
DTransaction reverts with "Caller is not a minter"
Attempts:
2 left
💡 Hint

Look at the require statement inside mint().

🔧 Debug
advanced
3:00remaining
Why does this contract fail to grant role?

This contract tries to grant EDITOR_ROLE but fails silently. Identify the bug.

Blockchain / Solidity
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);
    }
}
ACaller lacks DEFAULT_ADMIN_ROLE, so grantRole reverts
BMissing override for grantRole function
CEDITOR_ROLE is not initialized properly
DaddEditor function should be external, not public
Attempts:
2 left
💡 Hint

Who can call grantRole successfully?

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this role assignment?

Fix the syntax error in this role assignment snippet:

_setupRole(EDITOR_ROLE user);
A_setupRole(EDITOR_ROLE: user);
B_setupRole(EDITOR_ROLE, user);
C_setupRole(EDITOR_ROLE => user);
D_setupRole(EDITOR_ROLE user);
Attempts:
2 left
💡 Hint

Check the correct syntax for function arguments in Solidity.

🚀 Application
expert
3:00remaining
How many roles does this contract have after deployment?

Analyze this contract and determine how many distinct roles exist after deployment.

Blockchain / Solidity
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);
    }
}
A1 role: DEFAULT_ADMIN_ROLE only
B2 roles: DEFAULT_ADMIN_ROLE, ROLE_A
C3 roles: DEFAULT_ADMIN_ROLE, ROLE_A, ROLE_B
D4 roles: DEFAULT_ADMIN_ROLE, ROLE_A, ROLE_B, and a hidden role
Attempts:
2 left
💡 Hint

Consider all roles declared and used in the contract.