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);
}
})();