Skip to main content
Version banner. All TypeScript demos target @raydium-io/raydium-sdk-v2@0.2.42-alpha against Solana mainnet-beta, verified 2026-04. The Rust CPI skeleton targets raydium-cp-swap on the master branch, Anchor 0.30.x. Program IDs are pulled via constants from reference/program-addresses.

Prerequisites

Every demo on this page mirrors a file in raydium-sdk-V2-demo/src/cpmm; the GitHub link sits next to each section. Bootstrap follows the demo repo’s config.ts.template (source):
The Raydium instance is the SDK’s facade — every demo below uses it. It lazily fetches token lists and fee configs from api-v3.raydium.io; you can seed it with your own data in offline environments.

Create a CPMM pool

Source: src/cpmm/createCpmmPool.ts
A few things the SDK quietly takes care of:
  • Sorting the mints into token0/token1 order before deriving the PDA.
  • Paying the one-time create_pool_fee to poolFeeAccount.
  • Creating the caller’s associated token accounts if missing.
  • Choosing the right token program (SPL Token vs Token-2022) per side.
After confirmation you can fetch the live pool state with:

Swap (base-input)

Source: src/cpmm/swap.ts
Note: the SDK always re-fetches the pool state from an RPC inside getPoolInfoFromRpc. Do not quote off api-v3.raydium.io for a transaction you are about to sign — a quote that is one block stale can slip into ExceededSlippage at land time.

Swap (base-output)

Source: src/cpmm/swapBaseOut.ts

Deposit liquidity

Source: src/cpmm/deposit.ts
The SDK converts lpAmount into needed_token_0 and needed_token_1 using the pool’s current reserves, inflates each by 1 + slippage for the instruction’s maximum_* arguments, and builds the ATA creations if necessary.

Withdraw liquidity

Source: src/cpmm/withdraw.ts

Collect protocol/fund/creator fees

Source: src/cpmm/collectCreatorFee.ts, src/cpmm/collectAllCreatorFee.ts These instructions are admin- or creator-gated and typically invoked from a signer held by the Raydium multisig or the pool creator. The SDK surfaces them as raw builders:
Off-chain you can read accrued fees directly from PoolState:

Rust CPI skeleton

If you want to invoke CPMM from your own Anchor program — for example, a vault that swaps on behalf of its depositors — the CPI context looks like this. Account ordering follows products/cpmm/instructions.
If your CPI signs as a PDA (e.g., you manage a vault on behalf of depositors), swap CpiContext::new for CpiContext::new_with_signer and pass your seeds.

Common pitfalls

A short checklist before opening a support ticket:
  • Sorted mints. If your derived poolState PDA does not match the on-chain pool, you probably forgot to sort the mints.
  • Stale API quote. Never pass a reserve value from api-v3.raydium.io into CurveCalculator.swap. Fetch from an RPC.
  • Wrong token program. A Token-2022 mint’s vault is owned by the Token-2022 program, not by SPL Token. Always use the pool’s token_0_program / token_1_program fields.
  • Slippage under-denominated for transfer-fee mints. If either side of the pool is a Token-2022 transfer-fee mint, your minimum_amount_out must be denominated in what the user actually receives, not in what the vault sends.
  • NotApproved on a swap. Check PoolState.status — the admin may have paused swaps on that pool. See products/cpmm/instructions for the status bitmask.

Where to go next

Sources: