How can I allow either-or roles to call a function?

Currently, I could add the modifier onlyRole("MY_ROLE") to a function to allow anyone with this role to call the function.

How can I have something like eitherRole("RoleA", "RoleB") to say that anyone with either of the roles can call the function?

Hi, I think you can have a look at this contract: AccessControl.sol, and for the documentation, you can look at this: Access Control

I don't find in the documentation mentioning about whether I could define an either-or roles.

Am I missing something in the docs?

There is an exampe:

// contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20, AccessControl {
    // Create a new role identifier for the minter role
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

    constructor(address minter) ERC20("MyToken", "TKN") {
        // Grant the minter role to a specified account
        _setupRole(MINTER_ROLE, minter);
    }

    function mint(address to, uint256 amount) public {
        // Check that the calling account has the minter role
        require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter"); //<<---here!!!
        _mint(to, amount);
    }
}
1 Like

Ahh... You are right, I could use hasRole with an "or" operator in the require. Thanks a lot!

1 Like