Skip to main content
Version banner. All demos target @raydium-io/raydium-sdk-v2@0.2.42-alpha against Solana mainnet-beta, verified 2026-04. Program ID: 675kPX9MHTjS2zt1qfr1NYHuzeLXfQM9H24wFSUt1Mp8 (see reference/program-addresses).
New-pool creation is not shown here. The Raydium UI no longer offers AMM v4 pool creation — new pairs default to CPMM. The AMM v4 program itself still accepts Initialize2 on-chain; it just isn’t the recommended path. The demos below cover the live-pool operations every integrator still needs: swap, deposit, withdraw.

Setup

Fetch a pool by id

poolKeys is the struct the instruction builders consume. It carries every AMM v4 and OpenBook account in the order the program expects.

Swap (base-in)

The SDK routes AMM v4 swaps through the V2 entrypoints, which do not carry the OpenBook accounts. (After the 2026-07 upgrade, the market accounts are no longer validated even on the legacy v1 path.)

Swap (base-out)

Add liquidity

fixedSide: "a" tells the SDK you supplied the exact amountInA and that amountInB should be at-most maxAnotherAmount. The pool’s on-book liquidity is settled before the pro-rata math so the deposit ratio matches the freshest reserves.

Remove liquidity

Slippage mins protect against the pool’s state shifting between your pre-quote and the land time.

Compute-unit / priority fee tuning

AMM v4 swaps are heavy on compute because every instruction validates the full OpenBook state. A typical swap uses 180k–250k CU depending on how many open orders need settling on the way through. Always pass a compute-unit limit:
If you omit computeBudgetConfig, the SDK may still use its own default; inspect innerTransactions to confirm. See integration-guides/priority-fee-tuning.

Direct Rust CPI

If you must CPI into AMM v4 from your own Anchor program, you will need to model SwapBaseIn’s account list verbatim. A minimal sketch:
AMM v4 does not ship an Anchor crate for CPI. The sketch above uses a manually-constructed Instruction.
The CPI sketch above uses the legacy v1 SwapBaseIn layout (tag 9, 17 accounts) for reading/reproducing existing transactions. Since the 2026-07 upgrade the market accounts are accepted but ignored. For new code, prefer SwapBaseInV2 / SwapBaseOutV2 (tags 16 / 17), which drop the market accounts (and amm_open_orders) entirely — pass only 8 accounts: token_program, amm, amm_authority, the two pool vaults, the two user token accounts, and user_owner.

Pitfalls

  • Wrong account count on v1 swaps. The legacy SwapBaseIn / SwapBaseOut still require the full 17-account (or 18-account) list — a mismatched count reverts with WrongAccountsNumber. The market accounts’ contents are no longer validated, but you must still occupy their slots. Prefer the V2 entrypoints to avoid this.
  • Reading raw vault balances. Reserves are now vault-only; subtract accrued PnL (need_take_pnl_*). The SDK quote or api-v3.raydium.io/pools/info/ids handles this for you.
  • Calling removed instructions. MonitorStep, MigrateToOpenBook, WithdrawSrm, SimulateInfo, AdminCancelOrders, and legacy Initialize / PreInitialize now revert. Use Initialize2 for pool creation; there is no crank to call.
  • Token-2022 mints. Not supported. An AMM v4 pool cannot be created against a Token-2022 mint; any Token-2022 pair should be on CPMM or CLMM.

Where to go next

Sources: