@atomiqlabs/chain-solana 12.0.11 → 12.0.13

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.
Files changed (114) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +29 -29
  3. package/dist/index.js +45 -45
  4. package/dist/solana/SolanaChainType.d.ts +11 -11
  5. package/dist/solana/SolanaChainType.js +2 -2
  6. package/dist/solana/SolanaChains.d.ts +20 -20
  7. package/dist/solana/SolanaChains.js +25 -25
  8. package/dist/solana/SolanaInitializer.d.ts +18 -18
  9. package/dist/solana/SolanaInitializer.js +63 -63
  10. package/dist/solana/btcrelay/SolanaBtcRelay.d.ts +228 -228
  11. package/dist/solana/btcrelay/SolanaBtcRelay.js +441 -441
  12. package/dist/solana/btcrelay/headers/SolanaBtcHeader.d.ts +29 -29
  13. package/dist/solana/btcrelay/headers/SolanaBtcHeader.js +34 -34
  14. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.d.ts +46 -46
  15. package/dist/solana/btcrelay/headers/SolanaBtcStoredHeader.js +78 -78
  16. package/dist/solana/btcrelay/program/programIdl.json +671 -671
  17. package/dist/solana/chain/SolanaAction.d.ts +26 -26
  18. package/dist/solana/chain/SolanaAction.js +86 -86
  19. package/dist/solana/chain/SolanaChainInterface.d.ts +65 -65
  20. package/dist/solana/chain/SolanaChainInterface.js +125 -125
  21. package/dist/solana/chain/SolanaModule.d.ts +14 -14
  22. package/dist/solana/chain/SolanaModule.js +13 -13
  23. package/dist/solana/chain/modules/SolanaAddresses.d.ts +8 -8
  24. package/dist/solana/chain/modules/SolanaAddresses.js +22 -22
  25. package/dist/solana/chain/modules/SolanaBlocks.d.ts +28 -28
  26. package/dist/solana/chain/modules/SolanaBlocks.js +72 -72
  27. package/dist/solana/chain/modules/SolanaEvents.d.ts +68 -68
  28. package/dist/solana/chain/modules/SolanaEvents.js +225 -225
  29. package/dist/solana/chain/modules/SolanaFees.d.ts +121 -121
  30. package/dist/solana/chain/modules/SolanaFees.js +379 -379
  31. package/dist/solana/chain/modules/SolanaSignatures.d.ts +23 -23
  32. package/dist/solana/chain/modules/SolanaSignatures.js +39 -39
  33. package/dist/solana/chain/modules/SolanaSlots.d.ts +31 -31
  34. package/dist/solana/chain/modules/SolanaSlots.js +68 -68
  35. package/dist/solana/chain/modules/SolanaTokens.d.ts +136 -136
  36. package/dist/solana/chain/modules/SolanaTokens.js +248 -248
  37. package/dist/solana/chain/modules/SolanaTransactions.d.ts +124 -124
  38. package/dist/solana/chain/modules/SolanaTransactions.js +323 -323
  39. package/dist/solana/events/SolanaChainEvents.d.ts +88 -88
  40. package/dist/solana/events/SolanaChainEvents.js +256 -256
  41. package/dist/solana/events/SolanaChainEventsBrowser.d.ts +75 -75
  42. package/dist/solana/events/SolanaChainEventsBrowser.js +172 -172
  43. package/dist/solana/program/SolanaProgramBase.d.ts +40 -40
  44. package/dist/solana/program/SolanaProgramBase.js +43 -43
  45. package/dist/solana/program/SolanaProgramModule.d.ts +8 -8
  46. package/dist/solana/program/SolanaProgramModule.js +11 -11
  47. package/dist/solana/program/modules/SolanaProgramEvents.d.ts +53 -53
  48. package/dist/solana/program/modules/SolanaProgramEvents.js +114 -114
  49. package/dist/solana/swaps/SolanaSwapData.d.ts +71 -71
  50. package/dist/solana/swaps/SolanaSwapData.js +292 -292
  51. package/dist/solana/swaps/SolanaSwapModule.d.ts +10 -10
  52. package/dist/solana/swaps/SolanaSwapModule.js +11 -11
  53. package/dist/solana/swaps/SolanaSwapProgram.d.ts +224 -224
  54. package/dist/solana/swaps/SolanaSwapProgram.js +570 -567
  55. package/dist/solana/swaps/SwapTypeEnum.d.ts +11 -11
  56. package/dist/solana/swaps/SwapTypeEnum.js +42 -42
  57. package/dist/solana/swaps/modules/SolanaDataAccount.d.ts +94 -94
  58. package/dist/solana/swaps/modules/SolanaDataAccount.js +231 -231
  59. package/dist/solana/swaps/modules/SolanaLpVault.d.ts +71 -71
  60. package/dist/solana/swaps/modules/SolanaLpVault.js +173 -173
  61. package/dist/solana/swaps/modules/SwapClaim.d.ts +129 -129
  62. package/dist/solana/swaps/modules/SwapClaim.js +291 -291
  63. package/dist/solana/swaps/modules/SwapInit.d.ts +217 -217
  64. package/dist/solana/swaps/modules/SwapInit.js +519 -519
  65. package/dist/solana/swaps/modules/SwapRefund.d.ts +82 -82
  66. package/dist/solana/swaps/modules/SwapRefund.js +262 -262
  67. package/dist/solana/swaps/programIdl.json +945 -945
  68. package/dist/solana/swaps/programTypes.d.ts +943 -943
  69. package/dist/solana/swaps/programTypes.js +945 -945
  70. package/dist/solana/wallet/SolanaKeypairWallet.d.ts +9 -9
  71. package/dist/solana/wallet/SolanaKeypairWallet.js +33 -33
  72. package/dist/solana/wallet/SolanaSigner.d.ts +11 -11
  73. package/dist/solana/wallet/SolanaSigner.js +17 -17
  74. package/dist/utils/Utils.d.ts +53 -53
  75. package/dist/utils/Utils.js +170 -170
  76. package/package.json +41 -41
  77. package/src/index.ts +36 -36
  78. package/src/solana/SolanaChainType.ts +27 -27
  79. package/src/solana/SolanaChains.ts +23 -23
  80. package/src/solana/SolanaInitializer.ts +102 -102
  81. package/src/solana/btcrelay/SolanaBtcRelay.ts +589 -589
  82. package/src/solana/btcrelay/headers/SolanaBtcHeader.ts +57 -57
  83. package/src/solana/btcrelay/headers/SolanaBtcStoredHeader.ts +102 -102
  84. package/src/solana/btcrelay/program/programIdl.json +670 -670
  85. package/src/solana/chain/SolanaAction.ts +108 -108
  86. package/src/solana/chain/SolanaChainInterface.ts +192 -192
  87. package/src/solana/chain/SolanaModule.ts +20 -20
  88. package/src/solana/chain/modules/SolanaAddresses.ts +20 -20
  89. package/src/solana/chain/modules/SolanaBlocks.ts +78 -78
  90. package/src/solana/chain/modules/SolanaEvents.ts +256 -256
  91. package/src/solana/chain/modules/SolanaFees.ts +450 -450
  92. package/src/solana/chain/modules/SolanaSignatures.ts +39 -39
  93. package/src/solana/chain/modules/SolanaSlots.ts +82 -82
  94. package/src/solana/chain/modules/SolanaTokens.ts +307 -307
  95. package/src/solana/chain/modules/SolanaTransactions.ts +365 -365
  96. package/src/solana/events/SolanaChainEvents.ts +299 -299
  97. package/src/solana/events/SolanaChainEventsBrowser.ts +209 -209
  98. package/src/solana/program/SolanaProgramBase.ts +79 -79
  99. package/src/solana/program/SolanaProgramModule.ts +15 -15
  100. package/src/solana/program/modules/SolanaProgramEvents.ts +155 -155
  101. package/src/solana/swaps/SolanaSwapData.ts +430 -430
  102. package/src/solana/swaps/SolanaSwapModule.ts +16 -16
  103. package/src/solana/swaps/SolanaSwapProgram.ts +854 -849
  104. package/src/solana/swaps/SwapTypeEnum.ts +29 -29
  105. package/src/solana/swaps/modules/SolanaDataAccount.ts +307 -307
  106. package/src/solana/swaps/modules/SolanaLpVault.ts +215 -215
  107. package/src/solana/swaps/modules/SwapClaim.ts +389 -389
  108. package/src/solana/swaps/modules/SwapInit.ts +663 -663
  109. package/src/solana/swaps/modules/SwapRefund.ts +323 -323
  110. package/src/solana/swaps/programIdl.json +944 -944
  111. package/src/solana/swaps/programTypes.ts +1885 -1885
  112. package/src/solana/wallet/SolanaKeypairWallet.ts +36 -36
  113. package/src/solana/wallet/SolanaSigner.ts +24 -24
  114. package/src/utils/Utils.ts +180 -180
@@ -1,9 +1,9 @@
1
- import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
2
- import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
3
- export declare class SolanaKeypairWallet implements Wallet {
4
- readonly payer: Keypair;
5
- constructor(payer: Keypair);
6
- get publicKey(): PublicKey;
7
- signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]>;
8
- signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
9
- }
1
+ import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
2
+ import { Keypair, PublicKey, Transaction, VersionedTransaction } from "@solana/web3.js";
3
+ export declare class SolanaKeypairWallet implements Wallet {
4
+ readonly payer: Keypair;
5
+ constructor(payer: Keypair);
6
+ get publicKey(): PublicKey;
7
+ signAllTransactions<T extends Transaction | VersionedTransaction>(txs: T[]): Promise<T[]>;
8
+ signTransaction<T extends Transaction | VersionedTransaction>(tx: T): Promise<T>;
9
+ }
@@ -1,33 +1,33 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaKeypairWallet = void 0;
4
- const web3_js_1 = require("@solana/web3.js");
5
- class SolanaKeypairWallet {
6
- constructor(payer) {
7
- this.payer = payer;
8
- }
9
- get publicKey() {
10
- return this.payer.publicKey;
11
- }
12
- signAllTransactions(txs) {
13
- txs.forEach((tx) => {
14
- if (tx instanceof web3_js_1.Transaction) {
15
- tx.partialSign(this.payer);
16
- }
17
- else if (tx instanceof web3_js_1.VersionedTransaction) {
18
- tx.sign([this.payer]);
19
- }
20
- });
21
- return Promise.resolve(txs);
22
- }
23
- signTransaction(tx) {
24
- if (tx instanceof web3_js_1.Transaction) {
25
- tx.partialSign(this.payer);
26
- }
27
- else if (tx instanceof web3_js_1.VersionedTransaction) {
28
- tx.sign([this.payer]);
29
- }
30
- return Promise.resolve(tx);
31
- }
32
- }
33
- exports.SolanaKeypairWallet = SolanaKeypairWallet;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaKeypairWallet = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ class SolanaKeypairWallet {
6
+ constructor(payer) {
7
+ this.payer = payer;
8
+ }
9
+ get publicKey() {
10
+ return this.payer.publicKey;
11
+ }
12
+ signAllTransactions(txs) {
13
+ txs.forEach((tx) => {
14
+ if (tx instanceof web3_js_1.Transaction) {
15
+ tx.partialSign(this.payer);
16
+ }
17
+ else if (tx instanceof web3_js_1.VersionedTransaction) {
18
+ tx.sign([this.payer]);
19
+ }
20
+ });
21
+ return Promise.resolve(txs);
22
+ }
23
+ signTransaction(tx) {
24
+ if (tx instanceof web3_js_1.Transaction) {
25
+ tx.partialSign(this.payer);
26
+ }
27
+ else if (tx instanceof web3_js_1.VersionedTransaction) {
28
+ tx.sign([this.payer]);
29
+ }
30
+ return Promise.resolve(tx);
31
+ }
32
+ }
33
+ exports.SolanaKeypairWallet = SolanaKeypairWallet;
@@ -1,11 +1,11 @@
1
- import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
2
- import { AbstractSigner } from "@atomiqlabs/base";
3
- import { PublicKey, Signer } from "@solana/web3.js";
4
- export declare class SolanaSigner implements AbstractSigner {
5
- type: "AtomiqAbstractSigner";
6
- wallet: Wallet;
7
- keypair?: Signer;
8
- constructor(wallet: Wallet, keypair?: Signer);
9
- getPublicKey(): PublicKey;
10
- getAddress(): string;
11
- }
1
+ import { Wallet } from "@coral-xyz/anchor/dist/cjs/provider";
2
+ import { AbstractSigner } from "@atomiqlabs/base";
3
+ import { PublicKey, Signer } from "@solana/web3.js";
4
+ export declare class SolanaSigner implements AbstractSigner {
5
+ type: "AtomiqAbstractSigner";
6
+ wallet: Wallet;
7
+ keypair?: Signer;
8
+ constructor(wallet: Wallet, keypair?: Signer);
9
+ getPublicKey(): PublicKey;
10
+ getAddress(): string;
11
+ }
@@ -1,17 +1,17 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SolanaSigner = void 0;
4
- class SolanaSigner {
5
- constructor(wallet, keypair) {
6
- this.type = "AtomiqAbstractSigner";
7
- this.wallet = wallet;
8
- this.keypair = keypair;
9
- }
10
- getPublicKey() {
11
- return this.wallet.publicKey;
12
- }
13
- getAddress() {
14
- return this.wallet.publicKey.toString();
15
- }
16
- }
17
- exports.SolanaSigner = SolanaSigner;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SolanaSigner = void 0;
4
+ class SolanaSigner {
5
+ constructor(wallet, keypair) {
6
+ this.type = "AtomiqAbstractSigner";
7
+ this.wallet = wallet;
8
+ this.keypair = keypair;
9
+ }
10
+ getPublicKey() {
11
+ return this.wallet.publicKey;
12
+ }
13
+ getAddress() {
14
+ return this.wallet.publicKey.toString();
15
+ }
16
+ }
17
+ exports.SolanaSigner = SolanaSigner;
@@ -1,53 +1,53 @@
1
- import { PublicKey, Transaction } from "@solana/web3.js";
2
- import * as BN from "bn.js";
3
- export declare function timeoutPromise(timeoutMillis: number, abortSignal?: AbortSignal): Promise<void>;
4
- export declare function onceAsync<T>(executor: () => Promise<T>): () => Promise<T>;
5
- export declare function getLogger(prefix: string): {
6
- debug: (msg: any, ...args: any[]) => void;
7
- info: (msg: any, ...args: any[]) => void;
8
- warn: (msg: any, ...args: any[]) => void;
9
- error: (msg: any, ...args: any[]) => void;
10
- };
11
- export declare function tryWithRetries<T>(func: () => Promise<T>, retryPolicy?: {
12
- maxRetries?: number;
13
- delay?: number;
14
- exponential?: boolean;
15
- }, errorAllowed?: (e: any) => boolean, abortSignal?: AbortSignal): Promise<T>;
16
- export declare class SolanaTxUtils {
17
- private static LOW_VALUE;
18
- private static HIGH_VALUE;
19
- /**
20
- * Compact u16 array header size
21
- * @param n elements in the compact array
22
- * @returns size in bytes of array header
23
- */
24
- private static compactHeader;
25
- /**
26
- * Compact u16 array size
27
- * @param n elements in the compact array
28
- * @param size bytes per each element
29
- * @returns size in bytes of array
30
- */
31
- private static compactArraySize;
32
- /**
33
- * Returns # number of non-compute budget related instructions
34
- *
35
- * @param tx
36
- */
37
- static getNonComputeBudgetIxs(tx: Transaction): number;
38
- /**
39
- * @param tx a solana transaction
40
- * @param feePayer the publicKey of the signer
41
- * @returns size in bytes of the transaction
42
- */
43
- static getTxSize(tx: Transaction, feePayer: PublicKey): number;
44
- }
45
- export declare function toClaimHash(paymentHash: string, nonce: bigint, confirmations: number): string;
46
- export declare function fromClaimHash(claimHash: string): {
47
- paymentHash: string;
48
- nonce: BN;
49
- confirmations: number;
50
- };
51
- export declare function toEscrowHash(paymentHash: string, sequence: BN): string;
52
- export declare function toBN(value: bigint): BN;
53
- export declare function toBigInt(value: BN): bigint;
1
+ import { PublicKey, Transaction } from "@solana/web3.js";
2
+ import * as BN from "bn.js";
3
+ export declare function timeoutPromise(timeoutMillis: number, abortSignal?: AbortSignal): Promise<void>;
4
+ export declare function onceAsync<T>(executor: () => Promise<T>): () => Promise<T>;
5
+ export declare function getLogger(prefix: string): {
6
+ debug: (msg: any, ...args: any[]) => void;
7
+ info: (msg: any, ...args: any[]) => void;
8
+ warn: (msg: any, ...args: any[]) => void;
9
+ error: (msg: any, ...args: any[]) => void;
10
+ };
11
+ export declare function tryWithRetries<T>(func: () => Promise<T>, retryPolicy?: {
12
+ maxRetries?: number;
13
+ delay?: number;
14
+ exponential?: boolean;
15
+ }, errorAllowed?: (e: any) => boolean, abortSignal?: AbortSignal): Promise<T>;
16
+ export declare class SolanaTxUtils {
17
+ private static LOW_VALUE;
18
+ private static HIGH_VALUE;
19
+ /**
20
+ * Compact u16 array header size
21
+ * @param n elements in the compact array
22
+ * @returns size in bytes of array header
23
+ */
24
+ private static compactHeader;
25
+ /**
26
+ * Compact u16 array size
27
+ * @param n elements in the compact array
28
+ * @param size bytes per each element
29
+ * @returns size in bytes of array
30
+ */
31
+ private static compactArraySize;
32
+ /**
33
+ * Returns # number of non-compute budget related instructions
34
+ *
35
+ * @param tx
36
+ */
37
+ static getNonComputeBudgetIxs(tx: Transaction): number;
38
+ /**
39
+ * @param tx a solana transaction
40
+ * @param feePayer the publicKey of the signer
41
+ * @returns size in bytes of the transaction
42
+ */
43
+ static getTxSize(tx: Transaction, feePayer: PublicKey): number;
44
+ }
45
+ export declare function toClaimHash(paymentHash: string, nonce: bigint, confirmations: number): string;
46
+ export declare function fromClaimHash(claimHash: string): {
47
+ paymentHash: string;
48
+ nonce: BN;
49
+ confirmations: number;
50
+ };
51
+ export declare function toEscrowHash(paymentHash: string, sequence: BN): string;
52
+ export declare function toBN(value: bigint): BN;
53
+ export declare function toBigInt(value: BN): bigint;
@@ -1,170 +1,170 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toBigInt = exports.toBN = exports.toEscrowHash = exports.fromClaimHash = exports.toClaimHash = exports.SolanaTxUtils = exports.tryWithRetries = exports.getLogger = exports.onceAsync = exports.timeoutPromise = void 0;
4
- const web3_js_1 = require("@solana/web3.js");
5
- const BN = require("bn.js");
6
- const buffer_1 = require("buffer");
7
- const sha2_1 = require("@noble/hashes/sha2");
8
- function timeoutPromise(timeoutMillis, abortSignal) {
9
- return new Promise((resolve, reject) => {
10
- const timeout = setTimeout(resolve, timeoutMillis);
11
- if (abortSignal != null)
12
- abortSignal.addEventListener("abort", () => {
13
- clearTimeout(timeout);
14
- reject(new Error("Aborted"));
15
- });
16
- });
17
- }
18
- exports.timeoutPromise = timeoutPromise;
19
- function onceAsync(executor) {
20
- let promise;
21
- return () => {
22
- if (promise == null) {
23
- promise = executor();
24
- return promise;
25
- }
26
- else {
27
- return promise.catch(() => promise = executor());
28
- }
29
- };
30
- }
31
- exports.onceAsync = onceAsync;
32
- function getLogger(prefix) {
33
- return {
34
- debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug(prefix + msg, ...args),
35
- info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info(prefix + msg, ...args),
36
- warn: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 1) && console.warn(prefix + msg, ...args),
37
- error: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 0) && console.error(prefix + msg, ...args)
38
- };
39
- }
40
- exports.getLogger = getLogger;
41
- const logger = getLogger("Utils: ");
42
- async function tryWithRetries(func, retryPolicy, errorAllowed, abortSignal) {
43
- retryPolicy = retryPolicy || {};
44
- retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
45
- retryPolicy.delay = retryPolicy.delay || 500;
46
- retryPolicy.exponential = retryPolicy.exponential == null ? true : retryPolicy.exponential;
47
- let err = null;
48
- for (let i = 0; i < retryPolicy.maxRetries; i++) {
49
- try {
50
- const resp = await func();
51
- return resp;
52
- }
53
- catch (e) {
54
- if (errorAllowed != null && errorAllowed(e))
55
- throw e;
56
- err = e;
57
- logger.error("tryWithRetries(): error on try number: " + i, e);
58
- }
59
- if (abortSignal != null && abortSignal.aborted)
60
- throw new Error("Aborted");
61
- if (i !== retryPolicy.maxRetries - 1) {
62
- await timeoutPromise(retryPolicy.exponential ? retryPolicy.delay * Math.pow(2, i) : retryPolicy.delay, abortSignal);
63
- }
64
- }
65
- throw err;
66
- }
67
- exports.tryWithRetries = tryWithRetries;
68
- class SolanaTxUtils {
69
- /**
70
- * Compact u16 array header size
71
- * @param n elements in the compact array
72
- * @returns size in bytes of array header
73
- */
74
- static compactHeader(n) {
75
- return (n <= SolanaTxUtils.LOW_VALUE ? 1 : n <= SolanaTxUtils.HIGH_VALUE ? 2 : 3);
76
- }
77
- /**
78
- * Compact u16 array size
79
- * @param n elements in the compact array
80
- * @param size bytes per each element
81
- * @returns size in bytes of array
82
- */
83
- static compactArraySize(n, size) {
84
- return SolanaTxUtils.compactHeader(n) + n * size;
85
- }
86
- /**
87
- * Returns # number of non-compute budget related instructions
88
- *
89
- * @param tx
90
- */
91
- static getNonComputeBudgetIxs(tx) {
92
- let counter = 0;
93
- for (let ix of tx.instructions) {
94
- if (!ix.programId.equals(web3_js_1.ComputeBudgetProgram.programId))
95
- counter++;
96
- }
97
- return counter;
98
- }
99
- /**
100
- * @param tx a solana transaction
101
- * @param feePayer the publicKey of the signer
102
- * @returns size in bytes of the transaction
103
- */
104
- static getTxSize(tx, feePayer) {
105
- const feePayerPk = [feePayer.toBase58()];
106
- const signers = new Set(feePayerPk);
107
- const accounts = new Set(feePayerPk);
108
- const ixsSize = tx.instructions.reduce((acc, ix) => {
109
- ix.keys.forEach(({ pubkey, isSigner }) => {
110
- const pk = pubkey.toBase58();
111
- if (isSigner)
112
- signers.add(pk);
113
- accounts.add(pk);
114
- });
115
- accounts.add(ix.programId.toBase58());
116
- const nIndexes = ix.keys.length;
117
- const opaqueData = ix.data.length;
118
- return (acc +
119
- 1 + // PID index
120
- SolanaTxUtils.compactArraySize(nIndexes, 1) +
121
- SolanaTxUtils.compactArraySize(opaqueData, 1));
122
- }, 0);
123
- return (SolanaTxUtils.compactArraySize(signers.size, 64) + // signatures
124
- 3 + // header
125
- SolanaTxUtils.compactArraySize(accounts.size, 32) + // accounts
126
- 32 + // blockhash
127
- SolanaTxUtils.compactHeader(tx.instructions.length) + // instructions
128
- ixsSize);
129
- }
130
- ;
131
- }
132
- exports.SolanaTxUtils = SolanaTxUtils;
133
- // COMPACT ARRAY
134
- SolanaTxUtils.LOW_VALUE = 127; // 0x7f
135
- SolanaTxUtils.HIGH_VALUE = 16383; // 0x3fff
136
- function toClaimHash(paymentHash, nonce, confirmations) {
137
- return paymentHash +
138
- nonce.toString(16).padStart(16, "0") +
139
- confirmations.toString(16).padStart(4, "0");
140
- }
141
- exports.toClaimHash = toClaimHash;
142
- function fromClaimHash(claimHash) {
143
- if (claimHash.length !== 84)
144
- throw new Error("Claim hash invalid length: " + claimHash.length);
145
- return {
146
- paymentHash: claimHash.slice(0, 64),
147
- nonce: new BN(claimHash.slice(64, 80), "hex"),
148
- confirmations: parseInt(claimHash.slice(80, 84), 16)
149
- };
150
- }
151
- exports.fromClaimHash = fromClaimHash;
152
- function toEscrowHash(paymentHash, sequence) {
153
- return buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
154
- buffer_1.Buffer.from(paymentHash, "hex"),
155
- sequence.toArrayLike(buffer_1.Buffer, "be", 8)
156
- ]))).toString("hex");
157
- }
158
- exports.toEscrowHash = toEscrowHash;
159
- function toBN(value) {
160
- if (value == null)
161
- return null;
162
- return new BN(value.toString(10));
163
- }
164
- exports.toBN = toBN;
165
- function toBigInt(value) {
166
- if (value == null)
167
- return null;
168
- return BigInt(value.toString(10));
169
- }
170
- exports.toBigInt = toBigInt;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBigInt = exports.toBN = exports.toEscrowHash = exports.fromClaimHash = exports.toClaimHash = exports.SolanaTxUtils = exports.tryWithRetries = exports.getLogger = exports.onceAsync = exports.timeoutPromise = void 0;
4
+ const web3_js_1 = require("@solana/web3.js");
5
+ const BN = require("bn.js");
6
+ const buffer_1 = require("buffer");
7
+ const sha2_1 = require("@noble/hashes/sha2");
8
+ function timeoutPromise(timeoutMillis, abortSignal) {
9
+ return new Promise((resolve, reject) => {
10
+ const timeout = setTimeout(resolve, timeoutMillis);
11
+ if (abortSignal != null)
12
+ abortSignal.addEventListener("abort", () => {
13
+ clearTimeout(timeout);
14
+ reject(new Error("Aborted"));
15
+ });
16
+ });
17
+ }
18
+ exports.timeoutPromise = timeoutPromise;
19
+ function onceAsync(executor) {
20
+ let promise;
21
+ return () => {
22
+ if (promise == null) {
23
+ promise = executor();
24
+ return promise;
25
+ }
26
+ else {
27
+ return promise.catch(() => promise = executor());
28
+ }
29
+ };
30
+ }
31
+ exports.onceAsync = onceAsync;
32
+ function getLogger(prefix) {
33
+ return {
34
+ debug: (msg, ...args) => global.atomiqLogLevel >= 3 && console.debug(prefix + msg, ...args),
35
+ info: (msg, ...args) => global.atomiqLogLevel >= 2 && console.info(prefix + msg, ...args),
36
+ warn: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 1) && console.warn(prefix + msg, ...args),
37
+ error: (msg, ...args) => (global.atomiqLogLevel == null || global.atomiqLogLevel >= 0) && console.error(prefix + msg, ...args)
38
+ };
39
+ }
40
+ exports.getLogger = getLogger;
41
+ const logger = getLogger("Utils: ");
42
+ async function tryWithRetries(func, retryPolicy, errorAllowed, abortSignal) {
43
+ retryPolicy = retryPolicy || {};
44
+ retryPolicy.maxRetries = retryPolicy.maxRetries || 5;
45
+ retryPolicy.delay = retryPolicy.delay || 500;
46
+ retryPolicy.exponential = retryPolicy.exponential == null ? true : retryPolicy.exponential;
47
+ let err = null;
48
+ for (let i = 0; i < retryPolicy.maxRetries; i++) {
49
+ try {
50
+ const resp = await func();
51
+ return resp;
52
+ }
53
+ catch (e) {
54
+ if (errorAllowed != null && errorAllowed(e))
55
+ throw e;
56
+ err = e;
57
+ logger.error("tryWithRetries(): error on try number: " + i, e);
58
+ }
59
+ if (abortSignal != null && abortSignal.aborted)
60
+ throw new Error("Aborted");
61
+ if (i !== retryPolicy.maxRetries - 1) {
62
+ await timeoutPromise(retryPolicy.exponential ? retryPolicy.delay * Math.pow(2, i) : retryPolicy.delay, abortSignal);
63
+ }
64
+ }
65
+ throw err;
66
+ }
67
+ exports.tryWithRetries = tryWithRetries;
68
+ class SolanaTxUtils {
69
+ /**
70
+ * Compact u16 array header size
71
+ * @param n elements in the compact array
72
+ * @returns size in bytes of array header
73
+ */
74
+ static compactHeader(n) {
75
+ return (n <= SolanaTxUtils.LOW_VALUE ? 1 : n <= SolanaTxUtils.HIGH_VALUE ? 2 : 3);
76
+ }
77
+ /**
78
+ * Compact u16 array size
79
+ * @param n elements in the compact array
80
+ * @param size bytes per each element
81
+ * @returns size in bytes of array
82
+ */
83
+ static compactArraySize(n, size) {
84
+ return SolanaTxUtils.compactHeader(n) + n * size;
85
+ }
86
+ /**
87
+ * Returns # number of non-compute budget related instructions
88
+ *
89
+ * @param tx
90
+ */
91
+ static getNonComputeBudgetIxs(tx) {
92
+ let counter = 0;
93
+ for (let ix of tx.instructions) {
94
+ if (!ix.programId.equals(web3_js_1.ComputeBudgetProgram.programId))
95
+ counter++;
96
+ }
97
+ return counter;
98
+ }
99
+ /**
100
+ * @param tx a solana transaction
101
+ * @param feePayer the publicKey of the signer
102
+ * @returns size in bytes of the transaction
103
+ */
104
+ static getTxSize(tx, feePayer) {
105
+ const feePayerPk = [feePayer.toBase58()];
106
+ const signers = new Set(feePayerPk);
107
+ const accounts = new Set(feePayerPk);
108
+ const ixsSize = tx.instructions.reduce((acc, ix) => {
109
+ ix.keys.forEach(({ pubkey, isSigner }) => {
110
+ const pk = pubkey.toBase58();
111
+ if (isSigner)
112
+ signers.add(pk);
113
+ accounts.add(pk);
114
+ });
115
+ accounts.add(ix.programId.toBase58());
116
+ const nIndexes = ix.keys.length;
117
+ const opaqueData = ix.data.length;
118
+ return (acc +
119
+ 1 + // PID index
120
+ SolanaTxUtils.compactArraySize(nIndexes, 1) +
121
+ SolanaTxUtils.compactArraySize(opaqueData, 1));
122
+ }, 0);
123
+ return (SolanaTxUtils.compactArraySize(signers.size, 64) + // signatures
124
+ 3 + // header
125
+ SolanaTxUtils.compactArraySize(accounts.size, 32) + // accounts
126
+ 32 + // blockhash
127
+ SolanaTxUtils.compactHeader(tx.instructions.length) + // instructions
128
+ ixsSize);
129
+ }
130
+ ;
131
+ }
132
+ exports.SolanaTxUtils = SolanaTxUtils;
133
+ // COMPACT ARRAY
134
+ SolanaTxUtils.LOW_VALUE = 127; // 0x7f
135
+ SolanaTxUtils.HIGH_VALUE = 16383; // 0x3fff
136
+ function toClaimHash(paymentHash, nonce, confirmations) {
137
+ return paymentHash +
138
+ nonce.toString(16).padStart(16, "0") +
139
+ confirmations.toString(16).padStart(4, "0");
140
+ }
141
+ exports.toClaimHash = toClaimHash;
142
+ function fromClaimHash(claimHash) {
143
+ if (claimHash.length !== 84)
144
+ throw new Error("Claim hash invalid length: " + claimHash.length);
145
+ return {
146
+ paymentHash: claimHash.slice(0, 64),
147
+ nonce: new BN(claimHash.slice(64, 80), "hex"),
148
+ confirmations: parseInt(claimHash.slice(80, 84), 16)
149
+ };
150
+ }
151
+ exports.fromClaimHash = fromClaimHash;
152
+ function toEscrowHash(paymentHash, sequence) {
153
+ return buffer_1.Buffer.from((0, sha2_1.sha256)(buffer_1.Buffer.concat([
154
+ buffer_1.Buffer.from(paymentHash, "hex"),
155
+ sequence.toArrayLike(buffer_1.Buffer, "be", 8)
156
+ ]))).toString("hex");
157
+ }
158
+ exports.toEscrowHash = toEscrowHash;
159
+ function toBN(value) {
160
+ if (value == null)
161
+ return null;
162
+ return new BN(value.toString(10));
163
+ }
164
+ exports.toBN = toBN;
165
+ function toBigInt(value) {
166
+ if (value == null)
167
+ return null;
168
+ return BigInt(value.toString(10));
169
+ }
170
+ exports.toBigInt = toBigInt;