WriteuseMutationencrypts input Curated

useDisperse

Multi-recipient confidential transfer in one tx — batch-encrypts amounts + per-recipient ACL.

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

Description

Disperse confidential tokens to multiple recipients. The SDK validates inputs, computes the wallet-mode subtotal split, encrypts all amounts + subtotals in a single proof batch, computes msg.value for gas-fee modes, and dispatches to the correct contract entry point based on mode.

Encryptor resolution. The hook-level encryptor (from DisperseHookOptions) is forwarded lazily to the SDK client. If missing when the mutation runs, the SDK throws Missing encryptor — pass encryptor… verbatim. Capture useZamaSDK() once at the top of your component and forward () => zamaSDK.relayer — never () => useZamaSDK().relayer inline (calls a React hook inside a mutation body, violating the rules of hooks; see CLAUDE.md Pitfall #3).

After success, invalidate useGetFees, useHasApprovedSubwallets, and any downstream token-balance queries owned by the consumer.

Signature

@tokenops/sdk/fhe-disperse/react
ts
function useDisperse(options?: DisperseHookOptions): UseMutationResult<DisperseResult, Error, DisperseArgs>;

Parameters

Shape of the object you pass to .mutate(args) is the SDK type DisperseArgs. Inspect the type for the full shape (discriminated unions collapse to a tagged variant at call time).

Run it

Connect your wallet and click Disperse — this dispatches a real Sepolia transaction through the same runner the stories use.

Interactive · live Sepolia tx
Loading editor…
Edit any input above, then press to run.
Console0 lines
No output yet. Run the snippet to see logs stream in.

Example

@tokenops/sdk/fhe-disperse/react · @example
tsx
function Component() {
  const zamaSDK = useZamaSDK();
  const disperse = useDisperse({ encryptor: () => zamaSDK.relayer });
  disperse.mutate({ token, mode: "direct", recipients, amounts });
}

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 disperse = useDisperse(/* options */);

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

See also