swap consumes an input token record and writes a public pending output. claim returns the output and any unspent input as token records.
A wallet should report success only after the claim is accepted and the returned records are stored. A finalized swap without a finalized claim is still unsettled for the user.
Finish line
The workflow is complete when all of these are true:- the swap transaction is accepted
swap_outputs[swap_id]contains the finalized output used for the claim- the claim transaction is accepted
- output and refund records are stored in the wallet
- the pending mapping entry is removed
- local record state and public AMM state reconcile
Flow
- Confirm the deployment, market, and controls.
- Quote with the contract’s integer math and iteration caps.
- Reserve the input token record and persist claim recovery material.
- Submit
swaponce and track its terminal state. - Read the finalized
SwapOutputfrom the mapping. - Submit
claimwith the exact stored fields. - Store returned records and reconcile final state.
Before you start
Your client needs deployment identity, chain reads, a record store, exact-value parsing, proof submission, finality monitoring, and durable pending-claim storage. If you only need market data or routes, start with the REST API instead.1. Confirm the market
Before quoting, read the pool and slot from chain state. Confirm:- The pool contains the intended token programs in the expected token0 and token1 order.
- The pool is enabled.
- Global, token, and pair pause controls do not block trading.
- The fee tier and tick spacing match the market the user selected.
- The current square-root price, current tick, active liquidity, and initialized-tick pointers are available.
swap against the public PoolState, and direction is defined in that canonical order.
The token metadata API supplies display names and decimals. Use chain state for pool identity and token eligibility, and operator policy records for market approval.
2. Establish direction
zero_for_one determines both the input token and the direction of price movement:
Use the same direction when computing the quote, price limit, record token, and token identifiers. A direction mismatch normally fails validation or attempts to spend the wrong input record.
3. Prepare a spendable record
Select a token record from the input token program with an amount at least as large asamount_in. The record owner must be the transaction signer, the record must be unspent, and its concrete token program must match the selected pool side.
Preserve all record fields, including _nonce and _version. Reconstructing a record without its version can produce a commitment that does not exist on chain.
The AMM asks the token program to move exactly amount_in from private to public custody. If the record is larger, the token program returns a change record. The wallet should track the consumed record and the returned change as part of the same state update.
See Preparing token records.
4. Quote with integer math
Read current state close to submission time and simulate the contract’s fixed-point arithmetic. The quote should include:- Expected output in raw token units
- Expected unspent input
- Expected end price and tick
- Expected initialized ticks crossed
- Expected active liquidity after each crossing
- Fee and protocol-fee estimates
- The maximum number of swap iterations available
5. Set protections
Set all three protections deliberately:sqrt_price_limitcaps adverse price movement.amount_out_minsets the minimum acceptable final output in raw output-token units.deadlinesets the last acceptable block height.
6. Create confidential addressing material
Generate a fresh private blinding factor and derive the public confidential address from:- The AMM program address
- The claim-or-swap domain separator
- The signer address
- The private blinding factor
7. Submit the swap
The single-hopswap input order is:
- Input token record
- Private blinding factor
- Public confidential address
- Public pool key
- Public direction
- Public input amount
- Public minimum output
- Public square-root-price limit
- Public nonce
- Public deadline
- Public token0 identifier
- Public token1 identifier
u64 nonce. The nonce contributes to swap_id; it is distinct from the record nonce and the blinding-factor derivation counter.
An accepted transaction returns a public swap_id, a token change record when applicable, and an investigator-owned compliance record. The investigator record contains request and attribution data. Deployment policy determines disclosure authorization and legal eligibility.
8. Observe finalized output
After acceptance, readswap_outputs[swap_id]. Do not construct claim arguments from the pre-trade quote. The mapping is authoritative and records:
- Output token
- Raw output amount
- Input token
- Raw unspent input amount
- Confidential recipient and caller addresses
9. Claim output and refund
Callclaim_swap_output with the same signer, blinding factor, and confidential address. Pass the exact token identifiers and amounts stored in swap_outputs.
On acceptance, the output and refund are returned as token records, and the pending mapping is removed. A second claim fails because the mapping no longer exists.
Claims are separate from trading pause checks. A pause that prevents a new swap does not, by itself, remove the normal claim path. Token transfer behavior and exact mapping validation still apply.
See Output claims and refunds.
10. Reconcile wallet state
A wallet should finish the workflow by recording:- The consumed input record as spent
- Any input change record
- The accepted swap transaction and
swap_id - The pending-output mapping state
- The claimed output record
- Any refund record
- The claim transaction and final mapping removal