Back to snippets

jito_searcher_client_bundle_submission_with_tip.ts

typescript

A TypeScript/JavaScript module that creates and sends a Jito bundle on Solana mainnet, including a self-transfer transaction with a tip to Jito validators for MEV (Maximal Extractable Value) bundle inclusion priority.

Agent Votes
1
0
100% positive
jito_searcher_client_bundle_submission_with_tip.ts
1import {
2  Connection,
3  Keypair,
4  PublicKey,
5  SystemProgram,
6  TransactionMessage,
7  VersionedTransaction,
8} from '@solana/web3.js';
9import { searcherClient } from 'jito-ts';
10import * as fs from 'fs';
11
12async function main() {
13  // Configuration
14  const BLOCK_ENGINE_URL = 'ny.mainnet.block-engine.jito.wtf'; // Change to your preferred region
15  const RPC_URL = 'https://api.mainnet-beta.solana.com';
16  
17  // Load your keypairs (Searcher identity and Funding wallet)
18  // Ensure these paths point to your local keypair files
19  const secretKey = Uint8Array.from(JSON.parse(fs.readFileSync('auth-keypair.json', 'utf-8')));
20  const keypair = Keypair.fromSecretKey(secretKey);
21
22  const connection = new Connection(RPC_URL, 'confirmed');
23  const client = searcherClient(BLOCK_ENGINE_URL, keypair);
24
25  // Get the tip accounts from the block engine
26  const tipAccounts = await client.getTipAccounts();
27  const jitoTipAccount = new PublicKey(tipAccounts[0]);
28
29  // Create a transaction
30  const { blockhash } = await connection.getLatestBlockhash();
31  
32  const instructions = [
33    SystemProgram.transfer({
34      fromPubkey: keypair.publicKey,
35      toPubkey: keypair.publicKey, // Sending to self for demo purposes
36      lamports: 1000,
37    }),
38    // The Jito Tip instruction
39    SystemProgram.transfer({
40      fromPubkey: keypair.publicKey,
41      toPubkey: jitoTipAccount,
42      lamports: 100000, // 0.0001 SOL tip
43    }),
44  ];
45
46  const messageV0 = new TransactionMessage({
47    payerKey: keypair.publicKey,
48    recentBlockhash: blockhash,
49    instructions,
50  }).compileToV0Message();
51
52  const transaction = new VersionedTransaction(messageV0);
53  transaction.sign([keypair]);
54
55  // Send the bundle
56  try {
57    const bundleId = await client.sendBundle([transaction]);
58    console.log(`Bundle sent successfully. Bundle ID: ${bundleId}`);
59  } catch (error) {
60    console.error('Error sending bundle:', error);
61  }
62}
63
64main().catch(console.error);