SDK 2.0 RC is out. Read the docs
WriteuseMutation

useEnsureOperator

Ensure spender is an ERC-7984 operator for the connected account on token, sending a setOperator transaction only when the grant is missing.

Import
@tokenops/sdk/fhe-vesting/react
Return
{ mutate, mutateAsync, isPending, error, data }
Lifecycle
Write

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

@tokenops/sdk/fhe-vesting/react
ts
function useEnsureOperator(options?: UseEnsureOperatorOptions): UseMutationResult<EnsureOperatorResult, Error, EnsureOperatorVariables>;

Parameters

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

PropertyTypeDescription
chainIdnumber | undefinedOverride the chain id used for client resolution.
telemetrySdkTelemetry | undefinedOptional telemetry adapter — forwarded to the headless helper.
Want to run a similar shape interactively? The Playground ships 8 ready presets across vesting / airdrop / disperse / faucet — deploy a manager, create a vesting, claim, and run the product equivalents. The deep-link above auto-selects the closest preset to useEnsureOperator; pick another from the dropdown if you'd rather start there.

Example

@tokenops/sdk/fhe-vesting/react · @example
tsx
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 Vesting › 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 ensureOperator = useEnsureOperator(/* options */);

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

See also

Other Write hooks in vesting: