Skip to main content

Permissions

The Permissions smart contract extension is usable with any base smart contract. It lets you create and assign roles to wallets, and restrict functions in your contract to only be callable by holders of a certain role.

Import

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

Available functionality

FunctionalityDescription
hasRoleCheck whether a wallet has a particular role.
grantRoleLets an authorized wallet grant a role to another wallet.
revokeRoleLets an authorized wallet revoke a role from another wallet.
renounceRoleLets a wallet renounce a role it holds.

Implementing the Contract extension

Import the contract extension and make your contract inherit it.

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

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

contract MyContract is Permissions {
// 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 `Permissions` 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 `Permissions`, and
* pass it `NUMBER_ROLE` as an argument.
*/
function setNumber(uint256 _newNumber) public onlyRole(NUMBER_ROLE) {
number = _newNumber;
}
}

Unlocked Features

The Permissions extension smart contract is open source, and is available here on github.

Once deployed, you'll be able to access the following features from the dashboard and the SDK:

  • You can interact with your smart contract’s Permissions features via the thirdweb SDKs. Read about the SDK API here.
  • Deploy your contract with deploy to get a UI on the dashboard to manage roles on your smart contract.

Permissions Enumerable

The PermissionsEnumberable smart contract extension is usable with any base smart contract. It is built upon the Permissions contract extension, and exposes convenient methods to fetch all wallets that hold a given role.

Available functionality

FunctionalityDescription
getRoleMemberUse with getRoleMemberCount to get all wallets that hold a given role.
getRoleMemberCountReturns the number of wallets that hold a given role.

Implementing the Contract extension

Import the contract extension and make your contract inherit it.

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

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

// Usage is the same as `extensions/Permissions.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 `Permissions` 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 `Permissions`, and
* pass it `NUMBER_ROLE` as an argument.
*/
function setNumber(uint256 _newNumber) public onlyRole(NUMBER_ROLE) {
number = _newNumber;
}
}

Unlocked Features

On the dashboard, you'll be able to view and edit permissions for the roles you configured under the Permissions tab:

Batch Upload

Within the SDK, you can read, grant, revoke, and override each of your roles.