Skip to main content

Permissions and Roles

Using the Permissions and Roles features on your contract allows you to add a fine-tuned permission control system to control who is allowed to interact with different parts of your contract functionality.

You can use our PermissionsEnumerable implementation to get permissions functionality for your contract out-of-the-box as follows:

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

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

// We add the interface to the contract
contract MyCustomContract is PermissionsEnumerable {

constructor() {
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
// you can setup any other roles here
}

// Now this contract will have permissions out of the box
// you can add the modifier `onlyRole(DEFAULT_ADMIN_ROLE)` (or any other role) to your functions to control access

}

If your contract implements all the functions in this standard, you will get support for the following SDK interface.

const contract = await sdk.getContract("{{contract_address}}");
const rolesAndMembers = await contract.roles.getAll();
await contract.roles.grantRole("admin", "0x...");
View in Javascript SDK Documentation

get

Call this to get a list of addresses that are members of a specific role.

const minterAddresses = await contract.roles.get("minter");
View in Javascript SDK Documentation

getAll

Call this to get get a list of addresses for all supported roles on the contract.

const rolesAndMembers = await contract.roles.getAll();
View in Javascript SDK Documentation

grant

Call this to grant a role to a specific address.

await contract.roles.grant("minter", "0x1234567890123456789012345678901234567890");
View in Javascript SDK Documentation

revoke

Call this to revoke a role from a specific address.

await contract.roles.revoke("minter", "0x1234567890123456789012345678901234567890");
View in Javascript SDK Documentation

setAll

Call this to OVERWRITE the list of addresses that are members of specific roles. Every role in the list will be overwritten with the new list of addresses provided with them. If you want to add or remove addresses for a single address use {@link ContractRoles.grant} and {@link ContractRoles.revoke} respectively instead.

const minterAddresses = await contract.roles.get("minter");
await contract.roles.setAll({
minter: []
});
console.log(await contract.roles.get("minter")); // No matter what members had the role before, the new list will be set to []
View in Javascript SDK Documentation