Skip to main content

Swaps

A swap exchanges one token in your private balance for another. The server quotes a route through an aggregator, the SDK signs that exact route into a Gateway unshield, and the output token comes back to you as a new private note. If the route can’t deliver the signed minimum when it executes, the input is refunded to your private balance instead. A swap needs a signed-in session. It runs on the root SDK only: a sdk.forChain() client rejects it with UNSUPPORTED_OPERATION.

Checking Availability

Swaps run through the Gateway’s 1inch-classic adapter. Check that the deployment has it and that it can quote right now:

Quoting and Swapping

Swaps address tokens by their pool token ID, not their contract address. Read the IDs from getRegisteredTokens(), and use tokenAllowsSwapOutput to check that a pair is routable before offering it. Then fetch a quote, show it, and execute the quote the user confirmed with swapFromQuote. The quote is passed through unchanged, so the route and minimum the user saw are exactly what gets signed.
A quote stays executable for SWAP_QUOTE_MAX_AGE_MS (two minutes) from quote.quotedAtMs; after that swapFromQuote rejects it. Use the timestamp to show a countdown, and re-quote rather than letting the user confirm a stale route. priceImpactBps on the quote is the trade’s price impact measured against the same route. It’s absent when the server didn’t measure it; treat that as unknown, not zero. economics.oracleVarianceBps compares the quote with a USD price feed instead, so it also absorbs that feed’s staleness; label it as a cross-check, not as price impact.

Fees

The Gateway fee replaces the regular unshield fee and is charged on top of inputAmount, so your shielded balance has to cover both. result.fee reports the fee that was charged.

One-Shot Swaps

swap quotes and executes in one call, for flows where nobody reviews the quote. For that reason it requires minAcceptableOutputAmount, a floor you choose independently of the quote, and rejects any quote whose minOutputAmount is below it. Leave room for slippage: a floor at the expected output rejects every quote.
Prefer createSwapQuote with swapFromQuote whenever a person can see the quote.

Tracking Settlement

A swap settles as a Gateway unshield. Read its progress with getUnshieldStatus(requestId); the gateway field follows the credited note:
The swap’s row in history carries the same information under gateway. Pass collapseGateway: true to fetchHistory to get one row per swap instead of one per on-chain leg.

Error Handling

A failed quote request and a refused submission each have their own codes. Read them with the helpers rather than matching on messages. GATEWAY_PAIR_DISABLED can come from either call, so both helpers return it. It means an operator disabled the pair, possibly after you loaded the catalog, and asking again won’t help. getRegisteredTokens({ forceRefresh: true }) reads the current pair policy instead of the copy the SDK cached.

Approval-Only Accounts

An approval-only account can quote like any other, then propose the swap with sdk.vault.buildSwapApprovalPlan(...). The plan bakes in a fresh quote, so the owner approves the route that will execute. Once the approval is on-chain, submit it with sdk.vault.submitPrebuiltSwapApprovalPlan(plan), or let reconcilePendingApprovals() submit it. See Approval plans.

Next Steps