>>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<<
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.
- Install dependencies with
yarn. - Set your Chainstack node and private key in
.env(RPC_URL,WALLET_PRIVATE_KEY). - In
src/swapConfig.ts, set thepoolId(the Raydium AMM v4 pool, default SOL-USDC), theinputMintand amount, andexecuteSwap(falsesimulates,truesends).
Run the swap:
yarn swap
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!
- 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.
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
Deploy a Solana node on Chainstack; the following steps will guide you:
Add your RPC endoint and private key to a .env file:
RPC_URL=YOUR_RPC_URL
WALLET_PRIVATE_KEY=YOUR_PRIVATE_KEYClone 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
yarnEdit the configuration in src/swapConfig.ts:
executeSwap—falsesimulates the swap,truesends itpoolId— the Raydium AMM v4 pool to swap on (default: SOL-USDC). Find one on raydium.io or viaraydium.api.fetchPoolByMintsinputMint+tokenAAmount— the token you swap from, and how muchslippage— 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