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 add this interface to your contract by implementing all of the functions in the IPermissionsEnumerable interface as shown below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@thirdweb-dev/contracts/ThirdwebContract.sol";
import "@thirdweb-dev/contracts/feature/interface/IPermissionsEnumerable.sol";
// We add the interface to the contract
contract MyCustomContract is ThirdwebContract, IPermissionsEnumerable {
// Implement the permissions functions below
}
Alternatively, you can use our PermissionsEnumerable implementation of the IPermissionsEnumerable interface 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/ThirdwebContract.sol";
import "@thirdweb-dev/contracts/feature/interface/PermissionsEnumerable.sol";
// We add the interface to the contract
contract MyCustomContract is ThirdwebContract, PermissionsEnumerable {
// Now this contract will have permissions out of the box
}
If your contract implements all the functions in this standard, you will get support for the following SDK interface.
- Javascript
const contract = await sdk.getContract("{{contract_address}}");
const rolesAndMembers = await contract.roles.getAll();
await contract.roles.grantRole("admin", "0x...");
get
Call this to get a list of addresses that are members of a specific role.
- Javascript
const minterAddresses: string[] = await contract.getRoleMemberList("minter");
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.
- Javascript
const minterAddresses: string[] = await contract.getRoleMemberList("minter");
await contract.setAll({
minter: []
});
console.log(await contract.getRoleMemberList("minter")); // No matter what members had the role before, the new list will be set to []