@atomiqlabs/chain-starknet 4.0.0-dev.26 → 4.0.0-dev.28

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 (154) hide show
  1. package/LICENSE +201 -201
  2. package/dist/index.d.ts +39 -39
  3. package/dist/index.js +55 -55
  4. package/dist/starknet/StarknetChainType.d.ts +13 -13
  5. package/dist/starknet/StarknetChainType.js +2 -2
  6. package/dist/starknet/StarknetInitializer.d.ts +28 -28
  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 +54 -54
  19. package/dist/starknet/chain/StarknetChainInterface.js +97 -97
  20. package/dist/starknet/chain/StarknetModule.d.ts +9 -9
  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 +82 -82
  33. package/dist/starknet/chain/modules/StarknetFees.js +121 -121
  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 -102
  38. package/dist/starknet/chain/modules/StarknetTransactions.d.ts +111 -110
  39. package/dist/starknet/chain/modules/StarknetTransactions.js +359 -351
  40. package/dist/starknet/contract/StarknetContractBase.d.ts +13 -13
  41. package/dist/starknet/contract/StarknetContractBase.js +20 -20
  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 +84 -84
  49. package/dist/starknet/events/StarknetChainEventsBrowser.js +307 -307
  50. package/dist/starknet/provider/RpcProviderWithRetries.d.ts +41 -41
  51. package/dist/starknet/provider/RpcProviderWithRetries.js +70 -70
  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 +89 -89
  55. package/dist/starknet/spv_swap/StarknetSpvVaultContract.js +477 -477
  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 +197 -197
  63. package/dist/starknet/swaps/StarknetSwapContract.js +440 -440
  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 -94
  89. package/dist/starknet/swaps/modules/StarknetSwapInit.js +235 -235
  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/StarknetBrowserSigner.d.ts +5 -5
  93. package/dist/starknet/wallet/StarknetBrowserSigner.js +11 -11
  94. package/dist/starknet/wallet/StarknetKeypairWallet.d.ts +7 -7
  95. package/dist/starknet/wallet/StarknetKeypairWallet.js +35 -35
  96. package/dist/starknet/wallet/StarknetPersistentSigner.d.ts +33 -33
  97. package/dist/starknet/wallet/StarknetPersistentSigner.js +248 -243
  98. package/dist/starknet/wallet/StarknetSigner.d.ts +18 -18
  99. package/dist/starknet/wallet/StarknetSigner.js +69 -69
  100. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.d.ts +7 -7
  101. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.js +35 -35
  102. package/dist/utils/Utils.d.ts +39 -39
  103. package/dist/utils/Utils.js +264 -264
  104. package/package.json +45 -45
  105. package/src/index.ts +48 -48
  106. package/src/starknet/StarknetChainType.ts +28 -28
  107. package/src/starknet/StarknetInitializer.ts +110 -110
  108. package/src/starknet/btcrelay/BtcRelayAbi.ts +338 -338
  109. package/src/starknet/btcrelay/StarknetBtcRelay.ts +494 -494
  110. package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +100 -100
  111. package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +141 -141
  112. package/src/starknet/chain/StarknetAction.ts +85 -85
  113. package/src/starknet/chain/StarknetChainInterface.ts +165 -165
  114. package/src/starknet/chain/StarknetModule.ts +19 -19
  115. package/src/starknet/chain/modules/ERC20Abi.ts +1029 -1029
  116. package/src/starknet/chain/modules/StarknetAccounts.ts +25 -25
  117. package/src/starknet/chain/modules/StarknetAddresses.ts +22 -22
  118. package/src/starknet/chain/modules/StarknetBlocks.ts +75 -75
  119. package/src/starknet/chain/modules/StarknetEvents.ts +104 -104
  120. package/src/starknet/chain/modules/StarknetFees.ts +162 -162
  121. package/src/starknet/chain/modules/StarknetSignatures.ts +91 -91
  122. package/src/starknet/chain/modules/StarknetTokens.ts +120 -120
  123. package/src/starknet/chain/modules/StarknetTransactions.ts +399 -392
  124. package/src/starknet/contract/StarknetContractBase.ts +30 -30
  125. package/src/starknet/contract/StarknetContractModule.ts +16 -16
  126. package/src/starknet/contract/modules/StarknetContractEvents.ts +134 -134
  127. package/src/starknet/events/StarknetChainEvents.ts +67 -67
  128. package/src/starknet/events/StarknetChainEventsBrowser.ts +420 -420
  129. package/src/starknet/provider/RpcProviderWithRetries.ts +83 -83
  130. package/src/starknet/spv_swap/SpvVaultContractAbi.ts +656 -656
  131. package/src/starknet/spv_swap/StarknetSpvVaultContract.ts +591 -591
  132. package/src/starknet/spv_swap/StarknetSpvVaultData.ts +195 -195
  133. package/src/starknet/spv_swap/StarknetSpvWithdrawalData.ts +79 -79
  134. package/src/starknet/swaps/EscrowManagerAbi.ts +582 -582
  135. package/src/starknet/swaps/StarknetSwapContract.ts +668 -668
  136. package/src/starknet/swaps/StarknetSwapData.ts +455 -455
  137. package/src/starknet/swaps/StarknetSwapModule.ts +17 -17
  138. package/src/starknet/swaps/handlers/IHandler.ts +20 -20
  139. package/src/starknet/swaps/handlers/claim/ClaimHandlers.ts +23 -23
  140. package/src/starknet/swaps/handlers/claim/HashlockClaimHandler.ts +53 -53
  141. package/src/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.ts +73 -73
  142. package/src/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.ts +67 -67
  143. package/src/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.ts +50 -50
  144. package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +102 -102
  145. package/src/starknet/swaps/handlers/refund/TimelockRefundHandler.ts +38 -38
  146. package/src/starknet/swaps/modules/StarknetLpVault.ts +147 -147
  147. package/src/starknet/swaps/modules/StarknetSwapClaim.ts +141 -141
  148. package/src/starknet/swaps/modules/StarknetSwapInit.ts +300 -300
  149. package/src/starknet/swaps/modules/StarknetSwapRefund.ts +196 -196
  150. package/src/starknet/wallet/StarknetBrowserSigner.ts +11 -11
  151. package/src/starknet/wallet/StarknetPersistentSigner.ts +314 -310
  152. package/src/starknet/wallet/StarknetSigner.ts +84 -84
  153. package/src/starknet/wallet/accounts/StarknetKeypairWallet.ts +44 -44
  154. package/src/utils/Utils.ts +262 -262
@@ -1,351 +1,359 @@
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
- function isStarknetTxInvoke(obj) {
8
- return typeof (obj) === "object" &&
9
- typeof (obj.details) === "object" &&
10
- (obj.txId == null || typeof (obj.txId) === "string") &&
11
- obj.type === "INVOKE" &&
12
- Array.isArray(obj.tx) &&
13
- (obj.signed == null || typeof (obj.signed) === "object");
14
- }
15
- exports.isStarknetTxInvoke = isStarknetTxInvoke;
16
- function isStarknetTxDeployAccount(obj) {
17
- return typeof (obj) === "object" &&
18
- typeof (obj.details) === "object" &&
19
- (obj.txId == null || typeof (obj.txId) === "string") &&
20
- obj.type === "DEPLOY_ACCOUNT" &&
21
- Array.isArray(obj.tx) &&
22
- (obj.signed == null || typeof (obj.signed) === "object");
23
- }
24
- exports.isStarknetTxDeployAccount = isStarknetTxDeployAccount;
25
- const MAX_UNCONFIRMED_TXS = 25;
26
- class StarknetTransactions extends StarknetModule_1.StarknetModule {
27
- constructor() {
28
- super(...arguments);
29
- this.latestConfirmedNonces = {};
30
- this.latestPendingNonces = {};
31
- this.latestSignedNonces = {};
32
- this._cbksBeforeTxReplace = [];
33
- this.cbksBeforeTxSigned = [];
34
- this._knownTxSet = new Set();
35
- }
36
- sendTransaction(tx) {
37
- switch (tx.type) {
38
- case "INVOKE":
39
- return this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
40
- case "DEPLOY_ACCOUNT":
41
- return this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
42
- default:
43
- throw new Error("Unsupported tx type!");
44
- }
45
- }
46
- /**
47
- * Returns the nonce of the account or 0, if the account is not deployed yet
48
- *
49
- * @param address
50
- */
51
- async getNonce(address) {
52
- try {
53
- return BigInt(await this.provider.getNonceForAddress(address, starknet_1.BlockTag.PRE_CONFIRMED));
54
- }
55
- catch (e) {
56
- if (e.message != null && e.message.includes("20: Contract not found")) {
57
- return BigInt(0);
58
- }
59
- throw e;
60
- }
61
- }
62
- /**
63
- * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
64
- * the transaction at regular interval
65
- *
66
- * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
67
- * @param abortSignal signal to abort waiting for tx confirmation
68
- * @private
69
- */
70
- async confirmTransaction(tx, abortSignal) {
71
- const checkTxns = new Set([tx.txId]);
72
- const txReplaceListener = (oldTx, oldTxId, newTx, newTxId) => {
73
- if (checkTxns.has(oldTxId))
74
- checkTxns.add(newTxId);
75
- return Promise.resolve();
76
- };
77
- this.onBeforeTxReplace(txReplaceListener);
78
- let state = "pending";
79
- let confirmedTxId = null;
80
- while (state === "pending" || state === "not_found") {
81
- await (0, Utils_1.timeoutPromise)(3000, abortSignal);
82
- for (let txId of checkTxns) {
83
- //TODO: Rebroadcast latest tx if possible, we might need to do that in case we use 2 different RPCs on the backend
84
- // as the other one might not have the tx in their mempool
85
- // if(state==="not_found" && tx.signed!=null) await this.sendSignedTransaction(tx).catch(e => {
86
- // if(e.baseError?.code === 59) return; //Transaction already in the mempool
87
- // this.logger.error("confirmTransaction(): Error on transaction re-send: ", e);
88
- // });
89
- state = await this._getTxIdStatus(txId);
90
- if (state === "rejected" || state === "reverted" || state === "success") {
91
- confirmedTxId = txId;
92
- break;
93
- }
94
- }
95
- }
96
- this.offBeforeTxReplace(txReplaceListener);
97
- if (state === "rejected")
98
- throw new Error("Transaction rejected!");
99
- const nextAccountNonce = (0, Utils_1.toBigInt)(tx.details.nonce) + 1n;
100
- const currentConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
101
- if (currentConfirmedNonce == null || nextAccountNonce > currentConfirmedNonce) {
102
- this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
103
- }
104
- if (state === "reverted")
105
- throw new Error("Transaction reverted!");
106
- return confirmedTxId;
107
- }
108
- /**
109
- * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
110
- *
111
- * @param signer
112
- * @param txs
113
- * @private
114
- */
115
- async prepareTransactions(signer, txs) {
116
- let nonce = await this.getNonce(signer.getAddress());
117
- const latestPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signer.getAddress())];
118
- if (latestPendingNonce != null && latestPendingNonce > nonce) {
119
- this.logger.debug("prepareTransactions(): Using 'pending' nonce from local cache!");
120
- nonce = latestPendingNonce;
121
- }
122
- //Add deploy account tx
123
- if (nonce === 0n) {
124
- const deployPayload = await signer.getDeployPayload();
125
- if (deployPayload != null)
126
- txs.unshift(await this.root.Accounts.getAccountDeployTransaction(deployPayload));
127
- }
128
- if (!signer.isManagingNoncesInternally) {
129
- if (nonce === 0n) {
130
- //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
131
- nonce = 1n;
132
- }
133
- for (let i = 0; i < txs.length; i++) {
134
- const tx = txs[i];
135
- if (tx.details.nonce != null)
136
- nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
137
- if (nonce == null)
138
- nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
139
- if (tx.details.nonce == null)
140
- tx.details.nonce = nonce;
141
- this.logger.debug("sendAndConfirm(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
142
- nonce += BigInt(1);
143
- }
144
- }
145
- for (let tx of txs) {
146
- for (let callback of this.cbksBeforeTxSigned) {
147
- await callback(tx);
148
- }
149
- }
150
- }
151
- /**
152
- * Sends out a signed transaction to the RPC
153
- *
154
- * @param tx Starknet tx to send
155
- * @param onBeforePublish a callback called before every transaction is published
156
- * @private
157
- */
158
- async sendSignedTransaction(tx, onBeforePublish) {
159
- if (onBeforePublish != null)
160
- await onBeforePublish(tx.txId, StarknetTransactions.serializeTx(tx));
161
- this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
162
- const txResult = await this.sendTransaction(tx);
163
- if (tx.txId !== txResult)
164
- this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
165
- this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
166
- return txResult;
167
- }
168
- /**
169
- * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
170
- * of a batch of starknet transactions
171
- *
172
- * @param signer
173
- * @param txs transactions to send
174
- * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
175
- * are re-sent at regular intervals)
176
- * @param abortSignal abort signal to abort waiting for transaction confirmations
177
- * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
178
- * are executed in order)
179
- * @param onBeforePublish a callback called before every transaction is published
180
- */
181
- async sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
182
- await this.prepareTransactions(signer, txs);
183
- const signedTxs = [];
184
- //Don't separate the signing process from the sending when using browser-based wallet
185
- if (signer.signTransaction != null)
186
- for (let i = 0; i < txs.length; i++) {
187
- const tx = txs[i];
188
- const signedTx = await signer.signTransaction(tx);
189
- signedTxs.push(signedTx);
190
- this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
191
- const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
192
- const currentSignedNonce = this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
193
- if (currentSignedNonce == null || nextAccountNonce > currentSignedNonce) {
194
- this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
195
- }
196
- }
197
- this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
198
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
199
- const txIds = [];
200
- if (parallel) {
201
- let promises = [];
202
- for (let i = 0; i < txs.length; i++) {
203
- let tx;
204
- if (signer.signTransaction == null) {
205
- const txId = await signer.sendTransaction(txs[i], onBeforePublish);
206
- tx = txs[i];
207
- tx.txId = txId;
208
- }
209
- else {
210
- const signedTx = signedTxs[i];
211
- await this.sendSignedTransaction(signedTx, onBeforePublish);
212
- tx = signedTx;
213
- }
214
- if (tx.details.nonce != null) {
215
- const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
216
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
217
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
218
- this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
219
- }
220
- }
221
- promises.push(this.confirmTransaction(tx, abortSignal));
222
- if (!waitForConfirmation)
223
- txIds.push(tx.txId);
224
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
225
- if (promises.length >= MAX_UNCONFIRMED_TXS) {
226
- if (waitForConfirmation)
227
- txIds.push(...await Promise.all(promises));
228
- promises = [];
229
- }
230
- }
231
- if (waitForConfirmation && promises.length > 0) {
232
- txIds.push(...await Promise.all(promises));
233
- }
234
- }
235
- else {
236
- for (let i = 0; i < txs.length; i++) {
237
- let tx;
238
- if (signer.signTransaction == null) {
239
- const txId = await signer.sendTransaction(txs[i], onBeforePublish);
240
- tx = txs[i];
241
- tx.txId = txId;
242
- }
243
- else {
244
- const signedTx = signedTxs[i];
245
- await this.sendSignedTransaction(signedTx, onBeforePublish);
246
- tx = signedTx;
247
- }
248
- if (tx.details.nonce != null) {
249
- const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
250
- const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
251
- if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
252
- this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
253
- }
254
- }
255
- const confirmPromise = this.confirmTransaction(tx, abortSignal);
256
- this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
257
- //Don't await the last promise when !waitForConfirmation
258
- let txHash = tx.txId;
259
- if (i < txs.length - 1 || waitForConfirmation)
260
- txHash = await confirmPromise;
261
- txIds.push(txHash);
262
- }
263
- }
264
- this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
265
- " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
266
- return txIds;
267
- }
268
- /**
269
- * Serializes the solana transaction, saves the transaction, signers & last valid blockheight
270
- *
271
- * @param tx
272
- */
273
- static serializeTx(tx) {
274
- return JSON.stringify(tx, (key, value) => {
275
- if (typeof (value) === "bigint")
276
- return (0, Utils_1.toHex)(value);
277
- return value;
278
- });
279
- }
280
- /**
281
- * Deserializes saved solana transaction, extracting the transaction, signers & last valid blockheight
282
- *
283
- * @param txData
284
- */
285
- static deserializeTx(txData) {
286
- return JSON.parse(txData);
287
- }
288
- /**
289
- * Gets the status of the raw starknet transaction
290
- *
291
- * @param tx
292
- */
293
- async getTxStatus(tx) {
294
- const parsedTx = StarknetTransactions.deserializeTx(tx);
295
- return await this.getTxIdStatus(parsedTx.txId);
296
- }
297
- /**
298
- * Gets the status of the starknet transaction with a specific txId
299
- *
300
- * @param txId
301
- */
302
- async _getTxIdStatus(txId) {
303
- const status = await this.provider.getTransactionStatus(txId).catch(e => {
304
- if (e.message != null && e.message.includes("29: Transaction hash not found"))
305
- return null;
306
- throw e;
307
- });
308
- if (status == null)
309
- return this._knownTxSet.has(txId) ? "pending" : "not_found";
310
- if (status.finality_status === starknet_1.ETransactionStatus.RECEIVED)
311
- return "pending";
312
- if (status.finality_status === starknet_1.ETransactionStatus.REJECTED)
313
- return "rejected";
314
- if (status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED) {
315
- return "success";
316
- }
317
- return "reverted";
318
- }
319
- /**
320
- * Gets the status of the starknet transaction with a specific txId
321
- *
322
- * @param txId
323
- */
324
- async getTxIdStatus(txId) {
325
- const status = await this._getTxIdStatus(txId);
326
- if (status === "rejected")
327
- return "reverted";
328
- return status;
329
- }
330
- onBeforeTxReplace(callback) {
331
- this._cbksBeforeTxReplace.push(callback);
332
- }
333
- offBeforeTxReplace(callback) {
334
- const index = this._cbksBeforeTxReplace.indexOf(callback);
335
- if (index === -1)
336
- return false;
337
- this._cbksBeforeTxReplace.splice(index, 1);
338
- return true;
339
- }
340
- onBeforeTxSigned(callback) {
341
- this.cbksBeforeTxSigned.push(callback);
342
- }
343
- offBeforeTxSigned(callback) {
344
- const index = this.cbksBeforeTxSigned.indexOf(callback);
345
- if (index === -1)
346
- return false;
347
- this.cbksBeforeTxSigned.splice(index, 1);
348
- return true;
349
- }
350
- }
351
- 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
+ function isStarknetTxInvoke(obj) {
8
+ return typeof (obj) === "object" &&
9
+ typeof (obj.details) === "object" &&
10
+ (obj.txId == null || typeof (obj.txId) === "string") &&
11
+ obj.type === "INVOKE" &&
12
+ Array.isArray(obj.tx) &&
13
+ (obj.signed == null || typeof (obj.signed) === "object");
14
+ }
15
+ exports.isStarknetTxInvoke = isStarknetTxInvoke;
16
+ function isStarknetTxDeployAccount(obj) {
17
+ return typeof (obj) === "object" &&
18
+ typeof (obj.details) === "object" &&
19
+ (obj.txId == null || typeof (obj.txId) === "string") &&
20
+ obj.type === "DEPLOY_ACCOUNT" &&
21
+ Array.isArray(obj.tx) &&
22
+ (obj.signed == null || typeof (obj.signed) === "object");
23
+ }
24
+ exports.isStarknetTxDeployAccount = isStarknetTxDeployAccount;
25
+ const MAX_UNCONFIRMED_TXS = 25;
26
+ class StarknetTransactions extends StarknetModule_1.StarknetModule {
27
+ constructor() {
28
+ super(...arguments);
29
+ this.latestConfirmedNonces = {};
30
+ this.latestPendingNonces = {};
31
+ this.latestSignedNonces = {};
32
+ this._cbksBeforeTxReplace = [];
33
+ this.cbksBeforeTxSigned = [];
34
+ this._knownTxSet = new Set();
35
+ }
36
+ sendTransaction(tx) {
37
+ switch (tx.type) {
38
+ case "INVOKE":
39
+ return this.provider.channel.invoke(tx.signed, tx.details).then(res => res.transaction_hash);
40
+ case "DEPLOY_ACCOUNT":
41
+ return this.provider.channel.deployAccount(tx.signed, tx.details).then((res) => res.transaction_hash);
42
+ default:
43
+ throw new Error("Unsupported tx type!");
44
+ }
45
+ }
46
+ /**
47
+ * Returns the nonce of the account or 0, if the account is not deployed yet
48
+ *
49
+ * @param address
50
+ * @param blockTag
51
+ */
52
+ async getNonce(address, blockTag = starknet_1.BlockTag.PRE_CONFIRMED) {
53
+ try {
54
+ return BigInt(await this.provider.getNonceForAddress(address, blockTag));
55
+ }
56
+ catch (e) {
57
+ if (e.message != null && e.message.includes("20: Contract not found")) {
58
+ return BigInt(0);
59
+ }
60
+ throw e;
61
+ }
62
+ }
63
+ /**
64
+ * Waits for transaction confirmation using WS subscription and occasional HTTP polling, also re-sends
65
+ * the transaction at regular interval
66
+ *
67
+ * @param tx starknet transaction to wait for confirmation for & keep re-sending until it confirms
68
+ * @param abortSignal signal to abort waiting for tx confirmation
69
+ * @private
70
+ */
71
+ async confirmTransaction(tx, abortSignal) {
72
+ const checkTxns = new Set([tx.txId]);
73
+ const txReplaceListener = (oldTx, oldTxId, newTx, newTxId) => {
74
+ if (checkTxns.has(oldTxId))
75
+ checkTxns.add(newTxId);
76
+ return Promise.resolve();
77
+ };
78
+ this.onBeforeTxReplace(txReplaceListener);
79
+ let state = "pending";
80
+ let confirmedTxId = null;
81
+ while (state === "pending" || state === "not_found") {
82
+ await (0, Utils_1.timeoutPromise)(3000, abortSignal);
83
+ for (let txId of checkTxns) {
84
+ //TODO: Rebroadcast latest tx if possible, we might need to do that in case we use 2 different RPCs on the backend
85
+ // as the other one might not have the tx in their mempool
86
+ // if(state==="not_found" && tx.signed!=null) await this.sendSignedTransaction(tx).catch(e => {
87
+ // if(e.baseError?.code === 59) return; //Transaction already in the mempool
88
+ // this.logger.error("confirmTransaction(): Error on transaction re-send: ", e);
89
+ // });
90
+ state = await this._getTxIdStatus(txId);
91
+ if (state === "rejected" || state === "reverted" || state === "success") {
92
+ confirmedTxId = txId;
93
+ break;
94
+ }
95
+ }
96
+ }
97
+ this.offBeforeTxReplace(txReplaceListener);
98
+ if (state === "rejected")
99
+ throw new Error("Transaction rejected!");
100
+ const nextAccountNonce = (0, Utils_1.toBigInt)(tx.details.nonce) + 1n;
101
+ const currentConfirmedNonce = this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
102
+ if (currentConfirmedNonce == null || nextAccountNonce > currentConfirmedNonce) {
103
+ this.latestConfirmedNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
104
+ }
105
+ if (state === "reverted")
106
+ throw new Error("Transaction reverted!");
107
+ return confirmedTxId;
108
+ }
109
+ /**
110
+ * Prepares starknet transactions, checks if the account is deployed, assigns nonces if needed & calls beforeTxSigned callback
111
+ *
112
+ * @param signer
113
+ * @param txs
114
+ * @private
115
+ */
116
+ async prepareTransactions(signer, txs) {
117
+ let nonce = await this.getNonce(signer.getAddress());
118
+ const latestPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(signer.getAddress())];
119
+ if (latestPendingNonce != null && latestPendingNonce > nonce) {
120
+ this.logger.debug("prepareTransactions(): Using 'pending' nonce from local cache!");
121
+ nonce = latestPendingNonce;
122
+ }
123
+ //Add deploy account tx
124
+ if (nonce === 0n) {
125
+ const deployPayload = await signer.getDeployPayload();
126
+ if (deployPayload != null)
127
+ txs.unshift(await this.root.Accounts.getAccountDeployTransaction(deployPayload));
128
+ }
129
+ if (!signer.isManagingNoncesInternally) {
130
+ if (nonce === 0n) {
131
+ //Just increment the nonce by one and hope the wallet is smart enough to deploy account first
132
+ nonce = 1n;
133
+ }
134
+ for (let i = 0; i < txs.length; i++) {
135
+ const tx = txs[i];
136
+ if (tx.details.nonce != null)
137
+ nonce = BigInt(tx.details.nonce); //Take the nonce from last tx
138
+ if (nonce == null)
139
+ nonce = BigInt(await this.root.provider.getNonceForAddress(signer.getAddress())); //Fetch the nonce
140
+ if (tx.details.nonce == null)
141
+ tx.details.nonce = nonce;
142
+ this.logger.debug("sendAndConfirm(): transaction prepared (" + (i + 1) + "/" + txs.length + "), nonce: " + tx.details.nonce);
143
+ nonce += BigInt(1);
144
+ }
145
+ }
146
+ for (let tx of txs) {
147
+ for (let callback of this.cbksBeforeTxSigned) {
148
+ await callback(tx);
149
+ }
150
+ }
151
+ }
152
+ /**
153
+ * Sends out a signed transaction to the RPC
154
+ *
155
+ * @param tx Starknet tx to send
156
+ * @param onBeforePublish a callback called before every transaction is published
157
+ * @private
158
+ */
159
+ async sendSignedTransaction(tx, onBeforePublish) {
160
+ if (onBeforePublish != null)
161
+ await onBeforePublish(tx.txId, StarknetTransactions.serializeTx(tx));
162
+ this.logger.debug("sendSignedTransaction(): sending transaction: ", tx.txId);
163
+ const txResult = await this.sendTransaction(tx);
164
+ if (tx.txId !== txResult)
165
+ this.logger.warn("sendSignedTransaction(): sent tx hash not matching the precomputed hash!");
166
+ this.logger.info("sendSignedTransaction(): tx sent, expected txHash: " + tx.txId + ", txHash: " + txResult);
167
+ return txResult;
168
+ }
169
+ /**
170
+ * Prepares, signs , sends (in parallel or sequentially) & optionally waits for confirmation
171
+ * of a batch of starknet transactions
172
+ *
173
+ * @param signer
174
+ * @param txs transactions to send
175
+ * @param waitForConfirmation whether to wait for transaction confirmations (this also makes sure the transactions
176
+ * are re-sent at regular intervals)
177
+ * @param abortSignal abort signal to abort waiting for transaction confirmations
178
+ * @param parallel whether the send all the transaction at once in parallel or sequentially (such that transactions
179
+ * are executed in order)
180
+ * @param onBeforePublish a callback called before every transaction is published
181
+ */
182
+ async sendAndConfirm(signer, txs, waitForConfirmation, abortSignal, parallel, onBeforePublish) {
183
+ await this.prepareTransactions(signer, txs);
184
+ const signedTxs = [];
185
+ //Don't separate the signing process from the sending when using browser-based wallet
186
+ if (signer.signTransaction != null)
187
+ for (let i = 0; i < txs.length; i++) {
188
+ const tx = txs[i];
189
+ const signedTx = await signer.signTransaction(tx);
190
+ signedTxs.push(signedTx);
191
+ this.logger.debug("sendAndConfirm(): transaction signed (" + (i + 1) + "/" + txs.length + "): " + signedTx.txId);
192
+ const nextAccountNonce = BigInt(signedTx.details.nonce) + 1n;
193
+ const currentSignedNonce = this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)];
194
+ if (currentSignedNonce == null || nextAccountNonce > currentSignedNonce) {
195
+ this.latestSignedNonces[(0, Utils_1.toHex)(signedTx.details.walletAddress)] = nextAccountNonce;
196
+ }
197
+ }
198
+ this.logger.debug("sendAndConfirm(): sending transactions, count: " + txs.length +
199
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
200
+ const txIds = [];
201
+ if (parallel) {
202
+ let promises = [];
203
+ for (let i = 0; i < txs.length; i++) {
204
+ let tx;
205
+ if (signer.signTransaction == null) {
206
+ const txId = await signer.sendTransaction(txs[i], onBeforePublish);
207
+ tx = txs[i];
208
+ tx.txId = txId;
209
+ }
210
+ else {
211
+ const signedTx = signedTxs[i];
212
+ await this.sendSignedTransaction(signedTx, onBeforePublish);
213
+ tx = signedTx;
214
+ }
215
+ if (tx.details.nonce != null) {
216
+ const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
217
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
218
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
219
+ this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
220
+ }
221
+ }
222
+ promises.push(this.confirmTransaction(tx, abortSignal));
223
+ if (!waitForConfirmation)
224
+ txIds.push(tx.txId);
225
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
226
+ if (promises.length >= MAX_UNCONFIRMED_TXS) {
227
+ if (waitForConfirmation)
228
+ txIds.push(...await Promise.all(promises));
229
+ promises = [];
230
+ }
231
+ }
232
+ if (waitForConfirmation && promises.length > 0) {
233
+ txIds.push(...await Promise.all(promises));
234
+ }
235
+ }
236
+ else {
237
+ for (let i = 0; i < txs.length; i++) {
238
+ let tx;
239
+ if (signer.signTransaction == null) {
240
+ const txId = await signer.sendTransaction(txs[i], onBeforePublish);
241
+ tx = txs[i];
242
+ tx.txId = txId;
243
+ }
244
+ else {
245
+ const signedTx = signedTxs[i];
246
+ await this.sendSignedTransaction(signedTx, onBeforePublish);
247
+ tx = signedTx;
248
+ }
249
+ if (tx.details.nonce != null) {
250
+ const nextAccountNonce = BigInt(tx.details.nonce) + 1n;
251
+ const currentPendingNonce = this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)];
252
+ if (currentPendingNonce == null || nextAccountNonce > currentPendingNonce) {
253
+ this.latestPendingNonces[(0, Utils_1.toHex)(tx.details.walletAddress)] = nextAccountNonce;
254
+ }
255
+ }
256
+ const confirmPromise = this.confirmTransaction(tx, abortSignal);
257
+ this.logger.debug("sendAndConfirm(): transaction sent (" + (i + 1) + "/" + txs.length + "): " + tx.txId);
258
+ //Don't await the last promise when !waitForConfirmation
259
+ let txHash = tx.txId;
260
+ if (i < txs.length - 1 || waitForConfirmation)
261
+ txHash = await confirmPromise;
262
+ txIds.push(txHash);
263
+ }
264
+ }
265
+ this.logger.info("sendAndConfirm(): sent transactions, count: " + txs.length +
266
+ " waitForConfirmation: " + waitForConfirmation + " parallel: " + parallel);
267
+ return txIds;
268
+ }
269
+ /**
270
+ * Serializes the starknet transaction, saves the transaction, signers & last valid blockheight
271
+ *
272
+ * @param tx
273
+ */
274
+ static serializeTx(tx) {
275
+ return JSON.stringify(tx, (key, value) => {
276
+ if (typeof (value) === "bigint")
277
+ return {
278
+ _type: "bigint",
279
+ _value: (0, Utils_1.toHex)(value)
280
+ };
281
+ return value;
282
+ });
283
+ }
284
+ /**
285
+ * Deserializes saved starknet transaction, extracting the transaction, signers & last valid blockheight
286
+ *
287
+ * @param txData
288
+ */
289
+ static deserializeTx(txData) {
290
+ return JSON.parse(txData, (key, value) => {
291
+ if (typeof (value) === "object" && value._type === "bigint")
292
+ return BigInt(value._value);
293
+ return value;
294
+ });
295
+ }
296
+ /**
297
+ * Gets the status of the raw starknet transaction
298
+ *
299
+ * @param tx
300
+ */
301
+ async getTxStatus(tx) {
302
+ const parsedTx = StarknetTransactions.deserializeTx(tx);
303
+ return await this.getTxIdStatus(parsedTx.txId);
304
+ }
305
+ /**
306
+ * Gets the status of the starknet transaction with a specific txId
307
+ *
308
+ * @param txId
309
+ */
310
+ async _getTxIdStatus(txId) {
311
+ const status = await this.provider.getTransactionStatus(txId).catch(e => {
312
+ if (e.message != null && e.message.includes("29: Transaction hash not found"))
313
+ return null;
314
+ throw e;
315
+ });
316
+ if (status == null)
317
+ return this._knownTxSet.has(txId) ? "pending" : "not_found";
318
+ if (status.finality_status === starknet_1.ETransactionStatus.RECEIVED)
319
+ return "pending";
320
+ if (status.finality_status === starknet_1.ETransactionStatus.REJECTED)
321
+ return "rejected";
322
+ if (status.execution_status === starknet_1.ETransactionExecutionStatus.SUCCEEDED) {
323
+ return "success";
324
+ }
325
+ return "reverted";
326
+ }
327
+ /**
328
+ * Gets the status of the starknet transaction with a specific txId
329
+ *
330
+ * @param txId
331
+ */
332
+ async getTxIdStatus(txId) {
333
+ const status = await this._getTxIdStatus(txId);
334
+ if (status === "rejected")
335
+ return "reverted";
336
+ return status;
337
+ }
338
+ onBeforeTxReplace(callback) {
339
+ this._cbksBeforeTxReplace.push(callback);
340
+ }
341
+ offBeforeTxReplace(callback) {
342
+ const index = this._cbksBeforeTxReplace.indexOf(callback);
343
+ if (index === -1)
344
+ return false;
345
+ this._cbksBeforeTxReplace.splice(index, 1);
346
+ return true;
347
+ }
348
+ onBeforeTxSigned(callback) {
349
+ this.cbksBeforeTxSigned.push(callback);
350
+ }
351
+ offBeforeTxSigned(callback) {
352
+ const index = this.cbksBeforeTxSigned.indexOf(callback);
353
+ if (index === -1)
354
+ return false;
355
+ this.cbksBeforeTxSigned.splice(index, 1);
356
+ return true;
357
+ }
358
+ }
359
+ exports.StarknetTransactions = StarknetTransactions;