Skip to main content

Command Palette

Search for a command to run...

Building MEV-Protected Swaps on Solana with Jupiter + Jito

Published
2 min read

Building MEV-Protected Swaps on Solana with Jupiter + Jito

Jupiter is the #1 DEX aggregator on Solana, routing trades across 20+ DEXes for the best price. Here's how to integrate it programmatically.

Why Jupiter?

  • Best price across all Solana DEXes (Raydium, Orca, Phoenix, etc.)
  • Single API call for complex multi-hop routes
  • Built-in slippage protection
  • MEV protection via Jito bundles

Step 1: Get a Quote

const fetch = require('node-fetch');

async function getQuote(inputMint, outputMint, amount) {
  const url = `https://quote-api.jup.ag/v6/quote?` +
    `inputMint=${inputMint}&outputMint=${outputMint}` +
    `&amount=${amount}&slippageBps=50`;

  const response = await fetch(url);
  return response.json();
}

// Example: Swap 0.1 SOL to USDC
const SOL = 'So11111111111111111111111111111111111111112';
const USDC = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v';
const quote = await getQuote(SOL, USDC, 100000000); // 0.1 SOL in lamports
console.log(`Best route: ${quote.outAmount / 1e6} USDC`);

Step 2: Execute the Swap

const { Connection, Keypair, VersionedTransaction } = require('@solana/web3.js');

async function executeSwap(quoteResponse, wallet) {
  // Get serialized transaction from Jupiter
  const swapRes = await fetch('https://quote-api.jup.ag/v6/swap', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      quoteResponse,
      userPublicKey: wallet.publicKey.toBase58(),
      wrapAndUnwrapSol: true,
      dynamicComputeUnitLimit: true,
      prioritizationFeeLamports: 'auto',
    }),
  });

  const { swapTransaction } = await swapRes.json();

  // Deserialize and sign
  const txBuf = Buffer.from(swapTransaction, 'base64');
  const tx = VersionedTransaction.deserialize(txBuf);
  tx.sign([wallet]);

  // Send via Jito for MEV protection
  const connection = new Connection('https://api.mainnet-beta.solana.com');
  const txId = await connection.sendRawTransaction(tx.serialize(), {
    skipPreflight: false,
  });

  await connection.confirmTransaction(txId, 'confirmed');
  return txId;
}

Step 3: Add MEV Protection with Jito

Jito bundles protect your swap from sandwich attacks:

const { JitoJsonRpcClient } = require('jito-js-rpc');

const jito = new JitoJsonRpcClient(
  'https://mainnet.block-engine.jito.wtf/api/v1'
);

// Add a small tip to the bundle
const tipIx = SystemProgram.transfer({
  fromPubkey: wallet.publicKey,
  toPubkey: new PublicKey('96gYZGLnJYVFmbjzopPSU6QiEV5fGqZNyN9nmNhvrZU5'), // Jito tip account
  lamports: 10000, // 0.00001 SOL
});

// Send as Jito bundle instead of regular transaction
const bundleId = await jito.sendBundle([serializedTx]);

Production Tips

  1. Always use dynamic compute limits — saves on transaction fees
  2. Set reasonable slippage — 50 bps (0.5%) for majors, 100-300 bps for memecoins
  3. Add retry logic — Solana transactions fail sometimes, retry 2-3x
  4. Monitor price impact — reject trades with >5% price impact

Already Built: Use Sol Scanner Bot

If you don't want to build from scratch, @solscanitbot already has all of this:

  • /buy <mint> <sol> — Buy any token via Jupiter
  • /sell <mint> <amount> — Sell with MEV protection
  • /limit <buy|sell> <mint> <price> <amount> — Limit orders
  • /dca <mint> <sol> <interval> — Dollar cost averaging
  • /snipe on — Auto-buy new launches

44 commands total. Free to use.


Source code: Sol Scanner Bot | Built with Jupiter V6 + Jito

More from this blog

T

TateLyman

33 posts