useGrantRole

Hook for granting a role on a smart contract.

Available to use on smart contracts that implement the Permissions interface.

Example

import {
useGrantRole,
useContract,
Web3Button,
} from "@thirdweb-dev/react";
const contractAddress = "{{contract_address}}";
const roleToGrant = "{{role}}";
const walletAddressToGrant = "{{wallet_address}}";
function App() {
const { contract } = useContract(contractAddress);
const {
mutateAsync: grantRole,
isLoading,
error,
} = useGrantRole(contract);
return (
<Web3Button
contractAddress={contractAddress}
action={() =>
grantRole({
role: roleToGrant, // name of your role.
address: walletAddressToGrant, // address to grant the role to.
})
}
>
Grant Role
</Web3Button>
);
}

Parameters

Returns

A mutation object to grant a member of a role on the contract

const { mutateAsync, isLoading, error } = useGrantRole(contract);

options

The mutation function accepts an object with the following properties:

role

The name of the role to grant the address.

Accepts any string value to include custom-defined roles.

Also accepts the default roles available on the prebuilt contracts :

string |
"admin" |
"minter" |
"transfer" |
"lister" |
"asset" |
"unwrap" |
"pauser" |
"factory";

address

The address to grant the role to.

To use the address of the connected wallet, use the useAddress hook.