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
| Hook | Wraps | Action | State |
|---|---|---|---|
useCreateDeposit | client.createDeposit() | createDeposit(params) | isLoading, error, txHash, depositDetails |
useAddFunds | client.addFunds() | addFunds(params) | isLoading, error, txHash |
useRemoveFunds | client.removeFunds() | removeFunds(params) | isLoading, error, txHash |
useWithdrawDeposit | client.withdrawDeposit() | withdrawDeposit(params) | isLoading, error, txHash |
useSetAcceptingIntents | client.setAcceptingIntents() | setAcceptingIntents(params) | isLoading, error, txHash |
useSetIntentRange | client.setIntentRange() | setIntentRange(params) | isLoading, error, txHash |
useSetCurrencyMinRate | client.setCurrencyMinRate() | setCurrencyMinRate(params) | isLoading, error, txHash |
useSetRetainOnEmpty | client.setRetainOnEmpty() | setRetainOnEmpty(params) | isLoading, error, txHash |
useSetDelegate | client.setDelegate() | setDelegate(params) | isLoading, error, txHash |
useRemoveDelegate | client.removeDelegate() | removeDelegate(params) | isLoading, error, txHash |
useSetDisputeProtectionEnabled | client.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
| Hook | Wraps | Actions | State |
|---|---|---|---|
useSignalIntent | client.signalIntent() | signalIntent(params), prepareSignalIntent(params) | isLoading, error, txHash, prepared |
useFulfillIntent | client.fulfillIntent() | fulfillIntent(params), prepareFulfillIntent(params) | isLoading, error, txHash, prepared |
useReleaseFundsToPayer | client.releaseFundsToPayer() | releaseFundsToPayer(params) | isLoading, error, txHash |
usePruneExpiredIntents | client.pruneExpiredIntents() | pruneExpiredIntents(params) | isLoading, error, txHash |
useExtendIntentLifetime | IntentGuardian reads and writes | refreshPolicy(), 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
| Field | Required | Description |
|---|---|---|
client | Yes | Zkp2pClient instance |
sendTransaction | Yes | Sends a prepared transaction and returns its hash |
referrer | No | One attribution code or a list of codes |
onSuccess | No | Success callback |
onError | No | Error 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
| Field | Required | Description |
|---|---|---|
client | Yes | Zkp2pClient instance |
sendTransaction | Yes | Sends one transaction |
sendBatch | No | Sends a batch, such as a smart-account user operation |
referrer | No | One attribution code or a list of codes |
onSuccess | No | Success callback |
onError | No | Error callback |
Actions
| Action | Use 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
| Hook | Wraps | Actions | State |
|---|---|---|---|
useSetVaultFee | client.setVaultFee() | setVaultFee(params), prepareSetVaultFee(params) | isLoading, error, txHash, prepared |
useSetVaultMinRate | client.setVaultMinRate() | setVaultMinRate(params), prepareSetVaultMinRate(params) | isLoading, error, txHash, prepared |
useSetVaultConfig | client.setVaultConfig() | setVaultConfig(params), prepareSetVaultConfig(params) | isLoading, error, txHash, prepared |
Payment method hooks
| Hook | Wraps | Action | State |
|---|---|---|---|
useAddPaymentMethods | client.addPaymentMethods() | addPaymentMethods(params) | isLoading, error, txHash |
useSetPaymentMethodActive | client.setPaymentMethodActive() | setPaymentMethodActive(params) | isLoading, error, txHash |
useRemovePaymentMethod | client.removePaymentMethod() | removePaymentMethod(params) | isLoading, error, txHash |
Currency hooks
| Hook | Wraps | Action | State |
|---|---|---|---|
useAddCurrencies | client.addCurrencies() | addCurrencies(params) | isLoading, error, txHash |
useDeactivateCurrency | client.deactivateCurrency() | deactivateCurrency(params) | isLoading, error, txHash |
useRemoveCurrency | client.removeCurrency() | removeCurrency(params) | isLoading, error, txHash |
Vault utilities
@zkp2p/sdk/react also exports these delegation helpers:
| Export | Purpose |
|---|---|
ZERO_RATE_MANAGER_ID, VAULT_ZERO_ADDRESS | Zero 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()andgetIntent() - 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.