Skip to main content

Swaps

A swap exchanges one shielded token for another without the funds leaving the shielded pool. Like an earn deposit, it is a gateway unshield: the SDK spends a private input note, the external call gateway runs a DEX route quoted by the server (1inch), and the measured output is credited back as a new private note. The route itself is public on-chain; the account that owns the input note is not.
Swaps need the external call gateway and a quote adapter on the deployment. They run through 1inch on OP Mainnet and Base, and through a test router on OP Sepolia and Base Sepolia; see Supported Networks. Detect support at runtime with sdk.vault.getGatewayConfig() and the swap capabilities getRegisteredTokens() advertises.

How it works

  1. Quote. The SDK asks the server for a route from the input token to the output token. The server validates the 1inch response and returns the exact router call, the expected output, and a minOutputAmount floor derived from your slippage bound (default 50 bps).
  2. Authorize and execute. The SDK signs a gateway authorization for exactly that call and floor. The pool spends the input note under the epoch proof, and the gateway calls the router. The output is measured on-chain, not trusted from the quote.
  3. Credit. The measured output creates a pending gateway deposit, which a later deposit proof credits as a new private note.
  4. Refund on failure. If the route reverts, the request expires, or the fill would land below the signed minOutputAmount, the input is credited back as a private note instead. The gateway fee is still charged, so don’t let users confirm a quote that has gone stale.

SDK API shape

The swap methods live on sdk.vault, and in React on the useSwap() hook. Quoting and executing both need a signed-in session. Swaps run on the root SDK only; a forChain() client rejects them. Token IDs are registry IDs from getRegisteredTokens(); tokenAllowsSwapOutput() says whether a token can swap into a given output. inputAmount is net, exactly what enters the router call; the gateway fee is charged on top, so the input notes must cover inputAmount + fee.
A swap returns a requestId. Track it with sdk.transactions.getUnshieldStatus and read the gateway projection, as for earn: gateway.creditStatus === 'completed' means the output note was credited. A rejected quote carries a reason code (GATEWAY_QUOTE_REJECTED, GATEWAY_QUOTE_UNAVAILABLE, …). Read it with asGatewayQuoteErrorCode(error), and pass the code to isRetryableGatewayQuoteError(code) to tell a transient provider failure from one that needs different input. See Error Handling. Operators can turn off a token pair. A quote or a submission for that pair then fails with GATEWAY_PAIR_DISABLED, which isn’t retryable. The token catalog your app loaded can predate the change. In TypeScript, getRegisteredTokens({ forceRefresh: true }) reads the current pair policy, skipping the SDK’s and the server’s cached copies. Approval-only accounts can quote too, then propose the swap with buildSwapApprovalPlan() instead of executing it directly.

Trust and privacy

A swap gets the same guarantees as earn: the gateway can only spend the note you authorized and credit the measured result back into the pool. The minOutputAmount floor is signed, so no operator can settle the swap into a worse-than-authorized fill. At worst, the input is refunded and the gateway fee is still charged. A swap hides who swapped. The router call, its tokens, and amounts are public on-chain. The output note joins the same shared note tree as every other deposit and transfer.

Using it from the SDK

  • CLI: swap quote, swap from-quote, swap execute, and swap status.
  • iOS / Android / React Native: createSwapQuote, swapFromQuote, and swap on the root PrivacyBoost instance. On React Native, they need a newer package than latest; see Installation.