0% found this document useful (0 votes)
107 views2 pages

Solana Token Arbitrage Script

This document is a JavaScript code snippet for executing arbitrage between Raydium and Jupiter on the Solana blockchain. It includes functions to buy a token on Raydium and sell it on Jupiter using specified token mint addresses and a wallet's private key. The script connects to the Solana RPC, fetches available pools, computes routes for transactions, and handles errors during the process.

Uploaded by

arpadk426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views2 pages

Solana Token Arbitrage Script

This document is a JavaScript code snippet for executing arbitrage between Raydium and Jupiter on the Solana blockchain. It includes functions to buy a token on Raydium and sell it on Jupiter using specified token mint addresses and a wallet's private key. The script connects to the Solana RPC, fetches available pools, computes routes for transactions, and handles errors during the process.

Uploaded by

arpadk426
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

const { Connection, PublicKey, Keypair } = require("@solana/web3.

js");
const { Jupiter, TOKEN_LIST_URL } = require("@jup-ag/core");
const { amm } = require("@raydium-io/raydium-sdk");

const TOKEN_MINT = "CAxoUYhQ4B1kqcsgir8LLLfKPQYbUDP65qA9tE9VMCWq"; // Token Mint


Address
const SOLANA_RPC_URL = "https://api.mainnet-beta.solana.com"; // Solana RPC URL
const PRIVATE_KEY = []; // Add your wallet private key here

(async () => {
const connection = new Connection(SOLANA_RPC_URL, "confirmed");
const wallet = Keypair.fromSecretKey(Uint8Array.from(PRIVATE_KEY));

// Step 1: Buy token on Raydium


async function buyOnRaydium(amountIn) {
console.log("Buying on Raydium...");

const raydiumPools = await amm.fetchAllPools(connection);


const route = amm.findBestRoute({
inputMint: PublicKey.default, // Assuming SOL -> Update if needed
outputMint: new PublicKey(TOKEN_MINT),
pools: raydiumPools,
amountIn,
});

if (!route) {
throw new Error("No route found for buying on Raydium.");
}

const tx = await amm.createTransaction(connection, wallet, route, amountIn);


const signature = await connection.sendTransaction(tx, [wallet]);
await connection.confirmTransaction(signature, "confirmed");

console.log("Bought tokens on Raydium:", signature);


}

// Step 2: Sell token on Jupiter


async function sellOnJupiter(amountOutMin) {
console.log("Selling on Jupiter...");

const jupiter = await Jupiter.load({


connection,
cluster: "mainnet-beta",
user: wallet,
});

const routes = await jupiter.computeRoutes({


inputMint: new PublicKey(TOKEN_MINT),
outputMint: PublicKey.default, // Assuming SOL -> Update if needed
inputAmount: amountOutMin,
});

if (!routes || routes.routesInfos.length === 0) {


throw new Error("No route found for selling on Jupiter.");
}

const { execute } = await jupiter.exchange({


routeInfo: routes.routesInfos[0],
});
const { txid } = await execute();
console.log("Sold tokens on Jupiter:", txid);
}

// Execute Arbitrage
try {
const buyAmount = 1 * 10 ** 9; // Adjust based on decimals
await buyOnRaydium(buyAmount);

const sellAmount = 0.95 * buyAmount; // Example, adjust for slippage


await sellOnJupiter(sellAmount);
} catch (error) {
console.error("Error during arbitrage:", error);
}
})();

You might also like