@streamflow/common 7.0.0-alpha.2 → 7.0.0-alpha.20

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.
@@ -1,64 +1,17 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __values = (this && this.__values) || function(o) {
39
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
40
- if (m) return m.call(o);
41
- if (o && typeof o.length === "number") return {
42
- next: function () {
43
- if (o && i >= o.length) o = void 0;
44
- return { value: o && o[i++], done: !o };
45
- }
46
- };
47
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
48
- };
49
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
50
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
51
4
  };
52
5
  Object.defineProperty(exports, "__esModule", { value: true });
53
6
  exports.getMintAndProgram = exports.prepareBaseInstructions = exports.checkOrCreateAtaBatch = exports.createAtaBatch = exports.generateCreateAtaBatchTx = exports.enrichAtaParams = exports.ataBatchExist = exports.ata = exports.confirmAndEnsureTransaction = exports.simulateTransaction = exports.sendAndConfirmTransaction = exports.executeMultipleTransactions = exports.executeTransaction = exports.signAndExecuteTransaction = exports.signTransaction = exports.prepareTransaction = exports.isTransactionVersioned = exports.isSignerKeypair = exports.isSignerWallet = exports.getProgramAccounts = exports.buildSendThrottler = void 0;
54
- var spl_token_1 = require("@solana/spl-token");
55
- var web3_js_1 = require("@solana/web3.js");
56
- var bs58_1 = __importDefault(require("bs58"));
57
- var p_queue_1 = __importDefault(require("p-queue"));
58
- var types_js_1 = require("./types.js");
59
- var utils_js_1 = require("../utils.js");
60
- var SIMULATE_TRIES = 3;
61
- var buildSendThrottler = function (sendRate) {
7
+ const spl_token_1 = require("@solana/spl-token");
8
+ const web3_js_1 = require("@solana/web3.js");
9
+ const bs58_1 = __importDefault(require("bs58"));
10
+ const p_queue_1 = __importDefault(require("p-queue"));
11
+ const types_js_1 = require("./types.js");
12
+ const utils_js_1 = require("../lib/utils.js");
13
+ const SIMULATE_TRIES = 3;
14
+ const buildSendThrottler = (sendRate) => {
62
15
  return new p_queue_1.default({ concurrency: sendRate, intervalCap: 1, interval: 1000 });
63
16
  };
64
17
  exports.buildSendThrottler = buildSendThrottler;
@@ -70,21 +23,18 @@ exports.buildSendThrottler = buildSendThrottler;
70
23
  * @param {PublicKey} programId - Solana program ID.
71
24
  * @return {Promise<Account[]>} - Array of resulting accounts.
72
25
  */
73
- function getProgramAccounts(connection, wallet, offset, programId) {
74
- return __awaiter(this, void 0, void 0, function () {
75
- return __generator(this, function (_a) {
76
- return [2 /*return*/, connection === null || connection === void 0 ? void 0 : connection.getProgramAccounts(programId, {
77
- filters: [
78
- {
79
- memcmp: {
80
- offset: offset,
81
- bytes: wallet.toBase58(),
82
- },
83
- },
84
- ],
85
- })];
86
- });
26
+ async function getProgramAccounts(connection, wallet, offset, programId) {
27
+ const programAccounts = await connection?.getProgramAccounts(programId, {
28
+ filters: [
29
+ {
30
+ memcmp: {
31
+ offset,
32
+ bytes: wallet.toBase58(),
33
+ },
34
+ },
35
+ ],
87
36
  });
37
+ return [...programAccounts];
88
38
  }
89
39
  exports.getProgramAccounts = getProgramAccounts;
90
40
  /**
@@ -125,60 +75,37 @@ exports.isTransactionVersioned = isTransactionVersioned;
125
75
  * @param partialSigners - optional signers that will be used to partially sign a Transaction
126
76
  * @returns Transaction and Blockhash
127
77
  */
128
- function prepareTransaction(connection, ixs, payer, commitment) {
129
- var partialSigners = [];
130
- for (var _i = 4; _i < arguments.length; _i++) {
131
- partialSigners[_i - 4] = arguments[_i];
78
+ async function prepareTransaction(connection, ixs, payer, commitment, ...partialSigners) {
79
+ if (!payer) {
80
+ throw new Error("Payer public key is not provided!");
132
81
  }
133
- return __awaiter(this, void 0, void 0, function () {
134
- var _a, hash, context, messageV0, tx, signers;
135
- return __generator(this, function (_b) {
136
- switch (_b.label) {
137
- case 0:
138
- if (!payer) {
139
- throw new Error("Payer public key is not provided!");
140
- }
141
- return [4 /*yield*/, connection.getLatestBlockhashAndContext(commitment)];
142
- case 1:
143
- _a = _b.sent(), hash = _a.value, context = _a.context;
144
- messageV0 = new web3_js_1.TransactionMessage({
145
- payerKey: payer,
146
- recentBlockhash: hash.blockhash,
147
- instructions: ixs,
148
- }).compileToV0Message();
149
- tx = new web3_js_1.VersionedTransaction(messageV0);
150
- signers = partialSigners.filter(function (item) { return !!item; });
151
- tx.sign(signers);
152
- return [2 /*return*/, { tx: tx, context: context, hash: hash }];
153
- }
154
- });
155
- });
82
+ const { value: hash, context } = await connection.getLatestBlockhashAndContext(commitment);
83
+ const messageV0 = new web3_js_1.TransactionMessage({
84
+ payerKey: payer,
85
+ recentBlockhash: hash.blockhash,
86
+ instructions: ixs,
87
+ }).compileToV0Message();
88
+ const tx = new web3_js_1.VersionedTransaction(messageV0);
89
+ const signers = partialSigners.filter((item) => !!item);
90
+ tx.sign(signers);
91
+ return { tx, context, hash };
156
92
  }
157
93
  exports.prepareTransaction = prepareTransaction;
158
- function signTransaction(invoker, tx) {
159
- return __awaiter(this, void 0, void 0, function () {
160
- var signedTx;
161
- return __generator(this, function (_a) {
162
- switch (_a.label) {
163
- case 0:
164
- if (!isSignerWallet(invoker)) return [3 /*break*/, 2];
165
- return [4 /*yield*/, invoker.signTransaction(tx)];
166
- case 1:
167
- signedTx = _a.sent();
168
- return [3 /*break*/, 3];
169
- case 2:
170
- if (isTransactionVersioned(tx)) {
171
- tx.sign([invoker]);
172
- }
173
- else {
174
- tx.partialSign(invoker);
175
- }
176
- signedTx = tx;
177
- _a.label = 3;
178
- case 3: return [2 /*return*/, signedTx];
179
- }
180
- });
181
- });
94
+ async function signTransaction(invoker, tx) {
95
+ let signedTx;
96
+ if (isSignerWallet(invoker)) {
97
+ signedTx = await invoker.signTransaction(tx);
98
+ }
99
+ else {
100
+ if (isTransactionVersioned(tx)) {
101
+ tx.sign([invoker]);
102
+ }
103
+ else {
104
+ tx.partialSign(invoker);
105
+ }
106
+ signedTx = tx;
107
+ }
108
+ return signedTx;
182
109
  }
183
110
  exports.signTransaction = signTransaction;
184
111
  /**
@@ -190,18 +117,9 @@ exports.signTransaction = signTransaction;
190
117
  * @param throttleParams - rate or throttler instance to throttle TX sending - to not spam the blockchain too much
191
118
  * @returns Transaction signature
192
119
  */
193
- function signAndExecuteTransaction(connection, invoker, tx, confirmationParams, throttleParams) {
194
- return __awaiter(this, void 0, void 0, function () {
195
- var signedTx;
196
- return __generator(this, function (_a) {
197
- switch (_a.label) {
198
- case 0: return [4 /*yield*/, signTransaction(invoker, tx)];
199
- case 1:
200
- signedTx = _a.sent();
201
- return [2 /*return*/, executeTransaction(connection, signedTx, confirmationParams, throttleParams)];
202
- }
203
- });
204
- });
120
+ async function signAndExecuteTransaction(connection, invoker, tx, confirmationParams, throttleParams) {
121
+ const signedTx = await signTransaction(invoker, tx);
122
+ return executeTransaction(connection, signedTx, confirmationParams, throttleParams);
205
123
  }
206
124
  exports.signAndExecuteTransaction = signAndExecuteTransaction;
207
125
  /**
@@ -219,21 +137,12 @@ exports.signAndExecuteTransaction = signAndExecuteTransaction;
219
137
  * @param throttleParams - rate or throttler instance to throttle TX sending - to not spam the blockchain too much
220
138
  * @returns Transaction signature
221
139
  */
222
- function executeTransaction(connection, tx, confirmationParams, throttleParams) {
223
- return __awaiter(this, void 0, void 0, function () {
224
- return __generator(this, function (_a) {
225
- switch (_a.label) {
226
- case 0:
227
- if (tx.signatures.length === 0) {
228
- throw Error("Error with transaction parameters.");
229
- }
230
- return [4 /*yield*/, simulateTransaction(connection, tx)];
231
- case 1:
232
- _a.sent();
233
- return [2 /*return*/, sendAndConfirmTransaction(connection, tx, confirmationParams, throttleParams)];
234
- }
235
- });
236
- });
140
+ async function executeTransaction(connection, tx, confirmationParams, throttleParams) {
141
+ if (tx.signatures.length === 0) {
142
+ throw Error("Error with transaction parameters.");
143
+ }
144
+ await simulateTransaction(connection, tx);
145
+ return sendAndConfirmTransaction(connection, tx, confirmationParams, throttleParams);
237
146
  }
238
147
  exports.executeTransaction = executeTransaction;
239
148
  /**
@@ -246,18 +155,11 @@ exports.executeTransaction = executeTransaction;
246
155
  * @param throttleParams.sendThrottler - throttler instance
247
156
  * @returns Raw Promise Results - should be handled by the consumer and unwrapped accordingly
248
157
  */
249
- function executeMultipleTransactions(connection, txs, confirmationParams, _a) {
250
- var _b = _a.sendRate, sendRate = _b === void 0 ? 1 : _b, sendThrottler = _a.sendThrottler;
251
- return __awaiter(this, void 0, void 0, function () {
252
- return __generator(this, function (_c) {
253
- if (!sendThrottler) {
254
- sendThrottler = (0, exports.buildSendThrottler)(sendRate);
255
- }
256
- return [2 /*return*/, Promise.allSettled(txs.map(function (tx) {
257
- return executeTransaction(connection, tx, confirmationParams, { sendRate: sendRate, sendThrottler: sendThrottler });
258
- }))];
259
- });
260
- });
158
+ async function executeMultipleTransactions(connection, txs, confirmationParams, { sendRate = 1, sendThrottler }) {
159
+ if (!sendThrottler) {
160
+ sendThrottler = (0, exports.buildSendThrottler)(sendRate);
161
+ }
162
+ return Promise.allSettled(txs.map((tx) => executeTransaction(connection, tx, confirmationParams, { sendRate: sendRate, sendThrottler: sendThrottler })));
261
163
  }
262
164
  exports.executeMultipleTransactions = executeMultipleTransactions;
263
165
  /**
@@ -273,137 +175,83 @@ exports.executeMultipleTransactions = executeMultipleTransactions;
273
175
  * @param throttleParams.sendRate - rate
274
176
  * @param throttleParams.sendThrottler - throttler instance
275
177
  */
276
- function sendAndConfirmTransaction(connection, tx, _a, _b) {
277
- var hash = _a.hash, context = _a.context, commitment = _a.commitment;
278
- var _c = _b.sendRate, sendRate = _c === void 0 ? 1 : _c, sendThrottler = _b.sendThrottler;
279
- return __awaiter(this, void 0, void 0, function () {
280
- var isVersioned, signature, blockheight, transactionSent, rawTransaction, e_1, value, e_2, _e_1;
281
- var _this = this;
282
- return __generator(this, function (_d) {
283
- switch (_d.label) {
284
- case 0:
285
- isVersioned = isTransactionVersioned(tx);
286
- if (isVersioned) {
287
- signature = bs58_1.default.encode(tx.signatures[0]);
288
- }
289
- else {
290
- signature = bs58_1.default.encode(tx.signature);
291
- }
292
- if (!sendThrottler) {
293
- sendThrottler = (0, exports.buildSendThrottler)(sendRate);
294
- }
295
- return [4 /*yield*/, connection.getBlockHeight(commitment)];
296
- case 1:
297
- blockheight = _d.sent();
298
- transactionSent = false;
299
- rawTransaction = tx.serialize();
300
- _d.label = 2;
301
- case 2:
302
- if (!(blockheight < hash.lastValidBlockHeight + 15)) return [3 /*break*/, 20];
303
- _d.label = 3;
304
- case 3:
305
- _d.trys.push([3, 6, , 9]);
306
- if (!(blockheight < hash.lastValidBlockHeight || !transactionSent)) return [3 /*break*/, 5];
307
- return [4 /*yield*/, sendThrottler.add(function () { return __awaiter(_this, void 0, void 0, function () {
308
- return __generator(this, function (_a) {
309
- return [2 /*return*/, connection.sendRawTransaction(rawTransaction, {
310
- maxRetries: 0,
311
- minContextSlot: context.slot,
312
- preflightCommitment: commitment,
313
- skipPreflight: true,
314
- })];
315
- });
316
- }); })];
317
- case 4:
318
- _d.sent();
319
- transactionSent = true;
320
- _d.label = 5;
321
- case 5: return [3 /*break*/, 9];
322
- case 6:
323
- e_1 = _d.sent();
324
- if (!(transactionSent ||
325
- (e_1 instanceof web3_js_1.SendTransactionError && e_1.message.includes("Minimum context slot has not been reached")))) return [3 /*break*/, 8];
326
- return [4 /*yield*/, (0, utils_js_1.sleep)(500)];
327
- case 7:
328
- _d.sent();
329
- return [3 /*break*/, 2];
330
- case 8: throw e_1;
331
- case 9: return [4 /*yield*/, (0, utils_js_1.sleep)(500)];
332
- case 10:
333
- _d.sent();
334
- _d.label = 11;
335
- case 11:
336
- _d.trys.push([11, 13, , 15]);
337
- return [4 /*yield*/, confirmAndEnsureTransaction(connection, signature)];
338
- case 12:
339
- value = _d.sent();
340
- if (value) {
341
- return [2 /*return*/, signature];
342
- }
343
- return [3 /*break*/, 15];
344
- case 13:
345
- e_2 = _d.sent();
346
- if (e_2 instanceof types_js_1.TransactionFailedError) {
347
- throw e_2;
348
- }
349
- return [4 /*yield*/, (0, utils_js_1.sleep)(500)];
350
- case 14:
351
- _d.sent();
352
- return [3 /*break*/, 15];
353
- case 15:
354
- _d.trys.push([15, 17, , 19]);
355
- return [4 /*yield*/, connection.getBlockHeight(commitment)];
356
- case 16:
357
- blockheight = _d.sent();
358
- return [3 /*break*/, 19];
359
- case 17:
360
- _e_1 = _d.sent();
361
- return [4 /*yield*/, (0, utils_js_1.sleep)(500)];
362
- case 18:
363
- _d.sent();
364
- return [3 /*break*/, 19];
365
- case 19: return [3 /*break*/, 2];
366
- case 20: throw new Error("Transaction ".concat(signature, " expired."));
178
+ async function sendAndConfirmTransaction(connection, tx, { hash, context, commitment }, { sendRate = 1, sendThrottler }) {
179
+ const isVersioned = isTransactionVersioned(tx);
180
+ let signature;
181
+ if (isVersioned) {
182
+ signature = bs58_1.default.encode(tx.signatures[0]);
183
+ }
184
+ else {
185
+ signature = bs58_1.default.encode(tx.signature);
186
+ }
187
+ if (!sendThrottler) {
188
+ sendThrottler = (0, exports.buildSendThrottler)(sendRate);
189
+ }
190
+ let blockheight = await connection.getBlockHeight(commitment);
191
+ let transactionSent = false;
192
+ const rawTransaction = tx.serialize();
193
+ while (blockheight < hash.lastValidBlockHeight + 15) {
194
+ try {
195
+ if (blockheight < hash.lastValidBlockHeight || !transactionSent) {
196
+ await sendThrottler.add(async () => connection.sendRawTransaction(rawTransaction, {
197
+ maxRetries: 0,
198
+ minContextSlot: context.slot,
199
+ preflightCommitment: commitment,
200
+ skipPreflight: true,
201
+ }));
202
+ transactionSent = true;
367
203
  }
368
- });
369
- });
204
+ }
205
+ catch (e) {
206
+ if (transactionSent ||
207
+ (e instanceof web3_js_1.SendTransactionError && e.message.includes("Minimum context slot has not been reached"))) {
208
+ await (0, utils_js_1.sleep)(500);
209
+ continue;
210
+ }
211
+ throw e;
212
+ }
213
+ await (0, utils_js_1.sleep)(500);
214
+ try {
215
+ const value = await confirmAndEnsureTransaction(connection, signature);
216
+ if (value) {
217
+ return signature;
218
+ }
219
+ }
220
+ catch (e) {
221
+ if (e instanceof types_js_1.TransactionFailedError) {
222
+ throw e;
223
+ }
224
+ await (0, utils_js_1.sleep)(500);
225
+ }
226
+ try {
227
+ blockheight = await connection.getBlockHeight(commitment);
228
+ }
229
+ catch (_e) {
230
+ await (0, utils_js_1.sleep)(500);
231
+ }
232
+ }
233
+ throw new Error(`Transaction ${signature} expired.`);
370
234
  }
371
235
  exports.sendAndConfirmTransaction = sendAndConfirmTransaction;
372
- function simulateTransaction(connection, tx) {
373
- return __awaiter(this, void 0, void 0, function () {
374
- var res, i, errMessage;
375
- return __generator(this, function (_a) {
376
- switch (_a.label) {
377
- case 0:
378
- i = 0;
379
- _a.label = 1;
380
- case 1:
381
- if (!(i < SIMULATE_TRIES)) return [3 /*break*/, 7];
382
- if (!isTransactionVersioned(tx)) return [3 /*break*/, 3];
383
- return [4 /*yield*/, connection.simulateTransaction(tx)];
384
- case 2:
385
- res = _a.sent();
386
- return [3 /*break*/, 5];
387
- case 3: return [4 /*yield*/, connection.simulateTransaction(tx)];
388
- case 4:
389
- res = _a.sent();
390
- _a.label = 5;
391
- case 5:
392
- if (res.value.err) {
393
- errMessage = JSON.stringify(res.value.err);
394
- if (!errMessage.includes("BlockhashNotFound") || i === SIMULATE_TRIES - 1) {
395
- throw new web3_js_1.SendTransactionError("failed to simulate transaction: " + errMessage, res.value.logs || undefined);
396
- }
397
- return [3 /*break*/, 6];
398
- }
399
- return [2 /*return*/, res];
400
- case 6:
401
- i++;
402
- return [3 /*break*/, 1];
403
- case 7: throw new web3_js_1.SendTransactionError("failed to simulate transaction");
236
+ async function simulateTransaction(connection, tx) {
237
+ let res;
238
+ for (let i = 0; i < SIMULATE_TRIES; i++) {
239
+ if (isTransactionVersioned(tx)) {
240
+ res = await connection.simulateTransaction(tx);
241
+ }
242
+ else {
243
+ res = await connection.simulateTransaction(tx);
244
+ }
245
+ if (res.value.err) {
246
+ const errMessage = JSON.stringify(res.value.err);
247
+ if (!errMessage.includes("BlockhashNotFound") || i === SIMULATE_TRIES - 1) {
248
+ throw new web3_js_1.SendTransactionError("failed to simulate transaction: " + errMessage, res.value.logs || undefined);
404
249
  }
405
- });
406
- });
250
+ continue;
251
+ }
252
+ return res;
253
+ }
254
+ throw new web3_js_1.SendTransactionError("failed to simulate transaction");
407
255
  }
408
256
  exports.simulateTransaction = simulateTransaction;
409
257
  /**
@@ -413,50 +261,41 @@ exports.simulateTransaction = simulateTransaction;
413
261
  * @param ignoreError - return status even if tx failed
414
262
  * @returns Transaction Status
415
263
  */
416
- function confirmAndEnsureTransaction(connection, signature, ignoreError) {
417
- return __awaiter(this, void 0, void 0, function () {
418
- var response, value;
419
- return __generator(this, function (_a) {
420
- switch (_a.label) {
421
- case 0: return [4 /*yield*/, connection.getSignatureStatus(signature)];
422
- case 1:
423
- response = _a.sent();
424
- if (!response) {
425
- return [2 /*return*/, null];
426
- }
427
- value = response.value;
428
- if (!value) {
429
- return [2 /*return*/, null];
430
- }
431
- if (!ignoreError && value.err) {
432
- // That's how solana-web3js does it, `err` here is an object that won't really be handled
433
- throw new types_js_1.TransactionFailedError("Raw transaction ".concat(signature, " failed (").concat(JSON.stringify({ err: value.err }), ")"));
434
- }
435
- switch (connection.commitment) {
436
- case "confirmed":
437
- case "single":
438
- case "singleGossip": {
439
- if (value.confirmationStatus === "processed") {
440
- return [2 /*return*/, null];
441
- }
442
- break;
443
- }
444
- case "finalized":
445
- case "max":
446
- case "root": {
447
- if (value.confirmationStatus === "processed" || value.confirmationStatus === "confirmed") {
448
- return [2 /*return*/, null];
449
- }
450
- break;
451
- }
452
- // exhaust enums to ensure full coverage
453
- case "processed":
454
- case "recent":
455
- }
456
- return [2 /*return*/, value];
264
+ async function confirmAndEnsureTransaction(connection, signature, ignoreError) {
265
+ const response = await connection.getSignatureStatuses([signature]);
266
+ if (!response || response.value.length === 0 || response.value[0] === null) {
267
+ return null;
268
+ }
269
+ const value = response.value[0];
270
+ if (!value) {
271
+ return null;
272
+ }
273
+ if (!ignoreError && value.err) {
274
+ // That's how solana-web3js does it, `err` here is an object that won't really be handled
275
+ throw new types_js_1.TransactionFailedError(`Raw transaction ${signature} failed (${JSON.stringify({ err: value.err })})`);
276
+ }
277
+ switch (connection.commitment) {
278
+ case "confirmed":
279
+ case "single":
280
+ case "singleGossip": {
281
+ if (value.confirmationStatus === "processed") {
282
+ return null;
457
283
  }
458
- });
459
- });
284
+ break;
285
+ }
286
+ case "finalized":
287
+ case "max":
288
+ case "root": {
289
+ if (value.confirmationStatus === "processed" || value.confirmationStatus === "confirmed") {
290
+ return null;
291
+ }
292
+ break;
293
+ }
294
+ // exhaust enums to ensure full coverage
295
+ case "processed":
296
+ case "recent":
297
+ }
298
+ return value;
460
299
  }
461
300
  exports.confirmAndEnsureTransaction = confirmAndEnsureTransaction;
462
301
  /**
@@ -476,60 +315,28 @@ exports.ata = ata;
476
315
  * @param paramsBatch - Array of Params for each ATA account: {mint, owner}
477
316
  * @returns Array of boolean where each member corresponds to an owner
478
317
  */
479
- function ataBatchExist(connection, paramsBatch) {
480
- return __awaiter(this, void 0, void 0, function () {
481
- var tokenAccounts, response;
482
- var _this = this;
483
- return __generator(this, function (_a) {
484
- switch (_a.label) {
485
- case 0: return [4 /*yield*/, Promise.all(paramsBatch.map(function (_a) {
486
- var mint = _a.mint, owner = _a.owner, programId = _a.programId;
487
- return __awaiter(_this, void 0, void 0, function () {
488
- return __generator(this, function (_b) {
489
- return [2 /*return*/, ata(mint, owner, programId)];
490
- });
491
- });
492
- }))];
493
- case 1:
494
- tokenAccounts = _a.sent();
495
- return [4 /*yield*/, connection.getMultipleAccountsInfo(tokenAccounts)];
496
- case 2:
497
- response = _a.sent();
498
- return [2 /*return*/, response.map(function (accInfo) { return !!accInfo; })];
499
- }
500
- });
501
- });
318
+ async function ataBatchExist(connection, paramsBatch) {
319
+ const tokenAccounts = await Promise.all(paramsBatch.map(async ({ mint, owner, programId }) => {
320
+ return ata(mint, owner, programId);
321
+ }));
322
+ const response = await connection.getMultipleAccountsInfo(tokenAccounts);
323
+ return response.map((accInfo) => !!accInfo);
502
324
  }
503
325
  exports.ataBatchExist = ataBatchExist;
504
- function enrichAtaParams(connection, paramsBatch) {
505
- return __awaiter(this, void 0, void 0, function () {
506
- var programIdByMint;
507
- var _this = this;
508
- return __generator(this, function (_a) {
509
- programIdByMint = {};
510
- return [2 /*return*/, Promise.all(paramsBatch.map(function (params) { return __awaiter(_this, void 0, void 0, function () {
511
- var mintStr, tokenProgramId;
512
- return __generator(this, function (_a) {
513
- switch (_a.label) {
514
- case 0:
515
- if (params.programId) {
516
- return [2 /*return*/, params];
517
- }
518
- mintStr = params.mint.toString();
519
- if (!!(mintStr in programIdByMint)) return [3 /*break*/, 2];
520
- return [4 /*yield*/, getMintAndProgram(connection, params.mint)];
521
- case 1:
522
- tokenProgramId = (_a.sent()).tokenProgramId;
523
- programIdByMint[mintStr] = tokenProgramId;
524
- _a.label = 2;
525
- case 2:
526
- params.programId = programIdByMint[mintStr];
527
- return [2 /*return*/, params];
528
- }
529
- });
530
- }); }))];
531
- });
532
- });
326
+ async function enrichAtaParams(connection, paramsBatch) {
327
+ const programIdByMint = {};
328
+ return Promise.all(paramsBatch.map(async (params) => {
329
+ if (params.programId) {
330
+ return params;
331
+ }
332
+ const mintStr = params.mint.toString();
333
+ if (!(mintStr in programIdByMint)) {
334
+ const { tokenProgramId } = await getMintAndProgram(connection, params.mint);
335
+ programIdByMint[mintStr] = tokenProgramId;
336
+ }
337
+ params.programId = programIdByMint[mintStr];
338
+ return params;
339
+ }));
533
340
  }
534
341
  exports.enrichAtaParams = enrichAtaParams;
535
342
  /**
@@ -540,45 +347,19 @@ exports.enrichAtaParams = enrichAtaParams;
540
347
  * @param commitment - optional commitment that will be used to fetch Blockhash
541
348
  * @returns Unsigned Transaction with create ATA instructions
542
349
  */
543
- function generateCreateAtaBatchTx(connection, payer, paramsBatch, commitment) {
544
- return __awaiter(this, void 0, void 0, function () {
545
- var ixs, _a, hash, context, messageV0, tx;
546
- var _this = this;
547
- return __generator(this, function (_b) {
548
- switch (_b.label) {
549
- case 0: return [4 /*yield*/, enrichAtaParams(connection, paramsBatch)];
550
- case 1:
551
- paramsBatch = _b.sent();
552
- return [4 /*yield*/, Promise.all(paramsBatch.map(function (_a) {
553
- var mint = _a.mint, owner = _a.owner, programId = _a.programId;
554
- return __awaiter(_this, void 0, void 0, function () {
555
- var _b, _c;
556
- return __generator(this, function (_d) {
557
- switch (_d.label) {
558
- case 0:
559
- _b = spl_token_1.createAssociatedTokenAccountInstruction;
560
- _c = [payer];
561
- return [4 /*yield*/, ata(mint, owner)];
562
- case 1: return [2 /*return*/, _b.apply(void 0, _c.concat([_d.sent(), owner, mint, programId]))];
563
- }
564
- });
565
- });
566
- }))];
567
- case 2:
568
- ixs = _b.sent();
569
- return [4 /*yield*/, connection.getLatestBlockhashAndContext({ commitment: commitment })];
570
- case 3:
571
- _a = _b.sent(), hash = _a.value, context = _a.context;
572
- messageV0 = new web3_js_1.TransactionMessage({
573
- payerKey: payer,
574
- recentBlockhash: hash.blockhash,
575
- instructions: ixs,
576
- }).compileToV0Message();
577
- tx = new web3_js_1.VersionedTransaction(messageV0);
578
- return [2 /*return*/, { tx: tx, hash: hash, context: context }];
579
- }
580
- });
581
- });
350
+ async function generateCreateAtaBatchTx(connection, payer, paramsBatch, commitment) {
351
+ paramsBatch = await enrichAtaParams(connection, paramsBatch);
352
+ const ixs = await Promise.all(paramsBatch.map(async ({ mint, owner, programId }) => {
353
+ return (0, spl_token_1.createAssociatedTokenAccountInstruction)(payer, await ata(mint, owner), owner, mint, programId);
354
+ }));
355
+ const { value: hash, context } = await connection.getLatestBlockhashAndContext({ commitment });
356
+ const messageV0 = new web3_js_1.TransactionMessage({
357
+ payerKey: payer,
358
+ recentBlockhash: hash.blockhash,
359
+ instructions: ixs,
360
+ }).compileToV0Message();
361
+ const tx = new web3_js_1.VersionedTransaction(messageV0);
362
+ return { tx, hash, context };
582
363
  }
583
364
  exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
584
365
  /**
@@ -590,23 +371,9 @@ exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
590
371
  * @param rate - throttle rate for tx sending
591
372
  * @returns Transaction signature
592
373
  */
593
- function createAtaBatch(connection, invoker, paramsBatch, commitment, rate) {
594
- return __awaiter(this, void 0, void 0, function () {
595
- var _a, tx, hash, context, _b, _c;
596
- return __generator(this, function (_d) {
597
- switch (_d.label) {
598
- case 0:
599
- _b = generateCreateAtaBatchTx;
600
- _c = [connection,
601
- invoker.publicKey];
602
- return [4 /*yield*/, enrichAtaParams(connection, paramsBatch)];
603
- case 1: return [4 /*yield*/, _b.apply(void 0, _c.concat([_d.sent(), commitment]))];
604
- case 2:
605
- _a = _d.sent(), tx = _a.tx, hash = _a.hash, context = _a.context;
606
- return [2 /*return*/, signAndExecuteTransaction(connection, invoker, tx, { hash: hash, context: context, commitment: commitment }, { sendRate: rate })];
607
- }
608
- });
609
- });
374
+ async function createAtaBatch(connection, invoker, paramsBatch, commitment, rate) {
375
+ const { tx, hash, context } = await generateCreateAtaBatchTx(connection, invoker.publicKey, await enrichAtaParams(connection, paramsBatch), commitment);
376
+ return signAndExecuteTransaction(connection, invoker, tx, { hash, context, commitment }, { sendRate: rate });
610
377
  }
611
378
  exports.createAtaBatch = createAtaBatch;
612
379
  /**
@@ -618,60 +385,23 @@ exports.createAtaBatch = createAtaBatch;
618
385
  * @param programId - Program ID of the Mint
619
386
  * @returns Array of Transaction Instructions that should be added to a transaction
620
387
  */
621
- function checkOrCreateAtaBatch(connection, owners, mint, invoker, programId) {
622
- return __awaiter(this, void 0, void 0, function () {
623
- var ixs, atas, owners_1, owners_1_1, owner, _a, _b, e_3_1, response, i;
624
- var e_3, _c;
625
- return __generator(this, function (_d) {
626
- switch (_d.label) {
627
- case 0:
628
- ixs = [];
629
- if (!!programId) return [3 /*break*/, 2];
630
- return [4 /*yield*/, getMintAndProgram(connection, mint)];
631
- case 1:
632
- programId = (_d.sent()).tokenProgramId;
633
- _d.label = 2;
634
- case 2:
635
- atas = [];
636
- _d.label = 3;
637
- case 3:
638
- _d.trys.push([3, 8, 9, 10]);
639
- owners_1 = __values(owners), owners_1_1 = owners_1.next();
640
- _d.label = 4;
641
- case 4:
642
- if (!!owners_1_1.done) return [3 /*break*/, 7];
643
- owner = owners_1_1.value;
644
- _b = (_a = atas).push;
645
- return [4 /*yield*/, ata(mint, owner, programId)];
646
- case 5:
647
- _b.apply(_a, [_d.sent()]);
648
- _d.label = 6;
649
- case 6:
650
- owners_1_1 = owners_1.next();
651
- return [3 /*break*/, 4];
652
- case 7: return [3 /*break*/, 10];
653
- case 8:
654
- e_3_1 = _d.sent();
655
- e_3 = { error: e_3_1 };
656
- return [3 /*break*/, 10];
657
- case 9:
658
- try {
659
- if (owners_1_1 && !owners_1_1.done && (_c = owners_1.return)) _c.call(owners_1);
660
- }
661
- finally { if (e_3) throw e_3.error; }
662
- return [7 /*endfinally*/];
663
- case 10: return [4 /*yield*/, connection.getMultipleAccountsInfo(atas)];
664
- case 11:
665
- response = _d.sent();
666
- for (i = 0; i < response.length; i++) {
667
- if (!response[i]) {
668
- ixs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(invoker.publicKey, atas[i], owners[i], mint, programId));
669
- }
670
- }
671
- return [2 /*return*/, ixs];
672
- }
673
- });
674
- });
388
+ async function checkOrCreateAtaBatch(connection, owners, mint, invoker, programId) {
389
+ const ixs = [];
390
+ if (!programId) {
391
+ programId = (await getMintAndProgram(connection, mint)).tokenProgramId;
392
+ }
393
+ // TODO: optimize fetching and maps/arrays
394
+ const atas = [];
395
+ for (const owner of owners) {
396
+ atas.push(await ata(mint, owner, programId));
397
+ }
398
+ const response = await connection.getMultipleAccountsInfo(atas);
399
+ for (let i = 0; i < response.length; i++) {
400
+ if (!response[i]) {
401
+ ixs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(invoker.publicKey, atas[i], owners[i], mint, programId));
402
+ }
403
+ }
404
+ return ixs;
675
405
  }
676
406
  exports.checkOrCreateAtaBatch = checkOrCreateAtaBatch;
677
407
  /**
@@ -679,9 +409,8 @@ exports.checkOrCreateAtaBatch = checkOrCreateAtaBatch;
679
409
  * - sets compute price if `computePrice` is provided
680
410
  * - sets compute limit if `computeLimit` is provided
681
411
  */
682
- function prepareBaseInstructions(connection, _a) {
683
- var computePrice = _a.computePrice, computeLimit = _a.computeLimit;
684
- var ixs = [];
412
+ function prepareBaseInstructions(connection, { computePrice, computeLimit }) {
413
+ const ixs = [];
685
414
  if (computePrice) {
686
415
  ixs.push(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: computePrice }));
687
416
  }
@@ -700,24 +429,15 @@ exports.prepareBaseInstructions = prepareBaseInstructions;
700
429
  *
701
430
  * @return Mint information
702
431
  */
703
- function getMintAndProgram(connection, address, commitment) {
704
- return __awaiter(this, void 0, void 0, function () {
705
- var accountInfo, programId;
706
- return __generator(this, function (_a) {
707
- switch (_a.label) {
708
- case 0: return [4 /*yield*/, connection.getAccountInfo(address, commitment)];
709
- case 1:
710
- accountInfo = _a.sent();
711
- programId = accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.owner;
712
- if (!(programId === null || programId === void 0 ? void 0 : programId.equals(spl_token_1.TOKEN_PROGRAM_ID)) && !(programId === null || programId === void 0 ? void 0 : programId.equals(spl_token_1.TOKEN_2022_PROGRAM_ID))) {
713
- programId = spl_token_1.TOKEN_PROGRAM_ID;
714
- }
715
- return [2 /*return*/, {
716
- mint: (0, spl_token_1.unpackMint)(address, accountInfo, programId),
717
- tokenProgramId: programId,
718
- }];
719
- }
720
- });
721
- });
432
+ async function getMintAndProgram(connection, address, commitment) {
433
+ const accountInfo = await connection.getAccountInfo(address, commitment);
434
+ let programId = accountInfo?.owner;
435
+ if (!programId?.equals(spl_token_1.TOKEN_PROGRAM_ID) && !programId?.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
436
+ programId = spl_token_1.TOKEN_PROGRAM_ID;
437
+ }
438
+ return {
439
+ mint: (0, spl_token_1.unpackMint)(address, accountInfo, programId),
440
+ tokenProgramId: programId,
441
+ };
722
442
  }
723
443
  exports.getMintAndProgram = getMintAndProgram;