forked from OnChainMee/jupitor-swap-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjitoService.js
More file actions
190 lines (163 loc) · 5.51 KB
/
jitoService.js
File metadata and controls
190 lines (163 loc) · 5.51 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
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 = bs58.encode(transaction.serialize());
// 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 createJitoBundle(transaction, komisyon, addressLookupTableAccounts) {
// try {
// const tipAccountPubkey = new PublicKey("Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY");
// // Ödeme işlemi için talimat oluştur
// const tipInstruction = SystemProgram.transfer({
// fromPubkey: keypair.publicKey,
// toPubkey: tipAccountPubkey,
// lamports: 100,
// });
// const latestBlockhash = await CONNECTION.getLatestBlockhash("finalized");
// // Yalnızca gerekli talimatları ekleyin
// const combinedTransaction = new Transaction();
// combinedTransaction.add(tipInstruction);
// if (transaction instanceof VersionedTransaction) {
// const message = TransactionMessage.decompile(transaction.message, { addressLookupTableAccounts });
// message.instructions.forEach((instruction) => {
// combinedTransaction.add(instruction);
// });
// } else {
// transaction.instructions.forEach((instruction) => {
// combinedTransaction.add(instruction);
// });
// }
// combinedTransaction.recentBlockhash = latestBlockhash.blockhash;
// combinedTransaction.feePayer = keypair.publicKey;
// combinedTransaction.sign(keypair);
// const encodedTransaction = bs58.default.encode(combinedTransaction.serialize({ verifySignatures: false }));
// console.log("✅ Combined transaction created successfully");
// return encodedTransaction;
// } catch (error) {
// console.error("❌ Error in createJitoBundle:", error);
// throw error;
// }
// }
async function sendJitoBundle(bundle) {
try {
const response = await axios.post(
JITO_RPC_URL,
{
jsonrpc: "2.0",
id: 1,
method: "sendTransaction",
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,
};