Roles · RBACuseMutation

useGrantRole

AccessControl: grant role to accountTarget on the singleton.

Import
@tokenops/sdk/fhe-disperse/react
Return
{ mutate, mutateAsync, isPending, error, data }
Lifecycle
Roles · RBAC

Description

AccessControl: grant role to accountTarget on the singleton. Caller must hold the role's admin role (typically DEFAULT_ADMIN_ROLE, i.e. the all-zero hash). Non-zero role hashes are keccak256(toBytes("<NAME>")).

After success, invalidate useHasRole({ role, account: accountTarget }).

Signature

@tokenops/sdk/fhe-disperse/react
ts
function useGrantRole(options?: DisperseHookOptions): UseMutationResult<Hex, Error, UseGrantRoleArgs>;

Parameters

Shape of the object you pass to .mutate(args).

PropertyTypeDescription
rolerequiredHex
accountTargetrequiredAddress
accountAccount | AddressOverride signer account. Falls back to walletClient.account.
Want to run a similar shape interactively? The Playground ships 8 ready presets across vesting / airdrop / disperse — deploy a manager, create a vesting, claim, and the airdrop / disperse equivalents. The deep-link above auto-selects the closest preset to useGrantRole; pick another from the dropdown if you'd rather start there.

Example

@tokenops/sdk/fhe-disperse/react · @example
tsx
import { keccak256, toBytes } from "viem";
const FEE_MANAGER_ROLE = keccak256(toBytes("FEE_MANAGER_ROLE"));
const grant = useGrantRole();
grant.mutate({ role: FEE_MANAGER_ROLE, accountTarget: opsAddress });

Pulled directly from the hook's TSDoc block — the same snippet your IDE shows on hover.

Errors

This mutation can reject with SDK-level, product-level, or viem-passthrough errors. Product classes carry the offending value as fields — render them inline instead of a generic "transaction failed." See Disperse › Errors for the per-class recovery table.

Invalidation recipe

After this mutation succeeds, invalidate the queries it affects so consumer UI re-fetches fresh state. The SDK never auto-invalidates — that's a consumer decision (different apps cache different shapes).

patterns/invalidation.ts
ts
import { useQueryClient } from "@tanstack/react-query";

const queryClient = useQueryClient();
const grantRole = useGrantRole(/* options */);

grantRole.mutate(args, {
  onSuccess() {
    // Coarse invalidation: refresh every cached read on this product surface.
    queryClient.invalidateQueries({
      queryKey: ["tokenops-sdk", "fhe-disperse"],
    });
  },
});

See also

Other Roles · RBAC hooks in disperse: