@streamflow/common 6.0.2 → 6.1.0

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,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import { AccountInfo, PublicKey } from "@solana/web3.js";
2
+ import { AccountInfo, BlockhashWithExpiryBlockHeight, Commitment, Context, PublicKey } from "@solana/web3.js";
3
3
  export interface ITransactionSolanaExt {
4
4
  computePrice?: number;
5
5
  computeLimit?: number;
@@ -20,3 +20,10 @@ export interface AtaParams {
20
20
  owner: PublicKey;
21
21
  programId?: PublicKey;
22
22
  }
23
+ export interface ConfirmationParams {
24
+ hash: BlockhashWithExpiryBlockHeight;
25
+ context: Context;
26
+ commitment?: Commitment;
27
+ }
28
+ export declare class TransactionFailedError extends Error {
29
+ }
@@ -1,2 +1,26 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.TransactionFailedError = void 0;
19
+ var TransactionFailedError = /** @class */ (function (_super) {
20
+ __extends(TransactionFailedError, _super);
21
+ function TransactionFailedError() {
22
+ return _super !== null && _super.apply(this, arguments) || this;
23
+ }
24
+ return TransactionFailedError;
25
+ }(Error));
26
+ exports.TransactionFailedError = TransactionFailedError;
@@ -1,7 +1,7 @@
1
1
  import { Mint } from "@solana/spl-token";
2
2
  import { SignerWalletAdapter } from "@solana/wallet-adapter-base";
3
- import { BlockhashWithExpiryBlockHeight, Commitment, Connection, Keypair, PublicKey, Transaction, TransactionInstruction } from "@solana/web3.js";
4
- import { Account, AtaParams, ITransactionSolanaExt } from "./types";
3
+ import { BlockhashWithExpiryBlockHeight, Commitment, Connection, Keypair, PublicKey, Transaction, TransactionInstruction, SignatureStatus, VersionedTransaction, Context } from "@solana/web3.js";
4
+ import { Account, AtaParams, ConfirmationParams, ITransactionSolanaExt } from "./types";
5
5
  /**
6
6
  * Wrapper function for Solana web3 getProgramAccounts with slightly better call interface
7
7
  * @param {Connection} connection - Solana web3 connection object.
@@ -23,6 +23,12 @@ export declare function isSignerWallet(walletOrKeypair: Keypair | SignerWalletAd
23
23
  * @returns {boolean} - Returns true if parameter is a Keypair.
24
24
  */
25
25
  export declare function isSignerKeypair(walletOrKeypair: Keypair | SignerWalletAdapter): walletOrKeypair is Keypair;
26
+ /**
27
+ * Utility function to check whether given transaction is Versioned
28
+ * @param tx {Transaction | VersionedTransaction} - Transaction to check
29
+ * @returns {boolean} - Returns true if transaction is Versioned.
30
+ */
31
+ export declare function isTransactionVersioned(tx: Transaction | VersionedTransaction): tx is VersionedTransaction;
26
32
  /**
27
33
  * Creates a Transaction with given instructions and optionally signs it.
28
34
  * @param connection - Solana client connection
@@ -33,23 +39,45 @@ export declare function isSignerKeypair(walletOrKeypair: Keypair | SignerWalletA
33
39
  * @returns Transaction and Blockhash
34
40
  */
35
41
  export declare function prepareTransaction(connection: Connection, ixs: TransactionInstruction[], payer: PublicKey | undefined | null, commitment?: Commitment, ...partialSigners: (Keypair | undefined)[]): Promise<{
36
- tx: Transaction;
42
+ tx: VersionedTransaction;
37
43
  hash: BlockhashWithExpiryBlockHeight;
44
+ context: Context;
38
45
  }>;
39
- export declare function signTransaction(invoker: Keypair | SignerWalletAdapter, tx: Transaction): Promise<Transaction>;
46
+ export declare function signTransaction<T extends Transaction | VersionedTransaction>(invoker: Keypair | SignerWalletAdapter, tx: T): Promise<T>;
40
47
  /**
41
48
  * Signs, sends and confirms Transaction
42
- * Confirmation strategy is not 100% reliable here as in times of congestion there can be a case that tx is executed,
43
- * but is not in `commitment` state and so it's not considered executed by the `sendAndConfirmRawTransaction` method,
44
- * and it raises an expiry error even though transaction may be executed soon.
45
- * So we add additional 50 blocks for checks to account for such issues.
46
49
  * @param connection - Solana client connection
47
50
  * @param invoker - Keypair used as signer
48
51
  * @param tx - Transaction instance
52
+ * @param {ConfirmationParams} confirmationParams - Confirmation Params that will be used for execution
53
+ * @returns Transaction signature
54
+ */
55
+ export declare function signAndExecuteTransaction(connection: Connection, invoker: Keypair | SignerWalletAdapter, tx: Transaction | VersionedTransaction, confirmationParams: ConfirmationParams): Promise<string>;
56
+ /**
57
+ * Sends and confirms Transaction
58
+ * Uses custom confirmation logic that:
59
+ * - simulates tx before sending separately
60
+ * - sends transaction without preFlight checks but with some valuable flags https://twitter.com/jordaaash/status/1774892862049800524?s=46&t=bhZ10V0r7IX5Lk5kKzxfGw
61
+ * - rebroadcasts a tx every 500 ms
62
+ * - after broadcasting check whether tx has executed once
63
+ * - catch errors for every actionable item, throw only the ones that signal that tx has failed
64
+ * - otherwise there is a chance of marking a landed tx as failed if it was broadcasted at least once
65
+ * @param connection - Solana client connection
66
+ * @param tx - Transaction instance
49
67
  * @param hash - blockhash information, the same hash should be used in the Transaction
68
+ * @param context - context at which blockhash has been retrieve
69
+ * @param commitment - optional commitment that will be used for simulation and confirmation
50
70
  * @returns Transaction signature
51
71
  */
52
- export declare function signAndExecuteTransaction(connection: Connection, invoker: Keypair | SignerWalletAdapter, tx: Transaction, hash: BlockhashWithExpiryBlockHeight): Promise<string>;
72
+ export declare function executeTransaction(connection: Connection, tx: Transaction | VersionedTransaction, { hash, context, commitment }: ConfirmationParams): Promise<string>;
73
+ /**
74
+ * Confirms and validates transaction success once
75
+ * @param connection - Solana client connection
76
+ * @param signature - Transaction signature
77
+ * @param passError - return status even if tx failed
78
+ * @returns Transaction Status
79
+ */
80
+ export declare function confirmAndEnsureTransaction(connection: Connection, signature: string, passError?: boolean): Promise<SignatureStatus | null>;
53
81
  /**
54
82
  * Shorthand call signature for getAssociatedTokenAddress, with allowance for address to be offCurve
55
83
  * @param {PublicKey} mint - SPL token Mint address.
@@ -71,20 +99,23 @@ export declare function enrichAtaParams(connection: Connection, paramsBatch: Ata
71
99
  * @param connection - Solana client connection
72
100
  * @param payer - Transaction invoker, should be a signer
73
101
  * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
102
+ * @param commitment - optional commitment that will be used to fetch Blockhash
74
103
  * @returns Unsigned Transaction with create ATA instructions
75
104
  */
76
- export declare function generateCreateAtaBatchTx(connection: Connection, payer: PublicKey, paramsBatch: AtaParams[]): Promise<{
77
- tx: Transaction;
105
+ export declare function generateCreateAtaBatchTx(connection: Connection, payer: PublicKey, paramsBatch: AtaParams[], commitment?: Commitment): Promise<{
106
+ tx: VersionedTransaction;
78
107
  hash: BlockhashWithExpiryBlockHeight;
108
+ context: Context;
79
109
  }>;
80
110
  /**
81
111
  * Creates ATA for an array of owners
82
112
  * @param connection - Solana client connection
83
113
  * @param invoker - Transaction invoker and payer
84
114
  * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
115
+ * @param commitment - optional commitment that will be used to fetch Blockhash
85
116
  * @returns Transaction signature
86
117
  */
87
- export declare function createAtaBatch(connection: Connection, invoker: Keypair | SignerWalletAdapter, paramsBatch: AtaParams[]): Promise<string>;
118
+ export declare function createAtaBatch(connection: Connection, invoker: Keypair | SignerWalletAdapter, paramsBatch: AtaParams[], commitment?: Commitment): Promise<string>;
88
119
  /**
89
120
  * Utility function that checks whether associated token accounts exist and return instructions to populate them if not
90
121
  * @param connection - Solana client connection
@@ -35,31 +35,6 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
35
35
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
36
  }
37
37
  };
38
- var __read = (this && this.__read) || function (o, n) {
39
- var m = typeof Symbol === "function" && o[Symbol.iterator];
40
- if (!m) return o;
41
- var i = m.call(o), r, ar = [], e;
42
- try {
43
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
44
- }
45
- catch (error) { e = { error: error }; }
46
- finally {
47
- try {
48
- if (r && !r.done && (m = i["return"])) m.call(i);
49
- }
50
- finally { if (e) throw e.error; }
51
- }
52
- return ar;
53
- };
54
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
56
- if (ar || !(i in from)) {
57
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
58
- ar[i] = from[i];
59
- }
60
- }
61
- return to.concat(ar || Array.prototype.slice.call(from));
62
- };
63
38
  var __values = (this && this.__values) || function(o) {
64
39
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
65
40
  if (m) return m.call(o);
@@ -75,10 +50,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
75
50
  return (mod && mod.__esModule) ? mod : { "default": mod };
76
51
  };
77
52
  Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.getMintAndProgram = exports.prepareBaseInstructions = exports.checkOrCreateAtaBatch = exports.createAtaBatch = exports.generateCreateAtaBatchTx = exports.enrichAtaParams = exports.ataBatchExist = exports.ata = exports.signAndExecuteTransaction = exports.signTransaction = exports.prepareTransaction = exports.isSignerKeypair = exports.isSignerWallet = exports.getProgramAccounts = void 0;
53
+ exports.getMintAndProgram = exports.prepareBaseInstructions = exports.checkOrCreateAtaBatch = exports.createAtaBatch = exports.generateCreateAtaBatchTx = exports.enrichAtaParams = exports.ataBatchExist = exports.ata = exports.confirmAndEnsureTransaction = exports.executeTransaction = exports.signAndExecuteTransaction = exports.signTransaction = exports.prepareTransaction = exports.isTransactionVersioned = exports.isSignerKeypair = exports.isSignerWallet = exports.getProgramAccounts = void 0;
79
54
  var spl_token_1 = require("@solana/spl-token");
80
55
  var web3_js_1 = require("@solana/web3.js");
81
56
  var bs58_1 = __importDefault(require("bs58"));
57
+ var types_1 = require("./types");
58
+ var utils_1 = require("../utils");
82
59
  /**
83
60
  * Wrapper function for Solana web3 getProgramAccounts with slightly better call interface
84
61
  * @param {Connection} connection - Solana web3 connection object.
@@ -124,6 +101,15 @@ function isSignerKeypair(walletOrKeypair) {
124
101
  walletOrKeypair.constructor.name === web3_js_1.Keypair.prototype.constructor.name);
125
102
  }
126
103
  exports.isSignerKeypair = isSignerKeypair;
104
+ /**
105
+ * Utility function to check whether given transaction is Versioned
106
+ * @param tx {Transaction | VersionedTransaction} - Transaction to check
107
+ * @returns {boolean} - Returns true if transaction is Versioned.
108
+ */
109
+ function isTransactionVersioned(tx) {
110
+ return "message" in tx;
111
+ }
112
+ exports.isTransactionVersioned = isTransactionVersioned;
127
113
  /**
128
114
  * Creates a Transaction with given instructions and optionally signs it.
129
115
  * @param connection - Solana client connection
@@ -139,34 +125,21 @@ function prepareTransaction(connection, ixs, payer, commitment) {
139
125
  partialSigners[_i - 4] = arguments[_i];
140
126
  }
141
127
  return __awaiter(this, void 0, void 0, function () {
142
- var hash, tx, partialSigners_1, partialSigners_1_1, signer;
143
- var _a, e_1, _b;
144
- return __generator(this, function (_c) {
145
- switch (_c.label) {
146
- case 0: return [4 /*yield*/, connection.getLatestBlockhash(commitment)];
128
+ var _a, hash, context, messageV0, tx, signers;
129
+ return __generator(this, function (_b) {
130
+ switch (_b.label) {
131
+ case 0: return [4 /*yield*/, connection.getLatestBlockhashAndContext(commitment)];
147
132
  case 1:
148
- hash = _c.sent();
149
- tx = (_a = new web3_js_1.Transaction({
150
- feePayer: payer,
151
- blockhash: hash.blockhash,
152
- lastValidBlockHeight: hash.lastValidBlockHeight,
153
- })).add.apply(_a, __spreadArray([], __read(ixs), false));
154
- try {
155
- for (partialSigners_1 = __values(partialSigners), partialSigners_1_1 = partialSigners_1.next(); !partialSigners_1_1.done; partialSigners_1_1 = partialSigners_1.next()) {
156
- signer = partialSigners_1_1.value;
157
- if (signer) {
158
- tx.partialSign(signer);
159
- }
160
- }
161
- }
162
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
163
- finally {
164
- try {
165
- if (partialSigners_1_1 && !partialSigners_1_1.done && (_b = partialSigners_1.return)) _b.call(partialSigners_1);
166
- }
167
- finally { if (e_1) throw e_1.error; }
168
- }
169
- return [2 /*return*/, { tx: tx, hash: hash }];
133
+ _a = _b.sent(), hash = _a.value, context = _a.context;
134
+ messageV0 = new web3_js_1.TransactionMessage({
135
+ payerKey: payer,
136
+ recentBlockhash: hash.blockhash,
137
+ instructions: ixs,
138
+ }).compileToV0Message();
139
+ tx = new web3_js_1.VersionedTransaction(messageV0);
140
+ signers = partialSigners.filter(function (item) { return !!item; });
141
+ tx.sign(signers);
142
+ return [2 /*return*/, { tx: tx, context: context, hash: hash }];
170
143
  }
171
144
  });
172
145
  });
@@ -184,7 +157,12 @@ function signTransaction(invoker, tx) {
184
157
  signedTx = _a.sent();
185
158
  return [3 /*break*/, 3];
186
159
  case 2:
187
- tx.partialSign(invoker);
160
+ if (isTransactionVersioned(tx)) {
161
+ tx.sign([invoker]);
162
+ }
163
+ else {
164
+ tx.partialSign(invoker);
165
+ }
188
166
  signedTx = tx;
189
167
  _a.label = 3;
190
168
  case 3: return [2 /*return*/, signedTx];
@@ -195,38 +173,207 @@ function signTransaction(invoker, tx) {
195
173
  exports.signTransaction = signTransaction;
196
174
  /**
197
175
  * Signs, sends and confirms Transaction
198
- * Confirmation strategy is not 100% reliable here as in times of congestion there can be a case that tx is executed,
199
- * but is not in `commitment` state and so it's not considered executed by the `sendAndConfirmRawTransaction` method,
200
- * and it raises an expiry error even though transaction may be executed soon.
201
- * So we add additional 50 blocks for checks to account for such issues.
202
176
  * @param connection - Solana client connection
203
177
  * @param invoker - Keypair used as signer
204
178
  * @param tx - Transaction instance
205
- * @param hash - blockhash information, the same hash should be used in the Transaction
179
+ * @param {ConfirmationParams} confirmationParams - Confirmation Params that will be used for execution
206
180
  * @returns Transaction signature
207
181
  */
208
- function signAndExecuteTransaction(connection, invoker, tx, hash) {
182
+ function signAndExecuteTransaction(connection, invoker, tx, confirmationParams) {
209
183
  return __awaiter(this, void 0, void 0, function () {
210
- var signedTx, rawTx, confirmationStrategy;
184
+ var signedTx;
211
185
  return __generator(this, function (_a) {
212
186
  switch (_a.label) {
213
187
  case 0: return [4 /*yield*/, signTransaction(invoker, tx)];
214
188
  case 1:
215
189
  signedTx = _a.sent();
216
- rawTx = signedTx.serialize();
217
- if (!hash.lastValidBlockHeight || !signedTx.signature || !hash.blockhash)
218
- throw Error("Error with transaction parameters.");
219
- confirmationStrategy = {
220
- lastValidBlockHeight: hash.lastValidBlockHeight + 50,
221
- signature: bs58_1.default.encode(signedTx.signature),
222
- blockhash: hash.blockhash,
223
- };
224
- return [2 /*return*/, (0, web3_js_1.sendAndConfirmRawTransaction)(connection, rawTx, confirmationStrategy)];
190
+ return [2 /*return*/, executeTransaction(connection, signedTx, confirmationParams)];
225
191
  }
226
192
  });
227
193
  });
228
194
  }
229
195
  exports.signAndExecuteTransaction = signAndExecuteTransaction;
196
+ /**
197
+ * Sends and confirms Transaction
198
+ * Uses custom confirmation logic that:
199
+ * - simulates tx before sending separately
200
+ * - sends transaction without preFlight checks but with some valuable flags https://twitter.com/jordaaash/status/1774892862049800524?s=46&t=bhZ10V0r7IX5Lk5kKzxfGw
201
+ * - rebroadcasts a tx every 500 ms
202
+ * - after broadcasting check whether tx has executed once
203
+ * - catch errors for every actionable item, throw only the ones that signal that tx has failed
204
+ * - otherwise there is a chance of marking a landed tx as failed if it was broadcasted at least once
205
+ * @param connection - Solana client connection
206
+ * @param tx - Transaction instance
207
+ * @param hash - blockhash information, the same hash should be used in the Transaction
208
+ * @param context - context at which blockhash has been retrieve
209
+ * @param commitment - optional commitment that will be used for simulation and confirmation
210
+ * @returns Transaction signature
211
+ */
212
+ function executeTransaction(connection, tx, _a) {
213
+ var hash = _a.hash, context = _a.context, commitment = _a.commitment;
214
+ return __awaiter(this, void 0, void 0, function () {
215
+ var i, res, errMessage, isVersioned, signature, blockheight, transactionSent, rawTransaction, e_1, value, e_2, _e_1;
216
+ return __generator(this, function (_b) {
217
+ switch (_b.label) {
218
+ case 0:
219
+ if (!hash.lastValidBlockHeight || tx.signatures.length === 0 || !hash.blockhash) {
220
+ throw Error("Error with transaction parameters.");
221
+ }
222
+ i = 0;
223
+ _b.label = 1;
224
+ case 1:
225
+ if (!(i < 3)) return [3 /*break*/, 7];
226
+ res = void 0;
227
+ if (!isTransactionVersioned(tx)) return [3 /*break*/, 3];
228
+ return [4 /*yield*/, connection.simulateTransaction(tx)];
229
+ case 2:
230
+ res = _b.sent();
231
+ return [3 /*break*/, 5];
232
+ case 3: return [4 /*yield*/, connection.simulateTransaction(tx)];
233
+ case 4:
234
+ res = _b.sent();
235
+ _b.label = 5;
236
+ case 5:
237
+ if (res.value.err) {
238
+ errMessage = JSON.stringify(res.value.err);
239
+ if (!errMessage.includes("BlockhashNotFound") || i === 2) {
240
+ throw new web3_js_1.SendTransactionError("failed to simulate transaction: " + errMessage, res.value.logs || undefined);
241
+ }
242
+ }
243
+ return [3 /*break*/, 7];
244
+ case 6:
245
+ i++;
246
+ return [3 /*break*/, 1];
247
+ case 7:
248
+ isVersioned = isTransactionVersioned(tx);
249
+ if (isVersioned) {
250
+ signature = bs58_1.default.encode(tx.signatures[0]);
251
+ }
252
+ else {
253
+ signature = bs58_1.default.encode(tx.signature);
254
+ }
255
+ return [4 /*yield*/, connection.getBlockHeight(commitment)];
256
+ case 8:
257
+ blockheight = _b.sent();
258
+ transactionSent = false;
259
+ rawTransaction = tx.serialize();
260
+ _b.label = 9;
261
+ case 9:
262
+ if (!(blockheight < hash.lastValidBlockHeight)) return [3 /*break*/, 24];
263
+ _b.label = 10;
264
+ case 10:
265
+ _b.trys.push([10, 12, , 13]);
266
+ return [4 /*yield*/, connection.sendRawTransaction(rawTransaction, {
267
+ maxRetries: 0,
268
+ minContextSlot: context.slot,
269
+ preflightCommitment: commitment,
270
+ skipPreflight: true,
271
+ })];
272
+ case 11:
273
+ _b.sent();
274
+ transactionSent = true;
275
+ return [3 /*break*/, 13];
276
+ case 12:
277
+ e_1 = _b.sent();
278
+ if (transactionSent ||
279
+ (e_1 instanceof web3_js_1.SendTransactionError && e_1.message.includes("Minimum context slot has not been reached"))) {
280
+ return [3 /*break*/, 9];
281
+ }
282
+ throw e_1;
283
+ case 13: return [4 /*yield*/, (0, utils_1.sleep)(500)];
284
+ case 14:
285
+ _b.sent();
286
+ _b.label = 15;
287
+ case 15:
288
+ _b.trys.push([15, 17, , 19]);
289
+ return [4 /*yield*/, confirmAndEnsureTransaction(connection, signature)];
290
+ case 16:
291
+ value = _b.sent();
292
+ if (value) {
293
+ return [2 /*return*/, signature];
294
+ }
295
+ return [3 /*break*/, 19];
296
+ case 17:
297
+ e_2 = _b.sent();
298
+ if (e_2 instanceof types_1.TransactionFailedError) {
299
+ throw e_2;
300
+ }
301
+ return [4 /*yield*/, (0, utils_1.sleep)(500)];
302
+ case 18:
303
+ _b.sent();
304
+ return [3 /*break*/, 19];
305
+ case 19:
306
+ _b.trys.push([19, 21, , 23]);
307
+ return [4 /*yield*/, connection.getBlockHeight(commitment)];
308
+ case 20:
309
+ blockheight = _b.sent();
310
+ return [3 /*break*/, 23];
311
+ case 21:
312
+ _e_1 = _b.sent();
313
+ return [4 /*yield*/, (0, utils_1.sleep)(500)];
314
+ case 22:
315
+ _b.sent();
316
+ return [3 /*break*/, 23];
317
+ case 23: return [3 /*break*/, 9];
318
+ case 24: throw new Error("Transaction ".concat(signature, " expired."));
319
+ }
320
+ });
321
+ });
322
+ }
323
+ exports.executeTransaction = executeTransaction;
324
+ /**
325
+ * Confirms and validates transaction success once
326
+ * @param connection - Solana client connection
327
+ * @param signature - Transaction signature
328
+ * @param passError - return status even if tx failed
329
+ * @returns Transaction Status
330
+ */
331
+ function confirmAndEnsureTransaction(connection, signature, passError) {
332
+ return __awaiter(this, void 0, void 0, function () {
333
+ var response, value;
334
+ return __generator(this, function (_a) {
335
+ switch (_a.label) {
336
+ case 0: return [4 /*yield*/, connection.getSignatureStatus(signature)];
337
+ case 1:
338
+ response = _a.sent();
339
+ if (!response) {
340
+ return [2 /*return*/, null];
341
+ }
342
+ value = response.value;
343
+ if (!value) {
344
+ return [2 /*return*/, null];
345
+ }
346
+ if (!passError && value.err) {
347
+ // That's how solana-web3js does it, `err` here is an object that won't really be handled
348
+ throw new types_1.TransactionFailedError("Raw transaction ".concat(signature, " failed (").concat(JSON.stringify({ err: value.err }), ")"));
349
+ }
350
+ switch (connection.commitment) {
351
+ case "confirmed":
352
+ case "single":
353
+ case "singleGossip": {
354
+ if (value.confirmationStatus === "processed") {
355
+ return [2 /*return*/, null];
356
+ }
357
+ break;
358
+ }
359
+ case "finalized":
360
+ case "max":
361
+ case "root": {
362
+ if (value.confirmationStatus === "processed" || value.confirmationStatus === "confirmed") {
363
+ return [2 /*return*/, null];
364
+ }
365
+ break;
366
+ }
367
+ // exhaust enums to ensure full coverage
368
+ case "processed":
369
+ case "recent":
370
+ }
371
+ return [2 /*return*/, value];
372
+ }
373
+ });
374
+ });
375
+ }
376
+ exports.confirmAndEnsureTransaction = confirmAndEnsureTransaction;
230
377
  /**
231
378
  * Shorthand call signature for getAssociatedTokenAddress, with allowance for address to be offCurve
232
379
  * @param {PublicKey} mint - SPL token Mint address.
@@ -305,12 +452,12 @@ exports.enrichAtaParams = enrichAtaParams;
305
452
  * @param connection - Solana client connection
306
453
  * @param payer - Transaction invoker, should be a signer
307
454
  * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
455
+ * @param commitment - optional commitment that will be used to fetch Blockhash
308
456
  * @returns Unsigned Transaction with create ATA instructions
309
457
  */
310
- function generateCreateAtaBatchTx(connection, payer, paramsBatch) {
458
+ function generateCreateAtaBatchTx(connection, payer, paramsBatch, commitment) {
311
459
  return __awaiter(this, void 0, void 0, function () {
312
- var ixs, hash, tx;
313
- var _a;
460
+ var ixs, _a, hash, context, messageV0, tx;
314
461
  var _this = this;
315
462
  return __generator(this, function (_b) {
316
463
  switch (_b.label) {
@@ -334,15 +481,16 @@ function generateCreateAtaBatchTx(connection, payer, paramsBatch) {
334
481
  }))];
335
482
  case 2:
336
483
  ixs = _b.sent();
337
- return [4 /*yield*/, connection.getLatestBlockhash()];
484
+ return [4 /*yield*/, connection.getLatestBlockhashAndContext({ commitment: commitment })];
338
485
  case 3:
339
- hash = _b.sent();
340
- tx = (_a = new web3_js_1.Transaction({
341
- feePayer: payer,
342
- blockhash: hash.blockhash,
343
- lastValidBlockHeight: hash.lastValidBlockHeight,
344
- })).add.apply(_a, __spreadArray([], __read(ixs), false));
345
- return [2 /*return*/, { tx: tx, hash: hash }];
486
+ _a = _b.sent(), hash = _a.value, context = _a.context;
487
+ messageV0 = new web3_js_1.TransactionMessage({
488
+ payerKey: payer,
489
+ recentBlockhash: hash.blockhash,
490
+ instructions: ixs,
491
+ }).compileToV0Message();
492
+ tx = new web3_js_1.VersionedTransaction(messageV0);
493
+ return [2 /*return*/, { tx: tx, hash: hash, context: context }];
346
494
  }
347
495
  });
348
496
  });
@@ -353,11 +501,12 @@ exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
353
501
  * @param connection - Solana client connection
354
502
  * @param invoker - Transaction invoker and payer
355
503
  * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
504
+ * @param commitment - optional commitment that will be used to fetch Blockhash
356
505
  * @returns Transaction signature
357
506
  */
358
- function createAtaBatch(connection, invoker, paramsBatch) {
507
+ function createAtaBatch(connection, invoker, paramsBatch, commitment) {
359
508
  return __awaiter(this, void 0, void 0, function () {
360
- var _a, tx, hash, _b, _c;
509
+ var _a, tx, hash, context, _b, _c;
361
510
  return __generator(this, function (_d) {
362
511
  switch (_d.label) {
363
512
  case 0:
@@ -365,10 +514,10 @@ function createAtaBatch(connection, invoker, paramsBatch) {
365
514
  _c = [connection,
366
515
  invoker.publicKey];
367
516
  return [4 /*yield*/, enrichAtaParams(connection, paramsBatch)];
368
- case 1: return [4 /*yield*/, _b.apply(void 0, _c.concat([_d.sent()]))];
517
+ case 1: return [4 /*yield*/, _b.apply(void 0, _c.concat([_d.sent(), commitment]))];
369
518
  case 2:
370
- _a = _d.sent(), tx = _a.tx, hash = _a.hash;
371
- return [2 /*return*/, signAndExecuteTransaction(connection, invoker, tx, hash)];
519
+ _a = _d.sent(), tx = _a.tx, hash = _a.hash, context = _a.context;
520
+ return [2 /*return*/, signAndExecuteTransaction(connection, invoker, tx, { hash: hash, context: context, commitment: commitment })];
372
521
  }
373
522
  });
374
523
  });
@@ -385,8 +534,8 @@ exports.createAtaBatch = createAtaBatch;
385
534
  */
386
535
  function checkOrCreateAtaBatch(connection, owners, mint, invoker, programId) {
387
536
  return __awaiter(this, void 0, void 0, function () {
388
- var ixs, atas, owners_1, owners_1_1, owner, _a, _b, e_2_1, response, i;
389
- var e_2, _c;
537
+ var ixs, atas, owners_1, owners_1_1, owner, _a, _b, e_3_1, response, i;
538
+ var e_3, _c;
390
539
  return __generator(this, function (_d) {
391
540
  switch (_d.label) {
392
541
  case 0:
@@ -410,14 +559,14 @@ function checkOrCreateAtaBatch(connection, owners, mint, invoker, programId) {
410
559
  return [3 /*break*/, 2];
411
560
  case 5: return [3 /*break*/, 8];
412
561
  case 6:
413
- e_2_1 = _d.sent();
414
- e_2 = { error: e_2_1 };
562
+ e_3_1 = _d.sent();
563
+ e_3 = { error: e_3_1 };
415
564
  return [3 /*break*/, 8];
416
565
  case 7:
417
566
  try {
418
567
  if (owners_1_1 && !owners_1_1.done && (_c = owners_1.return)) _c.call(owners_1);
419
568
  }
420
- finally { if (e_2) throw e_2.error; }
569
+ finally { if (e_3) throw e_3.error; }
421
570
  return [7 /*endfinally*/];
422
571
  case 8: return [4 /*yield*/, connection.getMultipleAccountsInfo(atas)];
423
572
  case 9:
package/dist/utils.d.ts CHANGED
@@ -20,3 +20,8 @@ export declare const getNumberFromBN: (value: BN, decimals: number) => number;
20
20
  * @returns {T}
21
21
  */
22
22
  export declare function handleContractError<T>(func: () => Promise<T>, callback?: (err: Error) => string | null): Promise<T>;
23
+ /**
24
+ * Pause async function execution for given amount of milliseconds
25
+ * @param ms millisecond to sleep for
26
+ */
27
+ export declare function sleep(ms: number): Promise<void>;
package/dist/utils.js CHANGED
@@ -39,7 +39,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
39
39
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
40
  };
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.handleContractError = exports.getNumberFromBN = exports.getBN = void 0;
42
+ exports.sleep = exports.handleContractError = exports.getNumberFromBN = exports.getBN = void 0;
43
43
  var bn_js_1 = __importDefault(require("bn.js"));
44
44
  var types_1 = require("./types");
45
45
  /**
@@ -97,3 +97,11 @@ function handleContractError(func, callback) {
97
97
  });
98
98
  }
99
99
  exports.handleContractError = handleContractError;
100
+ /**
101
+ * Pause async function execution for given amount of milliseconds
102
+ * @param ms millisecond to sleep for
103
+ */
104
+ function sleep(ms) {
105
+ return new Promise(function (resolve) { return setTimeout(resolve, ms); });
106
+ }
107
+ exports.sleep = sleep;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamflow/common",
3
- "version": "6.0.2",
3
+ "version": "6.1.0",
4
4
  "description": "Common utilities and types used by streamflow packages.",
5
5
  "homepage": "https://github.com/streamflow-finance/js-sdk/",
6
6
  "main": "dist/index.js",
@@ -23,7 +23,7 @@
23
23
  "lint-config": "eslint --print-config",
24
24
  "prepublishOnly": "npm run lint && npm run build"
25
25
  },
26
- "gitHead": "53a78cbfcb7053bf2b9b267ad8f678cade3e6fe1",
26
+ "gitHead": "170947ffbd1de885ac95c4b809b0e889d938eaaf",
27
27
  "devDependencies": {
28
28
  "@streamflow/eslint-config": "6.0.0",
29
29
  "@types/bn.js": "5.1.1",