-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtransactionUtils.js
More file actions
147 lines (130 loc) · 4.36 KB
/
transactionUtils.js
File metadata and controls
147 lines (130 loc) · 4.36 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
const {
PublicKey,
TransactionMessage,
VersionedTransaction,
ComputeBudgetProgram,
AddressLookupTableAccount,
SystemProgram,
} = require("@solana/web3.js");
const { SOLANA_RPC_URL } = require("./config");
const { Connection } = require("@solana/web3.js");
const connection = new Connection(SOLANA_RPC_URL);
function deserializeInstruction(instruction) {
return {
programId: new PublicKey(instruction.programId),
keys: instruction.accounts.map((key) => ({
pubkey: new PublicKey(key.pubkey),
isSigner: key.isSigner,
isWritable: key.isWritable,
})),
data: Buffer.from(instruction.data, "base64"),
};
}
async function getAddressLookupTableAccounts(keys) {
const addressLookupTableAccounts = await Promise.all(
keys.map(async (key) => {
const accountInfo = await connection.getAccountInfo(new PublicKey(key));
return {
key: new PublicKey(key),
state: accountInfo
? AddressLookupTableAccount.deserialize(accountInfo.data)
: null,
};
})
);
return addressLookupTableAccounts.filter((account) => account.state !== null);
}
async function simulateTransaction(
instructions,
payer,
addressLookupTableAccounts,
maxRetries = 5
) {
console.log("🔍 Simulating transaction to estimate compute units...");
const latestBlockhash = await connection.getLatestBlockhash("confirmed");
let retries = 0;
while (retries < maxRetries) {
try {
const messageV0 = new TransactionMessage({
payerKey: payer,
recentBlockhash: latestBlockhash.blockhash,
instructions: instructions.filter(Boolean),
}).compileToV0Message(addressLookupTableAccounts);
const transaction = new VersionedTransaction(messageV0);
const simulation = await connection.simulateTransaction(transaction, {
sigVerify: false,
replaceRecentBlockhash: true,
});
if (simulation.value.err) {
console.error(
"❌ Simulation error:",
JSON.stringify(simulation.value.err, null, 2)
);
if (simulation.value.logs) {
console.error("📜 Simulation logs:", simulation.value.logs);
}
throw new Error(
`❌ Simulation failed: ${JSON.stringify(simulation.value.err)}`
);
}
const unitsConsumed = simulation.value.unitsConsumed || 0;
console.log("✅ Simulation successful. Units consumed:", unitsConsumed);
const computeUnits = Math.ceil(unitsConsumed * 1.2);
return computeUnits;
} catch (error) {
console.log("🚀 ~ error:", error)
console.error("❌ Error during simulation:", error.message);
if (error.message.includes("InsufficientFundsForRent")) {
return { error: "InsufficientFundsForRent" };
}
retries++;
if (retries >= maxRetries) {
console.error("❌ Max retries reached. Simulation failed.");
return undefined;
}
console.log(`🔄 Retrying simulation (attempt ${retries + 1})...`);
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
function createVersionedTransaction(
instructions,
payer,
addressLookupTableAccounts,
recentBlockhash,
computeUnits,
priorityFee
) {
const computeBudgetIx = ComputeBudgetProgram.setComputeUnitLimit({
units: computeUnits,
});
const priorityFeeIx = ComputeBudgetProgram.setComputeUnitPrice({
microLamports: priorityFee.microLamports,
});
// 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 tipAccountPubkey = new PublicKey("Cw8CFyM9FkoMi7K7Crf6HNQqf4uEMzpKw6QNghXLvLkY");
const tipInstruction = SystemProgram.transfer({
fromPubkey: payer,
toPubkey: tipAccountPubkey,
lamports: 10000,
});
const finalInstructions = [computeBudgetIx, priorityFeeIx, tipInstruction, ...instructions];
const messageV0 = new TransactionMessage({
payerKey: payer,
recentBlockhash: recentBlockhash,
instructions: finalInstructions,
}).compileToV0Message(addressLookupTableAccounts);
return new VersionedTransaction(messageV0);
}
module.exports = {
deserializeInstruction,
getAddressLookupTableAccounts,
simulateTransaction,
createVersionedTransaction,
};