0
0
Blockchain / Solidityprogramming~10 mins

Access control patterns in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple owner check function in a smart contract.

Blockchain / Solidity
function isOwner(address user) public view returns (bool) {
    return user == [1];
}
Drag options to blanks, or click blank then click option'
Amsg.sender
Bowner
Caddress(this)
Dadmin
Attempts:
3 left
💡 Hint
Common Mistakes
Using msg.sender instead of the stored owner variable.
Comparing with address(this) which is the contract address.
2fill in blank
medium

Complete the code to add a modifier that restricts function access to the owner only.

Blockchain / Solidity
modifier onlyOwner() {
    require([1] == owner, "Not owner");
    _;
}
Drag options to blanks, or click blank then click option'
Aaddress(this)
Btx.origin
Cmsg.sender
Downer
Attempts:
3 left
💡 Hint
Common Mistakes
Using tx.origin which can be unsafe for access control.
Comparing owner to itself.
3fill in blank
hard

Fix the error in the access control check that uses a mapping for roles.

Blockchain / Solidity
mapping(address => bool) public admins;

function restrictedAction() public {
    require([1][msg.sender], "Not admin");
    // action code
}
Drag options to blanks, or click blank then click option'
Aadmins
BadminMapping
Cadmin
DadminList
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not exist.
Trying to access a mapping without brackets.
4fill in blank
hard

Fill both blanks to create a function that grants admin role only by the owner.

Blockchain / Solidity
function grantAdmin(address user) public [1] {
    admins[user] = true;
    emit AdminGranted(user);
}

modifier [2]() {
    require(msg.sender == owner, "Not owner");
    _;
}
Drag options to blanks, or click blank then click option'
AonlyOwner
Bpublic
ConlyAdmin
Dexternal
Attempts:
3 left
💡 Hint
Common Mistakes
Using public or external without access control.
Using a modifier name that does not exist.
5fill in blank
hard

Fill all three blanks to implement a role-based access control check inside a function.

Blockchain / Solidity
function performAction() public {
    require(roles[msg.sender] [1] [2], "Access denied");
    // action code
}

mapping(address => uint) public roles;
uint constant ADMIN_ROLE = [3];
Drag options to blanks, or click blank then click option'
A==
B>=
C1
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality instead of greater or equal comparison.
Setting ADMIN_ROLE to 0 which usually means no role.