@atomiqlabs/chain-starknet 8.0.13 → 8.1.10

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 (119) hide show
  1. package/dist/index.d.ts +18 -18
  2. package/dist/index.js +42 -42
  3. package/dist/starknet/StarknetChainType.d.ts +19 -19
  4. package/dist/starknet/StarknetChainType.js +2 -2
  5. package/dist/starknet/StarknetInitializer.d.ts +66 -63
  6. package/dist/starknet/StarknetInitializer.js +101 -101
  7. package/dist/starknet/btcrelay/BtcRelayAbi.d.ts +250 -250
  8. package/dist/starknet/btcrelay/BtcRelayAbi.js +341 -341
  9. package/dist/starknet/btcrelay/StarknetBtcRelay.d.ts +196 -196
  10. package/dist/starknet/btcrelay/StarknetBtcRelay.js +419 -411
  11. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.d.ts +70 -70
  12. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.js +115 -115
  13. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.d.ts +91 -91
  14. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.js +155 -155
  15. package/dist/starknet/chain/StarknetAction.d.ts +19 -19
  16. package/dist/starknet/chain/StarknetAction.js +74 -74
  17. package/dist/starknet/chain/StarknetChainInterface.d.ts +142 -143
  18. package/dist/starknet/chain/StarknetChainInterface.js +198 -199
  19. package/dist/starknet/chain/StarknetModule.d.ts +8 -8
  20. package/dist/starknet/chain/StarknetModule.js +12 -12
  21. package/dist/starknet/chain/modules/ERC20Abi.d.ts +755 -755
  22. package/dist/starknet/chain/modules/ERC20Abi.js +1032 -1032
  23. package/dist/starknet/chain/modules/StarknetAccounts.d.ts +6 -6
  24. package/dist/starknet/chain/modules/StarknetAccounts.js +26 -26
  25. package/dist/starknet/chain/modules/StarknetAddresses.d.ts +10 -10
  26. package/dist/starknet/chain/modules/StarknetAddresses.js +27 -27
  27. package/dist/starknet/chain/modules/StarknetBlocks.d.ts +27 -27
  28. package/dist/starknet/chain/modules/StarknetBlocks.js +82 -82
  29. package/dist/starknet/chain/modules/StarknetEvents.d.ts +47 -47
  30. package/dist/starknet/chain/modules/StarknetEvents.js +90 -90
  31. package/dist/starknet/chain/modules/StarknetFees.d.ts +118 -104
  32. package/dist/starknet/chain/modules/StarknetFees.js +150 -146
  33. package/dist/starknet/chain/modules/StarknetSignatures.d.ts +29 -29
  34. package/dist/starknet/chain/modules/StarknetSignatures.js +72 -72
  35. package/dist/starknet/chain/modules/StarknetTokens.d.ts +66 -66
  36. package/dist/starknet/chain/modules/StarknetTokens.js +99 -99
  37. package/dist/starknet/chain/modules/StarknetTransactions.d.ts +122 -115
  38. package/dist/starknet/chain/modules/StarknetTransactions.js +633 -612
  39. package/dist/starknet/contract/StarknetContractBase.d.ts +14 -13
  40. package/dist/starknet/contract/StarknetContractBase.js +21 -20
  41. package/dist/starknet/contract/StarknetContractModule.d.ts +8 -8
  42. package/dist/starknet/contract/StarknetContractModule.js +11 -11
  43. package/dist/starknet/contract/modules/StarknetContractEvents.d.ts +56 -57
  44. package/dist/starknet/contract/modules/StarknetContractEvents.js +111 -111
  45. package/dist/starknet/events/StarknetChainEvents.d.ts +21 -21
  46. package/dist/starknet/events/StarknetChainEvents.js +61 -61
  47. package/dist/starknet/events/StarknetChainEventsBrowser.d.ts +178 -190
  48. package/dist/starknet/events/StarknetChainEventsBrowser.js +523 -582
  49. package/dist/starknet/provider/RpcProviderWithRetries.d.ts +49 -53
  50. package/dist/starknet/provider/RpcProviderWithRetries.js +94 -94
  51. package/dist/starknet/provider/WebSocketChannelWithRetries.d.ts +21 -21
  52. package/dist/starknet/provider/WebSocketChannelWithRetries.js +46 -46
  53. package/dist/starknet/spv_swap/SpvVaultContractAbi.d.ts +488 -488
  54. package/dist/starknet/spv_swap/SpvVaultContractAbi.js +656 -656
  55. package/dist/starknet/spv_swap/StarknetSpvVaultContract.d.ts +225 -219
  56. package/dist/starknet/spv_swap/StarknetSpvVaultContract.js +663 -621
  57. package/dist/starknet/spv_swap/StarknetSpvVaultData.d.ts +108 -108
  58. package/dist/starknet/spv_swap/StarknetSpvVaultData.js +190 -190
  59. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.d.ts +56 -56
  60. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.js +103 -103
  61. package/dist/starknet/swaps/EscrowManagerAbi.d.ts +431 -431
  62. package/dist/starknet/swaps/EscrowManagerAbi.js +583 -583
  63. package/dist/starknet/swaps/StarknetSwapContract.d.ts +309 -278
  64. package/dist/starknet/swaps/StarknetSwapContract.js +755 -579
  65. package/dist/starknet/swaps/StarknetSwapData.d.ts +234 -234
  66. package/dist/starknet/swaps/StarknetSwapData.js +474 -474
  67. package/dist/starknet/swaps/StarknetSwapModule.d.ts +10 -10
  68. package/dist/starknet/swaps/StarknetSwapModule.js +12 -12
  69. package/dist/starknet/swaps/handlers/IHandler.d.ts +13 -13
  70. package/dist/starknet/swaps/handlers/IHandler.js +2 -2
  71. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.d.ts +13 -13
  72. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.js +13 -13
  73. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.d.ts +21 -21
  74. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.js +44 -44
  75. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
  76. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +48 -48
  77. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
  78. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +40 -40
  79. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +20 -20
  80. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +30 -30
  81. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +42 -45
  82. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +50 -54
  83. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
  84. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.js +27 -27
  85. package/dist/starknet/swaps/modules/StarknetLpVault.d.ts +67 -67
  86. package/dist/starknet/swaps/modules/StarknetLpVault.js +122 -122
  87. package/dist/starknet/swaps/modules/StarknetSwapClaim.d.ts +52 -52
  88. package/dist/starknet/swaps/modules/StarknetSwapClaim.js +99 -99
  89. package/dist/starknet/swaps/modules/StarknetSwapInit.d.ts +94 -94
  90. package/dist/starknet/swaps/modules/StarknetSwapInit.js +239 -239
  91. package/dist/starknet/swaps/modules/StarknetSwapRefund.d.ts +60 -60
  92. package/dist/starknet/swaps/modules/StarknetSwapRefund.js +126 -126
  93. package/dist/starknet/wallet/StarknetBrowserSigner.d.ts +11 -11
  94. package/dist/starknet/wallet/StarknetBrowserSigner.js +17 -17
  95. package/dist/starknet/wallet/StarknetPersistentSigner.d.ts +76 -76
  96. package/dist/starknet/wallet/StarknetPersistentSigner.js +291 -291
  97. package/dist/starknet/wallet/StarknetSigner.d.ts +72 -72
  98. package/dist/starknet/wallet/StarknetSigner.js +114 -114
  99. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.d.ts +18 -18
  100. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.js +45 -45
  101. package/dist/utils/Utils.d.ts +77 -77
  102. package/dist/utils/Utils.js +304 -303
  103. package/package.json +2 -2
  104. package/src/starknet/StarknetInitializer.ts +6 -3
  105. package/src/starknet/btcrelay/StarknetBtcRelay.ts +19 -6
  106. package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +7 -7
  107. package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +6 -6
  108. package/src/starknet/chain/StarknetAction.ts +1 -0
  109. package/src/starknet/chain/StarknetChainInterface.ts +0 -2
  110. package/src/starknet/chain/modules/StarknetFees.ts +15 -2
  111. package/src/starknet/chain/modules/StarknetTransactions.ts +24 -0
  112. package/src/starknet/contract/StarknetContractBase.ts +7 -4
  113. package/src/starknet/contract/StarknetContractModule.ts +1 -1
  114. package/src/starknet/contract/modules/StarknetContractEvents.ts +7 -7
  115. package/src/starknet/events/StarknetChainEventsBrowser.ts +2 -64
  116. package/src/starknet/provider/RpcProviderWithRetries.ts +1 -1
  117. package/src/starknet/spv_swap/StarknetSpvVaultContract.ts +84 -18
  118. package/src/starknet/swaps/StarknetSwapContract.ts +242 -6
  119. package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +0 -4
@@ -1,612 +1,633 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StarknetTransactions = void 0;
4
- exports.isStarknetTxInvoke = isStarknetTxInvoke;
5
- exports.isStarknetTxDeployAccount = isStarknetTxDeployAccount;
6
- const StarknetModule_1 = require("../StarknetModule");
7
- const starknet_1 = require("starknet");
8
- const Utils_1 = require("../../../utils/Utils");
9
- const base_1 = require("@atomiqlabs/base");
10
- function isStarknetTxInvoke(obj) {
11
- return typeof (obj) === "object" &&
12
- typeof (obj.details) === "object" &&
13
- (obj.txId == null || typeof (obj.txId) === "string") &&
14
- obj.type === "INVOKE" &&
15
- Array.isArray(obj.tx) &&
16
- (obj.signed == null || typeof (obj.signed) === "object");
17
- }
18
- function isStarknetTxDeployAccount(obj) {
19
- return typeof (obj) === "object" &&
20
- typeof (obj.details) === "object" &&
21
- (obj.txId == null || typeof (obj.txId) === "string") &&
22
- obj.type === "DEPLOY_ACCOUNT" &&
23
- typeof (obj.tx) === "object" &&
24
- (obj.signed == null || typeof (obj.signed) === "object");
25
- }
26
- const MAX_UNCONFIRMED_TXS = 25;
27
- class StarknetTransactions extends StarknetModule_1.StarknetModule {
28
- constructor() {
29
- super(...arguments);
30
- this.latestConfirmedNonces = {};
31
- this.latestPendingNonces = {};
32
- this.latestSignedNonces = {};
33
- this._cbksBeforeTxReplace = [];
34
- this.cbksBeforeTxSigned = [];
35
- this._knownTxSet = new Set();
36
- }
37
- sendTransaction(tx) {
38
- if (tx.signed == null)
39
- throw new Error("Cannot send unsigned transaction! signed field missing!");
40
- switch (tx.type) {
41
- case "INVOKE":
42
- return this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
43
- case "DEPLOY_ACCOUNT":
44
- return this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
45
- default:
46
- throw new Error("Unsupported tx type!");
47
- }
48
- }
49
- /**
50
- * Returns the nonce of the account or 0, if the account is not deployed yet
51
- *
52
- * @param address
53
- * @param blockTag
54
- */
55
- async getNonce(address, blockTag = starknet_1.BlockTag.PRE_CONFIRMED) {
56
- try {
57
- return BigInt(await this.provider.getNonceForAddress(address, blockTag));
58
- }
59
- catch (e) {
60
- if (e.baseError?.code === 20 ||
61
- (e.message != null && e.message.includes("20: Contract not found"))) {
62
- return BigInt(0);
63
- }
64
- throw e;
65
- }
66
- }
67
- async confirmTransactionWs(txId, abortSignal) {
68
- if (this.root.wsChannel == null)
69
- throw new Error("Underlying provider doesn't have a WS channel!");
70
- const subscription = await this.root.wsChannel.subscribeTransactionStatus({
71
- transactionHash: txId
72
- });
73
- const endSubscription = async () => {
74
- if (this.root.wsChannel.isConnected() && await subscription.unsubscribe())
75
- return;
76
- this.root.wsChannel.removeSubscription(subscription.id);
77
- };
78
- if (abortSignal != null && abortSignal.aborted) {
79
- await endSubscription();
80
- abortSignal.throwIfAborted();
81
- }
82
- const status = await new Promise((resolve, reject) => {
83
- if (abortSignal != null)
84
- abortSignal.onabort = () => {
85
- endSubscription().catch(err => this.logger.error("confirmTransactionWs(): End subscription error: ", err));
86
- reject(abortSignal.reason);
87
- };
88
- subscription.on((data) => {
89
- if (data.status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L2 && data.status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L1)
90
- return; //No pre-confs
91
- resolve(data.status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED ? "success" : "reverted");
92
- });
93
- });
94
- await endSubscription();
95
- this.logger.debug(`confirmTransactionWs(): Transaction ${txId} confirmed, transaction status: ${status}`);
96
- return {
97
- txId,
98
- status
99
- };
100
- }
101
- async confirmTransactionPolling(walletAddress, nonce, checkTxns, abortSignal) {
102
- let state = "pending";
103
- let confirmedTxId;
104
- while (state === "pending") {
105
- await (0, Utils_1.timeoutPromise)(3000, abortSignal);
106
- const latestConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)];
107
- const snapshot = [...checkTxns]; //Iterate over a snapshot
108
- const totalTxnCount = snapshot.length;
109
- let rejectedTxns = 0;
110
- let notFoundTxns = 0;
111
- for (let txId of snapshot) {
112
- let _state = await this._getTxIdStatus(txId);
113
- if (_state === "not_found")
114
- notFoundTxns++;
115
- if (_state === "rejected")
116
- rejectedTxns++;
117
- if (_state === "reverted" || _state === "success") {
118
- confirmedTxId = txId;
119
- state = _state;
120
- break;
121
- }
122
- }
123
- if (rejectedTxns === totalTxnCount) { //All rejected
124
- state = "rejected";
125
- break;
126
- }
127
- if (notFoundTxns === totalTxnCount) { //All not found, check the latest account nonce
128
- if (latestConfirmedNonce != null && latestConfirmedNonce > nonce) {
129
- //Confirmed nonce is already higher than the TX nonce, meaning the TX got replaced
130
- throw new Error("Transaction failed - replaced!");
131
- }
132
- this.logger.warn("confirmTransaction(): All transactions not found, fetching the latest account nonce...");
133
- const _latestConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)];
134
- const currentLatestNonce = await this.getNonce(walletAddress, starknet_1.BlockTag.LATEST);
135
- if (_latestConfirmedNonce == null || _latestConfirmedNonce < currentLatestNonce) {
136
- this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)] = currentLatestNonce;
137
- }
138
- }
139
- }
140
- if (state !== "rejected")
141
- this.logger.debug(`confirmTransactionPolling(): Transaction ${confirmedTxId} confirmed, transaction status: ${state}`);
142
- return {
143
- txId: confirmedTxId,
144
- status: state
145
- };
146
- }
147
- /**
148
- * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
149
- * the transaction at regular interval
150
- *
151
- * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
152
- * @param abortSignal signal to abort waiting for tx confirmation
153
- * @private
154
- */
155
- async confirmTransaction(tx, abortSignal) {
156
- if (tx.txId == null)
157
- throw new Error("txId is null!");
158
- const abortController = new AbortController();
159
- if (abortSignal != null)
160
- abortSignal.onabort = () => abortController.abort(abortSignal.reason);
161
- let txReplaceListener = undefined;
162
- let result;
163
- try {
164
- result = await new Promise((resolve, reject) => {
165
- const checkTxns = new Set([tx.txId]);
166
- txReplaceListener = (oldTx, oldTxId, newTx, newTxId) => {
167
- if (checkTxns.has(oldTxId))
168
- checkTxns.add(newTxId);
169
- //TODO: Enable this once WS subscriptions finally work (also unsubscribe should work!!!!)
170
- // if(this.root.wsChannel!=null) this.confirmTransactionWs(newTxId, abortController.signal)
171
- // .then(resolve)
172
- // .catch(reject);
173
- return Promise.resolve();
174
- };
175
- this.onBeforeTxReplace(txReplaceListener);
176
- this.confirmTransactionPolling(tx.details.walletAddress, BigInt(tx.details.nonce), checkTxns, abortController.signal)
177
- .then(resolve)
178
- .catch(reject);
179
- //TODO: Enable this once WS subscriptions finally work (also unsubscribe should work!!!!)
180
- // if(this.root.wsChannel!=null) this.confirmTransactionWs(tx.txId!, abortController.signal)
181
- // .then(resolve)
182
- // .catch(reject);
183
- });
184
- if (txReplaceListener != null)
185
- this.offBeforeTxReplace(txReplaceListener);
186
- abortController.abort();
187
- }
188
- catch (e) {
189
- if (txReplaceListener != null)
190
- this.offBeforeTxReplace(txReplaceListener);
191
- abortController.abort(e);
192
- throw e;
193
- }
194
- if (result.status === "rejected")
195
- throw new Error("Transaction rejected!");
196
- const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
197
- const currentConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
198
- if (currentConfirmedNonce == null || nextAccountNonce > currentConfirmedNonce) {
199
- this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
200
- }
201
- if (result.status === "reverted")
202
- throw new base_1.TransactionRevertedError("Transaction reverted!");
203
- return result.txId;
204
- }
205
- /**
206
- * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
207
- *
208
- * @param signer
209
- * @param txs
210
- * @private
211
- */
212
- async prepareTransactions(signer, txs) {
213
- let nonce = await this.getNonce(signer.getAddress());
214
- const latestPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signer.getAddress())];
215
- if (latestPendingNonce != null && latestPendingNonce > nonce) {
216
- this.logger.debug("prepareTransactions(): Using 'pending' nonce from local cache!");
217
- nonce = latestPendingNonce;
218
- }
219
- //Add deploy account tx
220
- if (nonce === 0n) {
221
- const deployPayload = await signer.getDeployPayload();
222
- if (deployPayload != null) {
223
- const tx = await this.root.Accounts.getAccountDeployTransaction(deployPayload);
224
- tx.addedInPrepare = true;
225
- txs.unshift(tx);
226
- }
227
- }
228
- if (!signer.isManagingNoncesInternally) {
229
- if (nonce === 0n) {
230
- //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
231
- nonce = 1n;
232
- }
233
- for (let i = 0; i < txs.length; i++) {
234
- const tx = txs[i];
235
- if (tx.details.nonce != null)
236
- nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
237
- if (nonce == null)
238
- nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
239
- if (tx.details.nonce == null)
240
- tx.details.nonce = nonce;
241
- this.logger.debug("prepareTransactions(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
242
- nonce += BigInt(1);
243
- }
244
- }
245
- for (let tx of txs) {
246
- for (let callback of this.cbksBeforeTxSigned) {
247
- await callback(tx);
248
- }
249
- }
250
- }
251
- /**
252
- * Sends out a signed transaction to the RPC
253
- *
254
- * @param tx Starknet tx to send
255
- * @param onBeforePublish a callback called before every transaction is published
256
- * @private
257
- */
258
- async sendSignedTransaction(tx, onBeforePublish) {
259
- if (tx.txId == null)
260
- throw new Error("Expecting signed tx with txId field populated!");
261
- if (onBeforePublish != null)
262
- await onBeforePublish(tx.txId, StarknetTransactions.serializeTx(tx));
263
- this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
264
- const txResult = await this.sendTransaction(tx);
265
- if (tx.txId !== txResult)
266
- this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
267
- this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
268
- return txResult;
269
- }
270
- /**
271
- * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
272
- * of a batch of starknet transactions
273
- *
274
- * @param signer
275
- * @param _txs transactions to send
276
- * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
277
- * are re-sent at regular intervals)
278
- * @param abortSignal abort signal to abort waiting for transaction confirmations
279
- * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
280
- * are executed in order)
281
- * @param onBeforePublish a callback called before every transaction is published
282
- */
283
- async sendAndConfirm(signer, _txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
284
- const txs = _txs;
285
- await this.prepareTransactions(signer, txs);
286
- const signedTxs = [];
287
- //Don't separate the signing process from the sending when using browser-based wallet
288
- if (signer.signTransaction != null)
289
- for (let i = 0; i < txs.length; i++) {
290
- const tx = txs[i];
291
- const signedTx = await signer.signTransaction(tx);
292
- signedTx.addedInPrepare = tx.addedInPrepare;
293
- signedTxs.push(signedTx);
294
- this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
295
- const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
296
- const currentSignedNonce = this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
297
- if (currentSignedNonce == null || nextAccountNonce > currentSignedNonce) {
298
- this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
299
- }
300
- }
301
- this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
302
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
303
- const txIds = [];
304
- if (parallel) {
305
- let promises = [];
306
- for (let i = 0; i < txs.length; i++) {
307
- let tx;
308
- if (signer.signTransaction == null) {
309
- const txId = await signer.sendTransaction(txs[i], txs[i].addedInPrepare ? undefined : onBeforePublish);
310
- tx = txs[i];
311
- tx.txId = txId;
312
- }
313
- else {
314
- const signedTx = signedTxs[i];
315
- await this.sendSignedTransaction(signedTx, signedTx.addedInPrepare ? undefined : onBeforePublish);
316
- tx = signedTx;
317
- }
318
- if (tx.details.nonce != null) {
319
- const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
320
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
321
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
322
- this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
323
- }
324
- }
325
- if (!tx.addedInPrepare) {
326
- promises.push(this.confirmTransaction(tx, abortSignal));
327
- if (!waitForConfirmation)
328
- txIds.push(tx.txId);
329
- }
330
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
331
- if (promises.length >= MAX_UNCONFIRMED_TXS) {
332
- if (waitForConfirmation)
333
- txIds.push(...await Promise.all(promises));
334
- promises = [];
335
- }
336
- }
337
- if (waitForConfirmation && promises.length > 0) {
338
- txIds.push(...await Promise.all(promises));
339
- }
340
- }
341
- else {
342
- for (let i = 0; i < txs.length; i++) {
343
- let tx;
344
- if (signer.signTransaction == null) {
345
- const txId = await signer.sendTransaction(txs[i], txs[i].addedInPrepare ? undefined : onBeforePublish);
346
- tx = txs[i];
347
- tx.txId = txId;
348
- }
349
- else {
350
- const signedTx = signedTxs[i];
351
- await this.sendSignedTransaction(signedTx, signedTx.addedInPrepare ? undefined : onBeforePublish);
352
- tx = signedTx;
353
- }
354
- if (tx.details.nonce != null) {
355
- const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
356
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
357
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
358
- this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
359
- }
360
- }
361
- const confirmPromise = this.confirmTransaction(tx, abortSignal);
362
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
363
- //Don't await the last promise when !waitForConfirmation
364
- let txHash = tx.txId;
365
- if (i < txs.length - 1 || waitForConfirmation)
366
- txHash = await confirmPromise;
367
- if (!tx.addedInPrepare)
368
- txIds.push(txHash);
369
- }
370
- }
371
- this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
372
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
373
- return txIds;
374
- }
375
- async sendSignedAndConfirm(signedTxs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
376
- signedTxs.forEach(val => {
377
- if (val.signed == null)
378
- throw new Error("Transactions have to be signed!");
379
- });
380
- this.logger.debug("sendSignedAndConfirm(): sending transactions, count: " + signedTxs.length +
381
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
382
- const txIds = [];
383
- if (parallel) {
384
- let promises = [];
385
- for (let i = 0; i < signedTxs.length; i++) {
386
- const signedTx = signedTxs[i];
387
- await this.sendSignedTransaction(signedTx, onBeforePublish);
388
- if (signedTx.details.nonce != null) {
389
- const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
390
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
391
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
392
- this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
393
- }
394
- }
395
- promises.push(this.confirmTransaction(signedTx, abortSignal));
396
- if (!waitForConfirmation)
397
- txIds.push(signedTx.txId);
398
- this.logger.debug("sendSignedAndConfirm(): transaction sent (" + (i + 1) + "/" + signedTxs.length + "): " + signedTx.txId);
399
- if (promises.length >= MAX_UNCONFIRMED_TXS) {
400
- if (waitForConfirmation)
401
- txIds.push(...await Promise.all(promises));
402
- promises = [];
403
- }
404
- }
405
- if (waitForConfirmation && promises.length > 0) {
406
- txIds.push(...await Promise.all(promises));
407
- }
408
- }
409
- else {
410
- for (let i = 0; i < signedTxs.length; i++) {
411
- const signedTx = signedTxs[i];
412
- await this.sendSignedTransaction(signedTx, onBeforePublish);
413
- if (signedTx.details.nonce != null) {
414
- const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
415
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
416
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
417
- this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
418
- }
419
- }
420
- const confirmPromise = this.confirmTransaction(signedTx, abortSignal);
421
- this.logger.debug("sendSignedAndConfirm(): transaction sent (" + (i + 1) + "/" + signedTxs.length + "): " + signedTx.txId);
422
- //Don't await the last promise when !waitForConfirmation
423
- let txHash = signedTx.txId;
424
- if (i < signedTxs.length - 1 || waitForConfirmation)
425
- txHash = await confirmPromise;
426
- txIds.push(txHash);
427
- }
428
- }
429
- this.logger.info("sendSignedAndConfirm(): sent transactions, count: " + signedTxs.length +
430
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
431
- return txIds;
432
- }
433
- /**
434
- * Serializes the starknet transaction, saves the transaction, signers & last valid blockheight
435
- *
436
- * @param tx
437
- */
438
- static serializeTx(tx) {
439
- const details = {
440
- ...tx.details,
441
- nonce: (0, Utils_1.toHex)(tx.details.nonce),
442
- resourceBounds: (0, Utils_1.serializeResourceBounds)(tx.details.resourceBounds),
443
- tip: (0, Utils_1.toHex)(tx.details.tip),
444
- paymasterData: tx.details.paymasterData.map(val => (0, Utils_1.toHex)(val)),
445
- accountDeploymentData: tx.details.accountDeploymentData.map(val => (0, Utils_1.toHex)(val)),
446
- maxFee: tx.details.maxFee == null ? undefined : (0, Utils_1.toHex)(tx.details.maxFee)
447
- };
448
- if (isStarknetTxInvoke(tx)) {
449
- const calls = tx.tx.map(call => ({
450
- ...call,
451
- calldata: call.calldata == null ? [] : starknet_1.CallData.compile(call.calldata),
452
- }));
453
- const signed = tx.signed == null ? undefined : {
454
- ...tx.signed,
455
- resourceBounds: tx.signed.resourceBounds == null
456
- ? undefined
457
- : (0, Utils_1.serializeResourceBounds)(tx.signed.resourceBounds),
458
- calldata: tx.signed.calldata == null ? [] : starknet_1.CallData.compile(tx.signed.calldata),
459
- signature: (0, Utils_1.serializeSignature)(tx.signed.signature)
460
- };
461
- return JSON.stringify({
462
- type: tx.type,
463
- tx: calls,
464
- details,
465
- signed,
466
- txId: tx.txId
467
- });
468
- }
469
- else if (isStarknetTxDeployAccount(tx)) {
470
- const deployPaylod = {
471
- ...tx.tx,
472
- constructorCalldata: tx.tx.constructorCalldata == null ? [] : starknet_1.CallData.compile(tx.tx.constructorCalldata),
473
- addressSalt: (0, Utils_1.toHex)(tx.tx.addressSalt) ?? undefined
474
- };
475
- const signed = tx.signed == null ? undefined : {
476
- ...tx.signed,
477
- resourceBounds: tx.signed.resourceBounds == null
478
- ? undefined
479
- : (0, Utils_1.serializeResourceBounds)(tx.signed.resourceBounds),
480
- constructorCalldata: tx.tx.constructorCalldata == null ? [] : starknet_1.CallData.compile(tx.tx.constructorCalldata),
481
- addressSalt: (0, Utils_1.toHex)(tx.tx.addressSalt) ?? undefined,
482
- signature: (0, Utils_1.serializeSignature)(tx.signed.signature)
483
- };
484
- return JSON.stringify({
485
- type: tx.type,
486
- tx: deployPaylod,
487
- details,
488
- signed,
489
- txId: tx.txId
490
- });
491
- }
492
- else
493
- throw new Error(`Unknown transaction type: ${tx.type}`);
494
- }
495
- /**
496
- * Deserializes saved starknet transaction, extracting the transaction, signers & last valid blockheight
497
- *
498
- * @param txData
499
- */
500
- static deserializeTx(txData) {
501
- const _serializedTx = JSON.parse(txData, (key, value) => {
502
- //For backwards compatibility
503
- if (typeof (value) === "object" && value._type === "bigint")
504
- return value._value;
505
- return value;
506
- });
507
- const serializedDetails = _serializedTx.details;
508
- const details = {
509
- ...serializedDetails,
510
- resourceBounds: (0, Utils_1.deserializeResourceBounds)(serializedDetails.resourceBounds)
511
- };
512
- if (_serializedTx.type === "INVOKE") {
513
- const serializedSignedTx = _serializedTx.signed;
514
- const signed = serializedSignedTx == null ? undefined : {
515
- ...serializedSignedTx,
516
- signature: (0, Utils_1.deserializeSignature)(serializedSignedTx.signature)
517
- };
518
- const serializedCalls = _serializedTx.tx;
519
- const calls = serializedCalls;
520
- return {
521
- type: "INVOKE",
522
- tx: calls,
523
- details,
524
- signed,
525
- txId: _serializedTx.txId
526
- };
527
- }
528
- else if (_serializedTx.type === "DEPLOY_ACCOUNT") {
529
- const serializedSignedTx = _serializedTx.signed;
530
- const signed = serializedSignedTx == null ? undefined : {
531
- ...serializedSignedTx,
532
- signature: (0, Utils_1.deserializeSignature)(serializedSignedTx.signature)
533
- };
534
- const serializedPayload = _serializedTx.tx;
535
- const payload = serializedPayload;
536
- return {
537
- type: "DEPLOY_ACCOUNT",
538
- tx: payload,
539
- details,
540
- signed,
541
- txId: _serializedTx.txId
542
- };
543
- }
544
- else
545
- throw new Error(`Unknown transaction type: ${_serializedTx.type}`);
546
- }
547
- /**
548
- * Gets the status of the raw starknet transaction
549
- *
550
- * @param tx
551
- */
552
- async getTxStatus(tx) {
553
- const parsedTx = StarknetTransactions.deserializeTx(tx);
554
- if (parsedTx.txId == null)
555
- throw new Error("Expected signed transaction with txId field populated!");
556
- return await this.getTxIdStatus(parsedTx.txId);
557
- }
558
- /**
559
- * Gets the status of the starknet transaction with a specific txId
560
- *
561
- * @param txId
562
- */
563
- async _getTxIdStatus(txId) {
564
- const status = await this.provider.getTransactionStatus(txId).catch(e => {
565
- if (e.baseError?.code === 29 ||
566
- (e.message != null && e.message.includes("29: Transaction hash not found")))
567
- return null;
568
- throw e;
569
- });
570
- if (status == null)
571
- return this._knownTxSet.has(txId) ? "pending" : "not_found";
572
- // REJECTED status was removed in starknet.js v9 - transactions are now either accepted or reverted
573
- if (status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L2 && status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L1)
574
- return "pending";
575
- if (status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED) {
576
- return "success";
577
- }
578
- return "reverted";
579
- }
580
- /**
581
- * Gets the status of the starknet transaction with a specific txId
582
- *
583
- * @param txId
584
- */
585
- async getTxIdStatus(txId) {
586
- const status = await this._getTxIdStatus(txId);
587
- if (status === "rejected")
588
- return "reverted";
589
- return status;
590
- }
591
- onBeforeTxReplace(callback) {
592
- this._cbksBeforeTxReplace.push(callback);
593
- }
594
- offBeforeTxReplace(callback) {
595
- const index = this._cbksBeforeTxReplace.indexOf(callback);
596
- if (index === -1)
597
- return false;
598
- this._cbksBeforeTxReplace.splice(index, 1);
599
- return true;
600
- }
601
- onBeforeTxSigned(callback) {
602
- this.cbksBeforeTxSigned.push(callback);
603
- }
604
- offBeforeTxSigned(callback) {
605
- const index = this.cbksBeforeTxSigned.indexOf(callback);
606
- if (index === -1)
607
- return false;
608
- this.cbksBeforeTxSigned.splice(index, 1);
609
- return true;
610
- }
611
- }
612
- exports.StarknetTransactions = StarknetTransactions;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StarknetTransactions = exports.isStarknetTxDeployAccount = exports.isStarknetTxInvoke = void 0;
4
+ const StarknetModule_1 = require("../StarknetModule");
5
+ const starknet_1 = require("starknet");
6
+ const Utils_1 = require("../../../utils/Utils");
7
+ const base_1 = require("@atomiqlabs/base");
8
+ function isStarknetTxInvoke(obj) {
9
+ return typeof (obj) === "object" &&
10
+ typeof (obj.details) === "object" &&
11
+ (obj.txId == null || typeof (obj.txId) === "string") &&
12
+ obj.type === "INVOKE" &&
13
+ Array.isArray(obj.tx) &&
14
+ (obj.signed == null || typeof (obj.signed) === "object");
15
+ }
16
+ exports.isStarknetTxInvoke = isStarknetTxInvoke;
17
+ function isStarknetTxDeployAccount(obj) {
18
+ return typeof (obj) === "object" &&
19
+ typeof (obj.details) === "object" &&
20
+ (obj.txId == null || typeof (obj.txId) === "string") &&
21
+ obj.type === "DEPLOY_ACCOUNT" &&
22
+ typeof (obj.tx) === "object" &&
23
+ (obj.signed == null || typeof (obj.signed) === "object");
24
+ }
25
+ exports.isStarknetTxDeployAccount = isStarknetTxDeployAccount;
26
+ const MAX_UNCONFIRMED_TXS = 25;
27
+ class StarknetTransactions extends StarknetModule_1.StarknetModule {
28
+ constructor() {
29
+ super(...arguments);
30
+ this.latestConfirmedNonces = {};
31
+ this.latestPendingNonces = {};
32
+ this.latestSignedNonces = {};
33
+ this._cbksBeforeTxReplace = [];
34
+ this.cbksBeforeTxSigned = [];
35
+ this._knownTxSet = new Set();
36
+ }
37
+ sendTransaction(tx) {
38
+ if (tx.signed == null)
39
+ throw new Error("Cannot send unsigned transaction! signed field missing!");
40
+ switch (tx.type) {
41
+ case "INVOKE":
42
+ return this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
43
+ case "DEPLOY_ACCOUNT":
44
+ return this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
45
+ default:
46
+ throw new Error("Unsupported tx type!");
47
+ }
48
+ }
49
+ /**
50
+ * Returns the nonce of the account or 0, if the account is not deployed yet
51
+ *
52
+ * @param address
53
+ * @param blockTag
54
+ */
55
+ async getNonce(address, blockTag = starknet_1.BlockTag.PRE_CONFIRMED) {
56
+ try {
57
+ return BigInt(await this.provider.getNonceForAddress(address, blockTag));
58
+ }
59
+ catch (e) {
60
+ if (e.baseError?.code === 20 ||
61
+ (e.message != null && e.message.includes("20: Contract not found"))) {
62
+ return BigInt(0);
63
+ }
64
+ throw e;
65
+ }
66
+ }
67
+ async confirmTransactionWs(txId, abortSignal) {
68
+ if (this.root.wsChannel == null)
69
+ throw new Error("Underlying provider doesn't have a WS channel!");
70
+ const subscription = await this.root.wsChannel.subscribeTransactionStatus({
71
+ transactionHash: txId
72
+ });
73
+ const endSubscription = async () => {
74
+ if (this.root.wsChannel.isConnected() && await subscription.unsubscribe())
75
+ return;
76
+ this.root.wsChannel.removeSubscription(subscription.id);
77
+ };
78
+ if (abortSignal != null && abortSignal.aborted) {
79
+ await endSubscription();
80
+ abortSignal.throwIfAborted();
81
+ }
82
+ const status = await new Promise((resolve, reject) => {
83
+ if (abortSignal != null)
84
+ abortSignal.onabort = () => {
85
+ endSubscription().catch(err => this.logger.error("confirmTransactionWs(): End subscription error: ", err));
86
+ reject(abortSignal.reason);
87
+ };
88
+ subscription.on((data) => {
89
+ if (data.status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L2 && data.status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L1)
90
+ return; //No pre-confs
91
+ resolve(data.status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED ? "success" : "reverted");
92
+ });
93
+ });
94
+ await endSubscription();
95
+ this.logger.debug(`confirmTransactionWs(): Transaction ${txId} confirmed, transaction status: ${status}`);
96
+ return {
97
+ txId,
98
+ status
99
+ };
100
+ }
101
+ async confirmTransactionPolling(walletAddress, nonce, checkTxns, abortSignal) {
102
+ let state = "pending";
103
+ let confirmedTxId;
104
+ while (state === "pending") {
105
+ await (0, Utils_1.timeoutPromise)(3000, abortSignal);
106
+ const latestConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)];
107
+ const snapshot = [...checkTxns]; //Iterate over a snapshot
108
+ const totalTxnCount = snapshot.length;
109
+ let rejectedTxns = 0;
110
+ let notFoundTxns = 0;
111
+ for (let txId of snapshot) {
112
+ let _state = await this._getTxIdStatus(txId);
113
+ if (_state === "not_found")
114
+ notFoundTxns++;
115
+ if (_state === "rejected")
116
+ rejectedTxns++;
117
+ if (_state === "reverted" || _state === "success") {
118
+ confirmedTxId = txId;
119
+ state = _state;
120
+ break;
121
+ }
122
+ }
123
+ if (rejectedTxns === totalTxnCount) { //All rejected
124
+ state = "rejected";
125
+ break;
126
+ }
127
+ if (notFoundTxns === totalTxnCount) { //All not found, check the latest account nonce
128
+ if (latestConfirmedNonce != null && latestConfirmedNonce > nonce) {
129
+ //Confirmed nonce is already higher than the TX nonce, meaning the TX got replaced
130
+ throw new Error("Transaction failed - replaced!");
131
+ }
132
+ this.logger.warn("confirmTransaction(): All transactions not found, fetching the latest account nonce...");
133
+ const _latestConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)];
134
+ const currentLatestNonce = await this.getNonce(walletAddress, starknet_1.BlockTag.LATEST);
135
+ if (_latestConfirmedNonce == null || _latestConfirmedNonce < currentLatestNonce) {
136
+ this.latestConfirmedNonces[(0, Utils_1.toHex)(walletAddress)] = currentLatestNonce;
137
+ }
138
+ }
139
+ }
140
+ if (state !== "rejected")
141
+ this.logger.debug(`confirmTransactionPolling(): Transaction ${confirmedTxId} confirmed, transaction status: ${state}`);
142
+ return {
143
+ txId: confirmedTxId,
144
+ status: state
145
+ };
146
+ }
147
+ /**
148
+ * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
149
+ * the transaction at regular interval
150
+ *
151
+ * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
152
+ * @param abortSignal signal to abort waiting for tx confirmation
153
+ * @private
154
+ */
155
+ async confirmTransaction(tx, abortSignal) {
156
+ if (tx.txId == null)
157
+ throw new Error("txId is null!");
158
+ const abortController = new AbortController();
159
+ if (abortSignal != null)
160
+ abortSignal.onabort = () => abortController.abort(abortSignal.reason);
161
+ let txReplaceListener = undefined;
162
+ let result;
163
+ try {
164
+ result = await new Promise((resolve, reject) => {
165
+ const checkTxns = new Set([tx.txId]);
166
+ txReplaceListener = (oldTx, oldTxId, newTx, newTxId) => {
167
+ if (checkTxns.has(oldTxId))
168
+ checkTxns.add(newTxId);
169
+ //TODO: Enable this once WS subscriptions finally work (also unsubscribe should work!!!!)
170
+ // if(this.root.wsChannel!=null) this.confirmTransactionWs(newTxId, abortController.signal)
171
+ // .then(resolve)
172
+ // .catch(reject);
173
+ return Promise.resolve();
174
+ };
175
+ this.onBeforeTxReplace(txReplaceListener);
176
+ this.confirmTransactionPolling(tx.details.walletAddress, BigInt(tx.details.nonce), checkTxns, abortController.signal)
177
+ .then(resolve)
178
+ .catch(reject);
179
+ //TODO: Enable this once WS subscriptions finally work (also unsubscribe should work!!!!)
180
+ // if(this.root.wsChannel!=null) this.confirmTransactionWs(tx.txId!, abortController.signal)
181
+ // .then(resolve)
182
+ // .catch(reject);
183
+ });
184
+ if (txReplaceListener != null)
185
+ this.offBeforeTxReplace(txReplaceListener);
186
+ abortController.abort();
187
+ }
188
+ catch (e) {
189
+ if (txReplaceListener != null)
190
+ this.offBeforeTxReplace(txReplaceListener);
191
+ abortController.abort(e);
192
+ throw e;
193
+ }
194
+ if (result.status === "rejected")
195
+ throw new Error("Transaction rejected!");
196
+ const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
197
+ const currentConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
198
+ if (currentConfirmedNonce == null || nextAccountNonce > currentConfirmedNonce) {
199
+ this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
200
+ }
201
+ if (result.status === "reverted")
202
+ throw new base_1.TransactionRevertedError("Transaction reverted!");
203
+ return result.txId;
204
+ }
205
+ /**
206
+ * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
207
+ *
208
+ * @param signer
209
+ * @param txs
210
+ * @private
211
+ */
212
+ async prepareTransactions(signer, txs) {
213
+ let nonce = await this.getNonce(signer.getAddress());
214
+ const latestPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signer.getAddress())];
215
+ if (latestPendingNonce != null && latestPendingNonce > nonce) {
216
+ this.logger.debug("prepareTransactions(): Using 'pending' nonce from local cache!");
217
+ nonce = latestPendingNonce;
218
+ }
219
+ //Add deploy account tx
220
+ if (nonce === 0n) {
221
+ const deployPayload = await signer.getDeployPayload();
222
+ if (deployPayload != null) {
223
+ const tx = await this.root.Accounts.getAccountDeployTransaction(deployPayload);
224
+ tx.addedInPrepare = true;
225
+ txs.unshift(tx);
226
+ }
227
+ }
228
+ if (!signer.isManagingNoncesInternally) {
229
+ if (nonce === 0n) {
230
+ //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
231
+ nonce = 1n;
232
+ }
233
+ for (let i = 0; i < txs.length; i++) {
234
+ const tx = txs[i];
235
+ if (tx.details.nonce != null)
236
+ nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
237
+ if (nonce == null)
238
+ nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
239
+ if (tx.details.nonce == null)
240
+ tx.details.nonce = nonce;
241
+ this.logger.debug("prepareTransactions(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
242
+ nonce += BigInt(1);
243
+ }
244
+ }
245
+ for (let tx of txs) {
246
+ for (let callback of this.cbksBeforeTxSigned) {
247
+ await callback(tx);
248
+ }
249
+ }
250
+ }
251
+ /**
252
+ * Sends out a signed transaction to the RPC
253
+ *
254
+ * @param tx Starknet tx to send
255
+ * @param onBeforePublish a callback called before every transaction is published
256
+ * @private
257
+ */
258
+ async sendSignedTransaction(tx, onBeforePublish) {
259
+ if (tx.txId == null)
260
+ throw new Error("Expecting signed tx with txId field populated!");
261
+ if (onBeforePublish != null)
262
+ await onBeforePublish(tx.txId, StarknetTransactions.serializeTx(tx));
263
+ this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
264
+ const txResult = await this.sendTransaction(tx);
265
+ if (tx.txId !== txResult)
266
+ this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
267
+ this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
268
+ return txResult;
269
+ }
270
+ /**
271
+ * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
272
+ * of a batch of starknet transactions
273
+ *
274
+ * @param signer
275
+ * @param _txs transactions to send
276
+ * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
277
+ * are re-sent at regular intervals)
278
+ * @param abortSignal abort signal to abort waiting for transaction confirmations
279
+ * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
280
+ * are executed in order)
281
+ * @param onBeforePublish a callback called before every transaction is published
282
+ */
283
+ async sendAndConfirm(signer, _txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
284
+ const txs = _txs;
285
+ await this.prepareTransactions(signer, txs);
286
+ const signedTxs = [];
287
+ //Don't separate the signing process from the sending when using browser-based wallet
288
+ if (signer.signTransaction != null)
289
+ for (let i = 0; i < txs.length; i++) {
290
+ const tx = txs[i];
291
+ const signedTx = await signer.signTransaction(tx);
292
+ signedTx.addedInPrepare = tx.addedInPrepare;
293
+ signedTxs.push(signedTx);
294
+ this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
295
+ const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
296
+ const currentSignedNonce = this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
297
+ if (currentSignedNonce == null || nextAccountNonce > currentSignedNonce) {
298
+ this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
299
+ }
300
+ }
301
+ this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
302
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
303
+ const txIds = [];
304
+ if (parallel) {
305
+ let promises = [];
306
+ for (let i = 0; i < txs.length; i++) {
307
+ let tx;
308
+ if (signer.signTransaction == null) {
309
+ const txId = await signer.sendTransaction(txs[i], txs[i].addedInPrepare ? undefined : onBeforePublish);
310
+ tx = txs[i];
311
+ tx.txId = txId;
312
+ }
313
+ else {
314
+ const signedTx = signedTxs[i];
315
+ await this.sendSignedTransaction(signedTx, signedTx.addedInPrepare ? undefined : onBeforePublish);
316
+ tx = signedTx;
317
+ }
318
+ if (tx.details.nonce != null) {
319
+ const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
320
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
321
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
322
+ this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
323
+ }
324
+ }
325
+ if (!tx.addedInPrepare) {
326
+ promises.push(this.confirmTransaction(tx, abortSignal));
327
+ if (!waitForConfirmation)
328
+ txIds.push(tx.txId);
329
+ }
330
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
331
+ if (promises.length >= MAX_UNCONFIRMED_TXS) {
332
+ if (waitForConfirmation)
333
+ txIds.push(...await Promise.all(promises));
334
+ promises = [];
335
+ }
336
+ }
337
+ if (waitForConfirmation && promises.length > 0) {
338
+ txIds.push(...await Promise.all(promises));
339
+ }
340
+ }
341
+ else {
342
+ for (let i = 0; i < txs.length; i++) {
343
+ let tx;
344
+ if (signer.signTransaction == null) {
345
+ const txId = await signer.sendTransaction(txs[i], txs[i].addedInPrepare ? undefined : onBeforePublish);
346
+ tx = txs[i];
347
+ tx.txId = txId;
348
+ }
349
+ else {
350
+ const signedTx = signedTxs[i];
351
+ await this.sendSignedTransaction(signedTx, signedTx.addedInPrepare ? undefined : onBeforePublish);
352
+ tx = signedTx;
353
+ }
354
+ if (tx.details.nonce != null) {
355
+ const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
356
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
357
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
358
+ this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
359
+ }
360
+ }
361
+ const confirmPromise = this.confirmTransaction(tx, abortSignal);
362
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
363
+ //Don't await the last promise when !waitForConfirmation
364
+ let txHash = tx.txId;
365
+ if (i < txs.length - 1 || waitForConfirmation)
366
+ txHash = await confirmPromise;
367
+ if (!tx.addedInPrepare)
368
+ txIds.push(txHash);
369
+ }
370
+ }
371
+ this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
372
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
373
+ return txIds;
374
+ }
375
+ async sendSignedAndConfirm(signedTxs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
376
+ signedTxs.forEach(val => {
377
+ if (val.signed == null)
378
+ throw new Error("Transactions have to be signed!");
379
+ });
380
+ this.logger.debug("sendSignedAndConfirm(): sending transactions, count: " + signedTxs.length +
381
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
382
+ const txIds = [];
383
+ if (parallel) {
384
+ let promises = [];
385
+ for (let i = 0; i < signedTxs.length; i++) {
386
+ const signedTx = signedTxs[i];
387
+ await this.sendSignedTransaction(signedTx, onBeforePublish);
388
+ if (signedTx.details.nonce != null) {
389
+ const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
390
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
391
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
392
+ this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
393
+ }
394
+ }
395
+ promises.push(this.confirmTransaction(signedTx, abortSignal));
396
+ if (!waitForConfirmation)
397
+ txIds.push(signedTx.txId);
398
+ this.logger.debug("sendSignedAndConfirm(): transaction sent (" + (i + 1) + "/" + signedTxs.length + "): " + signedTx.txId);
399
+ if (promises.length >= MAX_UNCONFIRMED_TXS) {
400
+ if (waitForConfirmation)
401
+ txIds.push(...await Promise.all(promises));
402
+ promises = [];
403
+ }
404
+ }
405
+ if (waitForConfirmation && promises.length > 0) {
406
+ txIds.push(...await Promise.all(promises));
407
+ }
408
+ }
409
+ else {
410
+ for (let i = 0; i < signedTxs.length; i++) {
411
+ const signedTx = signedTxs[i];
412
+ await this.sendSignedTransaction(signedTx, onBeforePublish);
413
+ if (signedTx.details.nonce != null) {
414
+ const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
415
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
416
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
417
+ this.latestPendingNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
418
+ }
419
+ }
420
+ const confirmPromise = this.confirmTransaction(signedTx, abortSignal);
421
+ this.logger.debug("sendSignedAndConfirm(): transaction sent (" + (i + 1) + "/" + signedTxs.length + "): " + signedTx.txId);
422
+ //Don't await the last promise when !waitForConfirmation
423
+ let txHash = signedTx.txId;
424
+ if (i < signedTxs.length - 1 || waitForConfirmation)
425
+ txHash = await confirmPromise;
426
+ txIds.push(txHash);
427
+ }
428
+ }
429
+ this.logger.info("sendSignedAndConfirm(): sent transactions, count: " + signedTxs.length +
430
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
431
+ return txIds;
432
+ }
433
+ /**
434
+ * Serializes the starknet transaction, saves the transaction, signers & last valid blockheight
435
+ *
436
+ * @param tx
437
+ */
438
+ static serializeTx(tx) {
439
+ const details = {
440
+ ...tx.details,
441
+ nonce: (0, Utils_1.toHex)(tx.details.nonce),
442
+ resourceBounds: (0, Utils_1.serializeResourceBounds)(tx.details.resourceBounds),
443
+ tip: (0, Utils_1.toHex)(tx.details.tip),
444
+ paymasterData: tx.details.paymasterData.map(val => (0, Utils_1.toHex)(val)),
445
+ accountDeploymentData: tx.details.accountDeploymentData.map(val => (0, Utils_1.toHex)(val)),
446
+ maxFee: tx.details.maxFee == null ? undefined : (0, Utils_1.toHex)(tx.details.maxFee)
447
+ };
448
+ if (isStarknetTxInvoke(tx)) {
449
+ const calls = tx.tx.map(call => ({
450
+ ...call,
451
+ calldata: call.calldata == null ? [] : starknet_1.CallData.compile(call.calldata),
452
+ }));
453
+ const signed = tx.signed == null ? undefined : {
454
+ ...tx.signed,
455
+ resourceBounds: tx.signed.resourceBounds == null
456
+ ? undefined
457
+ : (0, Utils_1.serializeResourceBounds)(tx.signed.resourceBounds),
458
+ calldata: tx.signed.calldata == null ? [] : starknet_1.CallData.compile(tx.signed.calldata),
459
+ signature: (0, Utils_1.serializeSignature)(tx.signed.signature)
460
+ };
461
+ return JSON.stringify({
462
+ type: tx.type,
463
+ tx: calls,
464
+ details,
465
+ signed,
466
+ txId: tx.txId
467
+ });
468
+ }
469
+ else if (isStarknetTxDeployAccount(tx)) {
470
+ const deployPaylod = {
471
+ ...tx.tx,
472
+ constructorCalldata: tx.tx.constructorCalldata == null ? [] : starknet_1.CallData.compile(tx.tx.constructorCalldata),
473
+ addressSalt: (0, Utils_1.toHex)(tx.tx.addressSalt) ?? undefined
474
+ };
475
+ const signed = tx.signed == null ? undefined : {
476
+ ...tx.signed,
477
+ resourceBounds: tx.signed.resourceBounds == null
478
+ ? undefined
479
+ : (0, Utils_1.serializeResourceBounds)(tx.signed.resourceBounds),
480
+ constructorCalldata: tx.tx.constructorCalldata == null ? [] : starknet_1.CallData.compile(tx.tx.constructorCalldata),
481
+ addressSalt: (0, Utils_1.toHex)(tx.tx.addressSalt) ?? undefined,
482
+ signature: (0, Utils_1.serializeSignature)(tx.signed.signature)
483
+ };
484
+ return JSON.stringify({
485
+ type: tx.type,
486
+ tx: deployPaylod,
487
+ details,
488
+ signed,
489
+ txId: tx.txId
490
+ });
491
+ }
492
+ else
493
+ throw new Error(`Unknown transaction type: ${tx.type}`);
494
+ }
495
+ /**
496
+ * Deserializes saved starknet transaction, extracting the transaction, signers & last valid blockheight
497
+ *
498
+ * @param txData
499
+ */
500
+ static deserializeTx(txData) {
501
+ const _serializedTx = JSON.parse(txData, (key, value) => {
502
+ //For backwards compatibility
503
+ if (typeof (value) === "object" && value._type === "bigint")
504
+ return value._value;
505
+ return value;
506
+ });
507
+ const serializedDetails = _serializedTx.details;
508
+ const details = {
509
+ ...serializedDetails,
510
+ resourceBounds: (0, Utils_1.deserializeResourceBounds)(serializedDetails.resourceBounds)
511
+ };
512
+ if (_serializedTx.type === "INVOKE") {
513
+ const serializedSignedTx = _serializedTx.signed;
514
+ const signed = serializedSignedTx == null ? undefined : {
515
+ ...serializedSignedTx,
516
+ signature: (0, Utils_1.deserializeSignature)(serializedSignedTx.signature)
517
+ };
518
+ const serializedCalls = _serializedTx.tx;
519
+ const calls = serializedCalls;
520
+ return {
521
+ type: "INVOKE",
522
+ tx: calls,
523
+ details,
524
+ signed,
525
+ txId: _serializedTx.txId
526
+ };
527
+ }
528
+ else if (_serializedTx.type === "DEPLOY_ACCOUNT") {
529
+ const serializedSignedTx = _serializedTx.signed;
530
+ const signed = serializedSignedTx == null ? undefined : {
531
+ ...serializedSignedTx,
532
+ signature: (0, Utils_1.deserializeSignature)(serializedSignedTx.signature)
533
+ };
534
+ const serializedPayload = _serializedTx.tx;
535
+ const payload = serializedPayload;
536
+ return {
537
+ type: "DEPLOY_ACCOUNT",
538
+ tx: payload,
539
+ details,
540
+ signed,
541
+ txId: _serializedTx.txId
542
+ };
543
+ }
544
+ else
545
+ throw new Error(`Unknown transaction type: ${_serializedTx.type}`);
546
+ }
547
+ /**
548
+ * Gets the status of the raw starknet transaction
549
+ *
550
+ * @param tx
551
+ */
552
+ async getTxStatus(tx) {
553
+ const parsedTx = StarknetTransactions.deserializeTx(tx);
554
+ if (parsedTx.txId == null)
555
+ throw new Error("Expected signed transaction with txId field populated!");
556
+ return await this.getTxIdStatus(parsedTx.txId);
557
+ }
558
+ /**
559
+ * Gets the status of the starknet transaction with a specific txId
560
+ *
561
+ * @param txId
562
+ */
563
+ async _getTxIdStatus(txId) {
564
+ const status = await this.provider.getTransactionStatus(txId).catch(e => {
565
+ if (e.baseError?.code === 29 ||
566
+ (e.message != null && e.message.includes("29: Transaction hash not found")))
567
+ return null;
568
+ throw e;
569
+ });
570
+ if (status == null)
571
+ return this._knownTxSet.has(txId) ? "pending" : "not_found";
572
+ // REJECTED status was removed in starknet.js v9 - transactions are now either accepted or reverted
573
+ if (status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L2 && status.finality_status !== starknet_1.ETransactionStatus.ACCEPTED_ON_L1)
574
+ return "pending";
575
+ if (status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED) {
576
+ return "success";
577
+ }
578
+ return "reverted";
579
+ }
580
+ /**
581
+ * Gets the status of the starknet transaction with a specific txId
582
+ *
583
+ * @param txId
584
+ */
585
+ async getTxIdStatus(txId) {
586
+ const status = await this._getTxIdStatus(txId);
587
+ if (status === "rejected")
588
+ return "reverted";
589
+ return status;
590
+ }
591
+ async traceTransaction(txId, blockHash) {
592
+ let trace;
593
+ try {
594
+ trace = await this.provider.getTransactionTrace(txId);
595
+ }
596
+ catch (e) {
597
+ this.logger.warn("getSwapDataGetter(): getter: starknet_traceTransaction not supported by the RPC: ", e);
598
+ if (blockHash == null)
599
+ throw e;
600
+ const blockTraces = await this.provider.getBlockTransactionsTraces(blockHash);
601
+ const foundTrace = blockTraces.find(val => (0, Utils_1.toHex)(val.transaction_hash) === (0, Utils_1.toHex)(txId));
602
+ if (foundTrace == null)
603
+ throw new Error(`Cannot find ${txId} in the block traces, block: ${blockHash}`);
604
+ trace = foundTrace.trace_root;
605
+ }
606
+ if (trace == null)
607
+ return null;
608
+ if (trace.execute_invocation.revert_reason != null)
609
+ return null;
610
+ return trace.execute_invocation;
611
+ }
612
+ onBeforeTxReplace(callback) {
613
+ this._cbksBeforeTxReplace.push(callback);
614
+ }
615
+ offBeforeTxReplace(callback) {
616
+ const index = this._cbksBeforeTxReplace.indexOf(callback);
617
+ if (index === -1)
618
+ return false;
619
+ this._cbksBeforeTxReplace.splice(index, 1);
620
+ return true;
621
+ }
622
+ onBeforeTxSigned(callback) {
623
+ this.cbksBeforeTxSigned.push(callback);
624
+ }
625
+ offBeforeTxSigned(callback) {
626
+ const index = this.cbksBeforeTxSigned.indexOf(callback);
627
+ if (index === -1)
628
+ return false;
629
+ this.cbksBeforeTxSigned.splice(index, 1);
630
+ return true;
631
+ }
632
+ }
633
+ exports.StarknetTransactions = StarknetTransactions;