Package for performing token swaps on Solana - Jito integrated ๐
Made with the Public and Free Swap API by Solxtence <3
Useful for Sniper Bots, Volume Bots, Trading bots, Swap Platforms, DeFi applications...
- Pump.fun
- Moonshot
- Raydium
- Raydium CPMM
- Orca
- Jupiter AMMs
Your private key is never sent to the Solxtence API or any third party. It's only used locally to sign the transaction in your code. However, it's always recommended to review the code of any packages you are using for potential security issues or malicious code.
- Install Node.js from nodejs.org
-
Create a new project folder
mkdir my-solana-swap cd my-solana-swap
-
Initialize your project
npm init -y
-
Install the
solana-swap
packagenpm i @solxtence/solana-swap
-
Create a new file named
swap.js
in your project folder
Open swap.js
in a text editor and let's build the script step by step:
-
Import required modules:
const { Keypair } = require("@solana/web3.js"); const bs58 = require("bs58"); const { SolanaSwap } = require("@solxtence/solana-swap");
This imports the necessary functions and classes we'll use.
-
Create the main function:
async function swapIt(useJito = false, jitoTip = 0.002) { // `jitoTip` in SOL amount // We'll add the swap logic here }
This function will contain our swap logic.
-
Set up the keypair:
const privateKey = "YOUR_PRIVATE_KEY_HERE"; const keypair = Keypair.fromSecretKey(bs58.decode(privateKey));
Replace
YOUR_PRIVATE_KEY_HERE
with your actual Solana wallet private key. This creates a keypair for signing transactions. -
Initialize SolanaSwap:
const solanaSwap = new SolanaSwap( keypair, "https://api.mainnet-beta.solana.com" );
This sets up the SolanaSwap instance with your keypair and a Solana RPC endpoint.
-
Get swap instructions:
const swapResponse = await solanaSwap.getSwapInstructions( "So11111111111111111111111111111111111111112", // From Token (SOL) "4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R", // To Token (example) 0.005, // Amount to swap 10, // Slippage tolerance (in percentage) keypair.publicKey.toBase58(), 0.0005, // Priority fee useJito ? jitoTip : undefined // If `useJito` is true, a jito tip instruction will be included in the TX );
This fetches the instructions from our Swap API for the swap. Customize the token addresses, amount, slippage, and fees as needed.
-
Perform the swap:
try { const txid = await solanaSwap.performSwap(swapResponse, { sendConfig: { skipPreflight: true }, maxConfirmationAttempts: 30, confirmationTimeout: 500, commitmentLevel: "processed", useJito, }); console.log("TX Hash:", txid); console.log("TX on Solscan:", `https://solscan.io/tx/${txid}`); } catch (error) { console.error("Error while performing the swap:", error.message); }
This executes the swap and handles the result, whether successful or not.
-
Call the function:
swapIt(false); // Set to true to use Jito for faster transactions
If you want to send the transaction with Jito, excute the function like so:
swapIt(true, 0.003); // Adjust the number of your jito tip
Find the full example here.
Run your script using:
node swap.js
- Always double-check token addresses and amounts before swapping
- Keep your private key secret and secure
- It's good practice to review any code that interacts with your wallet or performs financial transactions
- If you get an error about missing modules, make sure you've installed all required packages (step 3)
- If the swap fails, check that you have enough balance and that the token addresses are correct
- If any unexpected error happens, just open an issue here and we will fix it as soon as possible <3
Remember, when dealing with real funds, start with small amounts to test everything works correctly!
No, the Swap API is completely free - no fees included!