> ## Documentation Index
> Fetch the complete documentation index at: https://docs.privacyboost.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Swaps

> Swap one shielded token for another without leaving the pool. The external call gateway spends a private note, runs a server-quoted DEX route, and credits the measured output as a new private note.

# Swaps

A **swap** exchanges one shielded token for another without the funds leaving
the shielded pool. Like an [earn](https://docs.privacyboost.io/sdk/concepts/earn.md) 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.

> [!NOTE]
>
> 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](https://docs.privacyboost.io/sdk/concepts/configuration.md#supported-networks). Detect
> support at runtime with `sdk.vault.getGatewayConfig()` and the swap
> capabilities `getRegisteredTokens()` advertises.

## How it works

```text theme={null}
 input note (private) ──spend──▶  gateway executes the quoted route  ──▶  output note (private)
                                          │
                        failure / expiry / below minOut
                                          ▼
                                  input refunded as a private note
```

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`](https://docs.privacyboost.io/sdk/typescript/guides/swaps), and in
React on the [`useSwap()`](https://docs.privacyboost.io/sdk/react/guides/swaps) hook. Quoting and executing
both need a signed-in session. Swaps run on the root SDK only; a `forChain()`
client rejects them.

| Method | Purpose |
| - | - |
| `createSwapQuote(params)` | Preview a quote: expected output, signed minimum, and price impact when the server measured it. Submits nothing. The gateway fee comes from `getGatewayFees()`; see [Fees](https://docs.privacyboost.io/sdk/concepts/fees.md). |
| `swapFromQuote({ quote })` | Execute a quote from `createSwapQuote`, unchanged, so the economics the user saw are the ones signed. Rejects quotes older than two minutes (`SWAP_QUOTE_MAX_AGE_MS`). |
| `swap(params)` | Quote and execute in one call. Requires `minAcceptableOutputAmount`, a floor you choose, because nobody reviews the quote it fetches. |

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`.

```typescript theme={null}
const quote = await sdk.vault.createSwapQuote({
  inputTokenId: 1,                    // a registry token id, e.g. WETH
  outputTokenId: 2,                   // e.g. USDC
  inputAmount: 100_000_000_000_000_000n, // 0.1 WETH, net of the gateway fee
  maxSlippageBps: 50,
});

// Show quote.expectedOutputAmount and quote.minOutputAmount, then:
const result = await sdk.vault.swapFromQuote({ quote });
```

A swap returns a `requestId`. Track it with `sdk.transactions.getUnshieldStatus`
and read the `gateway` projection, as for [earn](https://docs.privacyboost.io/sdk/concepts/earn.md#tracking-settlement):
`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](https://docs.privacyboost.io/sdk/concepts/error-handling.md#swap-quote-errors).

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](https://docs.privacyboost.io/sdk/concepts/authentication.md#approval-only-eoa-multichain-authentication)
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](https://docs.privacyboost.io/sdk/concepts/earn.md#trust-and-privacy):
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](https://docs.privacyboost.io/sdk/react-native/installation.md).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.