Visit github.com/defiapis/raydium-swap for an easy example you can clone and use in under 5 minutes.
Note: We take a 0.1% fee on each trade using this api.
const { trimMainnetJson } = require("@defiapis/raydium-swap/trim");
Takes list of token pairs:
const poolsToFetch = [
{
tokenAAddress: "So11111111111111111111111111111111111111112", // SOL
tokenBAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
},
{
tokenAAddress: "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // RAY
tokenBAddress: "So11111111111111111111111111111111111111112", // SOL
},
];
const { swap } = require("@defiapis/raydium-swap");
Takes swapConfig object:
const swapConfig = {
executeSwap: true, // Send tx when true, simulate tx when false
useVersionedTransaction: true,
tokenAAmount: 0.01, // Swap 0.01 SOL for USDC in this example
tokenAAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case
tokenBAddress: "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // USDC address
maxLamports: 150000, // Micro lamports for priority fee
direction: "in", // Swap direction: 'in' or 'out'
liquidityFile: `${__dirname}/trimmed_mainnet.json`,
maxRetries: 20,
rpcUrl: "<your rpc url>",
walletPrivateKey: "<your wallet private key>",
};
npm init -y
npm install @defiapis/raydium-swap
const swapConfig = {
executeSwap: true, // Send tx when true, simulate tx when false
useVersionedTransaction: true,
tokenAAmount: 0.01, // Swap 0.01 SOL for USDC in this example
tokenAAddress: "So11111111111111111111111111111111111111112", // Token to swap for the other, SOL in this case
tokenBAddress: "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // USD address
maxLamports: 150000, // Micro lamports for priority fee
direction: "in", // Swap direction: 'in' or 'out'
liquidityFile: `${__dirname}/trimmed_mainnet.json`,
maxRetries: 20,
rpcUrl: "<your rpc url>",
walletPrivateKey: "<your wallet private key>",
pathToTrimmedMainnet: "./"
};
const poolsToFetch = [
{
tokenAAddress: "So11111111111111111111111111111111111111112", // SOL
tokenBAddress: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC
},
{
tokenAAddress: "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // RAY
tokenBAddress: "So11111111111111111111111111111111111111112", // SOL
}
];
module.exports = { swapConfig, poolsToFetch }
wget https://api.raydium.io/v2/sdk/liquidity/mainnet.json
Edit the config.js file with any pairs you will be trading. Note: SOL-USDC is the same pool as USDC-SOL so you only need to add one.
const { trimMainnetJson } = require("@defiapis/raydium-swap/trim");
const { poolsToFetch } = require("./config.js")
trimMainnetJson(poolsToFetch)
node trim.js
IMPORTANT: Update rpcUrl
with your RPC url. We recommend ChainStack. Update walletPrivateKey
with your wallet private key.
Optionally create a .env file and use values from there in case you publish your code to github.
Update tokenAAmount to the amount of tokenA you want to swap in config.js
.
Update tokenAAddress and tokenBAddress to the token mint addresses of the two tokens you are swapping. Again, make sure you have added these pools to poolsToFetch and have run trimMainnetJson.
const { swap } = require('@defiapis/raydium-swap');
const { swapConfig } = require('./config.js')
(async () => {
let myTx = await swap(swapConfig);
console.log(`Signature: ${myTx}`);
})();
node swap.js
Note: we take a 0.1% fee on each swap using this api.