useEnsureOperator
Ensure spender is an ERC-7984 operator for the connected account on token, sending a setOperator transaction only when the grant is missing.
@tokenops/sdk/fhe-disperse/react{ mutate, mutateAsync, isPending, error, data }Description
Ensure spender is an ERC-7984 operator for the connected account on
token, sending a setOperator transaction only when the grant is
missing. Resolves to { alreadyOperator, hash } — hash is null on
the no-op path.
This is the mutation half of the operator-prerequisite pair; use
useIsOperator for the read side, and invalidate/refetch it after
this mutation succeeds.
Signature
function useEnsureOperator(options?: UseEnsureOperatorOptions): UseMutationResult<EnsureOperatorResult, Error, EnsureOperatorVariables>;Parameters
Shape of the object you pass to .mutate(args).
| Property | Type | Description |
|---|---|---|
| chainId | number | undefined | Override the chain id used for client resolution. |
| telemetry | SdkTelemetry | undefined | Optional telemetry adapter — forwarded to the headless helper. |
Example
const ensure = useEnsureOperator();
const oneHour = BigInt(Math.floor(Date.now() / 1000) + 3600);
ensure.mutate({ token, spender: manager, deadline: oneHour });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 generic-fallback 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).
import { useQueryClient } from "@tanstack/react-query";
const queryClient = useQueryClient();
const ensureOperator = useEnsureOperator(/* options */);
ensureOperator.mutate(args, {
onSuccess() {
// Coarse invalidation: refresh every cached read on this product surface.
queryClient.invalidateQueries({
queryKey: ["tokenops-sdk", "fhe-disperse"],
});
},
});See also
Other Write hooks in disperse: