Skip to main content

Version info

  • SDK: @raydium-io/raydium-sdk-v2@0.2.42-alpha
  • Network: mainnet-beta
  • Router program ID: routeUGWgWzqBWFcrCfv8tritsqukccJPu3q5GPP3xS
  • Verified: April 2026

Example 1: SDK-based routing

Source: src/trade/routeSwap.ts The Raydium SDK abstracts route building. Use the SDK’s trade functions to compose a multi-hop route and execute it through the router automatically.

Setup

Build a multi-hop swap

Routing in @raydium-io/raydium-sdk-v2 is exposed on raydium.tradeV2. The end-to-end shape — fetching pool data, computing routes, ranking by output, and building the swap transaction — is shown below; this matches the canonical example in raydium-sdk-V2-demo/src/trade/routeSwap.ts.

Expected behavior

The SDK handles:
  • Route discovery across AMM v4, CPMM, CLMM, and Stable AMM.
  • Account derivation (pool states, vaults, observation accounts, ATA pre-creation).
  • Instruction packing for the router program (Router) when the route is multi-hop, or a direct pool swap when a single pool already gives the best price.
  • Slippage enforcement via the slippage parameter on getAllRouteComputeAmountOut.
raydium.tradeV2.swap may return more than one transaction — the first commonly initializes intermediate ATAs and the second performs the swap itself. Always pass sequentially: true to execute() so they confirm in order.

Example 2: Raw instruction building (Rust-like pseudocode)

If you need finer control or are building a program that CPIs into the router, construct instructions manually. The example below uses tag 8 (SwapBaseIn) — the recommended Current variant — and routes through user-owned ATAs end to end.

Scenario: USDC → SOL (CPMM) → mSOL (CPMM)

Step 1: Derive the user’s ATAs

Step 2: Gather accounts for each hop

Hop 1 is USDC/SOL on CPMM. Hop 2 is SOL/mSOL on CPMM.

Step 3: Build the instruction

Step 4: Send transaction


Example 3: Error handling

Common errors and how to recover:

ExceededSlippage

The output was less than minimum_amount_out. Retry with higher slippage tolerance or re-quote the route.

SqrtPriceX64 (CLMM)

A CLMM hop’s price drifted outside the limit_prices bounds. Update the bounds and retry.

InvalidOwner

An intermediate or output ATA is not owned by the caller. The router validates ownership on every slot; ensure each ATA you pass was derived from the user’s wallet (not from any other authority).

Tips and best practices

Pre-create intermediate ATAs

Before routing through a new intermediate token for the first time, create the user’s ATA so the route does not fail validation:
For wSOL specifically, prefer CreateSyncNative (tag 5) — it creates the ATA, transfers the SOL, and syncs in one instruction.

Quote before executing

Always query the pools and compute expected output before building the instruction:

Use the newer instruction variants (8–9)

Tags 8 and 9 (SwapBaseIn and SwapBaseOut) are more forgiving with limit_prices. Prefer them over the legacy variants if you don’t need CLMM price validation.

Where to go next

Sources: