-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjitoService.js
More file actions
148 lines (128 loc) · 3.81 KB
/
Copy pathjitoService.js
File metadata and controls
148 lines (128 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const axios = require("axios");
const { PublicKey, SystemProgram, Transaction, VersionedTransaction } = require("@solana/web3.js");
const bs58 = require("bs58");
const { JITO_RPC_URL, SOLANA_RPC_URL } = require("./config");
const { Connection } = require("@solana/web3.js");
const connection = new Connection(SOLANA_RPC_URL);
async function getTipAccounts() {
try {
const response = await axios.post(
JITO_RPC_URL,
{
jsonrpc: "2.0",
id: 1,
method: "getTipAccounts",
params: [],
},
{
headers: { "Content-Type": "application/json" },
}
);
if (response.data.error) {
throw new Error(response.data.error.message);
}
return response.data.result;
} catch (error) {
console.error("❌ Error getting tip accounts:", error.message);
throw error;
}
}
async function createJitoBundle(transaction, wallet) {
try {
const tipAccounts = await getTipAccounts();
if (!tipAccounts || tipAccounts.length === 0) {
throw new Error("❌ Failed to get Jito tip accounts");
}
const tipAccountPubkey = new PublicKey(
tipAccounts[Math.floor(Math.random() * tipAccounts.length)]
);
const tipInstruction = SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: tipAccountPubkey,
lamports: 10000,
});
const latestBlockhash = await connection.getLatestBlockhash("finalized");
const tipTransaction = new Transaction().add(tipInstruction);
tipTransaction.recentBlockhash = latestBlockhash.blockhash;
tipTransaction.feePayer = wallet.publicKey;
tipTransaction.sign(wallet);
const signature = bs58.encode(transaction.signatures[0]);
console.log("🔄 Encoding transactions...");
const bundle = [tipTransaction, transaction].map((tx, index) => {
console.log(`📦 Encoding transaction ${index + 1}`);
if (tx instanceof VersionedTransaction) {
console.log(`🔢 Transaction ${index + 1} is VersionedTransaction`);
return bs58.encode(tx.serialize());
} else {
console.log(`📜 Transaction ${index + 1} is regular Transaction`);
return bs58.encode(tx.serialize({ verifySignatures: false }));
}
});
console.log("✅ Bundle created successfully");
return bundle;
} catch (error) {
console.error("❌ Error in createJitoBundle:", error);
console.error("🔍 Error stack:", error.stack);
throw error;
}
}
async function sendJitoBundle(bundle) {
try {
const response = await axios.post(
JITO_RPC_URL,
{
jsonrpc: "2.0",
id: 1,
method: "sendBundle",
params: [bundle],
},
{
headers: { "Content-Type": "application/json" },
}
);
if (response.data.error) {
throw new Error(response.data.error.message);
}
return response.data.result;
} catch (error) {
console.error("❌ Error sending Jito bundle:", error.message);
throw error;
}
}
async function checkBundleStatus(bundleId) {
try {
const response = await axios.post(
JITO_RPC_URL,
{
jsonrpc: "2.0",
id: 1,
method: "getInflightBundleStatuses",
params: [[bundleId]],
},
{
headers: { "Content-Type": "application/json" },
}
);
if (response.data.error) {
throw new Error(response.data.error.message);
}
const result = response.data.result.value[0];
if (!result) {
console.log(`ℹ️ No status found for bundle ID: ${bundleId}`);
return null;
}
return {
bundleId: result.bundle_id,
status: result.status,
landedSlot: result.landed_slot,
};
} catch (error) {
console.error("❌ Error checking bundle status:", error.message);
return null;
}
}
module.exports = {
createJitoBundle,
sendJitoBundle,
checkBundleStatus,
};