@streamflow/common 6.0.1 → 6.0.2

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.
@@ -18,4 +18,5 @@ export interface CheckAssociatedTokenAccountsData {
18
18
  export interface AtaParams {
19
19
  mint: PublicKey;
20
20
  owner: PublicKey;
21
+ programId?: PublicKey;
21
22
  }
@@ -1,3 +1,4 @@
1
+ import { Mint } from "@solana/spl-token";
1
2
  import { SignerWalletAdapter } from "@solana/wallet-adapter-base";
2
3
  import { BlockhashWithExpiryBlockHeight, Commitment, Connection, Keypair, PublicKey, Transaction, TransactionInstruction } from "@solana/web3.js";
3
4
  import { Account, AtaParams, ITransactionSolanaExt } from "./types";
@@ -53,16 +54,18 @@ export declare function signAndExecuteTransaction(connection: Connection, invoke
53
54
  * Shorthand call signature for getAssociatedTokenAddress, with allowance for address to be offCurve
54
55
  * @param {PublicKey} mint - SPL token Mint address.
55
56
  * @param {PublicKey} owner - Owner of the Associated Token Address
57
+ * @param {PublicKey} programId - Program ID of the mint
56
58
  * @return {Promise<PublicKey>} - Associated Token Address
57
59
  */
58
- export declare function ata(mint: PublicKey, owner: PublicKey): Promise<PublicKey>;
60
+ export declare function ata(mint: PublicKey, owner: PublicKey, programId?: PublicKey): Promise<PublicKey>;
59
61
  /**
60
62
  * Function that checks whether ATA exists for each provided owner
61
63
  * @param connection - Solana client connection
62
- * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
63
- * @returns Array of boolean where each members corresponds to owners member
64
+ * @param paramsBatch - Array of Params for each ATA account: {mint, owner}
65
+ * @returns Array of boolean where each member corresponds to an owner
64
66
  */
65
67
  export declare function ataBatchExist(connection: Connection, paramsBatch: AtaParams[]): Promise<boolean[]>;
68
+ export declare function enrichAtaParams(connection: Connection, paramsBatch: AtaParams[]): Promise<AtaParams[]>;
66
69
  /**
67
70
  * Generates a Transaction to create ATA for an array of owners
68
71
  * @param connection - Solana client connection
@@ -88,12 +91,26 @@ export declare function createAtaBatch(connection: Connection, invoker: Keypair
88
91
  * @param owners - Array of ATA owners
89
92
  * @param mint - Mint for which ATA will be checked
90
93
  * @param invoker - Transaction invoker and payer
94
+ * @param programId - Program ID of the Mint
91
95
  * @returns Array of Transaction Instructions that should be added to a transaction
92
96
  */
93
- export declare function checkOrCreateAtaBatch(connection: Connection, owners: PublicKey[], mint: PublicKey, invoker: SignerWalletAdapter | Keypair): Promise<TransactionInstruction[]>;
97
+ export declare function checkOrCreateAtaBatch(connection: Connection, owners: PublicKey[], mint: PublicKey, invoker: SignerWalletAdapter | Keypair, programId?: PublicKey): Promise<TransactionInstruction[]>;
94
98
  /**
95
99
  * Create Base instructions for Solana
96
100
  * - sets compute price if `computePrice` is provided
97
101
  * - sets compute limit if `computeLimit` is provided
98
102
  */
99
103
  export declare function prepareBaseInstructions(connection: Connection, { computePrice, computeLimit }: ITransactionSolanaExt): TransactionInstruction[];
104
+ /**
105
+ * Retrieve information about a mint and its program ID, support all Token Programs.
106
+ *
107
+ * @param connection Connection to use
108
+ * @param address Mint account
109
+ * @param commitment Desired level of commitment for querying the state
110
+ *
111
+ * @return Mint information
112
+ */
113
+ export declare function getMintAndProgram(connection: Connection, address: PublicKey, commitment?: Commitment): Promise<{
114
+ mint: Mint;
115
+ programId: PublicKey;
116
+ }>;
@@ -75,7 +75,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
75
75
  return (mod && mod.__esModule) ? mod : { "default": mod };
76
76
  };
77
77
  Object.defineProperty(exports, "__esModule", { value: true });
78
- exports.prepareBaseInstructions = exports.checkOrCreateAtaBatch = exports.createAtaBatch = exports.generateCreateAtaBatchTx = exports.ataBatchExist = exports.ata = exports.signAndExecuteTransaction = exports.signTransaction = exports.prepareTransaction = exports.isSignerKeypair = exports.isSignerWallet = exports.getProgramAccounts = void 0;
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;
79
79
  var spl_token_1 = require("@solana/spl-token");
80
80
  var web3_js_1 = require("@solana/web3.js");
81
81
  var bs58_1 = __importDefault(require("bs58"));
@@ -231,17 +231,18 @@ exports.signAndExecuteTransaction = signAndExecuteTransaction;
231
231
  * Shorthand call signature for getAssociatedTokenAddress, with allowance for address to be offCurve
232
232
  * @param {PublicKey} mint - SPL token Mint address.
233
233
  * @param {PublicKey} owner - Owner of the Associated Token Address
234
+ * @param {PublicKey} programId - Program ID of the mint
234
235
  * @return {Promise<PublicKey>} - Associated Token Address
235
236
  */
236
- function ata(mint, owner) {
237
- return (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, true);
237
+ function ata(mint, owner, programId) {
238
+ return (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, true, programId);
238
239
  }
239
240
  exports.ata = ata;
240
241
  /**
241
242
  * Function that checks whether ATA exists for each provided owner
242
243
  * @param connection - Solana client connection
243
- * @param paramsBatch - Array of Params for an each ATA account: {mint, owner}
244
- * @returns Array of boolean where each members corresponds to owners member
244
+ * @param paramsBatch - Array of Params for each ATA account: {mint, owner}
245
+ * @returns Array of boolean where each member corresponds to an owner
245
246
  */
246
247
  function ataBatchExist(connection, paramsBatch) {
247
248
  return __awaiter(this, void 0, void 0, function () {
@@ -250,16 +251,10 @@ function ataBatchExist(connection, paramsBatch) {
250
251
  return __generator(this, function (_a) {
251
252
  switch (_a.label) {
252
253
  case 0: return [4 /*yield*/, Promise.all(paramsBatch.map(function (_a) {
253
- var mint = _a.mint, owner = _a.owner;
254
+ var mint = _a.mint, owner = _a.owner, programId = _a.programId;
254
255
  return __awaiter(_this, void 0, void 0, function () {
255
- var pubkey;
256
256
  return __generator(this, function (_b) {
257
- switch (_b.label) {
258
- case 0: return [4 /*yield*/, ata(mint, owner)];
259
- case 1:
260
- pubkey = _b.sent();
261
- return [2 /*return*/, pubkey];
262
- }
257
+ return [2 /*return*/, ata(mint, owner, programId)];
263
258
  });
264
259
  });
265
260
  }))];
@@ -274,6 +269,37 @@ function ataBatchExist(connection, paramsBatch) {
274
269
  });
275
270
  }
276
271
  exports.ataBatchExist = ataBatchExist;
272
+ function enrichAtaParams(connection, paramsBatch) {
273
+ return __awaiter(this, void 0, void 0, function () {
274
+ var programIdByMint;
275
+ var _this = this;
276
+ return __generator(this, function (_a) {
277
+ programIdByMint = {};
278
+ return [2 /*return*/, Promise.all(paramsBatch.map(function (params) { return __awaiter(_this, void 0, void 0, function () {
279
+ var mintStr, programId;
280
+ return __generator(this, function (_a) {
281
+ switch (_a.label) {
282
+ case 0:
283
+ if (params.programId) {
284
+ return [2 /*return*/, params];
285
+ }
286
+ mintStr = params.mint.toString();
287
+ if (!!(mintStr in programIdByMint)) return [3 /*break*/, 2];
288
+ return [4 /*yield*/, getMintAndProgram(connection, params.mint)];
289
+ case 1:
290
+ programId = (_a.sent()).programId;
291
+ programIdByMint[mintStr] = programId;
292
+ _a.label = 2;
293
+ case 2:
294
+ params.programId = programIdByMint[mintStr];
295
+ return [2 /*return*/, params];
296
+ }
297
+ });
298
+ }); }))];
299
+ });
300
+ });
301
+ }
302
+ exports.enrichAtaParams = enrichAtaParams;
277
303
  /**
278
304
  * Generates a Transaction to create ATA for an array of owners
279
305
  * @param connection - Solana client connection
@@ -288,25 +314,28 @@ function generateCreateAtaBatchTx(connection, payer, paramsBatch) {
288
314
  var _this = this;
289
315
  return __generator(this, function (_b) {
290
316
  switch (_b.label) {
291
- case 0: return [4 /*yield*/, Promise.all(paramsBatch.map(function (_a) {
292
- var mint = _a.mint, owner = _a.owner;
293
- return __awaiter(_this, void 0, void 0, function () {
294
- var _b, _c;
295
- return __generator(this, function (_d) {
296
- switch (_d.label) {
297
- case 0:
298
- _b = spl_token_1.createAssociatedTokenAccountInstruction;
299
- _c = [payer];
300
- return [4 /*yield*/, ata(mint, owner)];
301
- case 1: return [2 /*return*/, _b.apply(void 0, _c.concat([_d.sent(), owner, mint]))];
302
- }
303
- });
304
- });
305
- }))];
317
+ case 0: return [4 /*yield*/, enrichAtaParams(connection, paramsBatch)];
306
318
  case 1:
319
+ paramsBatch = _b.sent();
320
+ return [4 /*yield*/, Promise.all(paramsBatch.map(function (_a) {
321
+ var mint = _a.mint, owner = _a.owner, programId = _a.programId;
322
+ return __awaiter(_this, void 0, void 0, function () {
323
+ var _b, _c;
324
+ return __generator(this, function (_d) {
325
+ switch (_d.label) {
326
+ case 0:
327
+ _b = spl_token_1.createAssociatedTokenAccountInstruction;
328
+ _c = [payer];
329
+ return [4 /*yield*/, ata(mint, owner)];
330
+ case 1: return [2 /*return*/, _b.apply(void 0, _c.concat([_d.sent(), owner, mint, programId]))];
331
+ }
332
+ });
333
+ });
334
+ }))];
335
+ case 2:
307
336
  ixs = _b.sent();
308
337
  return [4 /*yield*/, connection.getLatestBlockhash()];
309
- case 2:
338
+ case 3:
310
339
  hash = _b.sent();
311
340
  tx = (_a = new web3_js_1.Transaction({
312
341
  feePayer: payer,
@@ -328,12 +357,17 @@ exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
328
357
  */
329
358
  function createAtaBatch(connection, invoker, paramsBatch) {
330
359
  return __awaiter(this, void 0, void 0, function () {
331
- var _a, tx, hash;
332
- return __generator(this, function (_b) {
333
- switch (_b.label) {
334
- case 0: return [4 /*yield*/, generateCreateAtaBatchTx(connection, invoker.publicKey, paramsBatch)];
335
- case 1:
336
- _a = _b.sent(), tx = _a.tx, hash = _a.hash;
360
+ var _a, tx, hash, _b, _c;
361
+ return __generator(this, function (_d) {
362
+ switch (_d.label) {
363
+ case 0:
364
+ _b = generateCreateAtaBatchTx;
365
+ _c = [connection,
366
+ invoker.publicKey];
367
+ return [4 /*yield*/, enrichAtaParams(connection, paramsBatch)];
368
+ case 1: return [4 /*yield*/, _b.apply(void 0, _c.concat([_d.sent()]))];
369
+ case 2:
370
+ _a = _d.sent(), tx = _a.tx, hash = _a.hash;
337
371
  return [2 /*return*/, signAndExecuteTransaction(connection, invoker, tx, hash)];
338
372
  }
339
373
  });
@@ -346,9 +380,10 @@ exports.createAtaBatch = createAtaBatch;
346
380
  * @param owners - Array of ATA owners
347
381
  * @param mint - Mint for which ATA will be checked
348
382
  * @param invoker - Transaction invoker and payer
383
+ * @param programId - Program ID of the Mint
349
384
  * @returns Array of Transaction Instructions that should be added to a transaction
350
385
  */
351
- function checkOrCreateAtaBatch(connection, owners, mint, invoker) {
386
+ function checkOrCreateAtaBatch(connection, owners, mint, invoker, programId) {
352
387
  return __awaiter(this, void 0, void 0, function () {
353
388
  var ixs, atas, owners_1, owners_1_1, owner, _a, _b, e_2_1, response, i;
354
389
  var e_2, _c;
@@ -366,7 +401,7 @@ function checkOrCreateAtaBatch(connection, owners, mint, invoker) {
366
401
  if (!!owners_1_1.done) return [3 /*break*/, 5];
367
402
  owner = owners_1_1.value;
368
403
  _b = (_a = atas).push;
369
- return [4 /*yield*/, ata(mint, owner)];
404
+ return [4 /*yield*/, ata(mint, owner, programId)];
370
405
  case 3:
371
406
  _b.apply(_a, [_d.sent()]);
372
407
  _d.label = 4;
@@ -389,7 +424,7 @@ function checkOrCreateAtaBatch(connection, owners, mint, invoker) {
389
424
  response = _d.sent();
390
425
  for (i = 0; i < response.length; i++) {
391
426
  if (!response[i]) {
392
- ixs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(invoker.publicKey, atas[i], owners[i], mint));
427
+ ixs.push((0, spl_token_1.createAssociatedTokenAccountInstruction)(invoker.publicKey, atas[i], owners[i], mint, programId));
393
428
  }
394
429
  }
395
430
  return [2 /*return*/, ixs];
@@ -415,3 +450,33 @@ function prepareBaseInstructions(connection, _a) {
415
450
  return ixs;
416
451
  }
417
452
  exports.prepareBaseInstructions = prepareBaseInstructions;
453
+ /**
454
+ * Retrieve information about a mint and its program ID, support all Token Programs.
455
+ *
456
+ * @param connection Connection to use
457
+ * @param address Mint account
458
+ * @param commitment Desired level of commitment for querying the state
459
+ *
460
+ * @return Mint information
461
+ */
462
+ function getMintAndProgram(connection, address, commitment) {
463
+ return __awaiter(this, void 0, void 0, function () {
464
+ var accountInfo, programId;
465
+ return __generator(this, function (_a) {
466
+ switch (_a.label) {
467
+ case 0: return [4 /*yield*/, connection.getAccountInfo(address, commitment)];
468
+ case 1:
469
+ accountInfo = _a.sent();
470
+ programId = accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.owner;
471
+ 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))) {
472
+ programId = spl_token_1.TOKEN_PROGRAM_ID;
473
+ }
474
+ return [2 /*return*/, {
475
+ mint: (0, spl_token_1.unpackMint)(address, accountInfo, programId),
476
+ programId: programId,
477
+ }];
478
+ }
479
+ });
480
+ });
481
+ }
482
+ exports.getMintAndProgram = getMintAndProgram;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamflow/common",
3
- "version": "6.0.1",
3
+ "version": "6.0.2",
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": "07782122a48131b4589edd9c536bb7f29dcbe0c9",
26
+ "gitHead": "53a78cbfcb7053bf2b9b267ad8f678cade3e6fe1",
27
27
  "devDependencies": {
28
28
  "@streamflow/eslint-config": "6.0.0",
29
29
  "@types/bn.js": "5.1.1",