Skip to main content
What this does. Loads a CPMM pool from RPC, quotes a swap with 0.5% slippage, builds the transaction, signs with your keypair, and submits it. End-to-end in ~30 lines.

Setup

Make sure you’ve read the Quick start prerequisites and have RPC_URL, KEYPAIR, and the deps installed.

The script

Save as swap.mjs:

Run it

Pick any CPMM pool you have liquidity for. Example with the canonical SOL/USDC CPMM pool:
Expected output:

What just happened

  1. Raydium.load initialised the SDK — fetched the global config, set up your wallet context.
  2. getPoolInfoFromRpc pulled the live pool state directly from RPC (not from the API cache). For high-value swaps you always want fresh state.
  3. CurveCalculator.swap computed the constant-product output net of the pool’s fee. This is the same math the program runs on-chain, so you can compare quotes off- and on-chain.
  4. raydium.cpmm.swap built the transaction with V0 format (address lookup tables enabled) and added an explicit compute-budget config. The compute-budget tip helps the tx land in busy windows.
  5. execute({ sendAndConfirm: true }) signed, sent, and waited for confirmation.

Common errors

  • Pool not found — Wrong POOL_ID, or you’re pointed at the wrong cluster (mainnet pool ID against a devnet RPC, etc.).
  • Insufficient funds for transaction — Your wallet doesn’t have enough SOL for the swap input + fees + ATA rent.
  • Slippage tolerance exceeded — The pool’s price moved between quote and execution. Re-run; or raise the slippage parameter; or use the SDK’s computeAmountOut which always re-fetches reserves.
  • Token account not initialized — Output token’s ATA didn’t exist and the implicit-create instruction landed but failed for some reason; check your wallet’s SOL balance and try again.

Next