Last active
July 10, 2024 04:41
-
-
Save zhe-t/60938c69e29276b7a9f098e1b0672c79 to your computer and use it in GitHub Desktop.
Smart send a transaction using Jito dedicated endpoint
This file contains 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 { Connection, SendOptions } from '@solana/web3.js'; | |
export type JitoRegion = 'mainnet' | 'amsterdam' | 'frankfurt' | 'ny' | 'tokyo'; | |
export const JitoEndpoints = { | |
mainnet: 'https://mainnet.block-engine.jito.wtf/api/v1/transactions', | |
amsterdam: 'https://amsterdam.mainnet.block-engine.jito.wtf/api/v1/transactions', | |
frankfurt: 'https://frankfurt.mainnet.block-engine.jito.wtf/api/v1/transactions', | |
ny: 'https://ny.mainnet.block-engine.jito.wtf/api/v1/transactions', | |
tokyo: 'https://tokyo.mainnet.block-engine.jito.wtf/api/v1/transactions', | |
}; | |
export function getJitoEndpoint(region: JitoRegion) { | |
return JitoEndpoints[region]; | |
} | |
/** | |
* Send a transaction using Jito. This only supports sending a single transaction on mainnet only. | |
* See https://jito-labs.gitbook.io/mev/searcher-resources/json-rpc-api-reference/transactions-endpoint/sendtransaction. | |
* @param args.serialisedTx - A single transaction to be sent, in serialised form | |
* @param args.region - The region of the Jito endpoint to use | |
*/ | |
export async function sendTxUsingJito({ | |
serializedTx, | |
region = 'mainnet' | |
}: { | |
serializedTx: Uint8Array | Buffer | number[]; | |
region: JitoRegion; | |
}) { | |
let rpcEndpoint = getJitoEndpoint(region); | |
let encodedTx = bs58.encode(serializedTx); | |
let payload = { | |
jsonrpc: "2.0", | |
id: 1, | |
method: "sendTransaction", | |
params: [encodedTx] | |
}; | |
let res = await fetch(`${rpcEndpoint}?bundleOnly=true`, { | |
method: 'POST', | |
body: JSON.stringify(payload), | |
headers: { 'Content-Type': 'application/json' } | |
}); | |
let json = await res.json(); | |
if (json.error) { | |
throw new Error(json.error.message); | |
} | |
return json; | |
} |
Thx! Updated
hi bro, how to build the serializedTx, only add tip instruction in it, right ? for example, one transfer tx, only contain transfer ins,if i want to send jito, i need add another ins for the tip, some thing wrong?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello ser, you have the wrong endpoints. otherwise the txn won't go thru.
if you add ?bundleOnly=true at the end, then you can send bundles.
https://jito-labs.gitbook.io/mev/searcher-resources/json-rpc-api-reference/transactions-endpoint/sendtransaction