@streamflow/common 7.0.0-alpha.9 → 7.0.1

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 CHANGED
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./types.js"), exports);
18
- __exportStar(require("./utils.js"), exports);
18
+ __exportStar(require("./lib/assertions.js"), exports);
19
+ __exportStar(require("./lib/utils.js"), exports);
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.invariant = void 0;
4
+ const prefix = "Assertion failed";
5
+ const invariant = (condition, message) => {
6
+ if (condition) {
7
+ return;
8
+ }
9
+ const provided = typeof message === "function" ? message() : message;
10
+ const value = provided ? `${prefix}: ${provided}` : prefix;
11
+ throw new Error(value);
12
+ };
13
+ exports.invariant = invariant;
@@ -3,29 +3,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.sleep = exports.handleContractError = exports.getScaledBigNumber = exports.getNumberFromBigNumber = void 0;
7
- const bignumber_js_1 = __importDefault(require("bignumber.js"));
8
- const types_js_1 = require("./types.js");
6
+ exports.divCeilN = exports.getNumberFromBN = exports.getBN = void 0;
7
+ exports.handleContractError = handleContractError;
8
+ exports.sleep = sleep;
9
+ const bn_js_1 = __importDefault(require("bn.js"));
10
+ const types_js_1 = require("../types.js");
9
11
  /**
10
- * Used for token amounts conversion from their Big Number representation to number.
11
- * Get value in the highest units from BigNumber representation of the same value in the smallest units.
12
- * @param {BigNumber} value - Big Number representation of value in the smallest units.
12
+ * Used for conversion of token amounts to their Big Number representation.
13
+ * Get Big Number representation in the smallest units from the same value in the highest units.
14
+ * @param {number} value - Number of tokens you want to convert to its BN representation.
13
15
  * @param {number} decimals - Number of decimals the token has.
14
16
  */
15
- const getNumberFromBigNumber = (bigNum, decimals) => {
16
- return bigNum.div((0, bignumber_js_1.default)(10).pow(decimals)).toNumber();
17
+ const getBN = (value, decimals) => {
18
+ const decimalPart = value - Math.trunc(value);
19
+ const integerPart = new bn_js_1.default(Math.trunc(value));
20
+ const decimalE = new bn_js_1.default(decimalPart * 1e9);
21
+ const sum = integerPart.mul(new bn_js_1.default(1e9)).add(decimalE);
22
+ const resultE = sum.mul(new bn_js_1.default(10).pow(new bn_js_1.default(decimals)));
23
+ return resultE.div(new bn_js_1.default(1e9));
17
24
  };
18
- exports.getNumberFromBigNumber = getNumberFromBigNumber;
25
+ exports.getBN = getBN;
19
26
  /**
20
- * Used for conversion of token amounts to their Big Number representation.
21
- * Get Big Number representation in the smallest units from the same value in the highest units.
22
- * @param {number} value - Number of tokens you want to convert to its BigNumber representation.
27
+ * Used for token amounts conversion from their Big Number representation to number.
28
+ * Get value in the highest units from BN representation of the same value in the smallest units.
29
+ * @param {BN} value - Big Number representation of value in the smallest units.
23
30
  * @param {number} decimals - Number of decimals the token has.
24
31
  */
25
- const getScaledBigNumber = (value, decimals) => {
26
- return new bignumber_js_1.default(value).times(new bignumber_js_1.default(10).pow(decimals));
27
- };
28
- exports.getScaledBigNumber = getScaledBigNumber;
32
+ const getNumberFromBN = (value, decimals) => value.gt(new bn_js_1.default(2 ** 53 - 1)) ? value.div(new bn_js_1.default(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;
33
+ exports.getNumberFromBN = getNumberFromBN;
29
34
  /**
30
35
  * Used to make on chain calls to the contract and wrap raised errors if any
31
36
  * @param func function that interacts with the contract
@@ -46,7 +51,6 @@ async function handleContractError(func, callback) {
46
51
  throw err;
47
52
  }
48
53
  }
49
- exports.handleContractError = handleContractError;
50
54
  /**
51
55
  * Pause async function execution for given amount of milliseconds
52
56
  * @param ms millisecond to sleep for
@@ -54,4 +58,5 @@ exports.handleContractError = handleContractError;
54
58
  function sleep(ms) {
55
59
  return new Promise((resolve) => setTimeout(resolve, ms));
56
60
  }
57
- exports.sleep = sleep;
61
+ const divCeilN = (n, d) => n / d + (n % d ? BigInt(1) : BigInt(0));
62
+ exports.divCeilN = divCeilN;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getFilters = void 0;
4
+ const getFilters = (criteria, byteOffsets) => {
5
+ return Object.entries(criteria).reduce((acc, [key, value]) => {
6
+ const criteriaKey = key;
7
+ const effectiveByteOffset = byteOffsets[criteriaKey];
8
+ if (criteria[criteriaKey] && effectiveByteOffset) {
9
+ acc.push({
10
+ memcmp: {
11
+ offset: effectiveByteOffset,
12
+ bytes: value.toString(),
13
+ },
14
+ });
15
+ }
16
+ return acc;
17
+ }, []);
18
+ };
19
+ exports.getFilters = getFilters;
@@ -14,6 +14,8 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./account-filters.js"), exports);
17
18
  __exportStar(require("./instructions.js"), exports);
19
+ __exportStar(require("./public-key.js"), exports);
18
20
  __exportStar(require("./types.js"), exports);
19
21
  __exportStar(require("./utils.js"), exports);
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.pk = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const pk = (address) => {
6
+ return typeof address === "string" ? new web3_js_1.PublicKey(address) : address;
7
+ };
8
+ exports.pk = pk;
@@ -3,13 +3,33 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
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;
6
+ exports.buildSendThrottler = void 0;
7
+ exports.getProgramAccounts = getProgramAccounts;
8
+ exports.isSignerWallet = isSignerWallet;
9
+ exports.isSignerKeypair = isSignerKeypair;
10
+ exports.isTransactionVersioned = isTransactionVersioned;
11
+ exports.prepareTransaction = prepareTransaction;
12
+ exports.signTransaction = signTransaction;
13
+ exports.signAndExecuteTransaction = signAndExecuteTransaction;
14
+ exports.executeTransaction = executeTransaction;
15
+ exports.executeMultipleTransactions = executeMultipleTransactions;
16
+ exports.sendAndConfirmTransaction = sendAndConfirmTransaction;
17
+ exports.simulateTransaction = simulateTransaction;
18
+ exports.confirmAndEnsureTransaction = confirmAndEnsureTransaction;
19
+ exports.ata = ata;
20
+ exports.ataBatchExist = ataBatchExist;
21
+ exports.enrichAtaParams = enrichAtaParams;
22
+ exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
23
+ exports.createAtaBatch = createAtaBatch;
24
+ exports.checkOrCreateAtaBatch = checkOrCreateAtaBatch;
25
+ exports.prepareBaseInstructions = prepareBaseInstructions;
26
+ exports.getMintAndProgram = getMintAndProgram;
7
27
  const spl_token_1 = require("@solana/spl-token");
8
28
  const web3_js_1 = require("@solana/web3.js");
9
29
  const bs58_1 = __importDefault(require("bs58"));
10
30
  const p_queue_1 = __importDefault(require("p-queue"));
11
31
  const types_js_1 = require("./types.js");
12
- const utils_js_1 = require("../utils.js");
32
+ const utils_js_1 = require("../lib/utils.js");
13
33
  const SIMULATE_TRIES = 3;
14
34
  const buildSendThrottler = (sendRate) => {
15
35
  return new p_queue_1.default({ concurrency: sendRate, intervalCap: 1, interval: 1000 });
@@ -36,7 +56,6 @@ async function getProgramAccounts(connection, wallet, offset, programId) {
36
56
  });
37
57
  return [...programAccounts];
38
58
  }
39
- exports.getProgramAccounts = getProgramAccounts;
40
59
  /**
41
60
  * Utility function to check if the transaction initiator is a Wallet object
42
61
  * @param {Keypair | SignerWalletAdapter} walletOrKeypair - Wallet or Keypair in question
@@ -45,7 +64,6 @@ exports.getProgramAccounts = getProgramAccounts;
45
64
  function isSignerWallet(walletOrKeypair) {
46
65
  return walletOrKeypair.signTransaction !== undefined;
47
66
  }
48
- exports.isSignerWallet = isSignerWallet;
49
67
  /**
50
68
  * Utility function to check if the transaction initiator a Keypair object, tries to mitigate version mismatch issues
51
69
  * @param walletOrKeypair {Keypair | SignerWalletAdapter} walletOrKeypair - Wallet or Keypair in question
@@ -56,7 +74,6 @@ function isSignerKeypair(walletOrKeypair) {
56
74
  walletOrKeypair.constructor === web3_js_1.Keypair ||
57
75
  walletOrKeypair.constructor.name === web3_js_1.Keypair.prototype.constructor.name);
58
76
  }
59
- exports.isSignerKeypair = isSignerKeypair;
60
77
  /**
61
78
  * Utility function to check whether given transaction is Versioned
62
79
  * @param tx {Transaction | VersionedTransaction} - Transaction to check
@@ -65,7 +82,6 @@ exports.isSignerKeypair = isSignerKeypair;
65
82
  function isTransactionVersioned(tx) {
66
83
  return "message" in tx;
67
84
  }
68
- exports.isTransactionVersioned = isTransactionVersioned;
69
85
  /**
70
86
  * Creates a Transaction with given instructions and optionally signs it.
71
87
  * @param connection - Solana client connection
@@ -90,7 +106,6 @@ async function prepareTransaction(connection, ixs, payer, commitment, ...partial
90
106
  tx.sign(signers);
91
107
  return { tx, context, hash };
92
108
  }
93
- exports.prepareTransaction = prepareTransaction;
94
109
  async function signTransaction(invoker, tx) {
95
110
  let signedTx;
96
111
  if (isSignerWallet(invoker)) {
@@ -107,7 +122,6 @@ async function signTransaction(invoker, tx) {
107
122
  }
108
123
  return signedTx;
109
124
  }
110
- exports.signTransaction = signTransaction;
111
125
  /**
112
126
  * Signs, sends and confirms Transaction
113
127
  * @param connection - Solana client connection
@@ -121,7 +135,6 @@ async function signAndExecuteTransaction(connection, invoker, tx, confirmationPa
121
135
  const signedTx = await signTransaction(invoker, tx);
122
136
  return executeTransaction(connection, signedTx, confirmationParams, throttleParams);
123
137
  }
124
- exports.signAndExecuteTransaction = signAndExecuteTransaction;
125
138
  /**
126
139
  * Sends and confirms Transaction
127
140
  * Uses custom confirmation logic that:
@@ -144,7 +157,6 @@ async function executeTransaction(connection, tx, confirmationParams, throttlePa
144
157
  await simulateTransaction(connection, tx);
145
158
  return sendAndConfirmTransaction(connection, tx, confirmationParams, throttleParams);
146
159
  }
147
- exports.executeTransaction = executeTransaction;
148
160
  /**
149
161
  * Launches a PromisePool with all transaction being executed at the same time, allows to throttle all TXs through one Queue
150
162
  * @param connection - Solana client connection
@@ -161,7 +173,6 @@ async function executeMultipleTransactions(connection, txs, confirmationParams,
161
173
  }
162
174
  return Promise.allSettled(txs.map((tx) => executeTransaction(connection, tx, confirmationParams, { sendRate: sendRate, sendThrottler: sendThrottler })));
163
175
  }
164
- exports.executeMultipleTransactions = executeMultipleTransactions;
165
176
  /**
166
177
  * Sends and confirm transaction in a loop, constantly re-broadcsting the tx until Blockheight expires.
167
178
  * - we add additional 30 bocks to account for validators in an PRC pool divergence
@@ -232,7 +243,6 @@ async function sendAndConfirmTransaction(connection, tx, { hash, context, commit
232
243
  }
233
244
  throw new Error(`Transaction ${signature} expired.`);
234
245
  }
235
- exports.sendAndConfirmTransaction = sendAndConfirmTransaction;
236
246
  async function simulateTransaction(connection, tx) {
237
247
  let res;
238
248
  for (let i = 0; i < SIMULATE_TRIES; i++) {
@@ -245,15 +255,23 @@ async function simulateTransaction(connection, tx) {
245
255
  if (res.value.err) {
246
256
  const errMessage = JSON.stringify(res.value.err);
247
257
  if (!errMessage.includes("BlockhashNotFound") || i === SIMULATE_TRIES - 1) {
248
- throw new web3_js_1.SendTransactionError("failed to simulate transaction: " + errMessage, res.value.logs || undefined);
258
+ throw new web3_js_1.SendTransactionError({
259
+ action: "simulate",
260
+ signature: "",
261
+ transactionMessage: `failed to simulate transaction: ${typeof res.value.err === "string" ? res.value.err : errMessage}`,
262
+ logs: res.value.logs ?? undefined,
263
+ });
249
264
  }
250
265
  continue;
251
266
  }
252
267
  return res;
253
268
  }
254
- throw new web3_js_1.SendTransactionError("failed to simulate transaction");
269
+ throw new web3_js_1.SendTransactionError({
270
+ action: "simulate",
271
+ signature: "",
272
+ transactionMessage: "failed to simulate transaction",
273
+ });
255
274
  }
256
- exports.simulateTransaction = simulateTransaction;
257
275
  /**
258
276
  * Confirms and validates transaction success once
259
277
  * @param connection - Solana client connection
@@ -262,11 +280,11 @@ exports.simulateTransaction = simulateTransaction;
262
280
  * @returns Transaction Status
263
281
  */
264
282
  async function confirmAndEnsureTransaction(connection, signature, ignoreError) {
265
- const response = await connection.getSignatureStatus(signature);
266
- if (!response) {
283
+ const response = await connection.getSignatureStatuses([signature]);
284
+ if (!response || response.value.length === 0 || response.value[0] === null) {
267
285
  return null;
268
286
  }
269
- const { value } = response;
287
+ const value = response.value[0];
270
288
  if (!value) {
271
289
  return null;
272
290
  }
@@ -297,7 +315,6 @@ async function confirmAndEnsureTransaction(connection, signature, ignoreError) {
297
315
  }
298
316
  return value;
299
317
  }
300
- exports.confirmAndEnsureTransaction = confirmAndEnsureTransaction;
301
318
  /**
302
319
  * Shorthand call signature for getAssociatedTokenAddress, with allowance for address to be offCurve
303
320
  * @param {PublicKey} mint - SPL token Mint address.
@@ -308,7 +325,6 @@ exports.confirmAndEnsureTransaction = confirmAndEnsureTransaction;
308
325
  function ata(mint, owner, programId) {
309
326
  return (0, spl_token_1.getAssociatedTokenAddress)(mint, owner, true, programId);
310
327
  }
311
- exports.ata = ata;
312
328
  /**
313
329
  * Function that checks whether ATA exists for each provided owner
314
330
  * @param connection - Solana client connection
@@ -322,7 +338,6 @@ async function ataBatchExist(connection, paramsBatch) {
322
338
  const response = await connection.getMultipleAccountsInfo(tokenAccounts);
323
339
  return response.map((accInfo) => !!accInfo);
324
340
  }
325
- exports.ataBatchExist = ataBatchExist;
326
341
  async function enrichAtaParams(connection, paramsBatch) {
327
342
  const programIdByMint = {};
328
343
  return Promise.all(paramsBatch.map(async (params) => {
@@ -338,7 +353,6 @@ async function enrichAtaParams(connection, paramsBatch) {
338
353
  return params;
339
354
  }));
340
355
  }
341
- exports.enrichAtaParams = enrichAtaParams;
342
356
  /**
343
357
  * Generates a Transaction to create ATA for an array of owners
344
358
  * @param connection - Solana client connection
@@ -361,7 +375,6 @@ async function generateCreateAtaBatchTx(connection, payer, paramsBatch, commitme
361
375
  const tx = new web3_js_1.VersionedTransaction(messageV0);
362
376
  return { tx, hash, context };
363
377
  }
364
- exports.generateCreateAtaBatchTx = generateCreateAtaBatchTx;
365
378
  /**
366
379
  * Creates ATA for an array of owners
367
380
  * @param connection - Solana client connection
@@ -375,7 +388,6 @@ async function createAtaBatch(connection, invoker, paramsBatch, commitment, rate
375
388
  const { tx, hash, context } = await generateCreateAtaBatchTx(connection, invoker.publicKey, await enrichAtaParams(connection, paramsBatch), commitment);
376
389
  return signAndExecuteTransaction(connection, invoker, tx, { hash, context, commitment }, { sendRate: rate });
377
390
  }
378
- exports.createAtaBatch = createAtaBatch;
379
391
  /**
380
392
  * Utility function that checks whether associated token accounts exist and return instructions to populate them if not
381
393
  * @param connection - Solana client connection
@@ -403,7 +415,6 @@ async function checkOrCreateAtaBatch(connection, owners, mint, invoker, programI
403
415
  }
404
416
  return ixs;
405
417
  }
406
- exports.checkOrCreateAtaBatch = checkOrCreateAtaBatch;
407
418
  /**
408
419
  * Create Base instructions for Solana
409
420
  * - sets compute price if `computePrice` is provided
@@ -419,7 +430,6 @@ function prepareBaseInstructions(connection, { computePrice, computeLimit }) {
419
430
  }
420
431
  return ixs;
421
432
  }
422
- exports.prepareBaseInstructions = prepareBaseInstructions;
423
433
  /**
424
434
  * Retrieve information about a mint and its program ID, support all Token Programs.
425
435
  *
@@ -440,4 +450,3 @@ async function getMintAndProgram(connection, address, commitment) {
440
450
  tokenProgramId: programId,
441
451
  };
442
452
  }
443
- exports.getMintAndProgram = getMintAndProgram;
package/dist/cjs/types.js CHANGED
@@ -8,7 +8,7 @@ var ICluster;
8
8
  ICluster["Devnet"] = "devnet";
9
9
  ICluster["Testnet"] = "testnet";
10
10
  ICluster["Local"] = "local";
11
- })(ICluster = exports.ICluster || (exports.ICluster = {}));
11
+ })(ICluster || (exports.ICluster = ICluster = {}));
12
12
  var IChain;
13
13
  (function (IChain) {
14
14
  IChain["Solana"] = "Solana";
@@ -17,7 +17,7 @@ var IChain;
17
17
  IChain["BNB"] = "BNB";
18
18
  IChain["Polygon"] = "Polygon";
19
19
  IChain["Sui"] = "Sui";
20
- })(IChain = exports.IChain || (exports.IChain = {}));
20
+ })(IChain || (exports.IChain = IChain = {}));
21
21
  /**
22
22
  * Error wrapper for calls made to the contract on chain
23
23
  */
@@ -1,2 +1,3 @@
1
1
  export * from "./types.js";
2
- export * from "./utils.js";
2
+ export * from "./lib/assertions.js";
3
+ export * from "./lib/utils.js";
package/dist/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./types.js";
2
- export * from "./utils.js";
2
+ export * from "./lib/assertions.js";
3
+ export * from "./lib/utils.js";
@@ -0,0 +1 @@
1
+ export declare const invariant: (condition: any, message?: string | (() => string)) => asserts condition;
@@ -0,0 +1,9 @@
1
+ const prefix = "Assertion failed";
2
+ export const invariant = (condition, message) => {
3
+ if (condition) {
4
+ return;
5
+ }
6
+ const provided = typeof message === "function" ? message() : message;
7
+ const value = provided ? `${prefix}: ${provided}` : prefix;
8
+ throw new Error(value);
9
+ };
@@ -1,18 +1,18 @@
1
- import BigNumber from "bignumber.js";
1
+ import BN from "bn.js";
2
2
  /**
3
- * Used for token amounts conversion from their Big Number representation to number.
4
- * Get value in the highest units from BigNumber representation of the same value in the smallest units.
5
- * @param {BigNumber} value - Big Number representation of value in the smallest units.
3
+ * Used for conversion of token amounts to their Big Number representation.
4
+ * Get Big Number representation in the smallest units from the same value in the highest units.
5
+ * @param {number} value - Number of tokens you want to convert to its BN representation.
6
6
  * @param {number} decimals - Number of decimals the token has.
7
7
  */
8
- export declare const getNumberFromBigNumber: (bigNum: BigNumber, decimals: number) => number;
8
+ export declare const getBN: (value: number, decimals: number) => BN;
9
9
  /**
10
- * Used for conversion of token amounts to their Big Number representation.
11
- * Get Big Number representation in the smallest units from the same value in the highest units.
12
- * @param {number} value - Number of tokens you want to convert to its BigNumber representation.
10
+ * Used for token amounts conversion from their Big Number representation to number.
11
+ * Get value in the highest units from BN representation of the same value in the smallest units.
12
+ * @param {BN} value - Big Number representation of value in the smallest units.
13
13
  * @param {number} decimals - Number of decimals the token has.
14
14
  */
15
- export declare const getScaledBigNumber: (value: number | string | BigNumber, decimals: number) => BigNumber;
15
+ export declare const getNumberFromBN: (value: BN, decimals: number) => number;
16
16
  /**
17
17
  * Used to make on chain calls to the contract and wrap raised errors if any
18
18
  * @param func function that interacts with the contract
@@ -25,3 +25,4 @@ export declare function handleContractError<T>(func: () => Promise<T>, callback?
25
25
  * @param ms millisecond to sleep for
26
26
  */
27
27
  export declare function sleep(ms: number): Promise<void>;
28
+ export declare const divCeilN: (n: bigint, d: bigint) => bigint;
@@ -1,23 +1,26 @@
1
- import BigNumber from "bignumber.js";
2
- import { ContractError } from "./types.js";
1
+ import BN from "bn.js";
2
+ import { ContractError } from "../types.js";
3
3
  /**
4
- * Used for token amounts conversion from their Big Number representation to number.
5
- * Get value in the highest units from BigNumber representation of the same value in the smallest units.
6
- * @param {BigNumber} value - Big Number representation of value in the smallest units.
4
+ * Used for conversion of token amounts to their Big Number representation.
5
+ * Get Big Number representation in the smallest units from the same value in the highest units.
6
+ * @param {number} value - Number of tokens you want to convert to its BN representation.
7
7
  * @param {number} decimals - Number of decimals the token has.
8
8
  */
9
- export const getNumberFromBigNumber = (bigNum, decimals) => {
10
- return bigNum.div(BigNumber(10).pow(decimals)).toNumber();
9
+ export const getBN = (value, decimals) => {
10
+ const decimalPart = value - Math.trunc(value);
11
+ const integerPart = new BN(Math.trunc(value));
12
+ const decimalE = new BN(decimalPart * 1e9);
13
+ const sum = integerPart.mul(new BN(1e9)).add(decimalE);
14
+ const resultE = sum.mul(new BN(10).pow(new BN(decimals)));
15
+ return resultE.div(new BN(1e9));
11
16
  };
12
17
  /**
13
- * Used for conversion of token amounts to their Big Number representation.
14
- * Get Big Number representation in the smallest units from the same value in the highest units.
15
- * @param {number} value - Number of tokens you want to convert to its BigNumber representation.
18
+ * Used for token amounts conversion from their Big Number representation to number.
19
+ * Get value in the highest units from BN representation of the same value in the smallest units.
20
+ * @param {BN} value - Big Number representation of value in the smallest units.
16
21
  * @param {number} decimals - Number of decimals the token has.
17
22
  */
18
- export const getScaledBigNumber = (value, decimals) => {
19
- return new BigNumber(value).times(new BigNumber(10).pow(decimals));
20
- };
23
+ export const getNumberFromBN = (value, decimals) => value.gt(new BN(2 ** 53 - 1)) ? value.div(new BN(10 ** decimals)).toNumber() : value.toNumber() / 10 ** decimals;
21
24
  /**
22
25
  * Used to make on chain calls to the contract and wrap raised errors if any
23
26
  * @param func function that interacts with the contract
@@ -45,3 +48,4 @@ export async function handleContractError(func, callback) {
45
48
  export function sleep(ms) {
46
49
  return new Promise((resolve) => setTimeout(resolve, ms));
47
50
  }
51
+ export const divCeilN = (n, d) => n / d + (n % d ? BigInt(1) : BigInt(0));
@@ -0,0 +1,2 @@
1
+ import { MemcmpFilter, PublicKey } from "@solana/web3.js";
2
+ export declare const getFilters: <T extends Record<string, number | PublicKey>>(criteria: T, byteOffsets: Record<keyof T, number>) => MemcmpFilter[];
@@ -0,0 +1,15 @@
1
+ export const getFilters = (criteria, byteOffsets) => {
2
+ return Object.entries(criteria).reduce((acc, [key, value]) => {
3
+ const criteriaKey = key;
4
+ const effectiveByteOffset = byteOffsets[criteriaKey];
5
+ if (criteria[criteriaKey] && effectiveByteOffset) {
6
+ acc.push({
7
+ memcmp: {
8
+ offset: effectiveByteOffset,
9
+ bytes: value.toString(),
10
+ },
11
+ });
12
+ }
13
+ return acc;
14
+ }, []);
15
+ };
@@ -1,3 +1,5 @@
1
+ export * from "./account-filters.js";
1
2
  export * from "./instructions.js";
3
+ export * from "./public-key.js";
2
4
  export * from "./types.js";
3
5
  export * from "./utils.js";
@@ -1,3 +1,5 @@
1
+ export * from "./account-filters.js";
1
2
  export * from "./instructions.js";
3
+ export * from "./public-key.js";
2
4
  export * from "./types.js";
3
5
  export * from "./utils.js";
@@ -1,3 +1,3 @@
1
1
  import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js";
2
- import BigNumber from "bignumber.js";
3
- export declare const prepareWrappedAccount: (connection: Connection, senderAddress: PublicKey, amount: BigNumber) => Promise<TransactionInstruction[]>;
2
+ import BN from "bn.js";
3
+ export declare const prepareWrappedAccount: (connection: Connection, senderAddress: PublicKey, amount: BN) => Promise<TransactionInstruction[]>;
@@ -0,0 +1,2 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export declare const pk: (address: string | PublicKey) => PublicKey;
@@ -0,0 +1,4 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ export const pk = (address) => {
3
+ return typeof address === "string" ? new PublicKey(address) : address;
4
+ };
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import { AccountInfo, BlockhashWithExpiryBlockHeight, Commitment, Context, PublicKey } from "@solana/web3.js";
3
2
  import PQueue from "p-queue";
4
3
  export interface ITransactionSolanaExt {
@@ -3,7 +3,7 @@ import { ComputeBudgetProgram, Keypair, TransactionMessage, VersionedTransaction
3
3
  import bs58 from "bs58";
4
4
  import PQueue from "p-queue";
5
5
  import { TransactionFailedError, } from "./types.js";
6
- import { sleep } from "../utils.js";
6
+ import { sleep } from "../lib/utils.js";
7
7
  const SIMULATE_TRIES = 3;
8
8
  export const buildSendThrottler = (sendRate) => {
9
9
  return new PQueue({ concurrency: sendRate, intervalCap: 1, interval: 1000 });
@@ -228,13 +228,22 @@ export async function simulateTransaction(connection, tx) {
228
228
  if (res.value.err) {
229
229
  const errMessage = JSON.stringify(res.value.err);
230
230
  if (!errMessage.includes("BlockhashNotFound") || i === SIMULATE_TRIES - 1) {
231
- throw new SendTransactionError("failed to simulate transaction: " + errMessage, res.value.logs || undefined);
231
+ throw new SendTransactionError({
232
+ action: "simulate",
233
+ signature: "",
234
+ transactionMessage: `failed to simulate transaction: ${typeof res.value.err === "string" ? res.value.err : errMessage}`,
235
+ logs: res.value.logs ?? undefined,
236
+ });
232
237
  }
233
238
  continue;
234
239
  }
235
240
  return res;
236
241
  }
237
- throw new SendTransactionError("failed to simulate transaction");
242
+ throw new SendTransactionError({
243
+ action: "simulate",
244
+ signature: "",
245
+ transactionMessage: "failed to simulate transaction",
246
+ });
238
247
  }
239
248
  /**
240
249
  * Confirms and validates transaction success once
@@ -244,11 +253,11 @@ export async function simulateTransaction(connection, tx) {
244
253
  * @returns Transaction Status
245
254
  */
246
255
  export async function confirmAndEnsureTransaction(connection, signature, ignoreError) {
247
- const response = await connection.getSignatureStatus(signature);
248
- if (!response) {
256
+ const response = await connection.getSignatureStatuses([signature]);
257
+ if (!response || response.value.length === 0 || response.value[0] === null) {
249
258
  return null;
250
259
  }
251
- const { value } = response;
260
+ const value = response.value[0];
252
261
  if (!value) {
253
262
  return null;
254
263
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@streamflow/common",
3
- "version": "7.0.0-alpha.9",
3
+ "version": "7.0.1",
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/esm/index.js",
@@ -27,20 +27,21 @@
27
27
  "lint-config": "eslint --print-config",
28
28
  "prepublishOnly": "npm run lint && npm run build"
29
29
  },
30
- "gitHead": "5b3f47ca167e818df9b0d9c0f0818d9a67436c55",
30
+ "gitHead": "1d52555f15b31f9267ab841e3238a596ca6b7e6f",
31
31
  "devDependencies": {
32
- "@streamflow/eslint-config": "7.0.0-alpha.9",
32
+ "@streamflow/eslint-config": "7.0.1",
33
+ "@types/bn.js": "5.1.1",
33
34
  "date-fns": "2.28.0",
34
- "typescript": "^4.9.5"
35
+ "typescript": "^5.6.3"
35
36
  },
36
37
  "dependencies": {
37
38
  "@coral-xyz/borsh": "0.30.1",
38
- "@solana/buffer-layout": "4.0.1 ",
39
- "@solana/spl-token": "0.3.6",
39
+ "@solana/buffer-layout": "4.0.1",
40
+ "@solana/spl-token": "0.4.9",
40
41
  "@solana/wallet-adapter-base": "0.9.19",
41
- "@solana/web3.js": "1.90.2",
42
+ "@solana/web3.js": "1.95.4",
42
43
  "aptos": "1.21.0",
43
- "bignumber.js": "^9.1.2",
44
+ "bn.js": "5.2.1",
44
45
  "borsh": "^2.0.0",
45
46
  "bs58": "5.0.0",
46
47
  "p-queue": "^8.0.1"