JavaScript
Use full service RPC for fetching latest blockhash.
Two transactions from one signer, tip in the first, sent as one atomic bundle. Both are signed with the same blockhash so they expire together.
import { Connection, Keypair, PublicKey, SystemProgram, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import bs58 from "bs58";
const NOZOMI_ENDPOINT = "https://nozomi.temporal.xyz";
const NOZOMI_API_KEY = "<YOUR_API_KEY>";
const NOZOMI_TIP = new PublicKey("TEMPaMeCRFAS9EKF53Jd6KpHxgL47uWLcpFArU1Fanq");
const TIP_LAMPORTS = 1_000_000; // 0.001 SOL, the default minimum
const SOLANA_RPC_ENDPOINT = "https://api.mainnet-beta.solana.com";
function encodeBundle(rawTxs) {
if (rawTxs.length === 0 || rawTxs.length > 4) {
throw new Error("bundle must contain 1-4 transactions");
}
let total = 0;
for (const tx of rawTxs) {
if (tx.length < 66 || tx.length > 1232) {
throw new Error(`invalid tx size: ${tx.length}`);
}
total += 2 + tx.length;
}
const out = Buffer.allocUnsafe(total);
let off = 0;
for (const tx of rawTxs) {
out.writeUInt16BE(tx.length, off);
off += 2;
Buffer.from(tx).copy(out, off);
off += tx.length;
}
return out;
}
async function sendBundle(endpoint, apiKey, rawTxs) {
const res = await fetch(`${endpoint}/api/sendBundle?c=${apiKey}`, {
method: "POST",
headers: { "Content-Type": "application/octet-stream" },
body: encodeBundle(rawTxs),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`sendBundle failed (${res.status}): ${text}`);
}
}
function signTx(ixns, signer, blockhash) {
const message = new TransactionMessage({
payerKey: signer.publicKey,
recentBlockhash: blockhash,
instructions: ixns,
}).compileToV0Message();
const tx = new VersionedTransaction(message);
tx.sign([signer]);
return tx;
}
function buildFirstIxns() {
// your instruction building logic for the first transaction..
return [];
}
function buildSecondIxns() {
// your instruction building logic for the second transaction..
return [];
}
async function main() {
const rpc = new Connection(SOLANA_RPC_ENDPOINT);
const signer = Keypair.generate(); // replace with actual keypair loading logic
// One blockhash for every member so the bundle expires as a unit.
const { blockhash } = await rpc.getLatestBlockhash();
// Tip goes in the first member; the second is untipped.
const first = signTx(
[
...buildFirstIxns(),
SystemProgram.transfer({ fromPubkey: signer.publicKey, toPubkey: NOZOMI_TIP, lamports: TIP_LAMPORTS }),
],
signer,
blockhash
);
const second = signTx(buildSecondIxns(), signer, blockhash);
// sendBundle returns no signatures: record them before submitting.
const signatures = [first, second].map((tx) => bs58.encode(tx.signatures[0]));
console.log("bundle signatures", signatures);
await sendBundle(NOZOMI_ENDPOINT, NOZOMI_API_KEY, [first.serialize(), second.serialize()]);
console.log("bundle accepted");
// Confirm landing: every member lands in the same slot, or none does.
const statuses = await rpc.getSignatureStatuses(signatures);
console.log(statuses.value.map((s) => s?.slot ?? null));
}
main().catch((err) => {
console.error(err);
});Last updated

