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