@aztec/ethereum 3.0.0-canary.a9708bd → 3.0.0-devnet.2

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 (144) hide show
  1. package/dest/client.d.ts +1 -1
  2. package/dest/client.d.ts.map +1 -1
  3. package/dest/config.d.ts +11 -6
  4. package/dest/config.d.ts.map +1 -1
  5. package/dest/config.js +124 -64
  6. package/dest/contracts/empire_base.d.ts +1 -1
  7. package/dest/contracts/empire_base.d.ts.map +1 -1
  8. package/dest/contracts/empire_slashing_proposer.d.ts +2 -2
  9. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -1
  10. package/dest/contracts/empire_slashing_proposer.js +1 -1
  11. package/dest/contracts/fee_asset_handler.d.ts +3 -3
  12. package/dest/contracts/fee_asset_handler.d.ts.map +1 -1
  13. package/dest/contracts/governance.js +7 -3
  14. package/dest/contracts/governance_proposer.d.ts +1 -2
  15. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  16. package/dest/contracts/governance_proposer.js +1 -2
  17. package/dest/contracts/multicall.d.ts +3 -5
  18. package/dest/contracts/multicall.d.ts.map +1 -1
  19. package/dest/contracts/multicall.js +6 -4
  20. package/dest/contracts/rollup.d.ts +39 -19
  21. package/dest/contracts/rollup.d.ts.map +1 -1
  22. package/dest/contracts/rollup.js +84 -88
  23. package/dest/contracts/slasher_contract.d.ts +10 -0
  24. package/dest/contracts/slasher_contract.d.ts.map +1 -1
  25. package/dest/contracts/slasher_contract.js +18 -0
  26. package/dest/contracts/tally_slashing_proposer.d.ts +22 -3
  27. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -1
  28. package/dest/contracts/tally_slashing_proposer.js +55 -5
  29. package/dest/deploy_l1_contracts.d.ts +22 -7
  30. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  31. package/dest/deploy_l1_contracts.js +555 -362
  32. package/dest/index.d.ts +1 -1
  33. package/dest/index.d.ts.map +1 -1
  34. package/dest/index.js +1 -1
  35. package/dest/l1_artifacts.d.ts +8729 -6014
  36. package/dest/l1_artifacts.d.ts.map +1 -1
  37. package/dest/l1_artifacts.js +10 -5
  38. package/dest/l1_contract_addresses.d.ts +5 -1
  39. package/dest/l1_contract_addresses.d.ts.map +1 -1
  40. package/dest/l1_contract_addresses.js +16 -26
  41. package/dest/l1_reader.d.ts +1 -1
  42. package/dest/l1_reader.d.ts.map +1 -1
  43. package/dest/l1_reader.js +8 -8
  44. package/dest/l1_tx_utils/config.d.ts +59 -0
  45. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  46. package/dest/l1_tx_utils/config.js +73 -0
  47. package/dest/l1_tx_utils/constants.d.ts +6 -0
  48. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  49. package/dest/l1_tx_utils/constants.js +14 -0
  50. package/dest/l1_tx_utils/factory.d.ts +24 -0
  51. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  52. package/dest/l1_tx_utils/factory.js +12 -0
  53. package/dest/l1_tx_utils/index.d.ts +10 -0
  54. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  55. package/dest/l1_tx_utils/index.js +10 -0
  56. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  57. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  58. package/dest/l1_tx_utils/interfaces.js +4 -0
  59. package/dest/l1_tx_utils/l1_tx_utils.d.ts +95 -0
  60. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  61. package/dest/l1_tx_utils/l1_tx_utils.js +610 -0
  62. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts +26 -0
  63. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts.map +1 -0
  64. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.js +26 -0
  65. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +81 -0
  66. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  67. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +294 -0
  68. package/dest/l1_tx_utils/signer.d.ts +4 -0
  69. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  70. package/dest/l1_tx_utils/signer.js +16 -0
  71. package/dest/l1_tx_utils/types.d.ts +67 -0
  72. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  73. package/dest/l1_tx_utils/types.js +26 -0
  74. package/dest/l1_tx_utils/utils.d.ts +4 -0
  75. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  76. package/dest/l1_tx_utils/utils.js +14 -0
  77. package/dest/publisher_manager.d.ts +7 -2
  78. package/dest/publisher_manager.d.ts.map +1 -1
  79. package/dest/publisher_manager.js +36 -8
  80. package/dest/queries.d.ts.map +1 -1
  81. package/dest/queries.js +11 -12
  82. package/dest/test/chain_monitor.d.ts +11 -0
  83. package/dest/test/chain_monitor.d.ts.map +1 -1
  84. package/dest/test/chain_monitor.js +81 -12
  85. package/dest/test/delayed_tx_utils.d.ts +2 -2
  86. package/dest/test/delayed_tx_utils.d.ts.map +1 -1
  87. package/dest/test/delayed_tx_utils.js +2 -2
  88. package/dest/test/eth_cheat_codes.d.ts +32 -6
  89. package/dest/test/eth_cheat_codes.d.ts.map +1 -1
  90. package/dest/test/eth_cheat_codes.js +115 -28
  91. package/dest/test/rollup_cheat_codes.d.ts +11 -9
  92. package/dest/test/rollup_cheat_codes.d.ts.map +1 -1
  93. package/dest/test/rollup_cheat_codes.js +38 -6
  94. package/dest/test/upgrade_utils.d.ts.map +1 -1
  95. package/dest/test/upgrade_utils.js +3 -2
  96. package/dest/utils.d.ts.map +1 -1
  97. package/dest/utils.js +10 -161
  98. package/dest/zkPassportVerifierAddress.js +1 -1
  99. package/package.json +7 -7
  100. package/src/client.ts +1 -1
  101. package/src/config.ts +136 -68
  102. package/src/contracts/empire_base.ts +1 -1
  103. package/src/contracts/empire_slashing_proposer.ts +7 -3
  104. package/src/contracts/fee_asset_handler.ts +1 -1
  105. package/src/contracts/governance.ts +3 -3
  106. package/src/contracts/governance_proposer.ts +3 -4
  107. package/src/contracts/multicall.ts +12 -10
  108. package/src/contracts/rollup.ts +104 -106
  109. package/src/contracts/slasher_contract.ts +22 -0
  110. package/src/contracts/tally_slashing_proposer.ts +54 -6
  111. package/src/deploy_l1_contracts.ts +570 -328
  112. package/src/index.ts +1 -1
  113. package/src/l1_artifacts.ts +14 -6
  114. package/src/l1_contract_addresses.ts +17 -26
  115. package/src/l1_reader.ts +9 -9
  116. package/src/l1_tx_utils/README.md +177 -0
  117. package/src/l1_tx_utils/config.ts +140 -0
  118. package/src/l1_tx_utils/constants.ts +18 -0
  119. package/src/l1_tx_utils/factory.ts +64 -0
  120. package/src/l1_tx_utils/index.ts +12 -0
  121. package/src/l1_tx_utils/interfaces.ts +86 -0
  122. package/src/l1_tx_utils/l1_tx_utils.ts +718 -0
  123. package/src/l1_tx_utils/l1_tx_utils_with_blobs.ts +77 -0
  124. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +372 -0
  125. package/src/l1_tx_utils/signer.ts +28 -0
  126. package/src/l1_tx_utils/types.ts +85 -0
  127. package/src/l1_tx_utils/utils.ts +16 -0
  128. package/src/publisher_manager.ts +51 -9
  129. package/src/queries.ts +13 -8
  130. package/src/test/chain_monitor.ts +89 -9
  131. package/src/test/delayed_tx_utils.ts +2 -2
  132. package/src/test/eth_cheat_codes.ts +142 -29
  133. package/src/test/rollup_cheat_codes.ts +54 -14
  134. package/src/test/upgrade_utils.ts +3 -2
  135. package/src/utils.ts +13 -185
  136. package/src/zkPassportVerifierAddress.ts +1 -1
  137. package/dest/l1_tx_utils.d.ts +0 -250
  138. package/dest/l1_tx_utils.d.ts.map +0 -1
  139. package/dest/l1_tx_utils.js +0 -826
  140. package/dest/l1_tx_utils_with_blobs.d.ts +0 -19
  141. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  142. package/dest/l1_tx_utils_with_blobs.js +0 -85
  143. package/src/l1_tx_utils.ts +0 -1105
  144. package/src/l1_tx_utils_with_blobs.ts +0 -144
@@ -0,0 +1,718 @@
1
+ import { maxBigint } from '@aztec/foundation/bigint';
2
+ import { merge, pick } from '@aztec/foundation/collection';
3
+ import { InterruptError, TimeoutError } from '@aztec/foundation/error';
4
+ import { EthAddress } from '@aztec/foundation/eth-address';
5
+ import { type Logger, createLogger } from '@aztec/foundation/log';
6
+ import { retryUntil } from '@aztec/foundation/retry';
7
+ import { sleep } from '@aztec/foundation/sleep';
8
+ import { DateProvider } from '@aztec/foundation/timer';
9
+ import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
10
+
11
+ import pickBy from 'lodash.pickby';
12
+ import {
13
+ type Abi,
14
+ type BlockOverrides,
15
+ type Hex,
16
+ type NonceManager,
17
+ type PrepareTransactionRequestRequest,
18
+ type StateOverride,
19
+ type TransactionReceipt,
20
+ type TransactionSerializable,
21
+ createNonceManager,
22
+ formatGwei,
23
+ serializeTransaction,
24
+ } from 'viem';
25
+ import { jsonRpc } from 'viem/nonce';
26
+
27
+ import type { ViemClient } from '../types.js';
28
+ import { formatViemError } from '../utils.js';
29
+ import { type L1TxUtilsConfig, l1TxUtilsConfigMappings } from './config.js';
30
+ import { LARGE_GAS_LIMIT } from './constants.js';
31
+ import type { IL1TxMetrics, IL1TxStore } from './interfaces.js';
32
+ import { ReadOnlyL1TxUtils } from './readonly_l1_tx_utils.js';
33
+ import {
34
+ DroppedTransactionError,
35
+ type L1BlobInputs,
36
+ type L1TxConfig,
37
+ type L1TxRequest,
38
+ type L1TxState,
39
+ type SigningCallback,
40
+ TerminalTxUtilsState,
41
+ TxUtilsState,
42
+ UnknownMinedTxError,
43
+ } from './types.js';
44
+
45
+ const MAX_L1_TX_STATES = 32;
46
+
47
+ export class L1TxUtils extends ReadOnlyL1TxUtils {
48
+ protected nonceManager: NonceManager;
49
+ protected txs: L1TxState[] = [];
50
+
51
+ constructor(
52
+ public override client: ViemClient,
53
+ public address: EthAddress,
54
+ protected signer: SigningCallback,
55
+ logger: Logger = createLogger('ethereum:publisher'),
56
+ dateProvider: DateProvider = new DateProvider(),
57
+ config?: Partial<L1TxUtilsConfig>,
58
+ debugMaxGasLimit: boolean = false,
59
+ protected store?: IL1TxStore,
60
+ protected metrics?: IL1TxMetrics,
61
+ ) {
62
+ super(client, logger, dateProvider, config, debugMaxGasLimit);
63
+ this.nonceManager = createNonceManager({ source: jsonRpc() });
64
+ }
65
+
66
+ public get state() {
67
+ return this.txs.at(-1)?.status ?? TxUtilsState.IDLE;
68
+ }
69
+
70
+ public get lastMinedAtBlockNumber() {
71
+ const minedBlockNumbers = this.txs.map(tx => tx.receipt?.blockNumber).filter(bn => bn !== undefined);
72
+ return minedBlockNumbers.length === 0 ? undefined : maxBigint(...minedBlockNumbers);
73
+ }
74
+
75
+ protected async updateState(l1TxState: L1TxState, newState: TxUtilsState.MINED, l1Timestamp: number): Promise<void>;
76
+ protected async updateState(l1TxState: L1TxState, newState: TxUtilsState, l1Timestamp?: undefined): Promise<void>;
77
+ protected async updateState(l1TxState: L1TxState, newState: TxUtilsState, l1Timestamp?: number) {
78
+ const oldState = l1TxState.status;
79
+ l1TxState.status = newState;
80
+ const sender = this.getSenderAddress().toString();
81
+ this.logger.debug(
82
+ `Tx state changed from ${TxUtilsState[oldState]} to ${TxUtilsState[newState]} for nonce ${l1TxState.nonce} account ${sender}`,
83
+ );
84
+
85
+ // Record metrics
86
+ if (newState === TxUtilsState.MINED && l1Timestamp !== undefined) {
87
+ this.metrics?.recordMinedTx(l1TxState, new Date(l1Timestamp));
88
+ } else if (newState === TxUtilsState.NOT_MINED) {
89
+ this.metrics?.recordDroppedTx(l1TxState);
90
+ }
91
+
92
+ // Update state in the store
93
+ await this.store
94
+ ?.saveState(sender, l1TxState)
95
+ .catch(err => this.logger.error('Failed to persist L1 tx state', err));
96
+ }
97
+
98
+ public updateConfig(newConfig: Partial<L1TxUtilsConfig>) {
99
+ this.config = merge(this.config, newConfig);
100
+ this.logger.info(
101
+ 'Updated L1TxUtils config',
102
+ pickBy(newConfig, (_, key) => key in l1TxUtilsConfigMappings),
103
+ );
104
+ }
105
+
106
+ public getSenderAddress() {
107
+ return this.address;
108
+ }
109
+
110
+ public getSenderBalance(): Promise<bigint> {
111
+ return this.client.getBalance({
112
+ address: this.getSenderAddress().toString(),
113
+ });
114
+ }
115
+
116
+ /**
117
+ * Rehydrates transaction states from the store and resumes monitoring for pending transactions.
118
+ * This should be called on startup to restore state and resume monitoring of any in-flight transactions.
119
+ */
120
+ public async loadStateAndResumeMonitoring(): Promise<void> {
121
+ if (!this.store) {
122
+ return;
123
+ }
124
+
125
+ const account = this.getSenderAddress().toString();
126
+ const loadedStates = await this.store.loadStates(account);
127
+
128
+ if (loadedStates.length === 0) {
129
+ this.logger.debug(`No states to rehydrate for account ${account}`);
130
+ return;
131
+ }
132
+
133
+ // Convert loaded states (which have id) to the txs format
134
+ this.txs = loadedStates;
135
+ this.logger.info(`Rehydrated ${loadedStates.length} tx states for account ${account}`);
136
+
137
+ // Find all pending states and resume monitoring
138
+ const pendingStates = loadedStates.filter(state => !TerminalTxUtilsState.includes(state.status));
139
+ if (pendingStates.length === 0) {
140
+ return;
141
+ }
142
+
143
+ this.logger.info(`Resuming monitoring for ${pendingStates.length} pending transactions for account ${account}`, {
144
+ txs: pendingStates.map(s => ({ id: s.id, nonce: s.nonce, status: TxUtilsState[s.status] })),
145
+ });
146
+
147
+ for (const state of pendingStates) {
148
+ void this.monitorTransaction(state).catch(err => {
149
+ this.logger.error(
150
+ `Error monitoring rehydrated tx with nonce ${state.nonce} for account ${account}`,
151
+ formatViemError(err),
152
+ { nonce: state.nonce, account },
153
+ );
154
+ });
155
+ }
156
+ }
157
+
158
+ private async signTransaction(txRequest: TransactionSerializable): Promise<`0x${string}`> {
159
+ const signature = await this.signer(txRequest, this.getSenderAddress());
160
+ return serializeTransaction(txRequest, signature);
161
+ }
162
+
163
+ protected async prepareSignedTransaction(txData: PrepareTransactionRequestRequest) {
164
+ const txRequest = await this.client.prepareTransactionRequest(txData);
165
+ return await this.signTransaction(txRequest as TransactionSerializable);
166
+ }
167
+
168
+ /**
169
+ * Sends a transaction with gas estimation and pricing
170
+ * @param request - The transaction request (to, data, value)
171
+ * @param gasConfig - Optional gas configuration
172
+ * @returns The transaction hash and parameters used
173
+ */
174
+ public async sendTransaction(
175
+ request: L1TxRequest,
176
+ gasConfigOverrides?: L1TxConfig,
177
+ blobInputs?: L1BlobInputs,
178
+ stateChange: TxUtilsState = TxUtilsState.SENT,
179
+ ): Promise<{ txHash: Hex; state: L1TxState }> {
180
+ if (this.interrupted) {
181
+ throw new InterruptError(`Transaction sending is interrupted`);
182
+ }
183
+
184
+ try {
185
+ const gasConfig = merge(this.config, gasConfigOverrides);
186
+ const account = this.getSenderAddress().toString();
187
+
188
+ let gasLimit: bigint;
189
+ if (this.debugMaxGasLimit) {
190
+ gasLimit = LARGE_GAS_LIMIT;
191
+ } else if (gasConfig.gasLimit) {
192
+ gasLimit = gasConfig.gasLimit;
193
+ } else {
194
+ gasLimit = await this.estimateGas(account, request, gasConfig);
195
+ }
196
+ this.logger.trace(`Computed gas limit ${gasLimit}`, { gasLimit, ...request });
197
+
198
+ const gasPrice = await this.getGasPrice(gasConfig, !!blobInputs);
199
+
200
+ if (this.interrupted) {
201
+ throw new InterruptError(`Transaction sending is interrupted`);
202
+ }
203
+
204
+ const nonce = await this.nonceManager.consume({
205
+ client: this.client,
206
+ address: account,
207
+ chainId: this.client.chain.id,
208
+ });
209
+
210
+ const baseState = { request, gasLimit, blobInputs, gasPrice, nonce };
211
+ const txData = this.makeTxData(baseState, { isCancelTx: false });
212
+
213
+ const now = new Date(await this.getL1Timestamp());
214
+ if (gasConfig.txTimeoutAt && now > gasConfig.txTimeoutAt) {
215
+ throw new TimeoutError(
216
+ `Transaction timed out before sending (now ${now.toISOString()} > timeoutAt ${gasConfig.txTimeoutAt.toISOString()})`,
217
+ );
218
+ }
219
+
220
+ // Send the new tx
221
+ const signedRequest = await this.prepareSignedTransaction(txData);
222
+ const txHash = await this.client.sendRawTransaction({ serializedTransaction: signedRequest });
223
+
224
+ // Create the new state for monitoring
225
+ const l1TxState: L1TxState = {
226
+ ...baseState,
227
+ id: (await this.store?.consumeNextStateId(account)) ?? Math.max(...this.txs.map(tx => tx.id), 0),
228
+ txHashes: [txHash],
229
+ cancelTxHashes: [],
230
+ status: TxUtilsState.IDLE,
231
+ txConfigOverrides: gasConfigOverrides ?? {},
232
+ sentAtL1Ts: now,
233
+ lastSentAtL1Ts: now,
234
+ };
235
+
236
+ // And persist it
237
+ await this.updateState(l1TxState, stateChange);
238
+ await this.store?.saveBlobs(account, l1TxState.id, blobInputs);
239
+ this.txs.push(l1TxState);
240
+
241
+ // Clean up stale states
242
+ if (this.txs.length > MAX_L1_TX_STATES) {
243
+ const removed = this.txs.shift();
244
+ if (removed && this.store) {
245
+ await this.store.deleteState(account, removed.id);
246
+ }
247
+ }
248
+
249
+ this.logger.info(`Sent L1 transaction ${txHash}`, {
250
+ to: request.to,
251
+ value: request.value,
252
+ nonce,
253
+ account,
254
+ sentAt: now,
255
+ gasLimit,
256
+ maxFeePerGas: formatGwei(gasPrice.maxFeePerGas),
257
+ maxPriorityFeePerGas: formatGwei(gasPrice.maxPriorityFeePerGas),
258
+ ...(gasPrice.maxFeePerBlobGas && { maxFeePerBlobGas: formatGwei(gasPrice.maxFeePerBlobGas) }),
259
+ isBlobTx: !!blobInputs,
260
+ txConfig: gasConfigOverrides,
261
+ });
262
+
263
+ return { txHash, state: l1TxState };
264
+ } catch (err: any) {
265
+ const viemError = formatViemError(err, request.abi);
266
+ this.logger.error(`Failed to send L1 transaction`, viemError, {
267
+ request: pick(request, 'to', 'value'),
268
+ });
269
+ throw viemError;
270
+ }
271
+ }
272
+
273
+ private async tryGetTxReceipt(
274
+ txHashes: Hex[],
275
+ nonce: number,
276
+ isCancelTx: boolean,
277
+ ): Promise<TransactionReceipt | undefined> {
278
+ for (const hash of txHashes) {
279
+ try {
280
+ const receipt = await this.client.getTransactionReceipt({ hash });
281
+ if (receipt) {
282
+ const account = this.getSenderAddress().toString();
283
+ const what = isCancelTx ? 'Cancellation L1 transaction' : 'L1 transaction';
284
+ if (receipt.status === 'reverted') {
285
+ this.logger.warn(`${what} ${hash} with nonce ${nonce} reverted`, { receipt, nonce, account });
286
+ } else {
287
+ this.logger.info(`${what} ${hash} with nonce ${nonce} mined`, { receipt, nonce, account });
288
+ }
289
+ return receipt;
290
+ }
291
+ } catch (err) {
292
+ if (err instanceof Error && err.name === 'TransactionReceiptNotFoundError') {
293
+ continue;
294
+ } else {
295
+ this.logger.error(`Error getting receipt for tx ${hash}`, err);
296
+ continue;
297
+ }
298
+ }
299
+ }
300
+ }
301
+
302
+ /**
303
+ * Returns whether a given tx should be considered as timed out and no longer monitored.
304
+ * Relies on the txTimeout setting for user txs, and on the txCancellationTimeout for cancel txs.
305
+ * @remarks We check against the latestBlockTimestamp as opposed to the current time to avoid a race condition where
306
+ * the tx is mined in a block with the same timestamp as txTimeoutAt, but our execution node has not yet processed it,
307
+ * or the loop here has not yet checked the tx before that timeout.
308
+ */
309
+ private isTxTimedOut(state: L1TxState, l1Timestamp: number) {
310
+ const account = this.getSenderAddress().toString();
311
+ const { nonce } = state;
312
+
313
+ const txConfig = merge(this.config, state.txConfigOverrides);
314
+ const { txTimeoutAt, txTimeoutMs, maxSpeedUpAttempts } = txConfig;
315
+ const isCancelTx = state.cancelTxHashes.length > 0;
316
+
317
+ this.logger.trace(`Tx timeout check for ${account} with nonce ${nonce}`, {
318
+ nonce,
319
+ account,
320
+ l1Timestamp,
321
+ lastAttemptSent: state.lastSentAtL1Ts.getTime(),
322
+ initialTxTime: state.sentAtL1Ts.getTime(),
323
+ now: this.dateProvider.now(),
324
+ txTimeoutAt,
325
+ txTimeoutMs,
326
+ interrupted: this.interrupted,
327
+ });
328
+
329
+ if (this.interrupted) {
330
+ this.logger.warn(`Tx monitoring interrupted during nonce ${nonce} for ${account} check`, { nonce, account });
331
+ return true;
332
+ }
333
+
334
+ if (isCancelTx) {
335
+ // Note that we check against the lastSentAt time for cancellations, since we time them out
336
+ // after the last attempt to submit them, not the initial time.
337
+ const attempts = state.cancelTxHashes.length;
338
+ return (
339
+ attempts > maxSpeedUpAttempts &&
340
+ state.lastSentAtL1Ts.getTime() + txConfig.txCancellationFinalTimeoutMs! <= l1Timestamp
341
+ );
342
+ }
343
+
344
+ return (
345
+ (txTimeoutAt !== undefined && l1Timestamp >= txTimeoutAt.getTime()) ||
346
+ (txTimeoutMs !== undefined && txTimeoutMs > 0 && l1Timestamp - state.sentAtL1Ts.getTime() >= txTimeoutMs)
347
+ );
348
+ }
349
+
350
+ /**
351
+ * Monitors a transaction until completion, handling speed-ups if needed
352
+ */
353
+ protected async monitorTransaction(state: L1TxState): Promise<TransactionReceipt> {
354
+ const { nonce, gasLimit, blobInputs, txConfigOverrides: gasConfigOverrides } = state;
355
+ const gasConfig = merge(this.config, gasConfigOverrides);
356
+ const { maxSpeedUpAttempts, stallTimeMs } = gasConfig;
357
+ const isCancelTx = state.cancelTxHashes.length > 0;
358
+ const txHashes = isCancelTx ? state.cancelTxHashes : state.txHashes;
359
+ const isBlobTx = !!blobInputs;
360
+ const account = this.getSenderAddress().toString();
361
+
362
+ const initialTxHash = txHashes[0];
363
+ let currentTxHash = txHashes.at(-1)!;
364
+ let l1Timestamp: number;
365
+
366
+ while (true) {
367
+ l1Timestamp = await this.getL1Timestamp();
368
+
369
+ try {
370
+ const timePassed = l1Timestamp - state.lastSentAtL1Ts.getTime();
371
+ const [currentNonce, pendingNonce] = await Promise.all([
372
+ this.client.getTransactionCount({ address: account, blockTag: 'latest' }),
373
+ this.client.getTransactionCount({ address: account, blockTag: 'pending' }),
374
+ ]);
375
+
376
+ // If the current nonce on our account is greater than our transaction's nonce then a tx with the same nonce has been mined.
377
+ if (currentNonce > nonce) {
378
+ // We try getting the receipt twice, since sometimes anvil fails to return it if the tx has just been mined
379
+ const receipt =
380
+ (await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true)) ??
381
+ (await this.tryGetTxReceipt(state.txHashes, nonce, false)) ??
382
+ (await sleep(500)) ??
383
+ (await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true)) ??
384
+ (await this.tryGetTxReceipt(state.txHashes, nonce, false));
385
+
386
+ if (receipt) {
387
+ state.receipt = receipt;
388
+ await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
389
+ return receipt;
390
+ }
391
+
392
+ // If we get here then we have checked all of our tx versions and not found anything.
393
+ // We should consider the nonce as MINED
394
+ await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
395
+ throw new UnknownMinedTxError(nonce, account);
396
+ }
397
+
398
+ // If this is a cancel tx and its nonce is no longer on the mempool, we consider it dropped and stop monitoring
399
+ // If it is a regular tx, we let the loop speed it up after the stall time
400
+ if (isCancelTx && pendingNonce <= nonce && timePassed >= gasConfig.txUnseenConsideredDroppedMs) {
401
+ this.logger.warn(
402
+ `Cancellation tx with nonce ${nonce} for account ${account} has been dropped from the visible mempool`,
403
+ { nonce, account, pendingNonce, timePassed },
404
+ );
405
+ await this.updateState(state, TxUtilsState.NOT_MINED);
406
+ this.nonceManager.reset({ address: account, chainId: this.client.chain.id });
407
+ throw new DroppedTransactionError(nonce, account);
408
+ }
409
+
410
+ // Break if the tx has timed out (ie expired)
411
+ if (this.isTxTimedOut(state, l1Timestamp)) {
412
+ break;
413
+ }
414
+
415
+ // Speed up the transaction if it appears to be stuck (exceeded stall time and still have retry attempts)
416
+ const attempts = txHashes.length;
417
+ if (timePassed >= stallTimeMs && attempts <= maxSpeedUpAttempts) {
418
+ const newGasPrice = await this.getGasPrice(gasConfig, isBlobTx, attempts, state.gasPrice);
419
+ state.gasPrice = newGasPrice;
420
+
421
+ this.logger.debug(
422
+ `Tx ${currentTxHash} with nonce ${nonce} from ${account} appears stuck. ` +
423
+ `Attempting speed-up ${attempts}/${maxSpeedUpAttempts} ` +
424
+ `with new priority fee ${formatGwei(newGasPrice.maxPriorityFeePerGas)} gwei.`,
425
+ {
426
+ account,
427
+ nonce,
428
+ maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
429
+ maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
430
+ ...(newGasPrice.maxFeePerBlobGas && { maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas) }),
431
+ },
432
+ );
433
+
434
+ const txData = this.makeTxData(state, { isCancelTx });
435
+
436
+ const signedRequest = await this.prepareSignedTransaction(txData);
437
+ const newHash = await this.client.sendRawTransaction({ serializedTransaction: signedRequest });
438
+
439
+ this.logger.verbose(
440
+ `Sent L1 speed-up tx ${newHash} replacing ${currentTxHash} for nonce ${nonce} from ${account}`,
441
+ {
442
+ nonce,
443
+ account,
444
+ gasLimit,
445
+ maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
446
+ maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
447
+ txConfig: state.txConfigOverrides,
448
+ ...(newGasPrice.maxFeePerBlobGas && { maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas) }),
449
+ },
450
+ );
451
+
452
+ currentTxHash = newHash;
453
+ txHashes.push(currentTxHash);
454
+ state.lastSentAtL1Ts = new Date(l1Timestamp);
455
+ await this.updateState(state, isCancelTx ? TxUtilsState.CANCELLED : TxUtilsState.SPEED_UP);
456
+
457
+ await sleep(gasConfig.checkIntervalMs!);
458
+ continue;
459
+ }
460
+
461
+ this.logger.debug(
462
+ `Tx ${currentTxHash} from ${account} with nonce ${nonce} still pending after ${timePassed}ms`,
463
+ {
464
+ account,
465
+ nonce,
466
+ pendingNonce,
467
+ attempts,
468
+ timePassed,
469
+ isBlobTx,
470
+ isCancelTx,
471
+ ...pick(state.gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas', 'maxFeePerBlobGas'),
472
+ ...pick(
473
+ gasConfig,
474
+ 'txUnseenConsideredDroppedMs',
475
+ 'txCancellationFinalTimeoutMs',
476
+ 'maxSpeedUpAttempts',
477
+ 'stallTimeMs',
478
+ 'txTimeoutAt',
479
+ 'txTimeoutMs',
480
+ ),
481
+ },
482
+ );
483
+
484
+ await sleep(gasConfig.checkIntervalMs!);
485
+ } catch (err: any) {
486
+ if (err instanceof DroppedTransactionError || err instanceof UnknownMinedTxError) {
487
+ throw err;
488
+ }
489
+
490
+ const viemError = formatViemError(err);
491
+ this.logger.error(`Error while monitoring L1 tx ${currentTxHash}`, viemError, { nonce, account });
492
+ await sleep(gasConfig.checkIntervalMs!);
493
+ }
494
+ }
495
+
496
+ // Oh no, the transaction has timed out!
497
+ if (isCancelTx || !gasConfig.cancelTxOnTimeout) {
498
+ // If this was already a cancellation tx, or we are configured to not cancel txs, we just mark it as NOT_MINED
499
+ // and reset the nonce manager, so the next tx that comes along can reuse the nonce if/when this tx gets dropped.
500
+ // This is the nastiest scenario for us, since the new tx could acquire the next nonce, but then this tx is dropped,
501
+ // and the new tx would never get mined. Eventually, the new tx would also drop.
502
+ await this.updateState(state, TxUtilsState.NOT_MINED);
503
+ this.nonceManager.reset({ address: account, chainId: this.client.chain.id });
504
+ } else {
505
+ // Otherwise we fire the cancellation without awaiting to avoid blocking the caller,
506
+ // and monitor it in the background so we can speed it up as needed.
507
+ void this.attemptTxCancellation(state).catch(async err => {
508
+ await this.updateState(state, TxUtilsState.NOT_MINED);
509
+ this.logger.error(`Failed to send cancellation for timed out tx ${initialTxHash} with nonce ${nonce}`, err, {
510
+ account,
511
+ nonce,
512
+ initialTxHash,
513
+ });
514
+ });
515
+ }
516
+
517
+ const what = isCancelTx ? 'Cancellation L1' : 'L1';
518
+ this.logger.warn(`${what} transaction ${initialTxHash} with nonce ${nonce} from ${account} timed out`, {
519
+ initialTxHash,
520
+ currentTxHash,
521
+ nonce,
522
+ account,
523
+ txTimeoutAt: gasConfig.txTimeoutAt?.getTime(),
524
+ txTimeoutMs: gasConfig.txTimeoutMs,
525
+ txInitialTime: state.sentAtL1Ts.getTime(),
526
+ l1Timestamp,
527
+ now: this.dateProvider.now(),
528
+ attempts: txHashes.length - 1,
529
+ isInterrupted: this.interrupted,
530
+ });
531
+
532
+ throw new TimeoutError(`L1 transaction ${initialTxHash} timed out`);
533
+ }
534
+
535
+ /**
536
+ * Creates tx data to be signed by viem signTransaction method, using the state as input.
537
+ * If isCancelTx is true, creates a 0-value tx to self with 21k gas and no data instead,
538
+ * and an empty blob input if the original tx also had blobs.
539
+ */
540
+ private makeTxData(
541
+ state: Pick<L1TxState, 'request' | 'gasLimit' | 'blobInputs' | 'gasPrice' | 'nonce'>,
542
+ opts: { isCancelTx: boolean },
543
+ ): PrepareTransactionRequestRequest {
544
+ const { request, gasLimit, blobInputs, gasPrice, nonce } = state;
545
+ const isBlobTx = blobInputs !== undefined;
546
+
547
+ const baseTxOpts = { nonce, ...pick(gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas') };
548
+
549
+ if (opts.isCancelTx) {
550
+ const baseTxData = {
551
+ to: this.getSenderAddress().toString(),
552
+ value: 0n,
553
+ data: '0x' as const,
554
+ gas: 21_000n,
555
+ ...baseTxOpts,
556
+ };
557
+
558
+ return isBlobTx ? { ...baseTxData, ...this.makeEmptyBlobInputs(gasPrice.maxFeePerBlobGas!) } : baseTxData;
559
+ }
560
+
561
+ const baseTxData = {
562
+ ...request,
563
+ ...baseTxOpts,
564
+ gas: gasLimit,
565
+ };
566
+
567
+ return blobInputs ? { ...baseTxData, ...blobInputs, maxFeePerBlobGas: gasPrice.maxFeePerBlobGas! } : baseTxData;
568
+ }
569
+
570
+ /** Returns when all monitor loops have stopped. */
571
+ public async waitMonitoringStopped(timeoutSeconds = 10) {
572
+ const account = this.getSenderAddress().toString();
573
+ await retryUntil(
574
+ () => this.txs.every(tx => TerminalTxUtilsState.includes(tx.status)),
575
+ `monitoring stopped for ${account}`,
576
+ timeoutSeconds,
577
+ 0.1,
578
+ ).catch(() => this.logger.warn(`Timeout waiting for monitoring loops to stop for ${account}`));
579
+ }
580
+
581
+ /**
582
+ * Sends a transaction and monitors it until completion
583
+ * @param request - The transaction request (to, data, value)
584
+ * @param gasConfig - Optional gas configuration
585
+ * @returns The receipt of the successful transaction
586
+ */
587
+ public async sendAndMonitorTransaction(
588
+ request: L1TxRequest,
589
+ gasConfig?: L1TxConfig,
590
+ blobInputs?: L1BlobInputs,
591
+ ): Promise<{ receipt: TransactionReceipt; state: L1TxState }> {
592
+ const { state } = await this.sendTransaction(request, gasConfig, blobInputs);
593
+ const receipt = await this.monitorTransaction(state);
594
+ return { receipt, state };
595
+ }
596
+
597
+ public override async simulate(
598
+ request: L1TxRequest & { gas?: bigint; from?: Hex },
599
+ _blockOverrides: BlockOverrides<bigint, number> = {},
600
+ stateOverrides: StateOverride = [],
601
+ abi: Abi = RollupAbi,
602
+ _gasConfig?: L1TxUtilsConfig & { fallbackGasEstimate?: bigint; ignoreBlockGasLimit?: boolean },
603
+ ): Promise<{ gasUsed: bigint; result: `0x${string}` }> {
604
+ const blockOverrides = { ..._blockOverrides };
605
+ const gasConfig = merge(this.config, _gasConfig);
606
+ const gasPrice = await this.getGasPrice(gasConfig, false);
607
+
608
+ const call: any = {
609
+ to: request.to!,
610
+ data: request.data,
611
+ from: request.from ?? this.getSenderAddress().toString(),
612
+ maxFeePerGas: gasPrice.maxFeePerGas,
613
+ maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
614
+ gas: request.gas ?? LARGE_GAS_LIMIT,
615
+ };
616
+
617
+ if (!request.gas && !gasConfig.ignoreBlockGasLimit) {
618
+ // LARGE_GAS_LIMIT is set as call.gas, increase block gasLimit
619
+ blockOverrides.gasLimit = LARGE_GAS_LIMIT * 2n;
620
+ }
621
+
622
+ return this._simulate(call, blockOverrides, stateOverrides, gasConfig, abi);
623
+ }
624
+
625
+ /**
626
+ * Attempts to cancel a transaction by sending a 0-value tx to self with same nonce but higher gas prices
627
+ * Only sends the cancellation if the original tx is still pending, not if it was dropped
628
+ * @returns The hash of the cancellation transaction
629
+ */
630
+ protected async attemptTxCancellation(state: L1TxState): Promise<void> {
631
+ const isBlobTx = state.blobInputs !== undefined;
632
+ const { nonce, gasPrice: previousGasPrice } = state;
633
+ const account = this.getSenderAddress().toString();
634
+
635
+ // Do not send cancellation if interrupted
636
+ if (this.interrupted) {
637
+ this.logger.warn(
638
+ `Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as interrupted`,
639
+ { nonce, account },
640
+ );
641
+ await this.updateState(state, TxUtilsState.NOT_MINED);
642
+ this.nonceManager.reset({ address: account, chainId: this.client.chain.id });
643
+ return;
644
+ }
645
+
646
+ // Check if the original tx is still pending
647
+ const currentNonce = await this.client.getTransactionCount({ address: account, blockTag: 'pending' });
648
+ if (currentNonce < nonce) {
649
+ this.logger.verbose(
650
+ `Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as it is dropped`,
651
+ { nonce, account, currentNonce },
652
+ );
653
+ await this.updateState(state, TxUtilsState.NOT_MINED);
654
+ this.nonceManager.reset({ address: account, chainId: this.client.chain.id });
655
+ return;
656
+ }
657
+
658
+ // Get gas price with higher priority fee for cancellation
659
+ const cancelGasPrice = await this.getGasPrice(
660
+ {
661
+ ...this.config,
662
+ // Use high bump for cancellation to ensure it replaces the original tx
663
+ priorityFeeRetryBumpPercentage: 150, // 150% bump should be enough to replace any tx
664
+ },
665
+ isBlobTx,
666
+ state.txHashes.length,
667
+ previousGasPrice,
668
+ );
669
+
670
+ const { maxFeePerGas, maxPriorityFeePerGas, maxFeePerBlobGas } = cancelGasPrice;
671
+ this.logger.verbose(
672
+ `Attempting to cancel L1 ${isBlobTx ? 'blob' : 'vanilla'} transaction from account ${account} with nonce ${nonce} after time out`,
673
+ {
674
+ maxFeePerGas: formatGwei(maxFeePerGas),
675
+ maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
676
+ ...(maxFeePerBlobGas && { maxFeePerBlobGas: formatGwei(maxFeePerBlobGas) }),
677
+ },
678
+ );
679
+
680
+ // Send 0-value tx to self with higher gas price
681
+ state.gasPrice = cancelGasPrice;
682
+ state.lastSentAtL1Ts = new Date(await this.getL1Timestamp());
683
+
684
+ const txData = this.makeTxData(state, { isCancelTx: true });
685
+ const signedRequest = await this.prepareSignedTransaction(txData);
686
+ const cancelTxHash = await this.client.sendRawTransaction({ serializedTransaction: signedRequest });
687
+
688
+ state.cancelTxHashes.push(cancelTxHash);
689
+ await this.updateState(state, TxUtilsState.CANCELLED);
690
+
691
+ this.logger.warn(`Sent cancellation tx ${cancelTxHash} for timed out tx from ${account} with nonce ${nonce}`, {
692
+ nonce,
693
+ cancelGasPrice,
694
+ isBlobTx,
695
+ txHashes: state.txHashes,
696
+ });
697
+
698
+ // Do not await the cancel tx to be mined
699
+ void this.monitorTransaction(state).catch(err => {
700
+ this.logger.error(`Failed to mine cancellation tx ${cancelTxHash} for nonce ${nonce} account ${account}`, err, {
701
+ nonce,
702
+ account,
703
+ cancelTxHash,
704
+ });
705
+ });
706
+ }
707
+
708
+ /** Returns L1 timestamps in milliseconds */
709
+ private async getL1Timestamp() {
710
+ const { timestamp } = await this.client.getBlock({ blockTag: 'latest', includeTransactions: false });
711
+ return Number(timestamp) * 1000;
712
+ }
713
+
714
+ /** Makes empty blob inputs for the cancellation tx. To be overridden in L1TxUtilsWithBlobs. */
715
+ protected makeEmptyBlobInputs(_maxFeePerBlobGas: bigint): Required<L1BlobInputs> {
716
+ throw new Error('Cannot make empty blob inputs for cancellation');
717
+ }
718
+ }