Created
August 20, 2024 23:10
-
-
Save belivenn/7d203c087ba8c031d79dd14ae11abbd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Transaction, SystemProgram, Connection, Keypair, LAMPORTS_PER_SOL, sendAndConfirmTransaction, PublicKey, ComputeBudgetProgram } from "@solana/web3.js" | |
| import wallet from "./ji76hHmEkFAUR7oHjrascx9USXp713WijtniVC5Bs6S.json" | |
| import bs58 from 'bs58' | |
| // Import our dev wallet keypair from the wallet file | |
| const from = Keypair.fromSecretKey(new Uint8Array(wallet)); | |
| // Define our WBA public key | |
| const to = new PublicKey("HZwspWyrNGik1AcgGBqN7VaCqs4ZHob8pkYUsgCFZ5FV"); | |
| //Create a Solana devnet connection | |
| const connection = new Connection("https://api.mainnet-beta.solana.com"); | |
| const jito_tip_1 = new PublicKey("ADuUkR4vqLUMWXxW9gh6D6L8pMSawimctcNZ5pGwDcEt"); | |
| const tipAmount = 1020000; | |
| async function sendBundle(transaction:Transaction[]): Promise<string> { | |
| let serializedTransactions = []; | |
| for (const tx of transaction) { | |
| serializedTransactions.push(bs58.encode(tx.serialize())) | |
| } | |
| const requestBody = { | |
| jsonrpc: "2.0", | |
| id: 1, | |
| method: "sendBundle", | |
| params: [serializedTransactions], | |
| }; | |
| console.log('Sending bundle with request body:', JSON.stringify(requestBody, null, 2)); | |
| const response = await fetch('https://amsterdam.testnet.block-engine.jito.wtf/api/v1/bundles', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| // console.log('Sending bundle with response body:', JSON.stringify(response, null, 2)); | |
| // console.log('resposta send bundle', response ) | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`Failed to send bundle: ${response.statusText} - ${errorText}`); | |
| } | |
| const data = await response.json(); | |
| if (!data || !data.result) { | |
| throw new Error('Failed to send bundle'); | |
| } | |
| console.log('data.resultado sendbundle :', data) | |
| return data.result; | |
| } | |
| async function checkBundleStatus(bundleId: string): Promise<any> { | |
| const requestBody = { | |
| jsonrpc: "2.0", | |
| id: 1, | |
| method: "getBundleStatuses", | |
| params: [[bundleId]], | |
| }; | |
| console.log('Checking bundle status with request body:', JSON.stringify(requestBody, null, 2)); | |
| const response = await fetch('https://amsterdam.testnet.block-engine.jito.wtf/api/v1/bundles', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| // console.log('Checking bundle status with response body:', JSON.stringify(response, null, 2)); | |
| //console.log('resposta check bundle', response ) | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`Failed to check bundle status: ${response.statusText} - ${errorText}`); | |
| } | |
| const data = await response.json(); | |
| if (!data || !data.result) { | |
| throw new Error('Failed to check bundle status'); | |
| } | |
| console.log('data.resultado checkbundle :', data) | |
| return data.result; | |
| } | |
| async function getInflightBundleStatuses(bundleId: string): Promise<any> { | |
| const requestBody = { | |
| jsonrpc: "2.0", | |
| id: 1, | |
| method: "getInflightBundleStatuses", | |
| params: [[bundleId]], | |
| }; | |
| console.log('Checking bundle status with request body:', JSON.stringify(requestBody, null, 2)); | |
| const response = await fetch('https://amsterdam.mainnet.block-engine.jito.wtf/api/v1/bundles', { | |
| method: 'POST', | |
| headers: { 'Content-Type': 'application/json' }, | |
| body: JSON.stringify(requestBody), | |
| }); | |
| // console.log('Checking bundle status with response body:', JSON.stringify(response, null, 2)); | |
| //console.log('resposta check bundle', response ) | |
| if (!response.ok) { | |
| const errorText = await response.text(); | |
| throw new Error(`Failed to check bundle status: ${response.statusText} - ${errorText}`); | |
| } | |
| const data = await response.json(); | |
| if (!data || !data.result) { | |
| throw new Error('Failed to check bundle status'); | |
| } | |
| console.log('data.resultado getInflightBundleStatuses :', data.result.value) | |
| return data.result; | |
| } | |
| (async () => { | |
| try { | |
| const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({ | |
| microLamports: 20000, | |
| }); | |
| const transaction = new Transaction().add( | |
| SystemProgram.transfer({ | |
| fromPubkey: from.publicKey, | |
| toPubkey: to, | |
| lamports: LAMPORTS_PER_SOL/100000, | |
| }) | |
| ).add(addPriorityFee); | |
| transaction.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash; | |
| transaction.feePayer = from.publicKey; | |
| transaction.sign(from); | |
| const transaction_jito = new Transaction().add( | |
| SystemProgram.transfer({ | |
| fromPubkey: from.publicKey, | |
| toPubkey: to, | |
| lamports: LAMPORTS_PER_SOL/100000, | |
| }), | |
| SystemProgram.transfer({ | |
| fromPubkey: from.publicKey, | |
| toPubkey: jito_tip_1, | |
| lamports: tipAmount, | |
| }) | |
| ); | |
| transaction_jito.recentBlockhash = (await connection.getLatestBlockhash('confirmed')).blockhash; | |
| transaction_jito.feePayer = from.publicKey; | |
| transaction_jito.sign(from); | |
| const bundleId = await sendBundle([transaction, transaction_jito]); | |
| const InflightBundleStatuses = await getInflightBundleStatuses(bundleId); | |
| const bundleStatus = await checkBundleStatus(bundleId); | |
| } catch(e) { | |
| console.error(`Oops, something went wrong: ${e}`) | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment