@atomiqlabs/chain-starknet 8.0.13 → 8.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/dist/index.d.ts +18 -18
  2. package/dist/index.js +42 -42
  3. package/dist/starknet/StarknetChainType.d.ts +19 -19
  4. package/dist/starknet/StarknetChainType.js +2 -2
  5. package/dist/starknet/StarknetInitializer.d.ts +66 -63
  6. package/dist/starknet/StarknetInitializer.js +101 -101
  7. package/dist/starknet/btcrelay/BtcRelayAbi.d.ts +250 -250
  8. package/dist/starknet/btcrelay/BtcRelayAbi.js +341 -341
  9. package/dist/starknet/btcrelay/StarknetBtcRelay.d.ts +196 -196
  10. package/dist/starknet/btcrelay/StarknetBtcRelay.js +419 -411
  11. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.d.ts +70 -70
  12. package/dist/starknet/btcrelay/headers/StarknetBtcHeader.js +115 -115
  13. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.d.ts +91 -91
  14. package/dist/starknet/btcrelay/headers/StarknetBtcStoredHeader.js +155 -155
  15. package/dist/starknet/chain/StarknetAction.d.ts +19 -19
  16. package/dist/starknet/chain/StarknetAction.js +74 -74
  17. package/dist/starknet/chain/StarknetChainInterface.d.ts +142 -143
  18. package/dist/starknet/chain/StarknetChainInterface.js +198 -199
  19. package/dist/starknet/chain/StarknetModule.d.ts +8 -8
  20. package/dist/starknet/chain/StarknetModule.js +12 -12
  21. package/dist/starknet/chain/modules/ERC20Abi.d.ts +755 -755
  22. package/dist/starknet/chain/modules/ERC20Abi.js +1032 -1032
  23. package/dist/starknet/chain/modules/StarknetAccounts.d.ts +6 -6
  24. package/dist/starknet/chain/modules/StarknetAccounts.js +26 -26
  25. package/dist/starknet/chain/modules/StarknetAddresses.d.ts +10 -10
  26. package/dist/starknet/chain/modules/StarknetAddresses.js +27 -27
  27. package/dist/starknet/chain/modules/StarknetBlocks.d.ts +27 -27
  28. package/dist/starknet/chain/modules/StarknetBlocks.js +82 -82
  29. package/dist/starknet/chain/modules/StarknetEvents.d.ts +47 -47
  30. package/dist/starknet/chain/modules/StarknetEvents.js +90 -90
  31. package/dist/starknet/chain/modules/StarknetFees.d.ts +118 -104
  32. package/dist/starknet/chain/modules/StarknetFees.js +150 -146
  33. package/dist/starknet/chain/modules/StarknetSignatures.d.ts +29 -29
  34. package/dist/starknet/chain/modules/StarknetSignatures.js +72 -72
  35. package/dist/starknet/chain/modules/StarknetTokens.d.ts +66 -66
  36. package/dist/starknet/chain/modules/StarknetTokens.js +99 -99
  37. package/dist/starknet/chain/modules/StarknetTransactions.d.ts +122 -115
  38. package/dist/starknet/chain/modules/StarknetTransactions.js +633 -612
  39. package/dist/starknet/contract/StarknetContractBase.d.ts +14 -13
  40. package/dist/starknet/contract/StarknetContractBase.js +21 -20
  41. package/dist/starknet/contract/StarknetContractModule.d.ts +8 -8
  42. package/dist/starknet/contract/StarknetContractModule.js +11 -11
  43. package/dist/starknet/contract/modules/StarknetContractEvents.d.ts +56 -57
  44. package/dist/starknet/contract/modules/StarknetContractEvents.js +111 -111
  45. package/dist/starknet/events/StarknetChainEvents.d.ts +21 -21
  46. package/dist/starknet/events/StarknetChainEvents.js +61 -61
  47. package/dist/starknet/events/StarknetChainEventsBrowser.d.ts +178 -190
  48. package/dist/starknet/events/StarknetChainEventsBrowser.js +523 -582
  49. package/dist/starknet/provider/RpcProviderWithRetries.d.ts +49 -53
  50. package/dist/starknet/provider/RpcProviderWithRetries.js +94 -94
  51. package/dist/starknet/provider/WebSocketChannelWithRetries.d.ts +21 -21
  52. package/dist/starknet/provider/WebSocketChannelWithRetries.js +46 -46
  53. package/dist/starknet/spv_swap/SpvVaultContractAbi.d.ts +488 -488
  54. package/dist/starknet/spv_swap/SpvVaultContractAbi.js +656 -656
  55. package/dist/starknet/spv_swap/StarknetSpvVaultContract.d.ts +225 -219
  56. package/dist/starknet/spv_swap/StarknetSpvVaultContract.js +663 -621
  57. package/dist/starknet/spv_swap/StarknetSpvVaultData.d.ts +108 -108
  58. package/dist/starknet/spv_swap/StarknetSpvVaultData.js +190 -190
  59. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.d.ts +56 -56
  60. package/dist/starknet/spv_swap/StarknetSpvWithdrawalData.js +103 -103
  61. package/dist/starknet/swaps/EscrowManagerAbi.d.ts +431 -431
  62. package/dist/starknet/swaps/EscrowManagerAbi.js +583 -583
  63. package/dist/starknet/swaps/StarknetSwapContract.d.ts +309 -278
  64. package/dist/starknet/swaps/StarknetSwapContract.js +755 -579
  65. package/dist/starknet/swaps/StarknetSwapData.d.ts +234 -234
  66. package/dist/starknet/swaps/StarknetSwapData.js +474 -474
  67. package/dist/starknet/swaps/StarknetSwapModule.d.ts +10 -10
  68. package/dist/starknet/swaps/StarknetSwapModule.js +12 -12
  69. package/dist/starknet/swaps/handlers/IHandler.d.ts +13 -13
  70. package/dist/starknet/swaps/handlers/IHandler.js +2 -2
  71. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.d.ts +13 -13
  72. package/dist/starknet/swaps/handlers/claim/ClaimHandlers.js +13 -13
  73. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.d.ts +21 -21
  74. package/dist/starknet/swaps/handlers/claim/HashlockClaimHandler.js +44 -44
  75. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.d.ts +24 -24
  76. package/dist/starknet/swaps/handlers/claim/btc/BitcoinNoncedOutputClaimHandler.js +48 -48
  77. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.d.ts +25 -25
  78. package/dist/starknet/swaps/handlers/claim/btc/BitcoinOutputClaimHandler.js +40 -40
  79. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.d.ts +20 -20
  80. package/dist/starknet/swaps/handlers/claim/btc/BitcoinTxIdClaimHandler.js +30 -30
  81. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.d.ts +42 -45
  82. package/dist/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.js +50 -54
  83. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.d.ts +17 -17
  84. package/dist/starknet/swaps/handlers/refund/TimelockRefundHandler.js +27 -27
  85. package/dist/starknet/swaps/modules/StarknetLpVault.d.ts +67 -67
  86. package/dist/starknet/swaps/modules/StarknetLpVault.js +122 -122
  87. package/dist/starknet/swaps/modules/StarknetSwapClaim.d.ts +52 -52
  88. package/dist/starknet/swaps/modules/StarknetSwapClaim.js +99 -99
  89. package/dist/starknet/swaps/modules/StarknetSwapInit.d.ts +94 -94
  90. package/dist/starknet/swaps/modules/StarknetSwapInit.js +239 -239
  91. package/dist/starknet/swaps/modules/StarknetSwapRefund.d.ts +60 -60
  92. package/dist/starknet/swaps/modules/StarknetSwapRefund.js +126 -126
  93. package/dist/starknet/wallet/StarknetBrowserSigner.d.ts +11 -11
  94. package/dist/starknet/wallet/StarknetBrowserSigner.js +17 -17
  95. package/dist/starknet/wallet/StarknetPersistentSigner.d.ts +76 -76
  96. package/dist/starknet/wallet/StarknetPersistentSigner.js +291 -291
  97. package/dist/starknet/wallet/StarknetSigner.d.ts +72 -72
  98. package/dist/starknet/wallet/StarknetSigner.js +114 -114
  99. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.d.ts +18 -18
  100. package/dist/starknet/wallet/accounts/StarknetKeypairWallet.js +45 -45
  101. package/dist/utils/Utils.d.ts +77 -77
  102. package/dist/utils/Utils.js +304 -303
  103. package/package.json +2 -2
  104. package/src/starknet/StarknetInitializer.ts +6 -3
  105. package/src/starknet/btcrelay/StarknetBtcRelay.ts +19 -6
  106. package/src/starknet/btcrelay/headers/StarknetBtcHeader.ts +7 -7
  107. package/src/starknet/btcrelay/headers/StarknetBtcStoredHeader.ts +6 -6
  108. package/src/starknet/chain/StarknetAction.ts +1 -0
  109. package/src/starknet/chain/StarknetChainInterface.ts +0 -2
  110. package/src/starknet/chain/modules/StarknetFees.ts +15 -2
  111. package/src/starknet/chain/modules/StarknetTransactions.ts +24 -0
  112. package/src/starknet/contract/StarknetContractBase.ts +7 -4
  113. package/src/starknet/contract/StarknetContractModule.ts +1 -1
  114. package/src/starknet/contract/modules/StarknetContractEvents.ts +7 -7
  115. package/src/starknet/events/StarknetChainEventsBrowser.ts +2 -64
  116. package/src/starknet/provider/RpcProviderWithRetries.ts +1 -1
  117. package/src/starknet/spv_swap/StarknetSpvVaultContract.ts +84 -18
  118. package/src/starknet/swaps/StarknetSwapContract.ts +242 -6
  119. package/src/starknet/swaps/handlers/claim/btc/IBitcoinClaimHandler.ts +0 -4
@@ -1,291 +1,291 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StarknetPersistentSigner = void 0;
4
- const StarknetSigner_1 = require("./StarknetSigner");
5
- const StarknetTransactions_1 = require("../chain/modules/StarknetTransactions");
6
- const Utils_1 = require("../../utils/Utils");
7
- const starknet_1 = require("starknet");
8
- const promises_1 = require("fs/promises");
9
- const StarknetFees_1 = require("../chain/modules/StarknetFees");
10
- const transaction_1 = require("@scure/btc-signer/transaction");
11
- const promise_queue_ts_1 = require("promise-queue-ts");
12
- const WAIT_BEFORE_BUMP = 15 * 1000;
13
- const MIN_FEE_INCREASE_ABSOLUTE = 1n * 1000000n; //0.001GWei
14
- const MIN_FEE_INCREASE_PPM = 110000n; // +11%
15
- const MIN_TIP_INCREASE_ABSOLUTE = 1n * 1000000000n; //1GWei
16
- const MIN_TIP_INCREASE_PPM = 110000n; // +11%
17
- /**
18
- * A complex starknet signer implementation with internal nonce management, with race condition preventions,
19
- * automatic transaction rebroadcasting and failovers. Uses the NodeJS `fs` library to persist transaction
20
- * data across application restarts, hence this doesn't work on frontends and is intended to be used as a
21
- * robust backend wallet implementation.
22
- *
23
- * @category Wallets
24
- */
25
- class StarknetPersistentSigner extends StarknetSigner_1.StarknetSigner {
26
- constructor(account, chainInterface, directory, config) {
27
- var _a, _b, _c, _d, _e;
28
- super(account, true);
29
- this.pendingTxs = new Map();
30
- this.confirmedNonce = 0n;
31
- this.pendingNonce = 0n;
32
- this.stopped = false;
33
- this.saveCount = 0;
34
- this.sendTransactionQueue = new promise_queue_ts_1.PromiseQueue();
35
- this.signTransaction = undefined;
36
- this.chainInterface = chainInterface;
37
- this.directory = directory;
38
- this.config = config ?? {};
39
- (_a = this.config).minFeeIncreaseAbsolute ?? (_a.minFeeIncreaseAbsolute = MIN_FEE_INCREASE_ABSOLUTE);
40
- (_b = this.config).minFeeIncreasePpm ?? (_b.minFeeIncreasePpm = MIN_FEE_INCREASE_PPM);
41
- (_c = this.config).minTipIncreaseAbsolute ?? (_c.minTipIncreaseAbsolute = MIN_TIP_INCREASE_ABSOLUTE);
42
- (_d = this.config).minTipIncreasePpm ?? (_d.minTipIncreasePpm = MIN_TIP_INCREASE_PPM);
43
- (_e = this.config).waitBeforeBump ?? (_e.waitBeforeBump = WAIT_BEFORE_BUMP);
44
- this.logger = (0, Utils_1.getLogger)("StarknetPersistentSigner(" + this.account.address + "): ");
45
- }
46
- async load() {
47
- const fileExists = await (0, promises_1.access)(this.directory + "/txs.json", promises_1.constants.F_OK).then(() => true).catch(() => false);
48
- if (!fileExists)
49
- return;
50
- const res = await (0, promises_1.readFile)(this.directory + "/txs.json");
51
- if (res != null) {
52
- const pendingTxs = JSON.parse(res.toString());
53
- for (let nonceStr in pendingTxs) {
54
- const nonceData = pendingTxs[nonceStr];
55
- const nonce = BigInt(nonceStr);
56
- if (this.confirmedNonce >= nonce)
57
- continue; //Already confirmed
58
- if (this.pendingNonce < nonce) {
59
- this.pendingNonce = nonce;
60
- }
61
- const parsedPendingTxns = nonceData.txs.map(StarknetTransactions_1.StarknetTransactions.deserializeTx);
62
- this.pendingTxs.set(nonce, {
63
- txs: parsedPendingTxns,
64
- lastBumped: nonceData.lastBumped
65
- });
66
- for (let tx of parsedPendingTxns) {
67
- this.chainInterface.Transactions._knownTxSet.add(tx.txId);
68
- }
69
- }
70
- }
71
- }
72
- async save() {
73
- const pendingTxs = {};
74
- for (let [nonce, data] of this.pendingTxs) {
75
- pendingTxs[nonce.toString(10)] = {
76
- lastBumped: data.lastBumped,
77
- txs: data.txs.map(StarknetTransactions_1.StarknetTransactions.serializeTx)
78
- };
79
- }
80
- const requiredSaveCount = ++this.saveCount;
81
- if (this.priorSavePromise != null) {
82
- await this.priorSavePromise;
83
- }
84
- if (requiredSaveCount === this.saveCount) {
85
- this.priorSavePromise = (0, promises_1.writeFile)(this.directory + "/txs.json", JSON.stringify(pendingTxs));
86
- await this.priorSavePromise;
87
- }
88
- }
89
- async checkPastTransactions() {
90
- let _gasPrice = null;
91
- let _safeBlockNonce = null;
92
- for (let [nonce, data] of this.pendingTxs) {
93
- if (!data.sending && data.lastBumped < Date.now() - this.config.waitBeforeBump) {
94
- if (_safeBlockNonce == null) {
95
- _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
96
- this.confirmedNonce = _safeBlockNonce - 1n;
97
- }
98
- if (this.confirmedNonce >= nonce) {
99
- this.pendingTxs.delete(nonce);
100
- data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
101
- this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
102
- this.save();
103
- continue;
104
- }
105
- const lastTx = data.txs[data.txs.length - 1];
106
- if (_gasPrice == null) {
107
- const feeRate = await this.chainInterface.Fees.getFeeRate();
108
- _gasPrice = StarknetFees_1.StarknetFees.extractFromFeeRateString(feeRate);
109
- }
110
- let l1GasCost = BigInt(lastTx.details.resourceBounds.l1_gas.max_price_per_unit);
111
- let l2GasCost = BigInt(lastTx.details.resourceBounds.l2_gas.max_price_per_unit);
112
- let l1DataGasCost = BigInt(lastTx.details.resourceBounds.l1_data_gas.max_price_per_unit);
113
- let tip = BigInt(lastTx.details.tip);
114
- let feeBumped = false;
115
- if (_gasPrice.l1GasCost > l1GasCost) {
116
- //Bump by minimum allowed or to the actual _gasPrice.l1GasCost
117
- l1GasCost = (0, Utils_1.bigIntMax)(_gasPrice.l1GasCost, this.config.minFeeIncreaseAbsolute + (l1GasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
118
- feeBumped = true;
119
- }
120
- if (_gasPrice.l1DataGasCost > l1DataGasCost) {
121
- //Bump by minimum allowed or to the actual _gasPrice.l1GasCost
122
- l1DataGasCost = (0, Utils_1.bigIntMax)(_gasPrice.l1DataGasCost, this.config.minFeeIncreaseAbsolute + (l1DataGasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
123
- feeBumped = true;
124
- }
125
- if (_gasPrice.l2GasCost > l2GasCost || feeBumped) { //In case the fees for l1 and l1Data were bumped, we also need to bump the l2GasFee regardless
126
- l2GasCost = (0, Utils_1.bigIntMax)(_gasPrice.l2GasCost, this.config.minFeeIncreaseAbsolute + (l2GasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
127
- feeBumped = true;
128
- }
129
- if (feeBumped)
130
- tip = this.config.minTipIncreaseAbsolute + (tip * (1000000n + this.config.minTipIncreasePpm) / 1000000n);
131
- if (!feeBumped) {
132
- //Not fee bumped
133
- this.logger.debug("checkPastTransactions(): Tx yet unconfirmed but not increasing fee for ", lastTx.txId);
134
- //Rebroadcast the tx
135
- await this.chainInterface.Transactions.sendTransaction(lastTx).catch(e => {
136
- if (e.baseError?.code === 52) { //Invalid transaction nonce
137
- this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already confirmed: ", lastTx.txId);
138
- return;
139
- }
140
- if (e.baseError?.code === 59) { //Transaction already in the mempool
141
- this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already known to the RPC: ", lastTx.txId);
142
- return;
143
- }
144
- this.logger.error("checkPastTransactions(): Tx re-broadcast error", e);
145
- });
146
- data.lastBumped = Date.now();
147
- continue;
148
- }
149
- const newTx = (0, transaction_1.cloneDeep)(lastTx);
150
- delete newTx.signed;
151
- delete newTx.txId;
152
- newTx.details.tip = tip;
153
- newTx.details.resourceBounds.l1_gas.max_price_per_unit = l1GasCost;
154
- newTx.details.resourceBounds.l2_gas.max_price_per_unit = l2GasCost;
155
- newTx.details.resourceBounds.l1_data_gas.max_price_per_unit = l1DataGasCost;
156
- await this._signTransaction(newTx);
157
- this.logger.info(`checkPastTransactions(): Bump fee for tx ${lastTx.txId} -> ${newTx.txId}`);
158
- //Double check pending txns still has nonce after async signTransaction was called
159
- if (!this.pendingTxs.has(nonce))
160
- continue;
161
- for (let callback of this.chainInterface.Transactions._cbksBeforeTxReplace) {
162
- try {
163
- await callback(StarknetTransactions_1.StarknetTransactions.serializeTx(lastTx), lastTx.txId, StarknetTransactions_1.StarknetTransactions.serializeTx(newTx), newTx.txId);
164
- }
165
- catch (e) {
166
- this.logger.error("checkPastTransactions(): beforeTxReplace callback error: ", e);
167
- }
168
- }
169
- data.txs.push(newTx);
170
- data.lastBumped = Date.now();
171
- this.save();
172
- this.chainInterface.Transactions._knownTxSet.add(newTx.txId);
173
- //TODO: Better error handling when sending tx
174
- await this.chainInterface.Transactions.sendTransaction(newTx).catch(e => {
175
- if (e.baseError?.code === 52) { //Invalid transaction nonce
176
- return;
177
- }
178
- this.logger.error("checkPastTransactions(): Fee-bumped tx broadcast error", e);
179
- });
180
- }
181
- }
182
- }
183
- startFeeBumper() {
184
- let func;
185
- func = async () => {
186
- try {
187
- await this.checkPastTransactions();
188
- }
189
- catch (e) {
190
- this.logger.error("startFeeBumper(): Error when check past transactions: ", e);
191
- }
192
- if (this.stopped)
193
- return;
194
- this.feeBumper = setTimeout(func, 1000);
195
- };
196
- func();
197
- }
198
- async syncNonceFromChain() {
199
- const txCount = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
200
- this.confirmedNonce = txCount - 1n;
201
- if (this.pendingNonce < this.confirmedNonce) {
202
- this.logger.info(`syncNonceFromChain(): Re-synced latest nonce from chain, adjusting local pending nonce ${this.pendingNonce} -> ${this.confirmedNonce}`);
203
- this.pendingNonce = this.confirmedNonce;
204
- for (let [nonce, data] of this.pendingTxs) {
205
- if (nonce <= this.pendingNonce) {
206
- this.pendingTxs.delete(nonce);
207
- data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
208
- this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
209
- }
210
- }
211
- this.save();
212
- }
213
- }
214
- /**
215
- * @inheritDoc
216
- */
217
- async init() {
218
- try {
219
- await (0, promises_1.mkdir)(this.directory);
220
- }
221
- catch (e) { }
222
- const nonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
223
- this.confirmedNonce = nonce - 1n;
224
- this.pendingNonce = nonce - 1n;
225
- await this.load();
226
- this.startFeeBumper();
227
- }
228
- /**
229
- * @inheritDoc
230
- */
231
- stop() {
232
- this.stopped = true;
233
- if (this.feeBumper != null) {
234
- clearTimeout(this.feeBumper);
235
- this.feeBumper = null;
236
- }
237
- return Promise.resolve();
238
- }
239
- /**
240
- * Signs and sends the starknet transaction, the `onBeforePublish` callback is called after the transaction
241
- * is signed and before it is broadcast. Ensures that transactions are always sent in order by using a
242
- * "single-threaded" promise queue, and no nonce collision happen.
243
- *
244
- * @param transaction A transaction to sign and send
245
- * @param onBeforePublish A callback that is called before the transaction gets broadcasted
246
- */
247
- sendTransaction(transaction, onBeforePublish) {
248
- return this.sendTransactionQueue.enqueue(async () => {
249
- if (transaction.details.nonce != null) {
250
- if (transaction.details.nonce !== this.pendingNonce + 1n)
251
- throw new Error("Invalid transaction nonce!");
252
- }
253
- else {
254
- transaction.details.nonce = this.pendingNonce + 1n;
255
- }
256
- const signedTx = await this._signTransaction(transaction);
257
- if (onBeforePublish != null) {
258
- try {
259
- await onBeforePublish(signedTx.txId, StarknetTransactions_1.StarknetTransactions.serializeTx(signedTx));
260
- }
261
- catch (e) {
262
- this.logger.error("sendTransaction(): Error when calling onBeforePublish function: ", e);
263
- }
264
- }
265
- const pendingTxObject = { txs: [signedTx], lastBumped: Date.now(), sending: true };
266
- this.pendingNonce++;
267
- this.logger.debug("sendTransaction(): Incrementing pending nonce to: ", this.pendingNonce);
268
- this.pendingTxs.set(transaction.details.nonce, pendingTxObject);
269
- this.save();
270
- this.chainInterface.Transactions._knownTxSet.add(signedTx.txId);
271
- try {
272
- const result = await this.chainInterface.Transactions.sendTransaction(signedTx);
273
- pendingTxObject.sending = false;
274
- return result;
275
- }
276
- catch (e) {
277
- this.chainInterface.Transactions._knownTxSet.delete(signedTx.txId);
278
- this.pendingTxs.delete(transaction.details.nonce);
279
- this.pendingNonce--;
280
- this.logger.debug("sendTransaction(): Error when broadcasting transaction, reverting pending nonce to: ", this.pendingNonce);
281
- if (e.baseError?.code === 52) { //Invalid transaction nonce
282
- //Re-check nonce from on-chain
283
- this.logger.info("sendTransaction(): Got INVALID_TRANSACTION_NONCE (52) back from backend, re-checking latest nonce from chain!");
284
- await this.syncNonceFromChain();
285
- }
286
- throw e;
287
- }
288
- });
289
- }
290
- }
291
- exports.StarknetPersistentSigner = StarknetPersistentSigner;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StarknetPersistentSigner = void 0;
4
+ const StarknetSigner_1 = require("./StarknetSigner");
5
+ const StarknetTransactions_1 = require("../chain/modules/StarknetTransactions");
6
+ const Utils_1 = require("../../utils/Utils");
7
+ const starknet_1 = require("starknet");
8
+ const promises_1 = require("fs/promises");
9
+ const StarknetFees_1 = require("../chain/modules/StarknetFees");
10
+ const transaction_1 = require("@scure/btc-signer/transaction");
11
+ const promise_queue_ts_1 = require("promise-queue-ts");
12
+ const WAIT_BEFORE_BUMP = 15 * 1000;
13
+ const MIN_FEE_INCREASE_ABSOLUTE = 1n * 1000000n; //0.001GWei
14
+ const MIN_FEE_INCREASE_PPM = 110000n; // +11%
15
+ const MIN_TIP_INCREASE_ABSOLUTE = 1n * 1000000000n; //1GWei
16
+ const MIN_TIP_INCREASE_PPM = 110000n; // +11%
17
+ /**
18
+ * A complex starknet signer implementation with internal nonce management, with race condition preventions,
19
+ * automatic transaction rebroadcasting and failovers. Uses the NodeJS `fs` library to persist transaction
20
+ * data across application restarts, hence this doesn't work on frontends and is intended to be used as a
21
+ * robust backend wallet implementation.
22
+ *
23
+ * @category Wallets
24
+ */
25
+ class StarknetPersistentSigner extends StarknetSigner_1.StarknetSigner {
26
+ constructor(account, chainInterface, directory, config) {
27
+ var _a, _b, _c, _d, _e;
28
+ super(account, true);
29
+ this.pendingTxs = new Map();
30
+ this.confirmedNonce = 0n;
31
+ this.pendingNonce = 0n;
32
+ this.stopped = false;
33
+ this.saveCount = 0;
34
+ this.sendTransactionQueue = new promise_queue_ts_1.PromiseQueue();
35
+ this.signTransaction = undefined;
36
+ this.chainInterface = chainInterface;
37
+ this.directory = directory;
38
+ this.config = config ?? {};
39
+ (_a = this.config).minFeeIncreaseAbsolute ?? (_a.minFeeIncreaseAbsolute = MIN_FEE_INCREASE_ABSOLUTE);
40
+ (_b = this.config).minFeeIncreasePpm ?? (_b.minFeeIncreasePpm = MIN_FEE_INCREASE_PPM);
41
+ (_c = this.config).minTipIncreaseAbsolute ?? (_c.minTipIncreaseAbsolute = MIN_TIP_INCREASE_ABSOLUTE);
42
+ (_d = this.config).minTipIncreasePpm ?? (_d.minTipIncreasePpm = MIN_TIP_INCREASE_PPM);
43
+ (_e = this.config).waitBeforeBump ?? (_e.waitBeforeBump = WAIT_BEFORE_BUMP);
44
+ this.logger = (0, Utils_1.getLogger)("StarknetPersistentSigner(" + this.account.address + "): ");
45
+ }
46
+ async load() {
47
+ const fileExists = await (0, promises_1.access)(this.directory + "/txs.json", promises_1.constants.F_OK).then(() => true).catch(() => false);
48
+ if (!fileExists)
49
+ return;
50
+ const res = await (0, promises_1.readFile)(this.directory + "/txs.json");
51
+ if (res != null) {
52
+ const pendingTxs = JSON.parse(res.toString());
53
+ for (let nonceStr in pendingTxs) {
54
+ const nonceData = pendingTxs[nonceStr];
55
+ const nonce = BigInt(nonceStr);
56
+ if (this.confirmedNonce >= nonce)
57
+ continue; //Already confirmed
58
+ if (this.pendingNonce < nonce) {
59
+ this.pendingNonce = nonce;
60
+ }
61
+ const parsedPendingTxns = nonceData.txs.map(StarknetTransactions_1.StarknetTransactions.deserializeTx);
62
+ this.pendingTxs.set(nonce, {
63
+ txs: parsedPendingTxns,
64
+ lastBumped: nonceData.lastBumped
65
+ });
66
+ for (let tx of parsedPendingTxns) {
67
+ this.chainInterface.Transactions._knownTxSet.add(tx.txId);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ async save() {
73
+ const pendingTxs = {};
74
+ for (let [nonce, data] of this.pendingTxs) {
75
+ pendingTxs[nonce.toString(10)] = {
76
+ lastBumped: data.lastBumped,
77
+ txs: data.txs.map(StarknetTransactions_1.StarknetTransactions.serializeTx)
78
+ };
79
+ }
80
+ const requiredSaveCount = ++this.saveCount;
81
+ if (this.priorSavePromise != null) {
82
+ await this.priorSavePromise;
83
+ }
84
+ if (requiredSaveCount === this.saveCount) {
85
+ this.priorSavePromise = (0, promises_1.writeFile)(this.directory + "/txs.json", JSON.stringify(pendingTxs));
86
+ await this.priorSavePromise;
87
+ }
88
+ }
89
+ async checkPastTransactions() {
90
+ let _gasPrice = null;
91
+ let _safeBlockNonce = null;
92
+ for (let [nonce, data] of this.pendingTxs) {
93
+ if (!data.sending && data.lastBumped < Date.now() - this.config.waitBeforeBump) {
94
+ if (_safeBlockNonce == null) {
95
+ _safeBlockNonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
96
+ this.confirmedNonce = _safeBlockNonce - 1n;
97
+ }
98
+ if (this.confirmedNonce >= nonce) {
99
+ this.pendingTxs.delete(nonce);
100
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
101
+ this.logger.info("checkPastTransactions(): Tx confirmed, required fee bumps: ", data.txs.length);
102
+ this.save();
103
+ continue;
104
+ }
105
+ const lastTx = data.txs[data.txs.length - 1];
106
+ if (_gasPrice == null) {
107
+ const feeRate = await this.chainInterface.Fees.getFeeRate();
108
+ _gasPrice = StarknetFees_1.StarknetFees.extractFromFeeRateString(feeRate);
109
+ }
110
+ let l1GasCost = BigInt(lastTx.details.resourceBounds.l1_gas.max_price_per_unit);
111
+ let l2GasCost = BigInt(lastTx.details.resourceBounds.l2_gas.max_price_per_unit);
112
+ let l1DataGasCost = BigInt(lastTx.details.resourceBounds.l1_data_gas.max_price_per_unit);
113
+ let tip = BigInt(lastTx.details.tip);
114
+ let feeBumped = false;
115
+ if (_gasPrice.l1GasCost > l1GasCost) {
116
+ //Bump by minimum allowed or to the actual _gasPrice.l1GasCost
117
+ l1GasCost = (0, Utils_1.bigIntMax)(_gasPrice.l1GasCost, this.config.minFeeIncreaseAbsolute + (l1GasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
118
+ feeBumped = true;
119
+ }
120
+ if (_gasPrice.l1DataGasCost > l1DataGasCost) {
121
+ //Bump by minimum allowed or to the actual _gasPrice.l1GasCost
122
+ l1DataGasCost = (0, Utils_1.bigIntMax)(_gasPrice.l1DataGasCost, this.config.minFeeIncreaseAbsolute + (l1DataGasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
123
+ feeBumped = true;
124
+ }
125
+ if (_gasPrice.l2GasCost > l2GasCost || feeBumped) { //In case the fees for l1 and l1Data were bumped, we also need to bump the l2GasFee regardless
126
+ l2GasCost = (0, Utils_1.bigIntMax)(_gasPrice.l2GasCost, this.config.minFeeIncreaseAbsolute + (l2GasCost * (1000000n + this.config.minFeeIncreasePpm) / 1000000n));
127
+ feeBumped = true;
128
+ }
129
+ if (feeBumped)
130
+ tip = this.config.minTipIncreaseAbsolute + (tip * (1000000n + this.config.minTipIncreasePpm) / 1000000n);
131
+ if (!feeBumped) {
132
+ //Not fee bumped
133
+ this.logger.debug("checkPastTransactions(): Tx yet unconfirmed but not increasing fee for ", lastTx.txId);
134
+ //Rebroadcast the tx
135
+ await this.chainInterface.Transactions.sendTransaction(lastTx).catch(e => {
136
+ if (e.baseError?.code === 52) { //Invalid transaction nonce
137
+ this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already confirmed: ", lastTx.txId);
138
+ return;
139
+ }
140
+ if (e.baseError?.code === 59) { //Transaction already in the mempool
141
+ this.logger.debug("checkPastTransactions(): Tx re-broadcast success, tx already known to the RPC: ", lastTx.txId);
142
+ return;
143
+ }
144
+ this.logger.error("checkPastTransactions(): Tx re-broadcast error", e);
145
+ });
146
+ data.lastBumped = Date.now();
147
+ continue;
148
+ }
149
+ const newTx = (0, transaction_1.cloneDeep)(lastTx);
150
+ delete newTx.signed;
151
+ delete newTx.txId;
152
+ newTx.details.tip = tip;
153
+ newTx.details.resourceBounds.l1_gas.max_price_per_unit = l1GasCost;
154
+ newTx.details.resourceBounds.l2_gas.max_price_per_unit = l2GasCost;
155
+ newTx.details.resourceBounds.l1_data_gas.max_price_per_unit = l1DataGasCost;
156
+ await this._signTransaction(newTx);
157
+ this.logger.info(`checkPastTransactions(): Bump fee for tx ${lastTx.txId} -> ${newTx.txId}`);
158
+ //Double check pending txns still has nonce after async signTransaction was called
159
+ if (!this.pendingTxs.has(nonce))
160
+ continue;
161
+ for (let callback of this.chainInterface.Transactions._cbksBeforeTxReplace) {
162
+ try {
163
+ await callback(StarknetTransactions_1.StarknetTransactions.serializeTx(lastTx), lastTx.txId, StarknetTransactions_1.StarknetTransactions.serializeTx(newTx), newTx.txId);
164
+ }
165
+ catch (e) {
166
+ this.logger.error("checkPastTransactions(): beforeTxReplace callback error: ", e);
167
+ }
168
+ }
169
+ data.txs.push(newTx);
170
+ data.lastBumped = Date.now();
171
+ this.save();
172
+ this.chainInterface.Transactions._knownTxSet.add(newTx.txId);
173
+ //TODO: Better error handling when sending tx
174
+ await this.chainInterface.Transactions.sendTransaction(newTx).catch(e => {
175
+ if (e.baseError?.code === 52) { //Invalid transaction nonce
176
+ return;
177
+ }
178
+ this.logger.error("checkPastTransactions(): Fee-bumped tx broadcast error", e);
179
+ });
180
+ }
181
+ }
182
+ }
183
+ startFeeBumper() {
184
+ let func;
185
+ func = async () => {
186
+ try {
187
+ await this.checkPastTransactions();
188
+ }
189
+ catch (e) {
190
+ this.logger.error("startFeeBumper(): Error when check past transactions: ", e);
191
+ }
192
+ if (this.stopped)
193
+ return;
194
+ this.feeBumper = setTimeout(func, 1000);
195
+ };
196
+ func();
197
+ }
198
+ async syncNonceFromChain() {
199
+ const txCount = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
200
+ this.confirmedNonce = txCount - 1n;
201
+ if (this.pendingNonce < this.confirmedNonce) {
202
+ this.logger.info(`syncNonceFromChain(): Re-synced latest nonce from chain, adjusting local pending nonce ${this.pendingNonce} -> ${this.confirmedNonce}`);
203
+ this.pendingNonce = this.confirmedNonce;
204
+ for (let [nonce, data] of this.pendingTxs) {
205
+ if (nonce <= this.pendingNonce) {
206
+ this.pendingTxs.delete(nonce);
207
+ data.txs.forEach(tx => this.chainInterface.Transactions._knownTxSet.delete(tx.txId));
208
+ this.logger.info(`syncNonceFromChain(): Tx confirmed, nonce: ${nonce}, required fee bumps: `, data.txs.length);
209
+ }
210
+ }
211
+ this.save();
212
+ }
213
+ }
214
+ /**
215
+ * @inheritDoc
216
+ */
217
+ async init() {
218
+ try {
219
+ await (0, promises_1.mkdir)(this.directory);
220
+ }
221
+ catch (e) { }
222
+ const nonce = await this.chainInterface.Transactions.getNonce(this.account.address, starknet_1.BlockTag.LATEST);
223
+ this.confirmedNonce = nonce - 1n;
224
+ this.pendingNonce = nonce - 1n;
225
+ await this.load();
226
+ this.startFeeBumper();
227
+ }
228
+ /**
229
+ * @inheritDoc
230
+ */
231
+ stop() {
232
+ this.stopped = true;
233
+ if (this.feeBumper != null) {
234
+ clearTimeout(this.feeBumper);
235
+ this.feeBumper = null;
236
+ }
237
+ return Promise.resolve();
238
+ }
239
+ /**
240
+ * Signs and sends the starknet transaction, the `onBeforePublish` callback is called after the transaction
241
+ * is signed and before it is broadcast. Ensures that transactions are always sent in order by using a
242
+ * "single-threaded" promise queue, and no nonce collision happen.
243
+ *
244
+ * @param transaction A transaction to sign and send
245
+ * @param onBeforePublish A callback that is called before the transaction gets broadcasted
246
+ */
247
+ sendTransaction(transaction, onBeforePublish) {
248
+ return this.sendTransactionQueue.enqueue(async () => {
249
+ if (transaction.details.nonce != null) {
250
+ if (transaction.details.nonce !== this.pendingNonce + 1n)
251
+ throw new Error("Invalid transaction nonce!");
252
+ }
253
+ else {
254
+ transaction.details.nonce = this.pendingNonce + 1n;
255
+ }
256
+ const signedTx = await this._signTransaction(transaction);
257
+ if (onBeforePublish != null) {
258
+ try {
259
+ await onBeforePublish(signedTx.txId, StarknetTransactions_1.StarknetTransactions.serializeTx(signedTx));
260
+ }
261
+ catch (e) {
262
+ this.logger.error("sendTransaction(): Error when calling onBeforePublish function: ", e);
263
+ }
264
+ }
265
+ const pendingTxObject = { txs: [signedTx], lastBumped: Date.now(), sending: true };
266
+ this.pendingNonce++;
267
+ this.logger.debug("sendTransaction(): Incrementing pending nonce to: ", this.pendingNonce);
268
+ this.pendingTxs.set(transaction.details.nonce, pendingTxObject);
269
+ this.save();
270
+ this.chainInterface.Transactions._knownTxSet.add(signedTx.txId);
271
+ try {
272
+ const result = await this.chainInterface.Transactions.sendTransaction(signedTx);
273
+ pendingTxObject.sending = false;
274
+ return result;
275
+ }
276
+ catch (e) {
277
+ this.chainInterface.Transactions._knownTxSet.delete(signedTx.txId);
278
+ this.pendingTxs.delete(transaction.details.nonce);
279
+ this.pendingNonce--;
280
+ this.logger.debug("sendTransaction(): Error when broadcasting transaction, reverting pending nonce to: ", this.pendingNonce);
281
+ if (e.baseError?.code === 52) { //Invalid transaction nonce
282
+ //Re-check nonce from on-chain
283
+ this.logger.info("sendTransaction(): Got INVALID_TRANSACTION_NONCE (52) back from backend, re-checking latest nonce from chain!");
284
+ await this.syncNonceFromChain();
285
+ }
286
+ throw e;
287
+ }
288
+ });
289
+ }
290
+ }
291
+ exports.StarknetPersistentSigner = StarknetPersistentSigner;