Created
May 29, 2024 12:09
-
-
Save 0xdeepmehta/5421323057cf5f9259bff13145747d06 to your computer and use it in GitHub Desktop.
send tx jito
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
async processTrancationJito( | |
jitoTip: number, // in ui | |
tx: Transaction, | |
luts?: AddressLookupTableAccount[], | |
priorityFee?: number, // priorityFeeUi | |
) { | |
console.log(`this.provider.connection.commitment :: ${this.provider.connection.commitment}`); | |
const jitoTipInLamport = jitoTip * LAMPORTS_PER_SOL; | |
console.log(`jitoTipInLamport :: ${jitoTipInLamport}`) | |
if (jitoTip == 0) { | |
throw Error("Jito bundle tip has not been set."); | |
} | |
if (priorityFee) { | |
const priorityFeeMicroLamports = priorityFee * LAMPORTS_PER_SOL * 1_000_000; | |
console.log(`priorityFeeMicroLamports :: ${priorityFeeMicroLamports}`) | |
tx.instructions.unshift( | |
ComputeBudgetProgram.setComputeUnitPrice({ | |
microLamports: Math.round(priorityFeeMicroLamports), | |
}) | |
); | |
} | |
// https://jito-foundation.gitbook.io/mev/mev-payment-and-distribution/on-chain-addresses | |
tx.instructions.push( | |
SystemProgram.transfer({ | |
fromPubkey: this.provider.publicKey, | |
toPubkey: new PublicKey( | |
"DttWaMuVvTiduZRnguLF7jNxTgiMBZ1hyAumKUiL2KRL" // Jito tip account | |
), | |
lamports: jitoTipInLamport, // tip | |
}) | |
); | |
const recentBlockhash = await this.provider.connection.getLatestBlockhash(); | |
let vTx: VersionedTransaction = new VersionedTransaction( | |
new TransactionMessage({ | |
payerKey: this.provider.publicKey, | |
recentBlockhash: recentBlockhash.blockhash, | |
instructions: tx.instructions, | |
}).compileToV0Message([...(luts ?? [])]) | |
); | |
const totalSize = vTx.message.serialize().length; | |
const totalKeys = vTx.message.getAccountKeys({ addressLookupTableAccounts: luts }).length; | |
console.log(`tx totalSize :: ${totalSize}`) | |
console.log(`tx totalKeys :: ${totalKeys}`) | |
if (totalSize > 1232 || totalKeys >= 64) { | |
console.log("tx size is too big") | |
return false | |
} | |
vTx = (await this.wallet.signTransaction(vTx)) as VersionedTransaction; | |
let rawTx = vTx.serialize(); | |
const messageEncoded = Buffer.from(vTx.message.serialize()).toString("base64"); | |
console.log(`------ messageEncoded 👇 ------ \n ${messageEncoded}`); | |
// console.log(this.provider.connection.simulateTransaction(vTx)) | |
const encodedTx = bs58.encode(rawTx); | |
const jitoURL = "https://mainnet.block-engine.jito.wtf/api/v1/transactions"; | |
const payload = { | |
jsonrpc: "2.0", | |
id: 1, | |
method: "sendTransaction", | |
params: [encodedTx, { | |
"maxRetries": 0, | |
"skipPreflight": true, | |
"preflightCommitment": "processed" | |
}], | |
}; | |
// let txOpts = commitmentConfig(provider.connection.commitment); | |
let txSig: string; | |
try { | |
const response = await axios.post(jitoURL, payload, { | |
headers: { "Content-Type": "application/json" }, | |
}); | |
console.log(`JitoResponse :: ${JSON.stringify(response.data)}`) | |
txSig = response.data.result; | |
console.log(`txSig :: ${txSig}`) | |
} catch (error) { | |
console.error("Error:", error); | |
throw new Error("Jito Bundle Error: cannot send."); | |
} | |
let currentBlockHeight = await this.provider.connection.getBlockHeight( | |
this.provider.connection.commitment | |
); | |
while (currentBlockHeight < recentBlockhash.lastValidBlockHeight) { | |
// Keep resending to maximise the chance of confirmation | |
const txSigHash = await this.provider.connection.sendRawTransaction(rawTx, { | |
skipPreflight: true, | |
preflightCommitment: this.provider.connection.commitment, | |
maxRetries: 0, | |
}); | |
console.log(txSigHash) | |
let signatureStatus = await this.provider.connection.getSignatureStatus(txSig); | |
console.log("signatureStatus", signatureStatus.value) | |
currentBlockHeight = await this.provider.connection.getBlockHeight( | |
this.provider.connection.commitment | |
); | |
if (signatureStatus.value != null) { | |
if ( | |
signatureStatus.value?.confirmationStatus === 'processed' || signatureStatus.value?.confirmationStatus === 'confirmed' || signatureStatus.value?.confirmationStatus === 'finalized' | |
) { | |
return txSig; | |
} | |
} | |
await sleep(500); // Don't spam the RPC | |
} | |
throw Error(`Transaction ${txSig} was not confirmed`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment