@atomiqlabs/chain-starknet 3.0.0-dev.8 → 4.0.0-dev.3

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 (146) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +38 -38
  3. package/dist/index.js +54 -54
  4. package/dist/starknet/StarknetChainType.d.ts +13 -13
  5. package/dist/starknet/StarknetChainType.js +2 -2
  6. package/dist/starknet/StarknetInitializer.d.ts +27 -19
  7. package/dist/starknet/StarknetInitializer.js +69 -69
  8. package/dist/starknet/btcrelay/BtcRelayAbi.d.ts +250 -250
  9. package/dist/starknet/btcrelay/BtcRelayAbi.js +341 -341
  10. package/dist/starknet/btcrelay/StarknetBtcRelay.d.ts +186 -186
  11. package/dist/starknet/btcrelay/StarknetBtcRelay.js +379 -379
  12. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.d.ts +31 -31
  13. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.js +74 -74
  14. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.d.ts +51 -51
  15. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.js +113 -113
  16. package/dist/starknet/chain/StarknetAction.d.ts +19 -19
  17. package/dist/starknet/chain/StarknetAction.js +73 -73
  18. package/dist/starknet/chain/StarknetChainInterface.d.ts +52 -52
  19. package/dist/starknet/chain/StarknetChainInterface.js +91 -91
  20. package/dist/starknet/chain/StarknetModule.d.ts +14 -14
  21. package/dist/starknet/chain/StarknetModule.js +13 -13
  22. package/dist/starknet/chain/modules/ERC20Abi.d.ts +755 -755
  23. package/dist/starknet/chain/modules/ERC20Abi.js +1032 -1032
  24. package/dist/starknet/chain/modules/StarknetAccounts.d.ts +6 -6
  25. package/dist/starknet/chain/modules/StarknetAccounts.js +24 -24
  26. package/dist/starknet/chain/modules/StarknetAddresses.d.ts +9 -9
  27. package/dist/starknet/chain/modules/StarknetAddresses.js +26 -26
  28. package/dist/starknet/chain/modules/StarknetBlocks.d.ts +20 -19
  29. package/dist/starknet/chain/modules/StarknetBlocks.js +64 -49
  30. package/dist/starknet/chain/modules/StarknetEvents.d.ts +44 -44
  31. package/dist/starknet/chain/modules/StarknetEvents.js +88 -88
  32. package/dist/starknet/chain/modules/StarknetFees.d.ts +77 -77
  33. package/dist/starknet/chain/modules/StarknetFees.js +114 -114
  34. package/dist/starknet/chain/modules/StarknetSignatures.d.ts +29 -29
  35. package/dist/starknet/chain/modules/StarknetSignatures.js +72 -72
  36. package/dist/starknet/chain/modules/StarknetTokens.d.ts +69 -69
  37. package/dist/starknet/chain/modules/StarknetTokens.js +98 -98
  38. package/dist/starknet/chain/modules/StarknetTransactions.d.ts +93 -93
  39. package/dist/starknet/chain/modules/StarknetTransactions.js +255 -255
  40. package/dist/starknet/contract/StarknetContractBase.d.ts +13 -13
  41. package/dist/starknet/contract/StarknetContractBase.js +16 -16
  42. package/dist/starknet/contract/StarknetContractModule.d.ts +8 -8
  43. package/dist/starknet/contract/StarknetContractModule.js +11 -11
  44. package/dist/starknet/contract/modules/StarknetContractEvents.d.ts +51 -51
  45. package/dist/starknet/contract/modules/StarknetContractEvents.js +97 -97
  46. package/dist/starknet/events/StarknetChainEvents.d.ts +21 -21
  47. package/dist/starknet/events/StarknetChainEvents.js +52 -52
  48. package/dist/starknet/events/StarknetChainEventsBrowser.d.ts +90 -91
  49. package/dist/starknet/events/StarknetChainEventsBrowser.js +292 -294
  50. package/dist/starknet/provider/RpcProviderWithRetries.d.ts +21 -21
  51. package/dist/starknet/provider/RpcProviderWithRetries.js +32 -32
  52. package/dist/starknet/spv_swap/SpvVaultContractAbi.d.ts +488 -488
  53. package/dist/starknet/spv_swap/SpvVaultContractAbi.js +656 -656
  54. package/dist/starknet/spv_swap/StarknetSpvVaultContract.d.ts +66 -65
  55. package/dist/starknet/spv_swap/StarknetSpvVaultContract.js +382 -376
  56. package/dist/starknet/spv_swap/StarknetSpvVaultData.d.ts +49 -49
  57. package/dist/starknet/spv_swap/StarknetSpvVaultData.js +145 -144
  58. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.d.ts +25 -24
  59. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.js +72 -61
  60. package/dist/starknet/swaps/EscrowManagerAbi.d.ts +434 -434
  61. package/dist/starknet/swaps/EscrowManagerAbi.js +587 -587
  62. package/dist/starknet/swaps/StarknetSwapContract.d.ts +190 -196
  63. package/dist/starknet/swaps/StarknetSwapContract.js +409 -395
  64. package/dist/starknet/swaps/StarknetSwapData.d.ts +67 -67
  65. package/dist/starknet/swaps/StarknetSwapData.js +290 -290
  66. package/dist/starknet/swaps/StarknetSwapModule.d.ts +10 -10
  67. package/dist/starknet/swaps/StarknetSwapModule.js +11 -11
  68. package/dist/starknet/swaps/handlers/IHandler.d.ts +13 -13
  69. package/dist/starknet/swaps/handlers/IHandler.js +2 -2
  70. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.d.ts +13 -13
  71. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.js +13 -13
  72. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.d.ts +21 -21
  73. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.js +44 -44
  74. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
  75. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +48 -48
  76. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
  77. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +40 -40
  78. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +20 -20
  79. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +30 -30
  80. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +45 -45
  81. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +52 -52
  82. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
  83. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.js +27 -27
  84. package/dist/starknet/swaps/modules/StarknetLpVault.d.ts +69 -69
  85. package/dist/starknet/swaps/modules/StarknetLpVault.js +122 -122
  86. package/dist/starknet/swaps/modules/StarknetSwapClaim.d.ts +53 -53
  87. package/dist/starknet/swaps/modules/StarknetSwapClaim.js +100 -100
  88. package/dist/starknet/swaps/modules/StarknetSwapInit.d.ts +86 -84
  89. package/dist/starknet/swaps/modules/StarknetSwapInit.js +224 -200
  90. package/dist/starknet/swaps/modules/StarknetSwapRefund.d.ts +62 -62
  91. package/dist/starknet/swaps/modules/StarknetSwapRefund.js +128 -128
  92. package/dist/starknet/wallet/StarknetKeypairWallet.d.ts +7 -7
  93. package/dist/starknet/wallet/StarknetKeypairWallet.js +30 -30
  94. package/dist/starknet/wallet/StarknetSigner.d.ts +12 -12
  95. package/dist/starknet/wallet/StarknetSigner.js +46 -46
  96. package/dist/utils/Utils.d.ts +37 -37
  97. package/dist/utils/Utils.js +261 -257
  98. package/package.json +37 -37
  99. package/src/index.ts +47 -47
  100. package/src/starknet/StarknetChainType.ts +28 -28
  101. package/src/starknet/StarknetInitializer.ts +108 -100
  102. package/src/starknet/btcrelay/BtcRelayAbi.ts +338 -338
  103. package/src/starknet/btcrelay/StarknetBtcRelay.ts +494 -494
  104. package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +100 -100
  105. package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +141 -141
  106. package/src/starknet/chain/StarknetAction.ts +85 -85
  107. package/src/starknet/chain/StarknetChainInterface.ts +149 -149
  108. package/src/starknet/chain/StarknetModule.ts +19 -19
  109. package/src/starknet/chain/modules/ERC20Abi.ts +1029 -1029
  110. package/src/starknet/chain/modules/StarknetAccounts.ts +25 -25
  111. package/src/starknet/chain/modules/StarknetAddresses.ts +22 -22
  112. package/src/starknet/chain/modules/StarknetBlocks.ts +74 -58
  113. package/src/starknet/chain/modules/StarknetEvents.ts +104 -104
  114. package/src/starknet/chain/modules/StarknetFees.ts +154 -154
  115. package/src/starknet/chain/modules/StarknetSignatures.ts +91 -91
  116. package/src/starknet/chain/modules/StarknetTokens.ts +116 -116
  117. package/src/starknet/chain/modules/StarknetTransactions.ts +277 -277
  118. package/src/starknet/contract/StarknetContractBase.ts +26 -26
  119. package/src/starknet/contract/StarknetContractModule.ts +16 -16
  120. package/src/starknet/contract/modules/StarknetContractEvents.ts +134 -134
  121. package/src/starknet/events/StarknetChainEvents.ts +67 -67
  122. package/src/starknet/events/StarknetChainEventsBrowser.ts +410 -412
  123. package/src/starknet/provider/RpcProviderWithRetries.ts +43 -43
  124. package/src/starknet/spv_swap/SpvVaultContractAbi.ts +656 -656
  125. package/src/starknet/spv_swap/StarknetSpvVaultContract.ts +483 -477
  126. package/src/starknet/spv_swap/StarknetSpvVaultData.ts +195 -194
  127. package/src/starknet/spv_swap/StarknetSpvWithdrawalData.ts +79 -68
  128. package/src/starknet/swaps/EscrowManagerAbi.ts +586 -586
  129. package/src/starknet/swaps/StarknetSwapContract.ts +628 -605
  130. package/src/starknet/swaps/StarknetSwapData.ts +403 -403
  131. package/src/starknet/swaps/StarknetSwapModule.ts +17 -17
  132. package/src/starknet/swaps/handlers/IHandler.ts +20 -20
  133. package/src/starknet/swaps/handlers/claim/ClaimHandlers.ts +23 -23
  134. package/src/starknet/swaps/handlers/claim/HashlockClaimHandler.ts +53 -53
  135. package/src/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +73 -73
  136. package/src/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +67 -67
  137. package/src/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +50 -50
  138. package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +102 -102
  139. package/src/starknet/swaps/handlers/refund/TimelockRefundHandler.ts +38 -38
  140. package/src/starknet/swaps/modules/StarknetLpVault.ts +147 -147
  141. package/src/starknet/swaps/modules/StarknetSwapClaim.ts +141 -141
  142. package/src/starknet/swaps/modules/StarknetSwapInit.ts +286 -260
  143. package/src/starknet/swaps/modules/StarknetSwapRefund.ts +196 -196
  144. package/src/starknet/wallet/StarknetKeypairWallet.ts +39 -39
  145. package/src/starknet/wallet/StarknetSigner.ts +55 -55
  146. package/src/utils/Utils.ts +252 -248
@@ -1,255 +1,255 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StarknetTransactions = void 0;
4
- const StarknetModule_1 = require("../StarknetModule");
5
- const Utils_1 = require("../../../utils/Utils");
6
- class StarknetTransactions extends StarknetModule_1.StarknetModule {
7
- constructor() {
8
- super(...arguments);
9
- this.latestConfirmedNonces = {};
10
- }
11
- /**
12
- * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
13
- * the transaction at regular interval
14
- *
15
- * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
16
- * @param abortSignal signal to abort waiting for tx confirmation
17
- * @private
18
- */
19
- async confirmTransaction(tx, abortSignal) {
20
- let state = "pending";
21
- while (state === "pending" || state === "not_found") {
22
- await (0, Utils_1.timeoutPromise)(3000, abortSignal);
23
- state = await this._getTxIdStatus(tx.txId);
24
- if (state === "not_found" && tx.signed != null)
25
- await this.sendSignedTransaction(tx).catch(e => {
26
- if (e.baseError?.code === 59)
27
- return; //Transaction already in the mempool
28
- console.error("Error on transaction re-send: ", e);
29
- });
30
- }
31
- if (state === "rejected")
32
- throw new Error("Transaction rejected!");
33
- const nextAccountNonce = (0, Utils_1.toBigInt)(tx.details.nonce) + 1n;
34
- const currentNonce = this.latestConfirmedNonces[tx.details.walletAddress];
35
- if (currentNonce == null || nextAccountNonce > currentNonce) {
36
- this.latestConfirmedNonces[tx.details.walletAddress] = nextAccountNonce;
37
- }
38
- if (state === "reverted")
39
- throw new Error("Transaction reverted!");
40
- }
41
- /**
42
- * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
43
- *
44
- * @param signer
45
- * @param txs
46
- * @private
47
- */
48
- async prepareTransactions(signer, txs) {
49
- let nonce = await signer.getNonce();
50
- const latestConfirmedNonce = this.latestConfirmedNonces[signer.getAddress()];
51
- if (latestConfirmedNonce != null && latestConfirmedNonce > nonce) {
52
- console.debug("StarknetTransactions: prepareTransactions(): Using nonce from local cache!");
53
- nonce = latestConfirmedNonce;
54
- }
55
- if (nonce === BigInt(0) && signer.isWalletAccount()) {
56
- //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
57
- nonce = BigInt(1);
58
- }
59
- const deployPayload = await signer.checkAndGetDeployPayload(nonce);
60
- if (deployPayload != null) {
61
- txs.unshift(await this.root.Accounts.getAccountDeployTransaction(deployPayload));
62
- }
63
- for (let i = 0; i < txs.length; i++) {
64
- const tx = txs[i];
65
- if (tx.details.nonce != null)
66
- nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
67
- if (nonce == null)
68
- nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
69
- if (tx.details.nonce == null)
70
- tx.details.nonce = nonce;
71
- this.logger.debug("sendAndConfirm(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
72
- nonce += BigInt(1);
73
- if (this.cbkBeforeTxSigned != null)
74
- await this.cbkBeforeTxSigned(tx);
75
- }
76
- }
77
- /**
78
- * Sends out a signed transaction to the RPC
79
- *
80
- * @param tx Starknet tx to send
81
- * @param onBeforePublish a callback called before every transaction is published
82
- * @param signer
83
- * @private
84
- */
85
- async sendSignedTransaction(tx, onBeforePublish, signer) {
86
- if (onBeforePublish != null)
87
- await onBeforePublish(tx.txId, await this.serializeTx(tx));
88
- this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
89
- if (tx.signed == null) {
90
- let txHash;
91
- switch (tx.type) {
92
- case "INVOKE":
93
- txHash = (await signer.account.execute(tx.tx, tx.details)).transaction_hash;
94
- break;
95
- case "DEPLOY_ACCOUNT":
96
- txHash = (await signer.account.deployAccount(tx.tx, tx.details)).transaction_hash;
97
- break;
98
- default:
99
- throw new Error("Unsupported tx type!");
100
- }
101
- tx.txId = txHash;
102
- return txHash;
103
- }
104
- let txResult;
105
- switch (tx.type) {
106
- case "INVOKE":
107
- txResult = await this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
108
- break;
109
- case "DEPLOY_ACCOUNT":
110
- txResult = await this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
111
- break;
112
- default:
113
- throw new Error("Unsupported tx type!");
114
- }
115
- if (tx.txId !== txResult)
116
- this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
117
- this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
118
- return txResult;
119
- }
120
- /**
121
- * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
122
- * of a batch of starknet transactions
123
- *
124
- * @param signer
125
- * @param txs transactions to send
126
- * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
127
- * are re-sent at regular intervals)
128
- * @param abortSignal abort signal to abort waiting for transaction confirmations
129
- * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
130
- * are executed in order)
131
- * @param onBeforePublish a callback called before every transaction is published
132
- */
133
- async sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
134
- await this.prepareTransactions(signer, txs);
135
- if (!signer.isWalletAccount()) {
136
- for (let i = 0; i < txs.length; i++) {
137
- const tx = txs[i];
138
- switch (tx.type) {
139
- case "INVOKE":
140
- tx.signed = await signer.account.buildInvocation(tx.tx, tx.details);
141
- (0, Utils_1.calculateHash)(tx);
142
- break;
143
- case "DEPLOY_ACCOUNT":
144
- tx.signed = await signer.account.buildAccountDeployPayload(tx.tx, tx.details);
145
- (0, Utils_1.calculateHash)(tx);
146
- break;
147
- default:
148
- throw new Error("Unsupported tx type!");
149
- }
150
- this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
151
- }
152
- }
153
- this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
154
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
155
- const txIds = [];
156
- if (parallel) {
157
- const promises = [];
158
- for (let i = 0; i < txs.length; i++) {
159
- const signedTx = txs[i];
160
- const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
161
- if (waitForConfirmation)
162
- promises.push(this.confirmTransaction(signedTx, abortSignal));
163
- txIds.push(txId);
164
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
165
- }
166
- if (promises.length > 0)
167
- await Promise.all(promises);
168
- }
169
- else {
170
- for (let i = 0; i < txs.length; i++) {
171
- const signedTx = txs[i];
172
- const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
173
- const confirmPromise = this.confirmTransaction(signedTx, abortSignal);
174
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
175
- //Don't await the last promise when !waitForConfirmation
176
- if (i < txs.length - 1 || waitForConfirmation)
177
- await confirmPromise;
178
- txIds.push(txId);
179
- }
180
- }
181
- this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
182
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
183
- return txIds;
184
- }
185
- /**
186
- * Serializes the solana transaction, saves the transaction, signers & last valid blockheight
187
- *
188
- * @param tx
189
- */
190
- serializeTx(tx) {
191
- return Promise.resolve(JSON.stringify(tx, (key, value) => {
192
- if (typeof (value) === "bigint")
193
- return (0, Utils_1.toHex)(value);
194
- return value;
195
- }));
196
- }
197
- /**
198
- * Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
199
- *
200
- * @param txData
201
- */
202
- deserializeTx(txData) {
203
- return Promise.resolve(JSON.parse(txData));
204
- }
205
- /**
206
- * Gets the status of the raw starknet transaction
207
- *
208
- * @param tx
209
- */
210
- async getTxStatus(tx) {
211
- const parsedTx = await this.deserializeTx(tx);
212
- return await this.getTxIdStatus(parsedTx.txId);
213
- }
214
- /**
215
- * Gets the status of the starknet transaction with a specific txId
216
- *
217
- * @param txId
218
- */
219
- async _getTxIdStatus(txId) {
220
- const status = await this.provider.getTransactionStatus(txId).catch(e => {
221
- if (e.message != null && e.message.includes("29: Transaction hash not found"))
222
- return null;
223
- throw e;
224
- });
225
- if (status == null)
226
- return "not_found";
227
- if (status.finality_status === "RECEIVED")
228
- return "pending";
229
- if (status.finality_status === "REJECTED")
230
- return "rejected";
231
- if (status.execution_status === "SUCCEEDED") {
232
- return "success";
233
- }
234
- return "reverted";
235
- }
236
- /**
237
- * Gets the status of the starknet transaction with a specific txId
238
- *
239
- * @param txId
240
- */
241
- async getTxIdStatus(txId) {
242
- const status = await this._getTxIdStatus(txId);
243
- if (status === "rejected")
244
- return "reverted";
245
- return status;
246
- }
247
- onBeforeTxSigned(callback) {
248
- this.cbkBeforeTxSigned = callback;
249
- }
250
- offBeforeTxSigned(callback) {
251
- this.cbkBeforeTxSigned = null;
252
- return true;
253
- }
254
- }
255
- exports.StarknetTransactions = StarknetTransactions;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StarknetTransactions = void 0;
4
+ const StarknetModule_1 = require("../StarknetModule");
5
+ const Utils_1 = require("../../../utils/Utils");
6
+ class StarknetTransactions extends StarknetModule_1.StarknetModule {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.latestConfirmedNonces = {};
10
+ }
11
+ /**
12
+ * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
13
+ * the transaction at regular interval
14
+ *
15
+ * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
16
+ * @param abortSignal signal to abort waiting for tx confirmation
17
+ * @private
18
+ */
19
+ async confirmTransaction(tx, abortSignal) {
20
+ let state = "pending";
21
+ while (state === "pending" || state === "not_found") {
22
+ await (0, Utils_1.timeoutPromise)(3000, abortSignal);
23
+ state = await this._getTxIdStatus(tx.txId);
24
+ if (state === "not_found" && tx.signed != null)
25
+ await this.sendSignedTransaction(tx).catch(e => {
26
+ if (e.baseError?.code === 59)
27
+ return; //Transaction already in the mempool
28
+ this.logger.error("confirmTransaction(): Error on transaction re-send: ", e);
29
+ });
30
+ }
31
+ if (state === "rejected")
32
+ throw new Error("Transaction rejected!");
33
+ const nextAccountNonce = (0, Utils_1.toBigInt)(tx.details.nonce) + 1n;
34
+ const currentNonce = this.latestConfirmedNonces[tx.details.walletAddress];
35
+ if (currentNonce == null || nextAccountNonce > currentNonce) {
36
+ this.latestConfirmedNonces[tx.details.walletAddress] = nextAccountNonce;
37
+ }
38
+ if (state === "reverted")
39
+ throw new Error("Transaction reverted!");
40
+ }
41
+ /**
42
+ * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
43
+ *
44
+ * @param signer
45
+ * @param txs
46
+ * @private
47
+ */
48
+ async prepareTransactions(signer, txs) {
49
+ let nonce = await signer.getNonce();
50
+ const latestConfirmedNonce = this.latestConfirmedNonces[signer.getAddress()];
51
+ if (latestConfirmedNonce != null && latestConfirmedNonce > nonce) {
52
+ this.logger.debug("prepareTransactions(): Using nonce from local cache!");
53
+ nonce = latestConfirmedNonce;
54
+ }
55
+ if (nonce === BigInt(0) && signer.isWalletAccount()) {
56
+ //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
57
+ nonce = BigInt(1);
58
+ }
59
+ const deployPayload = await signer.checkAndGetDeployPayload(nonce);
60
+ if (deployPayload != null) {
61
+ txs.unshift(await this.root.Accounts.getAccountDeployTransaction(deployPayload));
62
+ }
63
+ for (let i = 0; i < txs.length; i++) {
64
+ const tx = txs[i];
65
+ if (tx.details.nonce != null)
66
+ nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
67
+ if (nonce == null)
68
+ nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
69
+ if (tx.details.nonce == null)
70
+ tx.details.nonce = nonce;
71
+ this.logger.debug("sendAndConfirm(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
72
+ nonce += BigInt(1);
73
+ if (this.cbkBeforeTxSigned != null)
74
+ await this.cbkBeforeTxSigned(tx);
75
+ }
76
+ }
77
+ /**
78
+ * Sends out a signed transaction to the RPC
79
+ *
80
+ * @param tx Starknet tx to send
81
+ * @param onBeforePublish a callback called before every transaction is published
82
+ * @param signer
83
+ * @private
84
+ */
85
+ async sendSignedTransaction(tx, onBeforePublish, signer) {
86
+ if (onBeforePublish != null)
87
+ await onBeforePublish(tx.txId, await this.serializeTx(tx));
88
+ this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
89
+ if (tx.signed == null) {
90
+ let txHash;
91
+ switch (tx.type) {
92
+ case "INVOKE":
93
+ txHash = (await signer.account.execute(tx.tx, tx.details)).transaction_hash;
94
+ break;
95
+ case "DEPLOY_ACCOUNT":
96
+ txHash = (await signer.account.deployAccount(tx.tx, tx.details)).transaction_hash;
97
+ break;
98
+ default:
99
+ throw new Error("Unsupported tx type!");
100
+ }
101
+ tx.txId = txHash;
102
+ return txHash;
103
+ }
104
+ let txResult;
105
+ switch (tx.type) {
106
+ case "INVOKE":
107
+ txResult = await this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
108
+ break;
109
+ case "DEPLOY_ACCOUNT":
110
+ txResult = await this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
111
+ break;
112
+ default:
113
+ throw new Error("Unsupported tx type!");
114
+ }
115
+ if (tx.txId !== txResult)
116
+ this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
117
+ this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
118
+ return txResult;
119
+ }
120
+ /**
121
+ * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
122
+ * of a batch of starknet transactions
123
+ *
124
+ * @param signer
125
+ * @param txs transactions to send
126
+ * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
127
+ * are re-sent at regular intervals)
128
+ * @param abortSignal abort signal to abort waiting for transaction confirmations
129
+ * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
130
+ * are executed in order)
131
+ * @param onBeforePublish a callback called before every transaction is published
132
+ */
133
+ async sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
134
+ await this.prepareTransactions(signer, txs);
135
+ if (!signer.isWalletAccount()) {
136
+ for (let i = 0; i < txs.length; i++) {
137
+ const tx = txs[i];
138
+ switch (tx.type) {
139
+ case "INVOKE":
140
+ tx.signed = await signer.account.buildInvocation(tx.tx, tx.details);
141
+ (0, Utils_1.calculateHash)(tx);
142
+ break;
143
+ case "DEPLOY_ACCOUNT":
144
+ tx.signed = await signer.account.buildAccountDeployPayload(tx.tx, tx.details);
145
+ (0, Utils_1.calculateHash)(tx);
146
+ break;
147
+ default:
148
+ throw new Error("Unsupported tx type!");
149
+ }
150
+ this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
151
+ }
152
+ }
153
+ this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
154
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
155
+ const txIds = [];
156
+ if (parallel) {
157
+ const promises = [];
158
+ for (let i = 0; i < txs.length; i++) {
159
+ const signedTx = txs[i];
160
+ const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
161
+ if (waitForConfirmation)
162
+ promises.push(this.confirmTransaction(signedTx, abortSignal));
163
+ txIds.push(txId);
164
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
165
+ }
166
+ if (promises.length > 0)
167
+ await Promise.all(promises);
168
+ }
169
+ else {
170
+ for (let i = 0; i < txs.length; i++) {
171
+ const signedTx = txs[i];
172
+ const txId = await this.sendSignedTransaction(signedTx, onBeforePublish, signer);
173
+ const confirmPromise = this.confirmTransaction(signedTx, abortSignal);
174
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
175
+ //Don't await the last promise when !waitForConfirmation
176
+ if (i < txs.length - 1 || waitForConfirmation)
177
+ await confirmPromise;
178
+ txIds.push(txId);
179
+ }
180
+ }
181
+ this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
182
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
183
+ return txIds;
184
+ }
185
+ /**
186
+ * Serializes the solana transaction, saves the transaction, signers & last valid blockheight
187
+ *
188
+ * @param tx
189
+ */
190
+ serializeTx(tx) {
191
+ return Promise.resolve(JSON.stringify(tx, (key, value) => {
192
+ if (typeof (value) === "bigint")
193
+ return (0, Utils_1.toHex)(value);
194
+ return value;
195
+ }));
196
+ }
197
+ /**
198
+ * Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
199
+ *
200
+ * @param txData
201
+ */
202
+ deserializeTx(txData) {
203
+ return Promise.resolve(JSON.parse(txData));
204
+ }
205
+ /**
206
+ * Gets the status of the raw starknet transaction
207
+ *
208
+ * @param tx
209
+ */
210
+ async getTxStatus(tx) {
211
+ const parsedTx = await this.deserializeTx(tx);
212
+ return await this.getTxIdStatus(parsedTx.txId);
213
+ }
214
+ /**
215
+ * Gets the status of the starknet transaction with a specific txId
216
+ *
217
+ * @param txId
218
+ */
219
+ async _getTxIdStatus(txId) {
220
+ const status = await this.provider.getTransactionStatus(txId).catch(e => {
221
+ if (e.message != null && e.message.includes("29: Transaction hash not found"))
222
+ return null;
223
+ throw e;
224
+ });
225
+ if (status == null)
226
+ return "not_found";
227
+ if (status.finality_status === "RECEIVED")
228
+ return "pending";
229
+ if (status.finality_status === "REJECTED")
230
+ return "rejected";
231
+ if (status.execution_status === "SUCCEEDED") {
232
+ return "success";
233
+ }
234
+ return "reverted";
235
+ }
236
+ /**
237
+ * Gets the status of the starknet transaction with a specific txId
238
+ *
239
+ * @param txId
240
+ */
241
+ async getTxIdStatus(txId) {
242
+ const status = await this._getTxIdStatus(txId);
243
+ if (status === "rejected")
244
+ return "reverted";
245
+ return status;
246
+ }
247
+ onBeforeTxSigned(callback) {
248
+ this.cbkBeforeTxSigned = callback;
249
+ }
250
+ offBeforeTxSigned(callback) {
251
+ this.cbkBeforeTxSigned = null;
252
+ return true;
253
+ }
254
+ }
255
+ exports.StarknetTransactions = StarknetTransactions;
@@ -1,13 +1,13 @@
1
- import { StarknetChainInterface } from "../chain/StarknetChainInterface";
2
- import { TypedContractV2 } from "starknet";
3
- import { Abi } from "abi-wan-kanabi";
4
- import { StarknetContractEvents } from "./modules/StarknetContractEvents";
5
- /**
6
- * Base class providing program specific utilities
7
- */
8
- export declare class StarknetContractBase<T extends Abi> {
9
- contract: TypedContractV2<T>;
10
- readonly Events: StarknetContractEvents<T>;
11
- readonly Chain: StarknetChainInterface;
12
- constructor(chainInterface: StarknetChainInterface, contractAddress: string, contractAbi: T);
13
- }
1
+ import { StarknetChainInterface } from "../chain/StarknetChainInterface";
2
+ import { TypedContractV2 } from "starknet";
3
+ import { Abi } from "abi-wan-kanabi";
4
+ import { StarknetContractEvents } from "./modules/StarknetContractEvents";
5
+ /**
6
+ * Base class providing program specific utilities
7
+ */
8
+ export declare class StarknetContractBase<T extends Abi> {
9
+ contract: TypedContractV2<T>;
10
+ readonly Events: StarknetContractEvents<T>;
11
+ readonly Chain: StarknetChainInterface;
12
+ constructor(chainInterface: StarknetChainInterface, contractAddress: string, contractAbi: T);
13
+ }
@@ -1,16 +1,16 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StarknetContractBase = void 0;
4
- const starknet_1 = require("starknet");
5
- const StarknetContractEvents_1 = require("./modules/StarknetContractEvents");
6
- /**
7
- * Base class providing program specific utilities
8
- */
9
- class StarknetContractBase {
10
- constructor(chainInterface, contractAddress, contractAbi) {
11
- this.Chain = chainInterface;
12
- this.contract = new starknet_1.Contract(contractAbi, contractAddress, chainInterface.provider).typedv2(contractAbi);
13
- this.Events = new StarknetContractEvents_1.StarknetContractEvents(chainInterface, this, contractAbi);
14
- }
15
- }
16
- exports.StarknetContractBase = StarknetContractBase;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StarknetContractBase = void 0;
4
+ const starknet_1 = require("starknet");
5
+ const StarknetContractEvents_1 = require("./modules/StarknetContractEvents");
6
+ /**
7
+ * Base class providing program specific utilities
8
+ */
9
+ class StarknetContractBase {
10
+ constructor(chainInterface, contractAddress, contractAbi) {
11
+ this.Chain = chainInterface;
12
+ this.contract = new starknet_1.Contract(contractAbi, contractAddress, chainInterface.provider).typedv2(contractAbi);
13
+ this.Events = new StarknetContractEvents_1.StarknetContractEvents(chainInterface, this, contractAbi);
14
+ }
15
+ }
16
+ exports.StarknetContractBase = StarknetContractBase;
@@ -1,8 +1,8 @@
1
- import { Abi } from "abi-wan-kanabi";
2
- import { StarknetContractBase } from "./StarknetContractBase";
3
- import { StarknetChainInterface } from "../chain/StarknetChainInterface";
4
- import { StarknetModule } from "../chain/StarknetModule";
5
- export declare class StarknetContractModule<TAbi extends Abi> extends StarknetModule {
6
- readonly contract: StarknetContractBase<TAbi>;
7
- constructor(chainInterface: StarknetChainInterface, contract: StarknetContractBase<TAbi>);
8
- }
1
+ import { Abi } from "abi-wan-kanabi";
2
+ import { StarknetContractBase } from "./StarknetContractBase";
3
+ import { StarknetChainInterface } from "../chain/StarknetChainInterface";
4
+ import { StarknetModule } from "../chain/StarknetModule";
5
+ export declare class StarknetContractModule<TAbi extends Abi> extends StarknetModule {
6
+ readonly contract: StarknetContractBase<TAbi>;
7
+ constructor(chainInterface: StarknetChainInterface, contract: StarknetContractBase<TAbi>);
8
+ }
@@ -1,11 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StarknetContractModule = void 0;
4
- const StarknetModule_1 = require("../chain/StarknetModule");
5
- class StarknetContractModule extends StarknetModule_1.StarknetModule {
6
- constructor(chainInterface, contract) {
7
- super(chainInterface);
8
- this.contract = contract;
9
- }
10
- }
11
- exports.StarknetContractModule = StarknetContractModule;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StarknetContractModule = void 0;
4
+ const StarknetModule_1 = require("../chain/StarknetModule");
5
+ class StarknetContractModule extends StarknetModule_1.StarknetModule {
6
+ constructor(chainInterface, contract) {
7
+ super(chainInterface);
8
+ this.contract = contract;
9
+ }
10
+ }
11
+ exports.StarknetContractModule = StarknetContractModule;