Skip to content

javascript ​

Send a bundle using JavaScript.

Using Fetch ​

javascript
const ENDPOINT = "https://fra.blocksprint.io/bundle/v1/YOUR_API_KEY";

export async function sendBundle(transactions) {
  // Serialize all transactions to base64
  const serializedTxs = transactions.map((tx) =>
    Buffer.from(tx.serialize()).toString("base64")
  );

  const response = await fetch(ENDPOINT, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({
      jsonrpc: "2.0",
      id: 1,
      method: "sendBundle",
      params: [
        serializedTxs,
        {
          encoding: "base64",
          skipPreflight: true,
        },
      ],
    }),
  });

  const result = await response.json();

  if (result.error) {
    throw new Error(result.error.message);
  }

  return result.result;
}

Full Arbitrage Example ​

javascript
import {
  Connection,
  Keypair,
  PublicKey,
  SystemProgram,
  Transaction,
} from "@solana/web3.js";
import { sendBundle } from "./sendBundle.js";

// Your Solana RPC (for the blockhash)
const RPC_URL = "https://api.mainnet-beta.solana.com";
// BlockSprint tip address, tip at least 0.001 SOL
const TIP_ADDRESS = new PublicKey("Sp4JHSh9cksfzXbgK7Pq2ovtn8LirLQydaJKTsiNT77");

async function executeArbitrage(wallet, dexA, dexB, amount) {
  const connection = new Connection(RPC_URL);

  // Get blockhash (same for all transactions in bundle)
  const { blockhash } = await connection.getLatestBlockhash();

  // Transaction 1: Buy on DEX A
  const buyTx = new Transaction();
  buyTx.add(dexA.createBuyInstruction(wallet.publicKey, amount));
  buyTx.recentBlockhash = blockhash;
  buyTx.feePayer = wallet.publicKey;
  buyTx.sign(wallet);

  // Transaction 2: Sell on DEX B, plus the 0.001 SOL tip
  const sellTx = new Transaction();
  sellTx.add(dexB.createSellInstruction(wallet.publicKey, amount));
  sellTx.add(
    SystemProgram.transfer({
      fromPubkey: wallet.publicKey,
      toPubkey: TIP_ADDRESS,
      lamports: 1_000_000,
    })
  );
  sellTx.recentBlockhash = blockhash;
  sellTx.feePayer = wallet.publicKey;
  sellTx.sign(wallet);

  // Send as bundle
  const result = await sendBundle([buyTx, sellTx]);

  console.log("Bundle ID:", result.bundle_id);
  console.log("Signatures:", result.signatures);

  return result;
}

Multi-Step Transaction ​

javascript
import {
  Connection,
  PublicKey,
  SystemProgram,
  Transaction,
} from "@solana/web3.js";
import { sendBundle } from "./sendBundle.js";

// Your Solana RPC (for the blockhash)
const RPC_URL = "https://api.mainnet-beta.solana.com";
// BlockSprint tip address, tip at least 0.001 SOL
const TIP_ADDRESS = new PublicKey("Sp4JHSh9cksfzXbgK7Pq2ovtn8LirLQydaJKTsiNT77");

async function executeMultiStep(wallet, steps) {
  const connection = new Connection(RPC_URL);
  const { blockhash } = await connection.getLatestBlockhash();

  const transactions = steps.map((step) => {
    const tx = new Transaction();
    tx.add(step.instruction);
    tx.recentBlockhash = blockhash;
    tx.feePayer = wallet.publicKey;
    tx.sign(wallet);
    return tx;
  });

  return sendBundle(transactions);
}

// Usage
const steps = [
  { instruction: approveInstruction },
  { instruction: swapInstruction },
  { instruction: transferInstruction },
  {
    instruction: SystemProgram.transfer({
      fromPubkey: wallet.publicKey,
      toPubkey: TIP_ADDRESS,
      lamports: 1_000_000, // 0.001 SOL tip
    }),
  },
];

await executeMultiStep(wallet, steps);

Error Handling ​

javascript
import { sendBundle } from "./sendBundle.js";

async function sendBundleSafe(transactions) {
  try {
    const result = await sendBundle(transactions);
    return { success: true, result };
  } catch (error) {
    if (error.message.includes("tip")) {
      console.error("Add a tip of at least 0.001 SOL:", error.message);
    } else if (error.message.includes("Rate limit")) {
      console.error("Rate limited, retry later");
    }
    return { success: false, error: error.message };
  }
}