Skip to content

Repository files navigation

>>WARNNING ON SCAMS IN ISSUES COMMENT SECTION<<

The issues comment section is often targeted by scam bots willing to redirect you to an external resource and drain your funds.

I have enabled a GitHub actions script to detect the common patterns and tag them, which obviously is not 100% accurate.

The official maintainers are in the MAINTAINERS.md file.

Not everyone is a scammer though, sometimes there are helpful outside devs who comment and I absolutely appreciate it.

>>END OF WARNING<<

TLDR quick run

This example uses the Raydium SDK v2 (@raydium-io/raydium-sdk-v2). The v2 SDK fetches pool data from Raydium's API/RPC, so there's no more ~500 MB mainnet.json to download and trim.

  1. Install dependencies with yarn.
  2. Set your Chainstack node and private key in .env (RPC_URL, WALLET_PRIVATE_KEY).
  3. In src/swapConfig.ts, set the poolId (the Raydium AMM v4 pool, default SOL-USDC), the inputMint and amount, and executeSwap (false simulates, true sends).

Run the swap:

yarn swap

Raydium SDK Swap Example

This project demonstrates how to perform a token swap on the Solana blockchain using Raydium and Chainstack. The example specifically illustrates swapping SOL (native Solana token) for USDC (a stablecoin).

Find the full guide on the Chainstack Developer Portal.

Shoutout to precious-void for the the base code used for this project!

Features

  • Uses the Raydium SDK v2 (@raydium-io/raydium-sdk-v2) to swap on Raydium AMM pools.
  • Fetches pool data from Raydium's API/RPC — no large local pool file to download or trim.
  • Builds versioned (v0) transactions with a configurable priority fee.
  • Simulates the swap before execution.
  • Easy configuration through a dedicated config file.

Prerequisites

Before you begin, ensure you have met the following requirements:

  • Node.js installed (v18 or above recommended)
  • Yarn
  • A Solana wallet with some SOL for testing the swap
  • An environment file (.env) with your RPC URL and WALLET_PRIVATE_KEY

Chainstack Solana node

Deploy a Solana node on Chainstack; the following steps will guide you:

  1. Sign up with Chainstack.
  2. Deploy a node.
  3. View node access and credentials.

Environment variables

Add your RPC endoint and private key to a .env file:

RPC_URL=YOUR_RPC_URL
WALLET_PRIVATE_KEY=YOUR_PRIVATE_KEY

Installation

Clone the repository locally and install the dependencies:

git clone https://github.com/chainstacklabs/raydium-sdk-swap-example-typescript.git
cd raydium-sdk-swap-example-typescript
yarn

Usage

Edit the configuration in src/swapConfig.ts:

  • executeSwapfalse simulates the swap, true sends it
  • poolId — the Raydium AMM v4 pool to swap on (default: SOL-USDC). Find one on raydium.io or via raydium.api.fetchPoolByMints
  • inputMint + tokenAAmount — the token you swap from, and how much
  • slippage — slippage tolerance (0.01 = 1%)
  • computeUnitPriceMicroLamports — priority fee, in micro-lamports per compute unit
export const swapConfig = {
  executeSwap: false, // true = send the transaction, false = simulate it
  poolId: "58oQChx4yWmvKdwLLZzBi4ChoCc2fqCUWBkwMihLYQo2", // SOL-USDC AMM v4 pool
  inputMint: "So11111111111111111111111111111111111111112", // the token you swap from (SOL)
  tokenAAmount: 0.001, // amount of inputMint to swap, in human units
  slippage: 0.01, // 1%
  computeUnitPriceMicroLamports: 100_000, // priority fee (micro-lamports per CU)
  maxRetries: 20,
};

Then run:

yarn swap

About

An example to swap tokens on Solana using the Raydium SDK, TypeScript, and Chainstack

Topics

Resources

Stars

186 stars

Watchers

6 watching

Forks

Used by

Contributors

Languages