Swaps
TheuseSwap hook swaps one shielded token for another without leaving the
privacy pool.
A swap is a Gateway operation. The input note is spent into a server-quoted
router call (1inch), and the output arrives as a new private note. If the route
returns less than the signed minimum, the input is refunded to your private
balance instead. The Gateway fee replaces the ordinary unshield fee and is
charged on top of the input amount, so the input notes must cover both.
useSwap
Swaps run in the key vault bound to the root SDK’s chain, so the hook always
uses the root client, whatever the provider’s
activeClient is. Executing a
swap unlocks first when the session is locked, like any other write.
Pick the Tokens
Swaps take pool token IDs, whichgetRegisteredTokens() reports along with
the decimals and USD prices the quote economics need. tokenAllowsSwapOutput
tells you whether a pair is routable, since a token’s swap capability can be
limited to an allowlist.
Quote, Review, Execute
Show the quote before the user confirms, then pass it back toswapFromQuote
exactly as createQuote returned it. Its callData and minOutputAmount are
what get signed, so the economics the user saw are the ones executed. Amounts in
a quote are raw-unit decimal strings.
SWAP_QUOTE_MAX_AGE_MS (two minutes) from
quote.quotedAtMs; use both to show a countdown rather than let the user
confirm a stale route. priceImpactBps is absent when the server did not
measure it, which means unknown, not zero. economics.oracleVarianceBps
compares the quote against a USD price feed and also absorbs the feed’s
staleness, so don’t label it price impact.
One-Shot Swaps
swap fetches its own quote and submits it immediately, so nobody reviews that
quote. It therefore requires minAcceptableOutputAmount, the least you accept
in raw output-token units. The SDK checks it against the quote’s
minOutputAmount, the figure the Gateway settles against, so leave room for
slippage: a floor at the expected output rejects every quote.
createQuote and swapFromQuote whenever a person is in the loop.
Fees
SwapResult.fee reports the Gateway fee charged, in raw units of the input
token. To show it before submitting, call getGatewayFees with the input
token’s address, the Gateway executor address (executorAddress from
useEarn().getGatewayConfig()), and the quote’s selector, which is the first
four bytes of its calldata (quote.callData.slice(0, 10)). See
useFees for turning the rates into an
amount.
Track the Swap
A swap settles as an unshield followed by a credit. Follow itsrequestId with
useUnshieldStatus: data.gateway.creditStatus reaches 'completed' once the
credited note exists, and data.gateway.settlement says whether the route
executed ('executed') or the input was refunded ('fallback').
client option picks which server to ask. Added in 1.3.0. Without
it the hook asks the provider’s active client,
which is the root SDK unless you set activeClient.
In history, a swap is two rows (the input leaving, the output arriving) unless
you fetch with collapseGateway: true, which folds them into one row in
useTransactions().gatewayOperations. See Transactions.
Error Handling
A rejected quote carries a Gateway code such asGATEWAY_QUOTE_REJECTED or
GATEWAY_QUOTE_UNAVAILABLE. asGatewayQuoteErrorCode reads it off the error,
and isRetryableGatewayQuoteError says whether asking again could succeed.
GATEWAY_PAIR_DISABLED means an operator disabled the pair, at quote time or
when the swap was submitted. It isn’t retryable. Reload the token catalog with
usePrivacyBoost()?.vault.getRegisteredTokens({ forceRefresh: true }) so the
pair drops out of your picker.
swapFromQuote; it proposes the swap with buildSwapApprovalPlan on the
TypeScript client instead.
Next Steps
- Transactions Guide
- Balances Guide
- API Reference: useSwap
- Earn: the other Gateway operation