@defiapis/raydium-swap v1.0.8
Raydium Swap Easy API
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.
Functions
1. trimMainnetJson()
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
},
];
2. swap() - returns promise, use await
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>",
};
Steps To Use
1. Initialize NPM on a new project
npm init -y
2. Install NPM Package
npm install @defiapis/raydium-swap
3. Create config file named config.js
in the root folder using example code
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 }
4. Download the latest Raydium mainnet.json to the project root (it's a ~500 MB file):
wget https://api.raydium.io/v2/sdk/liquidity/mainnet.json
4.1 Edit config.js
poolsToFetch variable with any pairs you will be trading
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.
4.2 Create trim.js
file in the root folder using example code
const { trimMainnetJson } = require("@defiapis/raydium-swap/trim");
const { poolsToFetch } = require("./config.js")
trimMainnetJson(poolsToFetch)
4.3 Run trim.js
file
node trim.js
5. Update config.js
file with your details
5.1 Update rpcUrl and walletPrivateKey
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.
5.2 Update tokenAAmount
Update tokenAAmount to the amount of tokenA you want to swap in config.js
.
5.3 Update tokenAAddress and tokenBAddress
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.
6. Set up swap file
6.1 Set up file named swap.js
with this example code
const { swap } = require('@defiapis/raydium-swap');
const { swapConfig } = require('./config.js')
(async () => {
let myTx = await swap(swapConfig);
console.log(`Signature: ${myTx}`);
})();
6.2 RUN SWAP
node swap.js
Fees
Note: we take a 0.1% fee on each swap using this api.