@aztec/ethereum 0.65.2 → 0.67.0

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 (41) hide show
  1. package/dest/constants.d.ts +1 -0
  2. package/dest/constants.d.ts.map +1 -1
  3. package/dest/constants.js +2 -1
  4. package/dest/contracts/rollup.d.ts +5 -0
  5. package/dest/contracts/rollup.d.ts.map +1 -1
  6. package/dest/contracts/rollup.js +16 -1
  7. package/dest/deploy_l1_contracts.d.ts +8 -4
  8. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  9. package/dest/deploy_l1_contracts.js +124 -55
  10. package/dest/eth_cheat_codes.d.ts +156 -0
  11. package/dest/eth_cheat_codes.d.ts.map +1 -0
  12. package/dest/eth_cheat_codes.js +294 -0
  13. package/dest/index.d.ts +4 -1
  14. package/dest/index.d.ts.map +1 -1
  15. package/dest/index.js +5 -2
  16. package/dest/l1_contract_addresses.d.ts +4 -1
  17. package/dest/l1_contract_addresses.d.ts.map +1 -1
  18. package/dest/l1_contract_addresses.js +8 -1
  19. package/dest/l1_reader.d.ts +1 -0
  20. package/dest/l1_reader.d.ts.map +1 -1
  21. package/dest/l1_reader.js +5 -2
  22. package/dest/l1_tx_utils.d.ts +101 -0
  23. package/dest/l1_tx_utils.d.ts.map +1 -0
  24. package/dest/l1_tx_utils.js +261 -0
  25. package/dest/test/tx_delayer.d.ts +3 -3
  26. package/dest/test/tx_delayer.d.ts.map +1 -1
  27. package/dest/test/tx_delayer.js +3 -3
  28. package/dest/utils.d.ts +2 -2
  29. package/dest/utils.d.ts.map +1 -1
  30. package/dest/utils.js +2 -2
  31. package/package.json +9 -3
  32. package/src/constants.ts +1 -0
  33. package/src/contracts/rollup.ts +20 -0
  34. package/src/deploy_l1_contracts.ts +148 -63
  35. package/src/eth_cheat_codes.ts +320 -0
  36. package/src/index.ts +4 -1
  37. package/src/l1_contract_addresses.ts +7 -0
  38. package/src/l1_reader.ts +5 -1
  39. package/src/l1_tx_utils.ts +404 -0
  40. package/src/test/tx_delayer.ts +4 -4
  41. package/src/utils.ts +4 -4
@@ -0,0 +1,404 @@
1
+ import {
2
+ type ConfigMappingsType,
3
+ bigintConfigHelper,
4
+ getDefaultConfig,
5
+ numberConfigHelper,
6
+ } from '@aztec/foundation/config';
7
+ import { type Logger } from '@aztec/foundation/log';
8
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
9
+ import { sleep } from '@aztec/foundation/sleep';
10
+
11
+ import {
12
+ type Account,
13
+ type Address,
14
+ type Chain,
15
+ type GetTransactionReturnType,
16
+ type Hex,
17
+ type HttpTransport,
18
+ type PublicClient,
19
+ type TransactionReceipt,
20
+ type WalletClient,
21
+ formatGwei,
22
+ } from 'viem';
23
+
24
+ // 1_000_000_000 Gwei = 1 ETH
25
+ // 1_000_000_000 Wei = 1 Gwei
26
+ // 1_000_000_000_000_000_000 Wei = 1 ETH
27
+
28
+ const WEI_CONST = 1_000_000_000n;
29
+
30
+ // setting a minimum bump percentage to 10% due to geth's implementation
31
+ // https://github.com/ethereum/go-ethereum/blob/e3d61e6db028c412f74bc4d4c7e117a9e29d0de0/core/txpool/legacypool/list.go#L298
32
+ const MIN_REPLACEMENT_BUMP_PERCENTAGE = 10n;
33
+
34
+ // Avg ethereum block time is ~12s
35
+ const BLOCK_TIME_MS = 12_000;
36
+
37
+ export interface L1TxUtilsConfig {
38
+ /**
39
+ * How much to increase calculated gas limit.
40
+ */
41
+ gasLimitBufferPercentage?: bigint;
42
+ /**
43
+ * Maximum gas price in gwei
44
+ */
45
+ maxGwei?: bigint;
46
+ /**
47
+ * Minimum gas price in gwei
48
+ */
49
+ minGwei?: bigint;
50
+ /**
51
+ * Priority fee bump percentage
52
+ */
53
+ priorityFeeBumpPercentage?: bigint;
54
+ /**
55
+ * How much to increase priority fee by each attempt (percentage)
56
+ */
57
+ priorityFeeRetryBumpPercentage?: bigint;
58
+ /**
59
+ * Maximum number of speed-up attempts
60
+ */
61
+ maxAttempts?: number;
62
+ /**
63
+ * How often to check tx status
64
+ */
65
+ checkIntervalMs?: number;
66
+ /**
67
+ * How long before considering tx stalled
68
+ */
69
+ stallTimeMs?: number;
70
+ /**
71
+ * How long to wait for a tx to be mined before giving up
72
+ */
73
+ txTimeoutMs?: number;
74
+ }
75
+
76
+ export const l1TxUtilsConfigMappings: ConfigMappingsType<L1TxUtilsConfig> = {
77
+ gasLimitBufferPercentage: {
78
+ description: 'How much to increase gas price by each attempt (percentage)',
79
+ env: 'L1_GAS_LIMIT_BUFFER_PERCENTAGE',
80
+ ...bigintConfigHelper(20n),
81
+ },
82
+ minGwei: {
83
+ description: 'Minimum gas price in gwei',
84
+ env: 'L1_GAS_PRICE_MIN',
85
+ ...bigintConfigHelper(1n),
86
+ },
87
+ maxGwei: {
88
+ description: 'Maximum gas price in gwei',
89
+ env: 'L1_GAS_PRICE_MAX',
90
+ ...bigintConfigHelper(100n),
91
+ },
92
+ priorityFeeBumpPercentage: {
93
+ description: 'How much to increase priority fee by each attempt (percentage)',
94
+ env: 'L1_PRIORITY_FEE_BUMP_PERCENTAGE',
95
+ ...bigintConfigHelper(20n),
96
+ },
97
+ priorityFeeRetryBumpPercentage: {
98
+ description: 'How much to increase priority fee by each retry attempt (percentage)',
99
+ env: 'L1_PRIORITY_FEE_RETRY_BUMP_PERCENTAGE',
100
+ ...bigintConfigHelper(50n),
101
+ },
102
+ maxAttempts: {
103
+ description: 'Maximum number of speed-up attempts',
104
+ env: 'L1_TX_MONITOR_MAX_ATTEMPTS',
105
+ ...numberConfigHelper(3),
106
+ },
107
+ checkIntervalMs: {
108
+ description: 'How often to check tx status',
109
+ env: 'L1_TX_MONITOR_CHECK_INTERVAL_MS',
110
+ ...numberConfigHelper(10_000),
111
+ },
112
+ stallTimeMs: {
113
+ description: 'How long before considering tx stalled',
114
+ env: 'L1_TX_MONITOR_STALL_TIME_MS',
115
+ ...numberConfigHelper(30_000),
116
+ },
117
+ txTimeoutMs: {
118
+ description: 'How long to wait for a tx to be mined before giving up. Set to 0 to disable.',
119
+ env: 'L1_TX_MONITOR_TX_TIMEOUT_MS',
120
+ ...numberConfigHelper(300_000), // 5 mins
121
+ },
122
+ };
123
+
124
+ export const defaultL1TxUtilsConfig = getDefaultConfig<L1TxUtilsConfig>(l1TxUtilsConfigMappings);
125
+
126
+ export interface L1TxRequest {
127
+ to: Address | null;
128
+ data: Hex;
129
+ value?: bigint;
130
+ }
131
+
132
+ interface GasPrice {
133
+ maxFeePerGas: bigint;
134
+ maxPriorityFeePerGas: bigint;
135
+ }
136
+
137
+ export class L1TxUtils {
138
+ private readonly config: L1TxUtilsConfig;
139
+
140
+ constructor(
141
+ private readonly publicClient: PublicClient,
142
+ private readonly walletClient: WalletClient<HttpTransport, Chain, Account>,
143
+ private readonly logger?: Logger,
144
+ config?: Partial<L1TxUtilsConfig>,
145
+ ) {
146
+ this.config = {
147
+ ...defaultL1TxUtilsConfig,
148
+ ...(config || {}),
149
+ };
150
+ }
151
+
152
+ /**
153
+ * Sends a transaction with gas estimation and pricing
154
+ * @param request - The transaction request (to, data, value)
155
+ * @param gasConfig - Optional gas configuration
156
+ * @returns The transaction hash and parameters used
157
+ */
158
+ public async sendTransaction(
159
+ request: L1TxRequest,
160
+ _gasConfig?: Partial<L1TxUtilsConfig> & { fixedGas?: bigint },
161
+ ): Promise<{ txHash: Hex; gasLimit: bigint; gasPrice: GasPrice }> {
162
+ const gasConfig = { ...this.config, ..._gasConfig };
163
+ const account = this.walletClient.account;
164
+ let gasLimit: bigint;
165
+
166
+ if (gasConfig.fixedGas) {
167
+ gasLimit = gasConfig.fixedGas;
168
+ } else {
169
+ gasLimit = await this.estimateGas(account, request);
170
+ }
171
+
172
+ const gasPrice = await this.getGasPrice(gasConfig);
173
+
174
+ const txHash = await this.walletClient.sendTransaction({
175
+ ...request,
176
+ gas: gasLimit,
177
+ maxFeePerGas: gasPrice.maxFeePerGas,
178
+ maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
179
+ });
180
+
181
+ this.logger?.verbose(`Sent L1 transaction ${txHash}`, {
182
+ gasLimit,
183
+ maxFeePerGas: formatGwei(gasPrice.maxFeePerGas),
184
+ maxPriorityFeePerGas: formatGwei(gasPrice.maxPriorityFeePerGas),
185
+ });
186
+
187
+ return { txHash, gasLimit, gasPrice };
188
+ }
189
+
190
+ /**
191
+ * Monitors a transaction until completion, handling speed-ups if needed
192
+ * @param request - Original transaction request (needed for speed-ups)
193
+ * @param initialTxHash - Hash of the initial transaction
194
+ * @param params - Parameters used in the initial transaction
195
+ * @param gasConfig - Optional gas configuration
196
+ */
197
+ public async monitorTransaction(
198
+ request: L1TxRequest,
199
+ initialTxHash: Hex,
200
+ params: { gasLimit: bigint },
201
+ _gasConfig?: Partial<L1TxUtilsConfig>,
202
+ ): Promise<TransactionReceipt> {
203
+ const gasConfig = { ...this.config, ..._gasConfig };
204
+ const account = this.walletClient.account;
205
+
206
+ // Retry a few times, in case the tx is not yet propagated.
207
+ const tx = await retry<GetTransactionReturnType>(
208
+ () => this.publicClient.getTransaction({ hash: initialTxHash }),
209
+ `Getting L1 transaction ${initialTxHash}`,
210
+ makeBackoff([1, 2, 3]),
211
+ this.logger,
212
+ true,
213
+ );
214
+
215
+ if (tx?.nonce === undefined || tx?.nonce === null) {
216
+ throw new Error(`Failed to get L1 transaction ${initialTxHash} nonce`);
217
+ }
218
+ const nonce = tx.nonce;
219
+
220
+ const txHashes = new Set<Hex>([initialTxHash]);
221
+ let currentTxHash = initialTxHash;
222
+ let attempts = 0;
223
+ let lastAttemptSent = Date.now();
224
+ const initialTxTime = lastAttemptSent;
225
+ let txTimedOut = false;
226
+
227
+ while (!txTimedOut) {
228
+ try {
229
+ const currentNonce = await this.publicClient.getTransactionCount({ address: account.address });
230
+ if (currentNonce > nonce) {
231
+ for (const hash of txHashes) {
232
+ try {
233
+ const receipt = await this.publicClient.getTransactionReceipt({ hash });
234
+ if (receipt) {
235
+ this.logger?.debug(`L1 transaction ${hash} mined`);
236
+ if (receipt.status === 'reverted') {
237
+ this.logger?.error(`L1 transaction ${hash} reverted`);
238
+ }
239
+ return receipt;
240
+ }
241
+ } catch (err) {
242
+ if (err instanceof Error && err.message.includes('reverted')) {
243
+ throw err;
244
+ }
245
+ }
246
+ }
247
+ }
248
+
249
+ // Retry a few times, in case the tx is not yet propagated.
250
+ const tx = await retry<GetTransactionReturnType>(
251
+ () => this.publicClient.getTransaction({ hash: currentTxHash }),
252
+ `Getting L1 transaction ${currentTxHash}`,
253
+ makeBackoff([1, 2, 3]),
254
+ this.logger,
255
+ true,
256
+ );
257
+ const timePassed = Date.now() - lastAttemptSent;
258
+
259
+ if (tx && timePassed < gasConfig.stallTimeMs!) {
260
+ this.logger?.debug(`L1 transaction ${currentTxHash} pending. Time passed: ${timePassed}ms.`);
261
+
262
+ // Check timeout before continuing
263
+ if (gasConfig.txTimeoutMs) {
264
+ txTimedOut = Date.now() - initialTxTime > gasConfig.txTimeoutMs;
265
+ if (txTimedOut) {
266
+ break;
267
+ }
268
+ }
269
+
270
+ await sleep(gasConfig.checkIntervalMs!);
271
+ continue;
272
+ }
273
+
274
+ if (timePassed > gasConfig.stallTimeMs! && attempts < gasConfig.maxAttempts!) {
275
+ attempts++;
276
+ const newGasPrice = await this.getGasPrice(
277
+ gasConfig,
278
+ attempts,
279
+ tx.maxFeePerGas && tx.maxPriorityFeePerGas
280
+ ? { maxFeePerGas: tx.maxFeePerGas, maxPriorityFeePerGas: tx.maxPriorityFeePerGas }
281
+ : undefined,
282
+ );
283
+
284
+ this.logger?.debug(
285
+ `L1 transaction ${currentTxHash} appears stuck. Attempting speed-up ${attempts}/${gasConfig.maxAttempts} ` +
286
+ `with new priority fee ${formatGwei(newGasPrice.maxPriorityFeePerGas)} gwei`,
287
+ );
288
+
289
+ currentTxHash = await this.walletClient.sendTransaction({
290
+ ...request,
291
+ nonce,
292
+ gas: params.gasLimit,
293
+ maxFeePerGas: newGasPrice.maxFeePerGas,
294
+ maxPriorityFeePerGas: newGasPrice.maxPriorityFeePerGas,
295
+ });
296
+
297
+ txHashes.add(currentTxHash);
298
+ lastAttemptSent = Date.now();
299
+ }
300
+ await sleep(gasConfig.checkIntervalMs!);
301
+ } catch (err: any) {
302
+ this.logger?.warn(`Error monitoring tx ${currentTxHash}:`, err);
303
+ if (err.message?.includes('reverted')) {
304
+ throw err;
305
+ }
306
+ await sleep(gasConfig.checkIntervalMs!);
307
+ }
308
+ // Check if tx has timed out.
309
+ if (gasConfig.txTimeoutMs) {
310
+ txTimedOut = Date.now() - initialTxTime > gasConfig.txTimeoutMs!;
311
+ }
312
+ }
313
+ throw new Error(`L1 transaction ${currentTxHash} timed out`);
314
+ }
315
+
316
+ /**
317
+ * Sends a transaction and monitors it until completion
318
+ * @param request - The transaction request (to, data, value)
319
+ * @param gasConfig - Optional gas configuration
320
+ * @returns The receipt of the successful transaction
321
+ */
322
+ public async sendAndMonitorTransaction(
323
+ request: L1TxRequest,
324
+ gasConfig?: Partial<L1TxUtilsConfig> & { fixedGas?: bigint },
325
+ ): Promise<TransactionReceipt> {
326
+ const { txHash, gasLimit } = await this.sendTransaction(request, gasConfig);
327
+ return this.monitorTransaction(request, txHash, { gasLimit }, gasConfig);
328
+ }
329
+
330
+ /**
331
+ * Gets the current gas price with bounds checking
332
+ */
333
+ private async getGasPrice(
334
+ _gasConfig?: L1TxUtilsConfig,
335
+ attempt: number = 0,
336
+ previousGasPrice?: typeof attempt extends 0 ? never : GasPrice,
337
+ ): Promise<GasPrice> {
338
+ const gasConfig = { ...this.config, ..._gasConfig };
339
+ const block = await this.publicClient.getBlock({ blockTag: 'latest' });
340
+ const baseFee = block.baseFeePerGas ?? 0n;
341
+
342
+ // Get initial priority fee from the network
343
+ let priorityFee = await this.publicClient.estimateMaxPriorityFeePerGas();
344
+ let maxFeePerGas = baseFee;
345
+
346
+ // Bump base fee so it's valid for next blocks if it stalls
347
+ const numBlocks = Math.ceil(gasConfig.stallTimeMs! / BLOCK_TIME_MS);
348
+ for (let i = 0; i < numBlocks; i++) {
349
+ // each block can go up 12.5% from previous baseFee
350
+ maxFeePerGas = (maxFeePerGas * (1_000n + 125n)) / 1_000n;
351
+ }
352
+
353
+ if (attempt > 0) {
354
+ const configBump =
355
+ gasConfig.priorityFeeRetryBumpPercentage ?? defaultL1TxUtilsConfig.priorityFeeRetryBumpPercentage!;
356
+ const bumpPercentage =
357
+ configBump > MIN_REPLACEMENT_BUMP_PERCENTAGE ? configBump : MIN_REPLACEMENT_BUMP_PERCENTAGE;
358
+
359
+ // Calculate minimum required fees based on previous attempt
360
+ const minPriorityFee = (previousGasPrice!.maxPriorityFeePerGas * (100n + bumpPercentage)) / 100n;
361
+ const minMaxFee = (previousGasPrice!.maxFeePerGas * (100n + bumpPercentage)) / 100n;
362
+
363
+ // Add priority fee to maxFeePerGas
364
+ maxFeePerGas += priorityFee;
365
+
366
+ // Use maximum between current network values and minimum required values
367
+ priorityFee = priorityFee > minPriorityFee ? priorityFee : minPriorityFee;
368
+ maxFeePerGas = maxFeePerGas > minMaxFee ? maxFeePerGas : minMaxFee;
369
+ } else {
370
+ // first attempt, just bump priority fee
371
+ priorityFee = (priorityFee * (100n + (gasConfig.priorityFeeBumpPercentage || 0n))) / 100n;
372
+ maxFeePerGas += priorityFee;
373
+ }
374
+
375
+ // Ensure we don't exceed maxGwei
376
+ const maxGweiInWei = gasConfig.maxGwei! * WEI_CONST;
377
+ maxFeePerGas = maxFeePerGas > maxGweiInWei ? maxGweiInWei : maxFeePerGas;
378
+
379
+ // Ensure priority fee doesn't exceed max fee
380
+ const maxPriorityFeePerGas = priorityFee > maxFeePerGas ? maxFeePerGas : priorityFee;
381
+
382
+ this.logger?.debug(`Computed gas price`, {
383
+ attempt,
384
+ baseFee: formatGwei(baseFee),
385
+ maxFeePerGas: formatGwei(maxFeePerGas),
386
+ maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
387
+ });
388
+
389
+ return { maxFeePerGas, maxPriorityFeePerGas };
390
+ }
391
+
392
+ /**
393
+ * Estimates gas and adds buffer
394
+ */
395
+ public async estimateGas(account: Account, request: L1TxRequest, _gasConfig?: L1TxUtilsConfig): Promise<bigint> {
396
+ const gasConfig = { ...this.config, ..._gasConfig };
397
+ const initialEstimate = await this.publicClient.estimateGas({ account, ...request });
398
+
399
+ // Add buffer based on either fixed amount or percentage
400
+ const withBuffer = initialEstimate + (initialEstimate * (gasConfig.gasLimitBufferPercentage ?? 0n)) / 100n;
401
+
402
+ return withBuffer;
403
+ }
404
+ }
@@ -1,4 +1,4 @@
1
- import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log';
1
+ import { type Logger, createLogger } from '@aztec/foundation/log';
2
2
  import { retryUntil } from '@aztec/foundation/retry';
3
3
 
4
4
  import { inspect } from 'util';
@@ -12,7 +12,7 @@ import {
12
12
  walletActions,
13
13
  } from 'viem';
14
14
 
15
- export function waitUntilBlock<T extends Client>(client: T, blockNumber: number | bigint, logger?: DebugLogger) {
15
+ export function waitUntilBlock<T extends Client>(client: T, blockNumber: number | bigint, logger?: Logger) {
16
16
  const publicClient =
17
17
  'getBlockNumber' in client && typeof client.getBlockNumber === 'function'
18
18
  ? (client as unknown as PublicClient)
@@ -30,7 +30,7 @@ export function waitUntilBlock<T extends Client>(client: T, blockNumber: number
30
30
  );
31
31
  }
32
32
 
33
- export function waitUntilL1Timestamp<T extends Client>(client: T, timestamp: number | bigint, logger?: DebugLogger) {
33
+ export function waitUntilL1Timestamp<T extends Client>(client: T, timestamp: number | bigint, logger?: Logger) {
34
34
  const publicClient =
35
35
  'getBlockNumber' in client && typeof client.getBlockNumber === 'function'
36
36
  ? (client as unknown as PublicClient)
@@ -94,7 +94,7 @@ export function withDelayer<T extends WalletClient>(
94
94
  client: T,
95
95
  opts: { ethereumSlotDuration: bigint | number },
96
96
  ): { client: T; delayer: Delayer } {
97
- const logger = createDebugLogger('aztec:ethereum:tx_delayer');
97
+ const logger = createLogger('ethereum:tx_delayer');
98
98
  const delayer = new DelayerImpl(opts);
99
99
  const extended = client
100
100
  // Tweak sendRawTransaction so it uses the delay defined in the delayer.
package/src/utils.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Fr } from '@aztec/foundation/fields';
2
- import { type DebugLogger } from '@aztec/foundation/log';
2
+ import { type Logger } from '@aztec/foundation/log';
3
3
 
4
4
  import {
5
5
  type Abi,
@@ -27,7 +27,7 @@ export function extractEvent<
27
27
  abi: TAbi,
28
28
  eventName: TEventName,
29
29
  filter?: (log: TEventType) => boolean,
30
- logger?: DebugLogger,
30
+ logger?: Logger,
31
31
  ): TEventType {
32
32
  const event = tryExtractEvent(logs, address, abi, eventName, filter, logger);
33
33
  if (!event) {
@@ -46,10 +46,10 @@ function tryExtractEvent<
46
46
  abi: TAbi,
47
47
  eventName: TEventName,
48
48
  filter?: (log: TEventType) => boolean,
49
- logger?: DebugLogger,
49
+ logger?: Logger,
50
50
  ): TEventType | undefined {
51
51
  for (const log of logs) {
52
- if (log.address === address) {
52
+ if (log.address.toLowerCase() === address.toLowerCase()) {
53
53
  try {
54
54
  const decodedEvent = decodeEventLog({ abi, ...log });
55
55
  if (decodedEvent.eventName === eventName) {