@atomiqlabs/chain-starknet 4.0.0-dev.12 → 4.0.0-dev.13

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