@hawksightco/hawk-sdk 1.1.39 → 1.1.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/classes/CreateTxMetadata.d.ts.map +1 -1
- package/dist/src/classes/CreateTxMetadata.js +2 -12
- package/dist/src/classes/MultiTransaction.d.ts +76 -0
- package/dist/src/classes/MultiTransaction.d.ts.map +1 -0
- package/dist/src/classes/MultiTransaction.js +288 -0
- package/dist/src/classes/Transaction copy.d.ts +162 -0
- package/dist/src/classes/Transaction copy.d.ts.map +1 -0
- package/dist/src/classes/Transaction copy.js +458 -0
- package/dist/src/classes/Transaction.d.ts +42 -12
- package/dist/src/classes/Transaction.d.ts.map +1 -1
- package/dist/src/classes/Transaction2.d.ts +108 -0
- package/dist/src/classes/Transaction2.d.ts.map +1 -0
- package/dist/src/classes/Transaction2.js +239 -0
- package/dist/src/classes/Transaction3.d.ts +166 -0
- package/dist/src/classes/Transaction3.d.ts.map +1 -0
- package/dist/src/classes/Transaction3.js +405 -0
- package/dist/src/classes/TransactionBatchExecute2.d.ts +18 -0
- package/dist/src/classes/TransactionBatchExecute2.d.ts.map +1 -1
- package/dist/src/classes/TransactionBatchExecute2.js +72 -1
- package/dist/src/classes/Transactions.d.ts +4 -2
- package/dist/src/classes/Transactions.d.ts.map +1 -1
- package/dist/src/classes/Transactions.js +17 -11
- package/dist/src/classes/TxGenerator.d.ts +2 -1
- package/dist/src/classes/TxGenerator.d.ts.map +1 -1
- package/dist/src/classes/TxGenerator.js +8 -23
- package/dist/src/functions.d.ts +25 -0
- package/dist/src/functions.d.ts.map +1 -1
- package/dist/src/functions.js +70 -1
- package/dist/src/meteora.d.ts +1 -1
- package/dist/src/meteora.d.ts.map +1 -1
- package/dist/src/meteora.js +37 -2
- package/dist/src/types.d.ts +6 -7
- package/dist/src/types.d.ts.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.Transaction = void 0;
|
|
36
|
+
const client = __importStar(require("@hawksightco/swagger-client"));
|
|
37
|
+
const web3 = __importStar(require("@solana/web3.js"));
|
|
38
|
+
const bn_js_1 = require("bn.js");
|
|
39
|
+
const functions_1 = require("../functions");
|
|
40
|
+
const Logging_1 = require("./Logging");
|
|
41
|
+
/**
|
|
42
|
+
* Represents a transaction object in Solana using the web3.js library.
|
|
43
|
+
* This class encapsulates the logic for creating, signing, and verifying signatures of transactions.
|
|
44
|
+
*/
|
|
45
|
+
class Transaction {
|
|
46
|
+
get txMessage() {
|
|
47
|
+
return this._txMessage;
|
|
48
|
+
}
|
|
49
|
+
get versionedTransaction() {
|
|
50
|
+
return this._versionedTransaction;
|
|
51
|
+
}
|
|
52
|
+
get priorityFeeEstimate() {
|
|
53
|
+
return this._priorityFeeEstimate;
|
|
54
|
+
}
|
|
55
|
+
/** The blockhash of a recent ledger entry */
|
|
56
|
+
get recentBlockhash() {
|
|
57
|
+
return this.latestBlockhash.blockhash;
|
|
58
|
+
}
|
|
59
|
+
get instructions() {
|
|
60
|
+
return this._instructions;
|
|
61
|
+
}
|
|
62
|
+
/** last valid block height */
|
|
63
|
+
get lastValidBlockHeight() {
|
|
64
|
+
return this.latestBlockhash.lastValidBlockHeight;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Constructs a new Transaction object.
|
|
68
|
+
*
|
|
69
|
+
* @param payerKey PublicKey of the transaction payer
|
|
70
|
+
* @param instructions Array of TransactionInstruction to be executed in this transaction
|
|
71
|
+
* @param recentBlockhash The blockhash of a recent ledger entry
|
|
72
|
+
* @param alts Address lookup tables that optimize account address storage
|
|
73
|
+
*/
|
|
74
|
+
constructor(txMetadataResponse, payerKey, latestBlockhash, alts, generalUtility, _mainIxs = []) {
|
|
75
|
+
this.txMetadataResponse = txMetadataResponse;
|
|
76
|
+
this.payerKey = payerKey;
|
|
77
|
+
this.latestBlockhash = latestBlockhash;
|
|
78
|
+
this.alts = alts;
|
|
79
|
+
this.generalUtility = generalUtility;
|
|
80
|
+
/** Estimated fee in SOL for priority fee when addPriorityFee() method is called. */
|
|
81
|
+
this._priorityFeeEstimate = "";
|
|
82
|
+
/**
|
|
83
|
+
* Blockhash from nonce account
|
|
84
|
+
*/
|
|
85
|
+
this.blockhashFromNonce = '';
|
|
86
|
+
let mainIxs;
|
|
87
|
+
if (_mainIxs.length > 0) {
|
|
88
|
+
mainIxs = _mainIxs;
|
|
89
|
+
txMetadataResponse.mainInstructions = mainIxs.map(ix => {
|
|
90
|
+
const accounts = ix.keys.map(meta => {
|
|
91
|
+
return {
|
|
92
|
+
pubkey: meta.pubkey.toString(),
|
|
93
|
+
isSigner: meta.isSigner,
|
|
94
|
+
isWritable: meta.isWritable,
|
|
95
|
+
};
|
|
96
|
+
});
|
|
97
|
+
return {
|
|
98
|
+
accounts,
|
|
99
|
+
programId: ix.programId.toString(),
|
|
100
|
+
data: ix.data.toString('base64'),
|
|
101
|
+
};
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
// Construct main instructions
|
|
106
|
+
mainIxs = txMetadataResponse.mainInstructions.map((ix) => {
|
|
107
|
+
return new web3.TransactionInstruction({
|
|
108
|
+
keys: ix.accounts.map((meta) => {
|
|
109
|
+
return {
|
|
110
|
+
pubkey: new web3.PublicKey(meta.pubkey),
|
|
111
|
+
isSigner: meta.isSigner,
|
|
112
|
+
isWritable: meta.isWritable,
|
|
113
|
+
};
|
|
114
|
+
}),
|
|
115
|
+
programId: new web3.PublicKey(ix.programId),
|
|
116
|
+
data: Buffer.from(ix.data, "base64"),
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
this._instructions = [...mainIxs];
|
|
121
|
+
const index = this.getNonceIndex();
|
|
122
|
+
this.hasNonceAdvanceIx = index > -1;
|
|
123
|
+
const [txMessage, versionedTransaction] = this.buildTransaction(latestBlockhash);
|
|
124
|
+
this._txMessage = txMessage;
|
|
125
|
+
this._versionedTransaction = versionedTransaction;
|
|
126
|
+
this.requiredSigners = this.getRequiredSigners();
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Generate nonde advance instruction (dummy instruction)
|
|
130
|
+
*/
|
|
131
|
+
nonceAdvanceIx(noncePubkey) {
|
|
132
|
+
// Generate dummy nonce ix
|
|
133
|
+
return web3.SystemProgram.nonceAdvance({
|
|
134
|
+
/** Nonce account */
|
|
135
|
+
noncePubkey,
|
|
136
|
+
/** Public key of the nonce authority */
|
|
137
|
+
authorizedPubkey: this.payerKey,
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Replace nonce pubkey
|
|
142
|
+
*/
|
|
143
|
+
replaceNoncePubkey(noncePubkey) {
|
|
144
|
+
if (this.hasNonceAdvanceIx) {
|
|
145
|
+
const nonceAdvanceIx = this.nonceAdvanceIx(noncePubkey);
|
|
146
|
+
const index = this.getNonceIndex();
|
|
147
|
+
this._instructions[index] = nonceAdvanceIx;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Get nonce index
|
|
152
|
+
*
|
|
153
|
+
* @returns
|
|
154
|
+
*/
|
|
155
|
+
getNonceIndex() {
|
|
156
|
+
const referenceIx = this.nonceAdvanceIx(web3.Keypair.generate().publicKey);
|
|
157
|
+
const index = this._instructions.findIndex(ix => {
|
|
158
|
+
const isNonceAdvanceIx = ix.data.toString("base64") == referenceIx.data.toString("base64") &&
|
|
159
|
+
ix.programId.toString() == referenceIx.programId.toString();
|
|
160
|
+
return isNonceAdvanceIx;
|
|
161
|
+
});
|
|
162
|
+
return index;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Get blockhash from nonce pubkey
|
|
166
|
+
*/
|
|
167
|
+
getBlockhashFromNonce(connection) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
if (this.blockhashFromNonce !== '') {
|
|
170
|
+
return this.blockhashFromNonce;
|
|
171
|
+
}
|
|
172
|
+
const index = this.getNonceIndex();
|
|
173
|
+
const noncePubkey = this._instructions[index].keys[0].pubkey;
|
|
174
|
+
const accountInfo = yield connection.getAccountInfo(noncePubkey);
|
|
175
|
+
const nonceAccount = web3.NonceAccount.fromAccountData(accountInfo.data);
|
|
176
|
+
this.blockhashFromNonce = nonceAccount.nonce;
|
|
177
|
+
return nonceAccount.nonce;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Signs the transaction with provided signers.
|
|
182
|
+
*
|
|
183
|
+
* @param signers Array of Signer objects whose private keys will be used to sign the transaction.
|
|
184
|
+
* @param idempotent Boolean indicating whether signing should be idempotent (default is false).
|
|
185
|
+
* @throws Error if a signer is not required or has already signed the transaction when not idempotent.
|
|
186
|
+
*/
|
|
187
|
+
sign(signers, idempotent = false) {
|
|
188
|
+
// First validate all signers
|
|
189
|
+
signers.forEach((signer) => {
|
|
190
|
+
const key = signer.publicKey.toString();
|
|
191
|
+
if (typeof this.requiredSigners[key] !== "boolean") {
|
|
192
|
+
throw new Error(`Key ${key} is not a required signer!`);
|
|
193
|
+
}
|
|
194
|
+
if (!idempotent && this.requiredSigners[key]) {
|
|
195
|
+
throw new Error(`Key ${key} has already been signed by required signer!`);
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
// If all signers are valid and the process is idempotent or they haven't signed yet, update and sign
|
|
199
|
+
signers.forEach((signer) => {
|
|
200
|
+
const key = signer.publicKey.toString();
|
|
201
|
+
this.requiredSigners[key] = true;
|
|
202
|
+
});
|
|
203
|
+
this.versionedTransaction.sign(signers);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Add a signature on the transaction with provided signature.
|
|
207
|
+
*
|
|
208
|
+
* @param publicKey The public key of the signer
|
|
209
|
+
* @param signature A signature of the signed transaction
|
|
210
|
+
* @throws Error if a signer is not required or has already signed the transaction.
|
|
211
|
+
*/
|
|
212
|
+
addSignature(publicKey, signature) {
|
|
213
|
+
const key = publicKey.toBase58();
|
|
214
|
+
if (typeof this.requiredSigners[key] !== "boolean") {
|
|
215
|
+
throw new Error(`Key ${key} is not a required signer!`);
|
|
216
|
+
}
|
|
217
|
+
if (this.requiredSigners[key]) {
|
|
218
|
+
throw new Error(`Key ${key} has already been signed by required signer!`);
|
|
219
|
+
}
|
|
220
|
+
this.requiredSigners[key] = true;
|
|
221
|
+
this.versionedTransaction.addSignature(publicKey, signature);
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Checks if all required signers have signed the transaction.
|
|
225
|
+
*
|
|
226
|
+
* @returns Boolean indicating whether all required signers have signed.
|
|
227
|
+
*/
|
|
228
|
+
isSignedByRequiredSigners() {
|
|
229
|
+
return Object.values(this.requiredSigners).every((isSigned) => isSigned);
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Add priority fee instructions (compute budget)
|
|
233
|
+
*
|
|
234
|
+
* This method adds priority fee instructions to the transaction based on the specified
|
|
235
|
+
* priority level and compute unit limit. It ensures that the total fee does not exceed
|
|
236
|
+
* the specified maximum priority fee (in SOL) if provided.
|
|
237
|
+
*
|
|
238
|
+
* @param connection - The connection to the Solana cluster.
|
|
239
|
+
* @param computeUnitLimit - The limit on the number of compute units.
|
|
240
|
+
* @param fixedPriority - If true, a fixed priority fee is used; if false, the fee is based on priority level.
|
|
241
|
+
* @param priorityLevelOrPriorityFee - The priority level for the fee estimation or the fixed priority fee.
|
|
242
|
+
* @param maxPriorityFee - The maximum priority fee in SOL (optional, only applicable when fixedPriority is false).
|
|
243
|
+
* @returns An array of transaction instructions.
|
|
244
|
+
*/
|
|
245
|
+
addPriorityFeeIx(connection_1, computeUnitLimit_1) {
|
|
246
|
+
return __awaiter(this, arguments, void 0, function* (connection, computeUnitLimit, fixedPriority = false, priorityLevelOrPriorityFee, maxPriorityFee) {
|
|
247
|
+
(0, Logging_1.Log)(`addPriorityFeeIx`);
|
|
248
|
+
// First, remove any existing priority fee instructions
|
|
249
|
+
this.removePriorityFeeIxs();
|
|
250
|
+
let totalPriorityFeeLamports;
|
|
251
|
+
if (fixedPriority && typeof priorityLevelOrPriorityFee === 'number') {
|
|
252
|
+
// For fixed priority, directly use the provided fixed priority fee
|
|
253
|
+
totalPriorityFeeLamports = priorityLevelOrPriorityFee - 5000;
|
|
254
|
+
}
|
|
255
|
+
else if (typeof priorityLevelOrPriorityFee === 'string') {
|
|
256
|
+
// Convert maxPriorityFee from SOL to lamports (1 SOL = 1_000_000_000 lamports)
|
|
257
|
+
const maxPriorityFeeLamports = maxPriorityFee !== undefined ? maxPriorityFee * 1000000000 : undefined;
|
|
258
|
+
// Get fee estimate by simulating the transaction
|
|
259
|
+
const startTime = new Date().getTime() / 1000;
|
|
260
|
+
(0, Logging_1.Log)(`addPriorityFeeIx: Starting getFeeEstimate function`);
|
|
261
|
+
let estimate = yield (0, functions_1.getFeeEstimate)(this.generalUtility, priorityLevelOrPriorityFee, this.txMetadataResponse);
|
|
262
|
+
(0, Logging_1.Log)(`addPriorityFeeIx: Starting getFeeEstimate function: Elapsed Time: ${(new Date().getTime() / 1000) - startTime}`);
|
|
263
|
+
// If priority is set to default or medium, we multiply estimate by 2 to increase its chance on blockchain.
|
|
264
|
+
if (priorityLevelOrPriorityFee === client.PriorityLevel.Default || priorityLevelOrPriorityFee === client.PriorityLevel.Medium) {
|
|
265
|
+
estimate = estimate * 2;
|
|
266
|
+
}
|
|
267
|
+
// Calculate the total fee in lamports
|
|
268
|
+
totalPriorityFeeLamports = new bn_js_1.BN(estimate).mul(new bn_js_1.BN(computeUnitLimit)).div(new bn_js_1.BN(1000000)).toNumber();
|
|
269
|
+
// If maxPriorityFee is defined and it is less than the total calculated fee, cap it
|
|
270
|
+
if (maxPriorityFeeLamports !== undefined && totalPriorityFeeLamports > maxPriorityFeeLamports) {
|
|
271
|
+
totalPriorityFeeLamports = maxPriorityFeeLamports - 5000;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
throw new Error('Invalid parameters');
|
|
276
|
+
}
|
|
277
|
+
// Convert the total priority fee back to microLamports per compute unit
|
|
278
|
+
const priorityFeePerUnitMicroLamports = new bn_js_1.BN(totalPriorityFeeLamports).mul(new bn_js_1.BN(1000000)).div(new bn_js_1.BN(computeUnitLimit)).toNumber();
|
|
279
|
+
// Create priority fee instructions for the transaction
|
|
280
|
+
const priorityFeeIxs = [
|
|
281
|
+
web3.ComputeBudgetProgram.setComputeUnitLimit({
|
|
282
|
+
units: computeUnitLimit,
|
|
283
|
+
}),
|
|
284
|
+
web3.ComputeBudgetProgram.setComputeUnitPrice({
|
|
285
|
+
microLamports: priorityFeePerUnitMicroLamports,
|
|
286
|
+
}),
|
|
287
|
+
];
|
|
288
|
+
// Store the total priority fee in lamports
|
|
289
|
+
this._priorityFeeEstimate = totalPriorityFeeLamports.toString();
|
|
290
|
+
// Append priority fee instructions at the beginning
|
|
291
|
+
this._instructions.unshift(...priorityFeeIxs);
|
|
292
|
+
// Rebuild versioned transaction with the latest blockhash
|
|
293
|
+
const result = yield connection.getLatestBlockhash();
|
|
294
|
+
const blockhash = {
|
|
295
|
+
blockhash: result.blockhash,
|
|
296
|
+
lastValidBlockHeight: result.lastValidBlockHeight,
|
|
297
|
+
};
|
|
298
|
+
if (this.hasNonceAdvanceIx) {
|
|
299
|
+
blockhash.blockhash = yield this.getBlockhashFromNonce(connection);
|
|
300
|
+
}
|
|
301
|
+
this.buildTransaction(blockhash);
|
|
302
|
+
return priorityFeeIxs;
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Gets the compute unit limit from a transaction simulation.
|
|
307
|
+
*
|
|
308
|
+
* This function simulates a transaction and calculates the compute unit limit
|
|
309
|
+
* based on the simulation results. If an additional compute limit is provided,
|
|
310
|
+
* it is added to the units consumed in the simulation. Otherwise, the units
|
|
311
|
+
* consumed are increased by 10%.
|
|
312
|
+
*
|
|
313
|
+
* @param {web3.Connection} connection - The connection object to the Solana cluster.
|
|
314
|
+
* @param {number} [additionalComputeLimit] - Optional additional compute limit to add to the units consumed.
|
|
315
|
+
* @returns {Promise<number>} - A promise that resolves to the total compute unit limit.
|
|
316
|
+
* @throws {Error} - Throws an error if the transaction simulation fails.
|
|
317
|
+
*/
|
|
318
|
+
getComputeUnitLimit(connection, additionalComputeLimit) {
|
|
319
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
320
|
+
const simulation = yield this.simulateTransaction(connection);
|
|
321
|
+
if (simulation.err !== null) {
|
|
322
|
+
if (simulation.logs === null) {
|
|
323
|
+
(0, Logging_1.Log)(simulation.err);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
for (const log of simulation.logs) {
|
|
327
|
+
(0, Logging_1.Log)(log);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
throw new Error(`Transaction simulation error. See logs above.`);
|
|
331
|
+
}
|
|
332
|
+
const totalUnitLimit = additionalComputeLimit ? simulation.unitsConsumed + additionalComputeLimit : Math.round(simulation.unitsConsumed * 1.1);
|
|
333
|
+
return totalUnitLimit;
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Simulate transaction
|
|
338
|
+
*
|
|
339
|
+
* @param connection
|
|
340
|
+
* @returns
|
|
341
|
+
*/
|
|
342
|
+
simulateTransaction(connection_1) {
|
|
343
|
+
return __awaiter(this, arguments, void 0, function* (connection, signers = []) {
|
|
344
|
+
const testInstructions = [
|
|
345
|
+
...this.instructions,
|
|
346
|
+
];
|
|
347
|
+
if (this.findSetComputeUnitLimitIndex(testInstructions) === -1) {
|
|
348
|
+
testInstructions.unshift(web3.ComputeBudgetProgram.setComputeUnitLimit({ units: 1400000 }));
|
|
349
|
+
}
|
|
350
|
+
if (this.findSetComputeUnitPriceIndex(testInstructions) === -1) {
|
|
351
|
+
testInstructions.unshift(web3.ComputeBudgetProgram.setComputeUnitPrice({
|
|
352
|
+
// CU * CU PRICE -> 1400000 * feeEstimate.priorityFeeEstimate
|
|
353
|
+
microLamports: 100000,
|
|
354
|
+
}));
|
|
355
|
+
}
|
|
356
|
+
const testVersionedTxn = new web3.VersionedTransaction(new web3.TransactionMessage({
|
|
357
|
+
instructions: testInstructions,
|
|
358
|
+
payerKey: this.payerKey,
|
|
359
|
+
recentBlockhash: web3.PublicKey.default.toString(),
|
|
360
|
+
}).compileToV0Message(this.alts));
|
|
361
|
+
if (signers.length > 0) {
|
|
362
|
+
testVersionedTxn.sign(signers);
|
|
363
|
+
}
|
|
364
|
+
const simulation = yield connection.simulateTransaction(testVersionedTxn, {
|
|
365
|
+
replaceRecentBlockhash: true,
|
|
366
|
+
sigVerify: false,
|
|
367
|
+
});
|
|
368
|
+
if (simulation.value.unitsConsumed === undefined) {
|
|
369
|
+
throw new Error("Unable to calculate compute budget.");
|
|
370
|
+
}
|
|
371
|
+
return {
|
|
372
|
+
err: simulation.value.err,
|
|
373
|
+
logs: simulation.value.logs,
|
|
374
|
+
accounts: simulation.value.accounts,
|
|
375
|
+
unitsConsumed: simulation.value.unitsConsumed,
|
|
376
|
+
returnData: simulation.value.returnData,
|
|
377
|
+
};
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Builds transaction object
|
|
382
|
+
*/
|
|
383
|
+
buildTransaction(latestBlockhash) {
|
|
384
|
+
this.latestBlockhash = latestBlockhash;
|
|
385
|
+
this._txMessage = new web3.TransactionMessage({
|
|
386
|
+
payerKey: this.payerKey,
|
|
387
|
+
instructions: this.instructions,
|
|
388
|
+
recentBlockhash: this.recentBlockhash,
|
|
389
|
+
});
|
|
390
|
+
this._versionedTransaction = new web3.VersionedTransaction(this.txMessage.compileToV0Message(this.alts));
|
|
391
|
+
return [this._txMessage, this._versionedTransaction];
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Gathers the public keys of all parties required to sign the transaction.
|
|
395
|
+
*
|
|
396
|
+
* @returns A record of signer public keys mapped to a boolean indicating whether they have signed.
|
|
397
|
+
*/
|
|
398
|
+
getRequiredSigners() {
|
|
399
|
+
const signerKeys = this.instructions.flatMap((ix) => ix.keys
|
|
400
|
+
.filter((meta) => meta.isSigner)
|
|
401
|
+
.map((meta) => meta.pubkey.toString()));
|
|
402
|
+
const result = {};
|
|
403
|
+
signerKeys.forEach((key) => (result[key] = false));
|
|
404
|
+
return result;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Find setComputeUnitLimit index within the instructions
|
|
408
|
+
*/
|
|
409
|
+
findSetComputeUnitLimitIndex(instructions) {
|
|
410
|
+
let _instructions = [];
|
|
411
|
+
if (instructions !== undefined) {
|
|
412
|
+
_instructions = instructions;
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
_instructions = this.instructions;
|
|
416
|
+
}
|
|
417
|
+
return _instructions.findIndex((ix) => {
|
|
418
|
+
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
419
|
+
"ComputeBudget111111111111111111111111111111";
|
|
420
|
+
const isSetComputeLimitIx = ix.data[0] === 2;
|
|
421
|
+
return isComputeBudgetProgram && isSetComputeLimitIx;
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Find setComputeUnitPrice index within the instructions
|
|
426
|
+
*/
|
|
427
|
+
findSetComputeUnitPriceIndex(instructions) {
|
|
428
|
+
let _instructions = [];
|
|
429
|
+
if (instructions !== undefined) {
|
|
430
|
+
_instructions = instructions;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
_instructions = this.instructions;
|
|
434
|
+
}
|
|
435
|
+
return _instructions.findIndex((ix) => {
|
|
436
|
+
const isComputeBudgetProgram = ix.programId.toString() ===
|
|
437
|
+
"ComputeBudget111111111111111111111111111111";
|
|
438
|
+
const isSetComputeUnitPriceIx = ix.data[0] === 3;
|
|
439
|
+
return isComputeBudgetProgram && isSetComputeUnitPriceIx;
|
|
440
|
+
});
|
|
441
|
+
}
|
|
442
|
+
/**
|
|
443
|
+
* Remove priority fee instructions
|
|
444
|
+
*/
|
|
445
|
+
removePriorityFeeIxs() {
|
|
446
|
+
while (true) {
|
|
447
|
+
const setComputeUnitLimitIxIndex = this.findSetComputeUnitLimitIndex();
|
|
448
|
+
const setComputeUnitPriceIxIndex = this.findSetComputeUnitPriceIndex();
|
|
449
|
+
if (setComputeUnitLimitIxIndex !== -1)
|
|
450
|
+
this._instructions.splice(setComputeUnitLimitIxIndex, 1);
|
|
451
|
+
if (setComputeUnitPriceIxIndex !== -1)
|
|
452
|
+
this._instructions.splice(setComputeUnitPriceIxIndex, 1);
|
|
453
|
+
if (setComputeUnitLimitIxIndex && setComputeUnitPriceIxIndex)
|
|
454
|
+
break;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
exports.Transaction = Transaction;
|
|
@@ -1,8 +1,32 @@
|
|
|
1
1
|
/// <reference types="@meteora-ag/dlmm/node_modules/@solana/web3.js" />
|
|
2
2
|
import * as client from "@hawksightco/swagger-client";
|
|
3
3
|
import * as web3 from "@solana/web3.js";
|
|
4
|
-
import { SimulatedTransactionResponse, TransactionMetadataResponse } from "../types";
|
|
5
4
|
import { GeneralUtility } from "./GeneralUtility";
|
|
5
|
+
export type SimulatedTransactionResponse = {
|
|
6
|
+
err: web3.TransactionError | string | null;
|
|
7
|
+
logs: Array<string> | null;
|
|
8
|
+
accounts?: (web3.SimulatedTransactionAccountInfo | null)[] | null;
|
|
9
|
+
unitsConsumed: number;
|
|
10
|
+
returnData?: web3.TransactionReturnData | null;
|
|
11
|
+
};
|
|
12
|
+
export type TransactionMetadataResponse = {
|
|
13
|
+
description: string;
|
|
14
|
+
estimatedFeeInSOL: string;
|
|
15
|
+
addressLookupTableAddresses: string[];
|
|
16
|
+
computeBudgetInstructions: Instruction[];
|
|
17
|
+
mainInstructions: Instruction[];
|
|
18
|
+
payer: string;
|
|
19
|
+
signature: string;
|
|
20
|
+
};
|
|
21
|
+
export type Instruction = {
|
|
22
|
+
accounts: {
|
|
23
|
+
isSigner: boolean;
|
|
24
|
+
isWritable: boolean;
|
|
25
|
+
pubkey: string;
|
|
26
|
+
}[];
|
|
27
|
+
data: string;
|
|
28
|
+
programId: string;
|
|
29
|
+
};
|
|
6
30
|
/**
|
|
7
31
|
* Represents a transaction object in Solana using the web3.js library.
|
|
8
32
|
* This class encapsulates the logic for creating, signing, and verifying signatures of transactions.
|
|
@@ -10,24 +34,27 @@ import { GeneralUtility } from "./GeneralUtility";
|
|
|
10
34
|
export declare class Transaction {
|
|
11
35
|
readonly txMetadataResponse: TransactionMetadataResponse;
|
|
12
36
|
readonly payerKey: web3.PublicKey;
|
|
13
|
-
|
|
37
|
+
protected latestBlockhash: {
|
|
38
|
+
blockhash: string;
|
|
39
|
+
lastValidBlockHeight: number;
|
|
40
|
+
};
|
|
14
41
|
readonly alts: web3.AddressLookupTableAccount[];
|
|
15
|
-
|
|
42
|
+
protected generalUtility: GeneralUtility;
|
|
16
43
|
/** Compiled TransactionMessage which includes payer, instructions, and recent blockhash */
|
|
17
|
-
|
|
44
|
+
protected _txMessage: web3.TransactionMessage;
|
|
18
45
|
get txMessage(): web3.TransactionMessage;
|
|
19
46
|
/** Versioned transaction built from the transaction message */
|
|
20
|
-
|
|
47
|
+
protected _versionedTransaction: web3.VersionedTransaction;
|
|
21
48
|
get versionedTransaction(): web3.VersionedTransaction;
|
|
22
49
|
/** A map indicating whether each required signer has signed the transaction */
|
|
23
50
|
readonly requiredSigners: Record<string, boolean>;
|
|
24
51
|
/** Estimated fee in SOL for priority fee when addPriorityFee() method is called. */
|
|
25
|
-
|
|
52
|
+
protected _priorityFeeEstimate: string;
|
|
26
53
|
get priorityFeeEstimate(): string;
|
|
27
54
|
/** The blockhash of a recent ledger entry */
|
|
28
55
|
get recentBlockhash(): string;
|
|
29
56
|
/** Array of TransactionInstruction to be executed in this transaction */
|
|
30
|
-
|
|
57
|
+
protected _instructions: web3.TransactionInstruction[];
|
|
31
58
|
get instructions(): web3.TransactionInstruction[];
|
|
32
59
|
/** last valid block height */
|
|
33
60
|
get lastValidBlockHeight(): number;
|
|
@@ -39,7 +66,10 @@ export declare class Transaction {
|
|
|
39
66
|
* @param recentBlockhash The blockhash of a recent ledger entry
|
|
40
67
|
* @param alts Address lookup tables that optimize account address storage
|
|
41
68
|
*/
|
|
42
|
-
constructor(txMetadataResponse: TransactionMetadataResponse, payerKey: web3.PublicKey, latestBlockhash:
|
|
69
|
+
constructor(txMetadataResponse: TransactionMetadataResponse, payerKey: web3.PublicKey, latestBlockhash: {
|
|
70
|
+
blockhash: string;
|
|
71
|
+
lastValidBlockHeight: number;
|
|
72
|
+
}, alts: web3.AddressLookupTableAccount[], generalUtility: GeneralUtility, _mainIxs?: web3.TransactionInstruction[]);
|
|
43
73
|
/**
|
|
44
74
|
* Signs the transaction with provided signers.
|
|
45
75
|
*
|
|
@@ -119,18 +149,18 @@ export declare class Transaction {
|
|
|
119
149
|
*
|
|
120
150
|
* @returns A record of signer public keys mapped to a boolean indicating whether they have signed.
|
|
121
151
|
*/
|
|
122
|
-
|
|
152
|
+
protected getRequiredSigners(): Record<string, boolean>;
|
|
123
153
|
/**
|
|
124
154
|
* Find setComputeUnitLimit index within the instructions
|
|
125
155
|
*/
|
|
126
|
-
|
|
156
|
+
protected findSetComputeUnitLimitIndex(instructions?: web3.TransactionInstruction[]): number;
|
|
127
157
|
/**
|
|
128
158
|
* Find setComputeUnitPrice index within the instructions
|
|
129
159
|
*/
|
|
130
|
-
|
|
160
|
+
protected findSetComputeUnitPriceIndex(instructions?: web3.TransactionInstruction[]): number;
|
|
131
161
|
/**
|
|
132
162
|
* Remove priority fee instructions
|
|
133
163
|
*/
|
|
134
|
-
|
|
164
|
+
protected removePriorityFeeIxs(): void;
|
|
135
165
|
}
|
|
136
166
|
//# sourceMappingURL=Transaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transaction.d.ts","sourceRoot":"","sources":["../../../src/classes/Transaction.ts"],"names":[],"mappings":";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