@aztec/ethereum 3.0.0-nightly.20251216 → 3.0.0-nightly.20251218

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 (45) hide show
  1. package/dest/deploy_aztec_l1_contracts.d.ts +1 -1
  2. package/dest/deploy_aztec_l1_contracts.d.ts.map +1 -1
  3. package/dest/deploy_aztec_l1_contracts.js +10 -8
  4. package/dest/l1_contract_addresses.d.ts +1 -1
  5. package/dest/l1_contract_addresses.d.ts.map +1 -1
  6. package/dest/l1_contract_addresses.js +3 -3
  7. package/dest/l1_tx_utils/constants.d.ts +7 -1
  8. package/dest/l1_tx_utils/constants.d.ts.map +1 -1
  9. package/dest/l1_tx_utils/constants.js +25 -0
  10. package/dest/l1_tx_utils/fee-strategies/index.d.ts +9 -0
  11. package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
  12. package/dest/l1_tx_utils/fee-strategies/index.js +11 -0
  13. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +18 -0
  14. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
  15. package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +111 -0
  16. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +32 -0
  17. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
  18. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +173 -0
  19. package/dest/l1_tx_utils/fee-strategies/types.d.ts +64 -0
  20. package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
  21. package/dest/l1_tx_utils/fee-strategies/types.js +24 -0
  22. package/dest/l1_tx_utils/index.d.ts +3 -1
  23. package/dest/l1_tx_utils/index.d.ts.map +1 -1
  24. package/dest/l1_tx_utils/index.js +2 -0
  25. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +232 -0
  26. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
  27. package/dest/l1_tx_utils/l1_fee_analyzer.js +506 -0
  28. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +1 -10
  29. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -1
  30. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +43 -121
  31. package/dest/utils.d.ts +14 -2
  32. package/dest/utils.d.ts.map +1 -1
  33. package/dest/utils.js +18 -0
  34. package/package.json +6 -5
  35. package/src/deploy_aztec_l1_contracts.ts +14 -7
  36. package/src/l1_contract_addresses.ts +22 -20
  37. package/src/l1_tx_utils/constants.ts +11 -0
  38. package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
  39. package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +159 -0
  40. package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +241 -0
  41. package/src/l1_tx_utils/fee-strategies/types.ts +88 -0
  42. package/src/l1_tx_utils/index.ts +2 -0
  43. package/src/l1_tx_utils/l1_fee_analyzer.ts +803 -0
  44. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +47 -158
  45. package/src/utils.ts +29 -0
@@ -0,0 +1,241 @@
1
+ import { median } from '@aztec/foundation/collection';
2
+
3
+ import { type GetFeeHistoryReturnType, formatGwei } from 'viem';
4
+
5
+ import type { ViemClient } from '../../types.js';
6
+ import { calculatePercentile, isBlobTransaction } from '../../utils.js';
7
+ import { WEI_CONST } from '../constants.js';
8
+ import {
9
+ HISTORICAL_BLOCK_COUNT,
10
+ type PriorityFeeStrategy,
11
+ type PriorityFeeStrategyContext,
12
+ type PriorityFeeStrategyResult,
13
+ } from './types.js';
14
+
15
+ /**
16
+ * Type for the promises required by the competitive strategy
17
+ */
18
+ type P75AllTxsStrategyPromises = {
19
+ networkEstimate: Promise<bigint>;
20
+ pendingBlock: Promise<Awaited<ReturnType<ViemClient['getBlock']>> | null>;
21
+ feeHistory: Promise<Awaited<ReturnType<ViemClient['getFeeHistory']>> | null>;
22
+ };
23
+
24
+ /**
25
+ * Fetches historical blocks and calculates reward percentiles for blob transactions only.
26
+ * Returns data in the same format as getFeeHistory for easy drop-in replacement.
27
+ *
28
+ * @param client - Viem client to use for RPC calls
29
+ * @param blockCount - Number of historical blocks to fetch
30
+ * @param rewardPercentiles - Array of percentiles to calculate (e.g., [75] for 75th percentile)
31
+ * @returns Object with reward field containing percentile fees for each block, similar to getFeeHistory
32
+ * @throws Error if fetching blocks fails
33
+ */
34
+ export async function getBlobPriorityFeeHistory(
35
+ client: ViemClient,
36
+ blockCount: number,
37
+ rewardPercentiles: number[],
38
+ ): Promise<GetFeeHistoryReturnType> {
39
+ const latestBlockNumber = await client.getBlockNumber();
40
+
41
+ // Fetch multiple blocks in parallel
42
+ const blockPromises = Array.from({ length: blockCount }, (_, i) =>
43
+ client.getBlock({
44
+ blockNumber: latestBlockNumber - BigInt(i),
45
+ includeTransactions: true,
46
+ }),
47
+ );
48
+
49
+ const blocks = await Promise.all(blockPromises);
50
+
51
+ // Process each block to extract blob transaction fees and other data
52
+ const baseFeePerGas: bigint[] = [];
53
+ const gasUsedRatio: number[] = [];
54
+ const reward: bigint[][] = [];
55
+
56
+ for (const block of blocks) {
57
+ // Collect base fee per gas
58
+ baseFeePerGas.push(block.baseFeePerGas || 0n);
59
+
60
+ // Calculate gas used ratio
61
+ const gasUsed = block.gasUsed || 0n;
62
+ const gasLimit = block.gasLimit || 1n; // Avoid division by zero
63
+ gasUsedRatio.push(Number(gasUsed) / Number(gasLimit));
64
+
65
+ if (!block.transactions || block.transactions.length === 0) {
66
+ // No transactions in this block - return zeros for each percentile
67
+ reward.push(rewardPercentiles.map(() => 0n));
68
+ continue;
69
+ }
70
+
71
+ // Extract priority fees from blob transactions only
72
+ const blobFees = block.transactions
73
+ .map(tx => {
74
+ // Transaction can be just a hash string
75
+ if (typeof tx === 'string') {
76
+ return 0n;
77
+ }
78
+
79
+ if (!isBlobTransaction(tx)) {
80
+ return 0n;
81
+ }
82
+ return tx.maxPriorityFeePerGas || 0n;
83
+ })
84
+ .filter((fee: bigint) => fee > 0n);
85
+
86
+ if (blobFees.length === 0) {
87
+ // No blob transactions in this block - return zeros for each percentile
88
+ reward.push(rewardPercentiles.map(() => 0n));
89
+ continue;
90
+ }
91
+
92
+ // Calculate requested percentiles
93
+ const percentiles = rewardPercentiles.map(percentile => calculatePercentile(blobFees, percentile));
94
+
95
+ reward.push(percentiles);
96
+ }
97
+
98
+ // Calculate oldest block number (the last block in our array)
99
+ const oldestBlock = latestBlockNumber - BigInt(blockCount - 1);
100
+
101
+ // Reverse arrays to match getFeeHistory behavior (oldest first)
102
+ return {
103
+ baseFeePerGas: baseFeePerGas.reverse(),
104
+ gasUsedRatio: gasUsedRatio.reverse(),
105
+ oldestBlock,
106
+ reward: reward.reverse(),
107
+ };
108
+ }
109
+
110
+ /**
111
+ * Similar to our current competitive priority fee strategy, but only considers blob transactions
112
+ * when calculating competitive priority fee for blob transactions.
113
+ * This strategy also has NO cap on the competitive fee if it's much higher than the network estimate.
114
+ * Analyzes p75 of pending transactions and 5-block fee history to determine a competitive priority fee.
115
+ * Falls back to network estimate if data is unavailable.
116
+ */
117
+ export const P75BlobTxsOnlyPriorityFeeStrategy: PriorityFeeStrategy<P75AllTxsStrategyPromises> = {
118
+ name: 'Competitive (P75 + History) - Blob Txs Only',
119
+ id: 'p75_pending_txs_and_history_blob_txs_only',
120
+
121
+ getRequiredPromises(client: ViemClient, opts: PriorityFeeStrategyContext): P75AllTxsStrategyPromises {
122
+ return {
123
+ networkEstimate: client.estimateMaxPriorityFeePerGas().catch(() => 0n),
124
+ pendingBlock: client.getBlock({ blockTag: 'pending', includeTransactions: true }).catch(() => null),
125
+ feeHistory: opts.isBlobTx
126
+ ? getBlobPriorityFeeHistory(client, HISTORICAL_BLOCK_COUNT, [75])
127
+ : client
128
+ .getFeeHistory({
129
+ blockCount: HISTORICAL_BLOCK_COUNT,
130
+ rewardPercentiles: [75],
131
+ blockTag: 'latest',
132
+ })
133
+ .catch(() => null),
134
+ };
135
+ },
136
+
137
+ calculate(
138
+ results: {
139
+ [K in keyof P75AllTxsStrategyPromises]: PromiseSettledResult<Awaited<P75AllTxsStrategyPromises[K]>>;
140
+ },
141
+ context: PriorityFeeStrategyContext,
142
+ ): PriorityFeeStrategyResult {
143
+ const { logger } = context;
144
+
145
+ // Extract network estimate from settled result
146
+ const networkEstimate =
147
+ results.networkEstimate.status === 'fulfilled' && typeof results.networkEstimate.value === 'bigint'
148
+ ? results.networkEstimate.value
149
+ : 0n;
150
+
151
+ let competitiveFee = networkEstimate;
152
+ const debugInfo: Record<string, string | number> = {
153
+ networkEstimateGwei: formatGwei(networkEstimate),
154
+ };
155
+
156
+ // Extract pending block from settled result
157
+ const pendingBlock = results.pendingBlock.status === 'fulfilled' ? results.pendingBlock.value : null;
158
+
159
+ // Analyze pending block transactions
160
+ if (pendingBlock?.transactions && pendingBlock.transactions.length > 0) {
161
+ const pendingFees = pendingBlock.transactions
162
+ .map(tx => {
163
+ if (typeof tx === 'string') {
164
+ return 0n;
165
+ }
166
+ if (context.isBlobTx) {
167
+ if (!isBlobTransaction(tx)) {
168
+ return 0n;
169
+ }
170
+ }
171
+ const fee = tx.maxPriorityFeePerGas || 0n;
172
+
173
+ return fee;
174
+ })
175
+ .filter((fee: bigint) => fee > 0n);
176
+
177
+ if (pendingFees.length > 0) {
178
+ const pendingCompetitiveFee = calculatePercentile(pendingFees, 75);
179
+
180
+ if (pendingCompetitiveFee > competitiveFee) {
181
+ competitiveFee = pendingCompetitiveFee;
182
+ }
183
+
184
+ debugInfo.pendingTxCount = pendingFees.length;
185
+ debugInfo.pendingP75Gwei = formatGwei(pendingCompetitiveFee);
186
+
187
+ logger?.debug('Analyzed pending transactions for competitive pricing', {
188
+ pendingTxCount: pendingFees.length,
189
+ pendingP75: formatGwei(pendingCompetitiveFee),
190
+ });
191
+ }
192
+ }
193
+
194
+ // Extract fee history from settled result
195
+ const feeHistory = results.feeHistory.status === 'fulfilled' ? results.feeHistory.value : null;
196
+
197
+ // Analyze fee history
198
+ if (feeHistory?.reward && feeHistory.reward.length > 0) {
199
+ // Extract 75th percentile fees from each block
200
+ const percentile75Fees = feeHistory.reward.map(rewards => rewards[0] || 0n).filter(fee => fee > 0n);
201
+
202
+ if (percentile75Fees.length > 0) {
203
+ // Calculate median of the 75th percentile fees across blocks
204
+ const medianHistoricalFee = median(percentile75Fees) ?? 0n;
205
+
206
+ // Debug: Log suspicious fees from history
207
+ if (medianHistoricalFee > 100n * WEI_CONST) {
208
+ logger?.warn('Suspicious high fee in history', {
209
+ historicalMedian: formatGwei(medianHistoricalFee),
210
+ allP75Fees: percentile75Fees.map(f => formatGwei(f)),
211
+ });
212
+ }
213
+
214
+ if (medianHistoricalFee > competitiveFee) {
215
+ competitiveFee = medianHistoricalFee;
216
+ }
217
+
218
+ debugInfo.historicalMedianGwei = formatGwei(medianHistoricalFee);
219
+
220
+ logger?.debug('Analyzed fee history for competitive pricing', {
221
+ historicalMedian: formatGwei(medianHistoricalFee),
222
+ });
223
+ }
224
+ }
225
+
226
+ // Log final decision
227
+ if (competitiveFee > networkEstimate) {
228
+ logger?.debug('Using competitive fee from market analysis', {
229
+ networkEstimate: formatGwei(networkEstimate),
230
+ competitive: formatGwei(competitiveFee),
231
+ });
232
+ }
233
+
234
+ debugInfo.finalFeeGwei = formatGwei(competitiveFee);
235
+
236
+ return {
237
+ priorityFee: competitiveFee,
238
+ debugInfo,
239
+ };
240
+ },
241
+ };
@@ -0,0 +1,88 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+
3
+ import type { ViemClient } from '../../types.js';
4
+ import type { L1TxUtilsConfig } from '../config.js';
5
+
6
+ /**
7
+ * Historical block count for fee history queries
8
+ */
9
+ export const HISTORICAL_BLOCK_COUNT = 5;
10
+
11
+ /**
12
+ * Result from a priority fee strategy calculation
13
+ */
14
+ export interface PriorityFeeStrategyResult {
15
+ /** The calculated priority fee in wei */
16
+ priorityFee: bigint;
17
+ /** Optional debug info about how the fee was calculated */
18
+ debugInfo?: Record<string, string | number>;
19
+ }
20
+
21
+ /**
22
+ * Context passed to the strategy calculation function (excluding promise results)
23
+ */
24
+ export interface PriorityFeeStrategyContext {
25
+ /** Gas configuration */
26
+ gasConfig: L1TxUtilsConfig;
27
+ /** Whether this is for a blob transaction */
28
+ isBlobTx: boolean;
29
+ /** Logger for debugging */
30
+ logger?: Logger;
31
+ }
32
+
33
+ /**
34
+ * A strategy for calculating the priority fee for L1 transactions.
35
+ * Each strategy defines what promises it needs and how to calculate the fee from their results.
36
+ * This design allows strategies to be plugged into both L1FeeAnalyzer and ReadOnlyL1TxUtils.
37
+ */
38
+ export interface PriorityFeeStrategy<TPromises extends Record<string, Promise<any>> = Record<string, Promise<any>>> {
39
+ /** Human-readable name for logging */
40
+ name: string;
41
+ /** Unique identifier for metrics */
42
+ id: string;
43
+ /**
44
+ * Returns the promises that need to be executed for this strategy.
45
+ * These will be run in parallel with Promise.allSettled.
46
+ * @param client - The viem client to use for RPC calls
47
+ * @returns An object of promises keyed by name
48
+ */
49
+ getRequiredPromises(client: ViemClient, opts: Partial<PriorityFeeStrategyContext>): TPromises;
50
+ /**
51
+ * Calculate the priority fee based on the settled promise results.
52
+ * @param results - The settled results of the promises from getRequiredPromises
53
+ * @param context - Contains gas config, whether it's a blob tx, and logger
54
+ * @returns The calculated priority fee result
55
+ */
56
+ calculate(
57
+ results: { [K in keyof TPromises]: PromiseSettledResult<Awaited<TPromises[K]>> },
58
+ context: PriorityFeeStrategyContext,
59
+ ): PriorityFeeStrategyResult;
60
+ }
61
+
62
+ /**
63
+ * Helper function to execute a strategy's promises and calculate the result.
64
+ * This can be used by both L1FeeAnalyzer and ReadOnlyL1TxUtils.
65
+ * @param strategy - The strategy to execute
66
+ * @param client - The viem client to use for RPC calls
67
+ * @param context - The context for calculation
68
+ * @returns The strategy result
69
+ */
70
+ export async function executeStrategy<TPromises extends Record<string, Promise<any>>>(
71
+ strategy: PriorityFeeStrategy<TPromises>,
72
+ client: ViemClient,
73
+ context: PriorityFeeStrategyContext,
74
+ ): Promise<PriorityFeeStrategyResult> {
75
+ const promises = strategy.getRequiredPromises(client, { isBlobTx: context.isBlobTx });
76
+ const keys = Object.keys(promises) as Array<keyof TPromises>;
77
+ const promiseArray = keys.map(k => promises[k]);
78
+
79
+ const settledResults = await Promise.allSettled(promiseArray);
80
+
81
+ // Reconstruct the results object with the same keys, preserving the type mapping
82
+ const results = {} as { [K in keyof TPromises]: PromiseSettledResult<Awaited<TPromises[K]>> };
83
+ keys.forEach((key, index) => {
84
+ results[key] = settledResults[index] as PromiseSettledResult<Awaited<TPromises[typeof key]>>;
85
+ });
86
+
87
+ return strategy.calculate(results, context);
88
+ }
@@ -1,7 +1,9 @@
1
1
  export * from './config.js';
2
2
  export * from './constants.js';
3
3
  export * from './factory.js';
4
+ export * from './fee-strategies/index.js';
4
5
  export * from './interfaces.js';
6
+ export * from './l1_fee_analyzer.js';
5
7
  export * from './l1_tx_utils.js';
6
8
  export * from './readonly_l1_tx_utils.js';
7
9
  export * from './signer.js';