sdk.swap()
Build an unsigned swap transaction ready to be signed and submitted to the Solana network.Parameters (SwapRequest)
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
userWallet | string | yes | — | User’s wallet address (base58). Must have sufficient balance. |
inputMint | string | yes | — | Input token mint address |
outputMint | string | yes | — | Output token mint address |
amount | string | yes | — | Amount in smallest token units |
swapMode | "ExactIn" | "ExactOut" | yes | — | Swap direction |
slippageBps | number | no | 50 | Slippage tolerance in basis points |
skipSimulation | boolean | no | false | Skip pre-flight simulation. Faster but no validation. |
Response (SwapResponse)
Response Fields
| Field | Type | Description |
|---|---|---|
transaction | string | Base64-encoded unsigned v0 transaction |
lastValidBlockHeight | number | Transaction expires after this block height (~60 seconds) |
amountIn | string | Input amount |
amountOut | string | Estimated output amount |
minAmountOut | string? | Minimum output after slippage (ExactIn). Transaction reverts if actual output is less. |
maxAmountIn | string? | Maximum input after slippage (ExactOut). Transaction reverts if actual input exceeds this. |
feeAmount | string | Aggregator fee in output token units |
simulation | SimulationResult? | Simulation result (null if skipSimulation=true) |
computeUnitsEstimate | number? | Estimated compute units |
route | string[] | Token mint path in execution order |
hopCount | number | Number of swap hops |
pools | string[] | Pool addresses used, in execution order |
isSplitRoute | boolean? | Whether liquidity is split across pools |
splitPercents | number[]? | Per-pool percentage split (only if isSplitRoute=true) |
SimulationResult
| Field | Type | Description |
|---|---|---|
success | boolean | Whether simulation passed |
computeUnitsConsumed | number | CU consumed |
computeUnitsTotal | number | CU limit |
logs | string[] | Transaction logs |
error | string | Error message if simulation failed |
slippageExceeded | boolean | Whether slippage check failed |
insufficientFunds | boolean | Whether wallet has insufficient balance |
accountsNeeded | string[] | Missing accounts (e.g. ATAs that need creation) |
Sign and Submit
After receiving theSwapResponse, deserialize, sign, and submit the transaction.
With @solana/web3.js
With Wallet Adapter (React)
Notes
- The transaction includes automatic ATA (Associated Token Account) creation if the user doesn’t have token accounts for the swap tokens.
- Compute budget is set based on hop count and DEX complexity.
- Address Lookup Tables are used to keep the transaction within Solana’s size limits.
- Submit the transaction quickly — it expires after
lastValidBlockHeight(~60 seconds).