@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.
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/lib/assertions.js +13 -0
- package/dist/cjs/lib/utils.js +62 -0
- package/dist/cjs/solana/account-filters.js +19 -0
- package/dist/cjs/solana/index.js +2 -0
- package/dist/cjs/solana/instructions.js +18 -88
- package/dist/cjs/solana/public-key.js +8 -0
- package/dist/cjs/solana/types.js +6 -24
- package/dist/cjs/solana/utils.js +233 -513
- package/dist/cjs/types.js +9 -27
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/lib/assertions.d.ts +1 -0
- package/dist/esm/lib/assertions.js +9 -0
- package/dist/esm/{utils.d.ts → lib/utils.d.ts} +10 -9
- package/dist/esm/{utils.js → lib/utils.js} +17 -13
- package/dist/esm/solana/account-filters.d.ts +2 -0
- package/dist/esm/solana/account-filters.js +15 -0
- package/dist/esm/solana/index.d.ts +2 -0
- package/dist/esm/solana/index.js +2 -0
- package/dist/esm/solana/instructions.d.ts +2 -2
- package/dist/esm/solana/public-key.d.ts +2 -0
- package/dist/esm/solana/public-key.js +4 -0
- package/dist/esm/solana/types.d.ts +4 -0
- package/dist/esm/solana/utils.js +6 -5
- package/package.json +8 -7
- package/dist/cjs/utils.js +0 -102
package/dist/cjs/solana/utils.js
CHANGED
|
@@ -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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
130
|
-
|
|
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
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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
|
-
|
|
195
|
-
|
|
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
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
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,
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
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,
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
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
|
-
|
|
418
|
-
|
|
419
|
-
return
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
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
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
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
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
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
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
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
|
-
|
|
595
|
-
|
|
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
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
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,
|
|
683
|
-
|
|
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
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
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;
|