Skip to main content

React Hooks

@zkp2p/sdk/react wraps Zkp2pClient writes in hooks that track loading state, errors, and transaction hashes. Some hooks also return prepared transactions for relayers or smart accounts.

Installation​

npm install @zkp2p/sdk viem react

React (>=16.8.0) is an optional peer dependency of the SDK, needed only for @zkp2p/sdk/react.

Hook pattern​

Each hook takes an options object with client (a Zkp2pClient, or null while it loads) and optional onSuccess and onError callbacks. It returns its actions plus isLoading, error, and txHash. Hooks with a prepare...() action also return the last prepared transaction.

import { useSignalIntent } from "@zkp2p/sdk/react";

function SignalIntentButton({ client, params }) {
const { signalIntent, isLoading, error, txHash } = useSignalIntent({
client,
});

return (
<button disabled={isLoading} onClick={() => signalIntent(params)}>
{txHash ?? error?.message ?? "Signal intent"}
</button>
);
}

Actions don't throw. On failure, or with a null client, they set error, call onError, and return null. useCreateVault and useVaultDelegation differ: they store error as a message string, and useVaultDelegation actions also rethrow.

Deposit hooks​

HookWrapsActionState
useCreateDepositclient.createDeposit()createDeposit(params)isLoading, error, txHash, depositDetails
useAddFundsclient.addFunds()addFunds(params)isLoading, error, txHash
useRemoveFundsclient.removeFunds()removeFunds(params)isLoading, error, txHash
useWithdrawDepositclient.withdrawDeposit()withdrawDeposit(params)isLoading, error, txHash
useSetAcceptingIntentsclient.setAcceptingIntents()setAcceptingIntents(params)isLoading, error, txHash
useSetIntentRangeclient.setIntentRange()setIntentRange(params)isLoading, error, txHash
useSetCurrencyMinRateclient.setCurrencyMinRate()setCurrencyMinRate(params)isLoading, error, txHash
useSetRetainOnEmptyclient.setRetainOnEmpty()setRetainOnEmpty(params)isLoading, error, txHash
useSetDelegateclient.setDelegate()setDelegate(params)isLoading, error, txHash
useRemoveDelegateclient.removeDelegate()removeDelegate(params)isLoading, error, txHash
useSetDisputeProtectionEnabledclient.setDisputeProtectionEnabled()setDisputeProtectionEnabled(params)isLoading, error, txHash

In SDK 0.15.0-rc.11, useSetDisputeProtectionEnabled still submits the O3-only write. O3 is retired; do not use this hook for current integrations. OrchestratorV4 has no per-deposit protection opt-out.

Intent hooks​

HookWrapsActionsState
useSignalIntentclient.signalIntent()signalIntent(params), prepareSignalIntent(params)isLoading, error, txHash, prepared
useFulfillIntentclient.fulfillIntent()fulfillIntent(params), prepareFulfillIntent(params)isLoading, error, txHash, prepared
useReleaseFundsToPayerclient.releaseFundsToPayer()releaseFundsToPayer(params)isLoading, error, txHash
usePruneExpiredIntentsclient.pruneExpiredIntents()pruneExpiredIntents(params)isLoading, error, txHash
useExtendIntentLifetimeIntentGuardian reads and writesrefreshPolicy(), refreshQuote(params), extendIntentLifetime(params), prepareExtendIntentLifetime(params)policy, quote, isAvailable, isLoading, error, errorCode, txHash, prepared

There's no useCancelIntent; call client.cancelIntent() or client.cancelIntent.prepare().

useExtendIntentLifetime reads the live guardian fee on every call and never caches it, so refresh the quote right before signing. isAvailable is false where no IntentGuardian is deployed. onError receives (error, code), with the codes listed under Intent lifetime extension.

Vault hooks​

useCreateVault​

Creates a vault through client.createRateManager() and sends it with your sendTransaction.

Options

FieldRequiredDescription
clientYesZkp2pClient instance
sendTransactionYesSends a prepared transaction and returns its hash
referrerNoOne attribution code or a list of codes
onSuccessNoSuccess callback
onErrorNoError callback

Call signature

createVault({
config: {
manager,
feeRecipient,
maxFee,
fee,
depositHook?,
minLiquidity?,
name,
uri,
},
})

Returns

createVault, prepareCreateVault, isLoading, error, and txHash.

useVaultDelegation​

Delegates deposits to a vault, or clears delegation, choosing the right path for each deposit. With sendBatch, it sends several deposits in one batch.

Options

FieldRequiredDescription
clientYesZkp2pClient instance
sendTransactionYesSends one transaction
sendBatchNoSends a batch, such as a smart-account user operation
referrerNoOne attribution code or a list of codes
onSuccessNoSuccess callback
onErrorNoError callback

Actions

ActionUse it for
delegateDeposit({ escrow, depositId, registry, rateManagerId, currentRateManagerId?, currentRateManagerRegistry?, preflightHook? })Delegate one deposit
delegateDeposits({ registry, rateManagerId, deposits, preflightHook? })Delegate many deposits
clearDelegation({ escrow, depositId })Clear one delegation
clearDelegations({ deposits })Clear many delegations

The batch actions return { hashes, failed }. The hook also returns isLoading and error.

Vault settings hooks​

HookWrapsActionsState
useSetVaultFeeclient.setVaultFee()setVaultFee(params), prepareSetVaultFee(params)isLoading, error, txHash, prepared
useSetVaultMinRateclient.setVaultMinRate()setVaultMinRate(params), prepareSetVaultMinRate(params)isLoading, error, txHash, prepared
useSetVaultConfigclient.setVaultConfig()setVaultConfig(params), prepareSetVaultConfig(params)isLoading, error, txHash, prepared

Payment method hooks​

HookWrapsActionState
useAddPaymentMethodsclient.addPaymentMethods()addPaymentMethods(params)isLoading, error, txHash
useSetPaymentMethodActiveclient.setPaymentMethodActive()setPaymentMethodActive(params)isLoading, error, txHash
useRemovePaymentMethodclient.removePaymentMethod()removePaymentMethod(params)isLoading, error, txHash

Currency hooks​

HookWrapsActionState
useAddCurrenciesclient.addCurrencies()addCurrencies(params)isLoading, error, txHash
useDeactivateCurrencyclient.deactivateCurrency()deactivateCurrency(params)isLoading, error, txHash
useRemoveCurrencyclient.removeCurrency()removeCurrency(params)isLoading, error, txHash

Vault utilities​

@zkp2p/sdk/react also exports these delegation helpers:

ExportPurpose
ZERO_RATE_MANAGER_ID, VAULT_ZERO_ADDRESSZero vault ID and zero registry address
isZeroRateManagerId(value)Whether a vault ID is the zero ID
normalizeRateManagerId(value), normalizeRegistry(value)Normalize a vault ID or registry address
getDelegationRoute(client, escrow)The delegation route for an escrow (v2)
classifyDelegationState(currentRateManagerId, currentRegistry, targetRateManagerId, targetRegistry)delegated_here, delegated_elsewhere, or not_delegated
assertDelegationMethodSupport(client, route, action)Throws if the client can't prepare the delegation call
asErrorMessage(error)Turn a caught value into a display message

Types: DelegationRoute, DelegationState, DelegationDepositTarget, BatchResult, SendTransactionFn, SendBatchFn, and a Use...Options type for every hook.

When to use hooks vs the core client​

Use hooks for component-level transaction state. Use the client directly for:

  • quotes with getQuote()
  • on-chain reads such as getDeposits() and getIntent()
  • indexer queries through client.indexer
  • methods without a hook, such as cancelIntent()

Help?​

For integration questions, join the Peer Telegram community. For order-specific help, email support@peer.xyz.