Skip to main content

PermissionsEnumerable

import "@thirdweb-dev/contracts/extension/PermissionsEnumerable.sol";

The PermissionsEnumerable extension provides role-based access control: you can create roles and write custom logic in your smart contract that depends on whether a given wallet holds a given role. The Enumerable part provides the capability to view all the addresses holding a specific role.

Usage​

This is an example smart contract demonstrating how to inherit from this extension and override the functions to add (optional) custom functionality.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/extension/PermissionsEnumerable.sol";

contract MyContract is PermissionsEnumerable {
// Any `bytes32` value is a valid role. You can create roles by defining them like this.
bytes32 public constant NUMBER_ROLE = keccak256("NUMBER_ROLE");

// See comments for `setNumber`, below.
uint256 public number;

/**
* The `PermissionsEnumerable` contract makes an already defined role available: the `DEFAULT_ADMIN_ROLE`.
*
* As an EXAMPLE, we grant the deployer of the contract this admin role.
*/
constructor() {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
}

/**
* EXAMPLE: here we have a function that we want to restrict only to holders of `NUMBER_ROLE`.
*
* To accomplish this, we use the `onlyRole` modifier made available by `PermissionsEnumerable`, and
* pass it `NUMBER_ROLE` as an argument.
*/
function setNumber(uint256 _newNumber) public onlyRole(NUMBER_ROLE) {
number = _newNumber;
}
}

SDK Usage​

By adding this extension to a smart contract, the following features, hooks and functions are unlocked in the SDK:

Base Contracts Implementing This Extension​

None of the base contracts implement this extension.

Full API Reference​

PermissionsEnumerable inherits from the Permissions extension so includes all of the functions in that extension. It also includes the following functions:

getRoleMember
function getRoleMember(bytes32 role, uint256 index) external view returns (address member);
  • Returns one of the accounts that have role. index must be a value between 0 and the value of getRoleMemberCount at the time, non-inclusive.
  • Parameters:
    • role: keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
    • index: Index in list of current members for the role.
getRoleMemberCount
function getRoleMemberCount(bytes32 role) external view returns (uint256 count);
  • Returns the number of accounts that have role. Can be used together with getRoleMember to enumerate all bearers of a role.
  • Parameters:
    • role: keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")