@aztec/ethereum 0.0.1-commit.d3ec352c → 0.0.1-commit.fcb71a6

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 (138) hide show
  1. package/dest/client.js +6 -2
  2. package/dest/config.d.ts +6 -42
  3. package/dest/config.d.ts.map +1 -1
  4. package/dest/config.js +9 -327
  5. package/dest/contracts/empire_base.d.ts +2 -1
  6. package/dest/contracts/empire_base.d.ts.map +1 -1
  7. package/dest/contracts/empire_slashing_proposer.d.ts +2 -1
  8. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -1
  9. package/dest/contracts/empire_slashing_proposer.js +9 -0
  10. package/dest/contracts/governance_proposer.d.ts +2 -1
  11. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  12. package/dest/contracts/governance_proposer.js +9 -0
  13. package/dest/contracts/inbox.d.ts +7 -3
  14. package/dest/contracts/inbox.d.ts.map +1 -1
  15. package/dest/contracts/inbox.js +4 -0
  16. package/dest/contracts/rollup.d.ts +19 -3
  17. package/dest/contracts/rollup.d.ts.map +1 -1
  18. package/dest/contracts/rollup.js +14 -0
  19. package/dest/contracts/tally_slashing_proposer.d.ts +3 -2
  20. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -1
  21. package/dest/contracts/tally_slashing_proposer.js +1 -1
  22. package/dest/deploy_aztec_l1_contracts.d.ts +247 -0
  23. package/dest/deploy_aztec_l1_contracts.d.ts.map +1 -0
  24. package/dest/deploy_aztec_l1_contracts.js +336 -0
  25. package/dest/deploy_l1_contract.d.ts +68 -0
  26. package/dest/deploy_l1_contract.d.ts.map +1 -0
  27. package/dest/deploy_l1_contract.js +312 -0
  28. package/dest/forwarder_proxy.d.ts +32 -0
  29. package/dest/forwarder_proxy.d.ts.map +1 -0
  30. package/dest/forwarder_proxy.js +93 -0
  31. package/dest/l1_artifacts.d.ts +136 -98
  32. package/dest/l1_artifacts.d.ts.map +1 -1
  33. package/dest/l1_contract_addresses.d.ts +1 -1
  34. package/dest/l1_contract_addresses.d.ts.map +1 -1
  35. package/dest/l1_contract_addresses.js +3 -3
  36. package/dest/l1_reader.d.ts +3 -1
  37. package/dest/l1_reader.d.ts.map +1 -1
  38. package/dest/l1_reader.js +6 -0
  39. package/dest/l1_tx_utils/config.d.ts +3 -3
  40. package/dest/l1_tx_utils/config.d.ts.map +1 -1
  41. package/dest/l1_tx_utils/config.js +17 -3
  42. package/dest/l1_tx_utils/constants.d.ts +7 -1
  43. package/dest/l1_tx_utils/constants.d.ts.map +1 -1
  44. package/dest/l1_tx_utils/constants.js +25 -0
  45. package/dest/l1_tx_utils/fee-strategies/index.d.ts +9 -0
  46. package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
  47. package/dest/l1_tx_utils/fee-strategies/index.js +11 -0
  48. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +18 -0
  49. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
  50. package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +111 -0
  51. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +32 -0
  52. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
  53. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +173 -0
  54. package/dest/l1_tx_utils/fee-strategies/types.d.ts +64 -0
  55. package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
  56. package/dest/l1_tx_utils/fee-strategies/types.js +24 -0
  57. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts +41 -0
  58. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts.map +1 -0
  59. package/dest/l1_tx_utils/forwarder_l1_tx_utils.js +48 -0
  60. package/dest/l1_tx_utils/index-blobs.d.ts +3 -0
  61. package/dest/l1_tx_utils/index-blobs.d.ts.map +1 -0
  62. package/dest/l1_tx_utils/index-blobs.js +2 -0
  63. package/dest/l1_tx_utils/index.d.ts +3 -1
  64. package/dest/l1_tx_utils/index.d.ts.map +1 -1
  65. package/dest/l1_tx_utils/index.js +2 -0
  66. package/dest/l1_tx_utils/interfaces.d.ts +2 -2
  67. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -1
  68. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +233 -0
  69. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
  70. package/dest/l1_tx_utils/l1_fee_analyzer.js +506 -0
  71. package/dest/l1_tx_utils/l1_tx_utils.d.ts +1 -1
  72. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -1
  73. package/dest/l1_tx_utils/l1_tx_utils.js +17 -4
  74. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +4 -11
  75. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -1
  76. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +68 -138
  77. package/dest/queries.d.ts +1 -1
  78. package/dest/queries.d.ts.map +1 -1
  79. package/dest/queries.js +6 -1
  80. package/dest/test/chain_monitor.d.ts +2 -2
  81. package/dest/test/chain_monitor.d.ts.map +1 -1
  82. package/dest/test/eth_cheat_codes.js +4 -2
  83. package/dest/test/rollup_cheat_codes.d.ts +2 -2
  84. package/dest/test/rollup_cheat_codes.d.ts.map +1 -1
  85. package/dest/test/rollup_cheat_codes.js +8 -3
  86. package/dest/test/start_anvil.d.ts +3 -1
  87. package/dest/test/start_anvil.d.ts.map +1 -1
  88. package/dest/test/tx_delayer.d.ts +1 -1
  89. package/dest/test/tx_delayer.d.ts.map +1 -1
  90. package/dest/test/tx_delayer.js +4 -3
  91. package/dest/types.d.ts +57 -2
  92. package/dest/types.d.ts.map +1 -1
  93. package/dest/utils.d.ts +15 -3
  94. package/dest/utils.d.ts.map +1 -1
  95. package/dest/utils.js +18 -0
  96. package/package.json +28 -10
  97. package/src/client.ts +2 -2
  98. package/src/config.ts +10 -406
  99. package/src/contracts/empire_base.ts +1 -1
  100. package/src/contracts/empire_slashing_proposer.ts +6 -1
  101. package/src/contracts/governance_proposer.ts +6 -1
  102. package/src/contracts/inbox.ts +7 -2
  103. package/src/contracts/rollup.ts +18 -2
  104. package/src/contracts/tally_slashing_proposer.ts +3 -1
  105. package/src/deploy_aztec_l1_contracts.ts +557 -0
  106. package/src/deploy_l1_contract.ts +362 -0
  107. package/src/forwarder_proxy.ts +108 -0
  108. package/src/l1_contract_addresses.ts +22 -20
  109. package/src/l1_reader.ts +8 -0
  110. package/src/l1_tx_utils/config.ts +24 -6
  111. package/src/l1_tx_utils/constants.ts +11 -0
  112. package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
  113. package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +159 -0
  114. package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +241 -0
  115. package/src/l1_tx_utils/fee-strategies/types.ts +88 -0
  116. package/src/l1_tx_utils/forwarder_l1_tx_utils.ts +119 -0
  117. package/src/l1_tx_utils/index-blobs.ts +2 -0
  118. package/src/l1_tx_utils/index.ts +2 -0
  119. package/src/l1_tx_utils/interfaces.ts +1 -1
  120. package/src/l1_tx_utils/l1_fee_analyzer.ts +804 -0
  121. package/src/l1_tx_utils/l1_tx_utils.ts +24 -4
  122. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +76 -176
  123. package/src/queries.ts +6 -0
  124. package/src/test/chain_monitor.ts +2 -1
  125. package/src/test/eth_cheat_codes.ts +2 -2
  126. package/src/test/rollup_cheat_codes.ts +4 -3
  127. package/src/test/start_anvil.ts +2 -0
  128. package/src/test/tx_delayer.ts +5 -3
  129. package/src/types.ts +62 -0
  130. package/src/utils.ts +30 -1
  131. package/dest/deploy_l1_contracts.d.ts +0 -673
  132. package/dest/deploy_l1_contracts.d.ts.map +0 -1
  133. package/dest/deploy_l1_contracts.js +0 -1491
  134. package/dest/index.d.ts +0 -18
  135. package/dest/index.d.ts.map +0 -1
  136. package/dest/index.js +0 -17
  137. package/src/deploy_l1_contracts.ts +0 -1869
  138. package/src/index.ts +0 -17
@@ -0,0 +1,804 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import { retryUntil } from '@aztec/foundation/retry';
4
+ import { DateProvider } from '@aztec/foundation/timer';
5
+
6
+ import { type Block, type FormattedTransaction, type Hex, formatGwei } from 'viem';
7
+
8
+ import type { ViemClient } from '../types.js';
9
+ import { calculatePercentile, isBlobTransaction } from '../utils.js';
10
+ import type { L1TxUtilsConfig } from './config.js';
11
+ import { BLOB_CAPACITY_SCHEDULE, GAS_PER_BLOB, WEI_CONST } from './constants.js';
12
+ import {
13
+ DEFAULT_PRIORITY_FEE_STRATEGIES,
14
+ type PriorityFeeStrategy,
15
+ type PriorityFeeStrategyContext,
16
+ executeStrategy,
17
+ } from './fee-strategies/index.js';
18
+ import type { L1BlobInputs, L1TxRequest } from './types.js';
19
+
20
+ /**
21
+ * Information about a blob transaction in the pending pool or mined block
22
+ */
23
+ export interface BlobTxInfo {
24
+ hash: Hex;
25
+ maxPriorityFeePerGas: bigint;
26
+ maxFeePerGas: bigint;
27
+ maxFeePerBlobGas: bigint;
28
+ blobCount: number;
29
+ gas: bigint;
30
+ }
31
+
32
+ /**
33
+ * Snapshot of the pending block state at the time of analysis
34
+ */
35
+ export interface PendingBlockSnapshot {
36
+ /** Timestamp when the snapshot was taken */
37
+ timestamp: number;
38
+ /** The latest L1 block number at the time of snapshot */
39
+ latestBlockNumber: bigint;
40
+ /** Base fee per gas of the latest block */
41
+ baseFeePerGas: bigint;
42
+ /** Blob base fee at the time of snapshot */
43
+ blobBaseFee: bigint;
44
+ /** Total number of transactions in the pending block */
45
+ pendingTxCount: number;
46
+ /** Number of blob transactions in the pending block */
47
+ pendingBlobTxCount: number;
48
+ /** Total number of blobs in pending blob transactions */
49
+ pendingBlobCount: number;
50
+ /** Details of blob transactions in the pending pool */
51
+ pendingBlobTxs: BlobTxInfo[];
52
+ /** 75th percentile priority fee from pending transactions */
53
+ pendingP75PriorityFee: bigint;
54
+ /** 75th percentile priority fee from pending blob transactions */
55
+ pendingBlobP75PriorityFee: bigint;
56
+ }
57
+
58
+ /**
59
+ * Result of a strategy's priority fee calculation for analysis
60
+ */
61
+ export interface StrategyAnalysisResult {
62
+ /** Strategy ID */
63
+ strategyId: string;
64
+ /** Strategy name */
65
+ strategyName: string;
66
+ /** Calculated priority fee from this strategy */
67
+ calculatedPriorityFee: bigint;
68
+ /** Debug info from the strategy calculation */
69
+ debugInfo?: Record<string, string | number>;
70
+ /** Whether this transaction would have been included with this strategy's fee */
71
+ wouldBeIncluded?: boolean;
72
+ /** If not included, reason why */
73
+ exclusionReason?: 'priority_fee_too_low' | 'block_full';
74
+ /** Priority fee delta compared to minimum included fee */
75
+ priorityFeeDelta?: bigint;
76
+ /** Estimated total cost in ETH for this strategy */
77
+ estimatedCostEth?: number;
78
+ /** Estimated overpayment in ETH vs minimum required */
79
+ estimatedOverpaymentEth?: number;
80
+ }
81
+
82
+ /**
83
+ * Transaction metadata and strategy analysis results
84
+ */
85
+ export interface ComputedGasPrices {
86
+ /** Estimated gas limit for the transaction */
87
+ gasLimit: bigint;
88
+ /** Number of blobs in our transaction */
89
+ blobCount: number;
90
+ /** Results from all strategies analyzed */
91
+ strategyResults?: StrategyAnalysisResult[];
92
+ }
93
+
94
+ /**
95
+ * Information about what actually got included in the mined block
96
+ */
97
+ export interface MinedBlockInfo {
98
+ /** The block number that was mined */
99
+ blockNumber: bigint;
100
+ /** The block hash */
101
+ blockHash: Hex;
102
+ /** Timestamp of the mined block */
103
+ blockTimestamp: bigint;
104
+ /** Base fee per gas in the mined block */
105
+ baseFeePerGas: bigint;
106
+ /** Blob gas used in the mined block */
107
+ blobGasUsed: bigint;
108
+ /** Total number of transactions in the mined block */
109
+ txCount: number;
110
+ /** Number of blob transactions that got included */
111
+ includedBlobTxCount: number;
112
+ /** Total number of blobs included in the block */
113
+ includedBlobCount: number;
114
+ /** Details of blob transactions that got included */
115
+ includedBlobTxs: BlobTxInfo[];
116
+ /** Minimum priority fee among included transactions */
117
+ minIncludedPriorityFee: bigint;
118
+ /** Minimum priority fee among included blob transactions */
119
+ minIncludedBlobPriorityFee: bigint;
120
+ }
121
+
122
+ /**
123
+ * Complete fee analysis result comparing our estimates to what happened
124
+ */
125
+ export interface L1FeeAnalysisResult {
126
+ /** Unique identifier for this analysis */
127
+ id: string;
128
+ /** L2 slot number this analysis was performed for */
129
+ l2SlotNumber: SlotNumber;
130
+ /** Snapshot of pending state when we computed our fees */
131
+ pendingSnapshot: PendingBlockSnapshot;
132
+ /** Our computed gas prices */
133
+ computedPrices: ComputedGasPrices;
134
+ /** Information about what we were trying to send */
135
+ txInfo: {
136
+ requestCount: number;
137
+ hasBlobData: boolean;
138
+ totalEstimatedGas: bigint;
139
+ };
140
+ /** Information about the block that was eventually mined (populated after block mines) */
141
+ minedBlock?: MinedBlockInfo;
142
+ /** Analysis results (populated after block mines) */
143
+ analysis?: {
144
+ /** Time in ms between our snapshot and block mining */
145
+ timeBeforeBlockMs: number;
146
+ /** How many blob txs from pending actually got included */
147
+ pendingBlobTxsIncludedCount: number;
148
+ /** How many blob txs from pending were NOT included */
149
+ pendingBlobTxsExcludedCount: number;
150
+ /** Number of blobs in the mined block */
151
+ blobsInBlock: number;
152
+ /** Maximum blob capacity for this block */
153
+ maxBlobCapacity: number;
154
+ /** Whether the block's blob space was full */
155
+ blockBlobsFull: boolean;
156
+ /** Actual cost in ETH if this analysis is linked to a mined tx */
157
+ actualCostEth?: number;
158
+ /** Strategy results ranked by estimated cost */
159
+ costRanking?: Array<{
160
+ strategyId: string;
161
+ strategyName: string;
162
+ estimatedCostEth: number;
163
+ wouldBeIncluded: boolean;
164
+ }>;
165
+ };
166
+ }
167
+
168
+ /** Callback type for when an analysis is completed */
169
+ export type L1FeeAnalysisCallback = (analysis: L1FeeAnalysisResult) => void;
170
+
171
+ /**
172
+ * Result of processing transactions to extract blob tx info and priority fees
173
+ */
174
+ interface ProcessedTransactions {
175
+ blobTxs: BlobTxInfo[];
176
+ allPriorityFees: bigint[];
177
+ blobPriorityFees: bigint[];
178
+ totalBlobCount: number;
179
+ }
180
+
181
+ /**
182
+ * Gets the maximum blob capacity for a given block timestamp
183
+ */
184
+ function getMaxBlobCapacity(blockTimestamp: bigint): number {
185
+ const timestamp = Number(blockTimestamp);
186
+ // Find the applicable schedule entry (sorted by timestamp descending)
187
+ for (const schedule of BLOB_CAPACITY_SCHEDULE) {
188
+ if (timestamp >= schedule.timestamp) {
189
+ return schedule.max;
190
+ }
191
+ }
192
+ // Fallback (should never hit)
193
+ return BLOB_CAPACITY_SCHEDULE[BLOB_CAPACITY_SCHEDULE.length - 1].max;
194
+ }
195
+
196
+ /**
197
+ * Processes a list of transactions to extract blob transaction info and priority fees.
198
+ * Handles both pending and mined transactions.
199
+ * Note: Only works with blocks fetched with includeTransactions: true
200
+ */
201
+ function processTransactions(transactions: readonly FormattedTransaction[] | undefined): ProcessedTransactions {
202
+ const blobTxs: BlobTxInfo[] = [];
203
+ const allPriorityFees: bigint[] = [];
204
+ const blobPriorityFees: bigint[] = [];
205
+ let totalBlobCount = 0;
206
+
207
+ if (!transactions) {
208
+ return { blobTxs, allPriorityFees, blobPriorityFees, totalBlobCount };
209
+ }
210
+
211
+ for (const tx of transactions) {
212
+ const priorityFee = tx.maxPriorityFeePerGas || 0n;
213
+ if (priorityFee > 0n) {
214
+ allPriorityFees.push(priorityFee);
215
+ }
216
+
217
+ // Check if this is a blob transaction
218
+ if (isBlobTransaction(tx)) {
219
+ const blobCount = tx.blobVersionedHashes.length;
220
+ totalBlobCount += blobCount;
221
+
222
+ if (priorityFee > 0n) {
223
+ blobPriorityFees.push(priorityFee);
224
+ }
225
+
226
+ blobTxs.push({
227
+ hash: tx.hash,
228
+ maxPriorityFeePerGas: priorityFee,
229
+ maxFeePerGas: tx.maxFeePerGas || 0n,
230
+ maxFeePerBlobGas: tx.maxFeePerBlobGas,
231
+ blobCount,
232
+ gas: tx.gas,
233
+ });
234
+ }
235
+ }
236
+
237
+ return { blobTxs, allPriorityFees, blobPriorityFees, totalBlobCount };
238
+ }
239
+
240
+ /**
241
+ * Analyzes L1 transaction fees in fisherman mode.
242
+ * Captures pending block state, records gas calculations, and compares to what gets included.
243
+ * Supports multiple priority fee calculation strategies for comparison.
244
+ */
245
+ export class L1FeeAnalyzer {
246
+ private pendingAnalyses: Map<string, L1FeeAnalysisResult> = new Map();
247
+ private pendingCallbacks: Map<string, L1FeeAnalysisCallback> = new Map();
248
+ private completedAnalyses: L1FeeAnalysisResult[] = [];
249
+ private analysisCounter = 0;
250
+ private strategies: PriorityFeeStrategy[];
251
+
252
+ constructor(
253
+ private client: ViemClient,
254
+ private dateProvider: DateProvider = new DateProvider(),
255
+ private logger: Logger = createLogger('ethereum:l1-fee-analyzer'),
256
+ private maxCompletedAnalyses: number = 100,
257
+ strategies: PriorityFeeStrategy[] = DEFAULT_PRIORITY_FEE_STRATEGIES,
258
+ private gasConfig: L1TxUtilsConfig = {},
259
+ ) {
260
+ this.strategies = strategies;
261
+ }
262
+
263
+ /**
264
+ * Executes all configured strategies and returns their results.
265
+ * Each strategy defines its own promises which are executed and passed to calculate.
266
+ * @param isBlobTx - Whether this is a blob transaction
267
+ * @returns Array of strategy results
268
+ */
269
+ async executeAllStrategies(isBlobTx: boolean): Promise<StrategyAnalysisResult[]> {
270
+ const results: StrategyAnalysisResult[] = [];
271
+ const context: PriorityFeeStrategyContext = {
272
+ gasConfig: this.gasConfig,
273
+ isBlobTx,
274
+ logger: this.logger,
275
+ };
276
+
277
+ for (const strategy of this.strategies) {
278
+ try {
279
+ const result = await executeStrategy(strategy, this.client, context);
280
+
281
+ results.push({
282
+ strategyId: strategy.id,
283
+ strategyName: strategy.name,
284
+ calculatedPriorityFee: result.priorityFee,
285
+ debugInfo: result.debugInfo,
286
+ });
287
+
288
+ this.logger.debug(`Strategy "${strategy.name}" calculated priority fee`, {
289
+ strategyId: strategy.id,
290
+ priorityFee: formatGwei(result.priorityFee),
291
+ ...result.debugInfo,
292
+ });
293
+ } catch (err) {
294
+ this.logger.error(`Error calculating priority fee for strategy "${strategy.name}"`, err, {
295
+ strategyId: strategy.id,
296
+ });
297
+ }
298
+ }
299
+
300
+ return results;
301
+ }
302
+
303
+ /**
304
+ * Captures a snapshot of the current pending block state
305
+ */
306
+ async capturePendingSnapshot(): Promise<PendingBlockSnapshot> {
307
+ const timestamp = this.dateProvider.now();
308
+
309
+ // Fetch data in parallel
310
+ const [latestBlock, pendingBlock, blobBaseFee] = await Promise.all([
311
+ this.client.getBlock({ blockTag: 'latest' }),
312
+ this.client.getBlock({ blockTag: 'pending', includeTransactions: true }).catch(() => null),
313
+ this.client.getBlobBaseFee().catch(() => 0n),
314
+ ]);
315
+
316
+ const baseFeePerGas = latestBlock.baseFeePerGas || 0n;
317
+ const latestBlockNumber = latestBlock.number;
318
+
319
+ // Extract blob transaction info from pending block
320
+ const {
321
+ blobTxs: pendingBlobTxs,
322
+ allPriorityFees: allPendingPriorityFees,
323
+ blobPriorityFees: pendingBlobPriorityFees,
324
+ totalBlobCount: pendingBlobCount,
325
+ } = processTransactions(pendingBlock?.transactions);
326
+
327
+ // Calculate 75th percentile priority fees
328
+ const pendingP75PriorityFee = calculatePercentile(allPendingPriorityFees, 75);
329
+ const pendingBlobP75PriorityFee = calculatePercentile(pendingBlobPriorityFees, 75);
330
+
331
+ const pendingTxCount = pendingBlock?.transactions?.length || 0;
332
+
333
+ return {
334
+ timestamp,
335
+ latestBlockNumber,
336
+ baseFeePerGas,
337
+ blobBaseFee,
338
+ pendingTxCount,
339
+ pendingBlobTxCount: pendingBlobTxs.length,
340
+ pendingBlobCount,
341
+ pendingBlobTxs,
342
+ pendingP75PriorityFee,
343
+ pendingBlobP75PriorityFee,
344
+ };
345
+ }
346
+
347
+ /**
348
+ * Starts a fee analysis for a transaction bundle
349
+ * @param l2SlotNumber - The L2 slot this analysis is for
350
+ * @param gasLimit - The estimated gas limit
351
+ * @param requests - The transaction requests being analyzed
352
+ * @param blobInputs - Blob inputs if this is a blob transaction
353
+ * @param onComplete - Optional callback to invoke when analysis completes
354
+ * @returns The analysis ID for tracking
355
+ */
356
+ async startAnalysis(
357
+ l2SlotNumber: SlotNumber,
358
+ gasLimit: bigint,
359
+ requests: L1TxRequest[],
360
+ blobInputs?: L1BlobInputs,
361
+ onComplete?: L1FeeAnalysisCallback,
362
+ ): Promise<string> {
363
+ const id = `fee-analysis-${++this.analysisCounter}-${Date.now()}`;
364
+
365
+ const blobCount = blobInputs?.blobs?.length || 0;
366
+ const isBlobTx = blobCount > 0;
367
+
368
+ // Execute all strategies and capture pending snapshot in parallel
369
+ const [pendingSnapshot, strategyResults] = await Promise.all([
370
+ this.capturePendingSnapshot(),
371
+ this.executeAllStrategies(isBlobTx),
372
+ ]);
373
+
374
+ const analysis: L1FeeAnalysisResult = {
375
+ id,
376
+ l2SlotNumber,
377
+ pendingSnapshot,
378
+ computedPrices: {
379
+ gasLimit,
380
+ blobCount,
381
+ strategyResults,
382
+ },
383
+ txInfo: {
384
+ requestCount: requests.length,
385
+ hasBlobData: isBlobTx,
386
+ totalEstimatedGas: gasLimit,
387
+ },
388
+ };
389
+
390
+ this.pendingAnalyses.set(id, analysis);
391
+ if (onComplete) {
392
+ this.pendingCallbacks.set(id, onComplete);
393
+ }
394
+
395
+ // Log strategy calculations
396
+ const strategyLogInfo = strategyResults.reduce(
397
+ (acc, s) => {
398
+ acc[`strategy_${s.strategyId}`] = formatGwei(s.calculatedPriorityFee);
399
+ return acc;
400
+ },
401
+ {} as Record<string, string>,
402
+ );
403
+
404
+ this.logger.debug('Started fee analysis with strategy calculations', {
405
+ id,
406
+ l2SlotNumber: l2SlotNumber.toString(),
407
+ pendingBlobTxCount: pendingSnapshot.pendingBlobTxCount,
408
+ pendingBlobCount: pendingSnapshot.pendingBlobCount,
409
+ pendingBlobP75: formatGwei(pendingSnapshot.pendingBlobP75PriorityFee),
410
+ strategiesAnalyzed: strategyResults.length,
411
+ ...strategyLogInfo,
412
+ });
413
+
414
+ // Start watching for the next block
415
+ void this.watchForNextBlock(id, pendingSnapshot.latestBlockNumber);
416
+
417
+ return id;
418
+ }
419
+
420
+ /**
421
+ * Watches for the next block to be mined and completes the analysis
422
+ */
423
+ private async watchForNextBlock(analysisId: string, startBlockNumber: bigint): Promise<void> {
424
+ const analysis = this.pendingAnalyses.get(analysisId);
425
+ if (!analysis) {
426
+ return;
427
+ }
428
+
429
+ try {
430
+ // wait for next block
431
+ await retryUntil(
432
+ async () => {
433
+ const currentBlockNumber = await this.client.getBlockNumber();
434
+ if (currentBlockNumber > startBlockNumber) {
435
+ return true;
436
+ }
437
+ return false;
438
+ },
439
+ 'Wait for next block',
440
+ 13_000,
441
+ 0.5,
442
+ );
443
+
444
+ const minedBlock = await this.client.getBlock({
445
+ includeTransactions: true,
446
+ });
447
+ this.completeAnalysis(analysisId, minedBlock);
448
+ } catch (err) {
449
+ this.logger.error('Error waiting for next block in fee analysis', err, { analysisId });
450
+ } finally {
451
+ this.pendingAnalyses.delete(analysisId);
452
+ }
453
+ }
454
+
455
+ /**
456
+ * Completes the analysis once the next block is mined
457
+ */
458
+ private completeAnalysis(analysisId: string, minedBlock: Block<bigint, true, 'latest'>): void {
459
+ const analysis = this.pendingAnalyses.get(analysisId);
460
+ if (!analysis) {
461
+ return;
462
+ }
463
+
464
+ // Extract blob transaction info from mined block
465
+ const {
466
+ blobTxs: includedBlobTxs,
467
+ allPriorityFees: includedPriorityFees,
468
+ blobPriorityFees: includedBlobPriorityFees,
469
+ totalBlobCount: includedBlobCount,
470
+ } = processTransactions(minedBlock.transactions);
471
+
472
+ // Get minimum included fees
473
+ const minIncludedPriorityFee = includedPriorityFees.length > 0 ? this.minBigInt(includedPriorityFees) : 0n;
474
+ const minIncludedBlobPriorityFee =
475
+ includedBlobPriorityFees.length > 0 ? this.minBigInt(includedBlobPriorityFees) : 0n;
476
+
477
+ // Populate mined block info
478
+ analysis.minedBlock = {
479
+ blockNumber: minedBlock.number,
480
+ blockHash: minedBlock.hash,
481
+ blockTimestamp: minedBlock.timestamp,
482
+ baseFeePerGas: minedBlock.baseFeePerGas || 0n,
483
+ blobGasUsed: minedBlock.blobGasUsed || 0n,
484
+ txCount: minedBlock.transactions?.length || 0,
485
+ includedBlobTxCount: includedBlobTxs.length,
486
+ includedBlobCount,
487
+ includedBlobTxs,
488
+ minIncludedPriorityFee,
489
+ minIncludedBlobPriorityFee,
490
+ };
491
+
492
+ // Calculate time before block mined
493
+ const blockTimestampMs = Number(minedBlock.timestamp) * 1000;
494
+ const timeBeforeBlockMs = blockTimestampMs - analysis.pendingSnapshot.timestamp;
495
+
496
+ // Calculate how many blobs were actually in the mined block
497
+ const blobsInBlock = minedBlock.blobGasUsed > 0n ? Number(minedBlock.blobGasUsed / GAS_PER_BLOB) : 0;
498
+ const maxBlobCapacity = getMaxBlobCapacity(minedBlock.timestamp);
499
+ const blockBlobsFull = blobsInBlock >= maxBlobCapacity;
500
+
501
+ // Count how many pending blob txs actually got included
502
+ const pendingBlobHashes = new Set(analysis.pendingSnapshot.pendingBlobTxs.map(tx => tx.hash));
503
+ const includedBlobHashes = new Set(includedBlobTxs.map(tx => tx.hash));
504
+ const pendingBlobTxsIncludedCount = [...pendingBlobHashes].filter(h => includedBlobHashes.has(h)).length;
505
+ const pendingBlobTxsExcludedCount = analysis.pendingSnapshot.pendingBlobTxCount - pendingBlobTxsIncludedCount;
506
+
507
+ analysis.analysis = {
508
+ timeBeforeBlockMs,
509
+ pendingBlobTxsIncludedCount,
510
+ pendingBlobTxsExcludedCount,
511
+ blobsInBlock,
512
+ maxBlobCapacity,
513
+ blockBlobsFull,
514
+ };
515
+
516
+ // Evaluate each strategy against the mined block
517
+ const isBlobTx = analysis.computedPrices.blobCount > 0;
518
+ const minPriorityFeeToCompare = isBlobTx ? minIncludedBlobPriorityFee : minIncludedPriorityFee;
519
+ const gasLimit = analysis.computedPrices.gasLimit;
520
+
521
+ if (analysis.computedPrices.strategyResults) {
522
+ for (const strategyResult of analysis.computedPrices.strategyResults) {
523
+ const strategyPriorityFee = strategyResult.calculatedPriorityFee;
524
+ const strategyPriorityFeeDelta = strategyPriorityFee - minPriorityFeeToCompare;
525
+
526
+ // Determine if this strategy would have resulted in inclusion
527
+ let strategyWouldBeIncluded = true;
528
+ let strategyExclusionReason: 'priority_fee_too_low' | 'block_full' | undefined;
529
+
530
+ if (isBlobTx) {
531
+ // For blob txs, only consider priority fee if blob space was full
532
+ if (
533
+ includedBlobPriorityFees.length > 0 &&
534
+ strategyPriorityFee < minIncludedBlobPriorityFee &&
535
+ blockBlobsFull
536
+ ) {
537
+ strategyWouldBeIncluded = false;
538
+ strategyExclusionReason = 'priority_fee_too_low';
539
+ }
540
+ } else {
541
+ // For non-blob txs, use the old logic
542
+ if (includedPriorityFees.length > 0 && strategyPriorityFee < minIncludedPriorityFee) {
543
+ strategyWouldBeIncluded = false;
544
+ strategyExclusionReason = 'priority_fee_too_low';
545
+ }
546
+ }
547
+
548
+ // Calculate estimated cost in ETH for this strategy
549
+ // Cost = gasLimit * (baseFee + priorityFee)
550
+ const baseFee = analysis.minedBlock.baseFeePerGas;
551
+
552
+ // Execution cost: gasLimit * (baseFee + priorityFee)
553
+ const executionCostWei = gasLimit * (baseFee + strategyPriorityFee);
554
+ const estimatedCostEth = Number(executionCostWei) / 1e18;
555
+
556
+ // Calculate minimum cost needed for inclusion
557
+ const minExecutionCostWei = gasLimit * (baseFee + minPriorityFeeToCompare);
558
+ const minCostEth = Number(minExecutionCostWei) / 1e18;
559
+
560
+ // Overpayment is the difference
561
+ const estimatedOverpaymentEth = estimatedCostEth - minCostEth;
562
+
563
+ // Update the strategy result with analysis data
564
+ strategyResult.wouldBeIncluded = strategyWouldBeIncluded;
565
+ strategyResult.exclusionReason = strategyExclusionReason;
566
+ strategyResult.priorityFeeDelta = strategyPriorityFeeDelta;
567
+ strategyResult.estimatedCostEth = estimatedCostEth;
568
+ strategyResult.estimatedOverpaymentEth = estimatedOverpaymentEth;
569
+
570
+ // Log per-strategy results
571
+ this.logger.info(`Strategy "${strategyResult.strategyName}" analysis`, {
572
+ id: analysisId,
573
+ strategyId: strategyResult.strategyId,
574
+ strategyName: strategyResult.strategyName,
575
+ calculatedPriorityFee: formatGwei(strategyPriorityFee),
576
+ minIncludedPriorityFee: formatGwei(minPriorityFeeToCompare),
577
+ priorityFeeDelta: formatGwei(strategyPriorityFeeDelta),
578
+ wouldBeIncluded: strategyWouldBeIncluded,
579
+ exclusionReason: strategyExclusionReason,
580
+ estimatedCostEth: estimatedCostEth.toFixed(6),
581
+ estimatedOverpaymentEth: estimatedOverpaymentEth.toFixed(6),
582
+ });
583
+ }
584
+
585
+ // Create cost ranking
586
+ const costRanking = analysis.computedPrices.strategyResults
587
+ .map(s => ({
588
+ strategyId: s.strategyId,
589
+ strategyName: s.strategyName,
590
+ estimatedCostEth: s.estimatedCostEth!,
591
+ wouldBeIncluded: s.wouldBeIncluded!,
592
+ }))
593
+ .sort((a, b) => a.estimatedCostEth - b.estimatedCostEth);
594
+
595
+ analysis.analysis!.costRanking = costRanking;
596
+
597
+ // Log cost ranking summary
598
+ this.logger.info('Strategy cost ranking', {
599
+ id: analysisId,
600
+ cheapestStrategy: costRanking[0]?.strategyName,
601
+ cheapestCost: costRanking[0]?.estimatedCostEth.toFixed(6),
602
+ cheapestWouldBeIncluded: costRanking[0]?.wouldBeIncluded,
603
+ mostExpensiveStrategy: costRanking[costRanking.length - 1]?.strategyName,
604
+ mostExpensiveCost: costRanking[costRanking.length - 1]?.estimatedCostEth.toFixed(6),
605
+ mostExpensiveWouldBeIncluded: costRanking[costRanking.length - 1]?.wouldBeIncluded,
606
+ costSpread:
607
+ costRanking.length > 1
608
+ ? (costRanking[costRanking.length - 1].estimatedCostEth - costRanking[0].estimatedCostEth).toFixed(6)
609
+ : '0',
610
+ });
611
+ }
612
+
613
+ // Log the overall results
614
+ this.logger.info('Fee analysis completed', {
615
+ id: analysisId,
616
+ l2SlotNumber: analysis.l2SlotNumber.toString(),
617
+ timeBeforeBlockMs,
618
+ pendingBlobTxCount: analysis.pendingSnapshot.pendingBlobTxCount,
619
+ includedBlobTxCount: analysis.minedBlock.includedBlobTxCount,
620
+ pendingBlobTxsIncludedCount,
621
+ pendingBlobTxsExcludedCount,
622
+ blobsInBlock,
623
+ maxBlobCapacity,
624
+ blockBlobsFull,
625
+ minIncludedPriorityFee: formatGwei(minIncludedPriorityFee),
626
+ minIncludedBlobPriorityFee: formatGwei(minIncludedBlobPriorityFee),
627
+ strategiesAnalyzed: analysis.computedPrices.strategyResults?.length ?? 0,
628
+ });
629
+
630
+ // Move to completed analyses
631
+ this.pendingAnalyses.delete(analysisId);
632
+ this.completedAnalyses.push(analysis);
633
+
634
+ // Trim old completed analyses if needed
635
+ while (this.completedAnalyses.length > this.maxCompletedAnalyses) {
636
+ this.completedAnalyses.shift();
637
+ }
638
+
639
+ // Invoke the callback for this specific analysis
640
+ const callback = this.pendingCallbacks.get(analysisId);
641
+ if (callback) {
642
+ try {
643
+ callback(analysis);
644
+ } catch (err) {
645
+ this.logger.error('Error in analysis complete callback', err);
646
+ }
647
+ this.pendingCallbacks.delete(analysisId);
648
+ }
649
+ }
650
+
651
+ /**
652
+ * Gets a specific analysis result by ID
653
+ */
654
+ getAnalysis(id: string): L1FeeAnalysisResult | undefined {
655
+ return this.pendingAnalyses.get(id) || this.completedAnalyses.find(a => a.id === id);
656
+ }
657
+
658
+ /**
659
+ * Gets all completed analyses
660
+ */
661
+ getCompletedAnalyses(): L1FeeAnalysisResult[] {
662
+ return [...this.completedAnalyses];
663
+ }
664
+
665
+ /**
666
+ * Gets statistics about all completed analyses
667
+ */
668
+ getAnalysisStats(): {
669
+ totalAnalyses: number;
670
+ avgTimeBeforeBlockMs: number;
671
+ avgBlobsInBlock: number;
672
+ blocksBlobsFull: number;
673
+ } {
674
+ const completed = this.completedAnalyses.filter(a => a.analysis);
675
+
676
+ if (completed.length === 0) {
677
+ return {
678
+ totalAnalyses: 0,
679
+ avgTimeBeforeBlockMs: 0,
680
+ avgBlobsInBlock: 0,
681
+ blocksBlobsFull: 0,
682
+ };
683
+ }
684
+
685
+ const avgTimeBeforeBlockMs =
686
+ completed.reduce((sum, a) => sum + a.analysis!.timeBeforeBlockMs, 0) / completed.length;
687
+
688
+ const avgBlobsInBlock = completed.reduce((sum, a) => sum + a.analysis!.blobsInBlock, 0) / completed.length;
689
+
690
+ const blocksBlobsFull = completed.filter(a => a.analysis!.blockBlobsFull).length;
691
+
692
+ return {
693
+ totalAnalyses: completed.length,
694
+ avgTimeBeforeBlockMs,
695
+ avgBlobsInBlock,
696
+ blocksBlobsFull,
697
+ };
698
+ }
699
+
700
+ /**
701
+ * Gets comparative statistics for all strategies across completed analyses
702
+ */
703
+ getStrategyComparison(): Array<{
704
+ strategyId: string;
705
+ strategyName: string;
706
+ totalAnalyses: number;
707
+ inclusionCount: number;
708
+ inclusionRate: number;
709
+ avgEstimatedCostEth: number;
710
+ totalEstimatedCostEth: number;
711
+ avgOverpaymentEth: number;
712
+ totalOverpaymentEth: number;
713
+ avgPriorityFeeDeltaGwei: number;
714
+ }> {
715
+ const completed = this.completedAnalyses.filter(a => a.analysis);
716
+
717
+ if (completed.length === 0) {
718
+ return [];
719
+ }
720
+
721
+ // Collect data by strategy ID
722
+ const strategyData = new Map<
723
+ string,
724
+ {
725
+ strategyName: string;
726
+ analyses: number;
727
+ inclusions: number;
728
+ totalCostEth: number;
729
+ totalOverpaymentEth: number;
730
+ totalPriorityFeeDelta: number;
731
+ }
732
+ >();
733
+
734
+ for (const analysis of completed) {
735
+ if (!analysis.computedPrices.strategyResults) {
736
+ continue;
737
+ }
738
+
739
+ for (const strategyResult of analysis.computedPrices.strategyResults) {
740
+ if (!strategyData.has(strategyResult.strategyId)) {
741
+ strategyData.set(strategyResult.strategyId, {
742
+ strategyName: strategyResult.strategyName,
743
+ analyses: 0,
744
+ inclusions: 0,
745
+ totalCostEth: 0,
746
+ totalOverpaymentEth: 0,
747
+ totalPriorityFeeDelta: 0,
748
+ });
749
+ }
750
+
751
+ const data = strategyData.get(strategyResult.strategyId)!;
752
+ data.analyses++;
753
+
754
+ if (strategyResult.wouldBeIncluded) {
755
+ data.inclusions++;
756
+ }
757
+
758
+ if (strategyResult.estimatedCostEth !== undefined) {
759
+ data.totalCostEth += strategyResult.estimatedCostEth;
760
+ }
761
+
762
+ if (strategyResult.estimatedOverpaymentEth !== undefined) {
763
+ data.totalOverpaymentEth += strategyResult.estimatedOverpaymentEth;
764
+ }
765
+
766
+ if (strategyResult.priorityFeeDelta !== undefined) {
767
+ data.totalPriorityFeeDelta += Number(strategyResult.priorityFeeDelta);
768
+ }
769
+ }
770
+ }
771
+
772
+ // Convert to output format
773
+ const results = Array.from(strategyData.entries()).map(([strategyId, data]) => ({
774
+ strategyId,
775
+ strategyName: data.strategyName,
776
+ totalAnalyses: data.analyses,
777
+ inclusionCount: data.inclusions,
778
+ inclusionRate: data.analyses > 0 ? data.inclusions / data.analyses : 0,
779
+ avgEstimatedCostEth: data.analyses > 0 ? data.totalCostEth / data.analyses : 0,
780
+ totalEstimatedCostEth: data.totalCostEth,
781
+ avgOverpaymentEth: data.analyses > 0 ? data.totalOverpaymentEth / data.analyses : 0,
782
+ totalOverpaymentEth: data.totalOverpaymentEth,
783
+ avgPriorityFeeDeltaGwei: data.analyses > 0 ? data.totalPriorityFeeDelta / data.analyses / Number(WEI_CONST) : 0,
784
+ }));
785
+
786
+ // Sort by inclusion rate descending, then by avg cost ascending
787
+ return results.sort((a, b) => {
788
+ if (Math.abs(a.inclusionRate - b.inclusionRate) > 0.01) {
789
+ return b.inclusionRate - a.inclusionRate;
790
+ }
791
+ return a.avgEstimatedCostEth - b.avgEstimatedCostEth;
792
+ });
793
+ }
794
+
795
+ /**
796
+ * Gets the minimum value from an array of bigints
797
+ */
798
+ private minBigInt(values: bigint[]): bigint {
799
+ if (values.length === 0) {
800
+ return 0n;
801
+ }
802
+ return values.reduce((min, val) => (val < min ? val : min), values[0]);
803
+ }
804
+ }