@aztec/ethereum 2.1.9-rc.3 → 2.1.9-rc.4

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 (40) hide show
  1. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  2. package/dest/deploy_l1_contracts.js +9 -8
  3. package/dest/l1_tx_utils/constants.d.ts +6 -0
  4. package/dest/l1_tx_utils/constants.d.ts.map +1 -1
  5. package/dest/l1_tx_utils/constants.js +25 -0
  6. package/dest/l1_tx_utils/fee-strategies/index.d.ts +10 -0
  7. package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
  8. package/dest/l1_tx_utils/fee-strategies/index.js +12 -0
  9. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +8 -0
  10. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
  11. package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +129 -0
  12. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +23 -0
  13. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
  14. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +191 -0
  15. package/dest/l1_tx_utils/fee-strategies/types.d.ts +51 -0
  16. package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
  17. package/dest/l1_tx_utils/fee-strategies/types.js +3 -0
  18. package/dest/l1_tx_utils/index.d.ts +2 -0
  19. package/dest/l1_tx_utils/index.d.ts.map +1 -1
  20. package/dest/l1_tx_utils/index.js +2 -0
  21. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +235 -0
  22. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
  23. package/dest/l1_tx_utils/l1_fee_analyzer.js +506 -0
  24. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +0 -13
  25. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -1
  26. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +30 -143
  27. package/dest/utils.d.ts +13 -1
  28. package/dest/utils.d.ts.map +1 -1
  29. package/dest/utils.js +18 -0
  30. package/package.json +6 -5
  31. package/src/deploy_l1_contracts.ts +7 -6
  32. package/src/l1_tx_utils/constants.ts +11 -0
  33. package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
  34. package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +163 -0
  35. package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +245 -0
  36. package/src/l1_tx_utils/fee-strategies/types.ts +56 -0
  37. package/src/l1_tx_utils/index.ts +2 -0
  38. package/src/l1_tx_utils/l1_fee_analyzer.ts +802 -0
  39. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +42 -186
  40. package/src/utils.ts +29 -0
@@ -1,4 +1,4 @@
1
- import { getKeys, median, merge, pick, times } from '@aztec/foundation/collection';
1
+ import { getKeys, merge, pick, times } from '@aztec/foundation/collection';
2
2
  import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { makeBackoff, retry } from '@aztec/foundation/retry';
4
4
  import { DateProvider } from '@aztec/foundation/timer';
@@ -30,10 +30,12 @@ import {
30
30
  MIN_REPLACEMENT_BUMP_PERCENTAGE,
31
31
  WEI_CONST,
32
32
  } from './constants.js';
33
+ import { P75AllTxsPriorityFeeStrategy, type PriorityFeeStrategy } from './fee-strategies/index.js';
33
34
  import type { GasPrice, L1BlobInputs, L1TxRequest, TransactionStats } from './types.js';
34
35
  import { getCalldataGasUsage, tryGetCustomErrorNameContractFunction } from './utils.js';
35
36
 
36
- const HISTORICAL_BLOCK_COUNT = 5;
37
+ // Change this to the current strategy we want to use
38
+ const CurrentStrategy: PriorityFeeStrategy = P75AllTxsPriorityFeeStrategy;
37
39
 
38
40
  export class ReadOnlyL1TxUtils {
39
41
  public config: Required<L1TxUtilsConfig>;
@@ -65,124 +67,6 @@ export class ReadOnlyL1TxUtils {
65
67
  return this.client.getBlockNumber();
66
68
  }
67
69
 
68
- /**
69
- * Analyzes pending transactions and recent fee history to determine a competitive priority fee.
70
- * Falls back to network estimate if data is unavailable or fails.
71
- * @param networkEstimateResult - Result from estimateMaxPriorityFeePerGas RPC call
72
- * @param pendingBlockResult - Result from getBlock with pending tag RPC call
73
- * @param feeHistoryResult - Result from getFeeHistory RPC call
74
- * @returns A competitive priority fee based on pending txs and recent block history
75
- */
76
- protected getCompetitivePriorityFee(
77
- networkEstimateResult: PromiseSettledResult<bigint | null>,
78
- pendingBlockResult: PromiseSettledResult<Awaited<ReturnType<ViemClient['getBlock']>> | null>,
79
- feeHistoryResult: PromiseSettledResult<Awaited<ReturnType<ViemClient['getFeeHistory']>> | null>,
80
- ): bigint {
81
- const networkEstimate =
82
- networkEstimateResult.status === 'fulfilled' && typeof networkEstimateResult.value === 'bigint'
83
- ? networkEstimateResult.value
84
- : 0n;
85
- let competitiveFee = networkEstimate;
86
-
87
- if (
88
- pendingBlockResult.status === 'fulfilled' &&
89
- pendingBlockResult.value !== null &&
90
- pendingBlockResult.value.transactions &&
91
- pendingBlockResult.value.transactions.length > 0
92
- ) {
93
- const pendingBlock = pendingBlockResult.value;
94
- // Extract priority fees from pending transactions
95
- const pendingFees = pendingBlock.transactions
96
- .map(tx => {
97
- // Transaction can be just a hash string, so we need to check if it's an object
98
- if (typeof tx === 'string') {
99
- return 0n;
100
- }
101
- const fee = tx.maxPriorityFeePerGas || 0n;
102
- // Debug: Log suspicious fees
103
- if (fee > 100n * WEI_CONST) {
104
- this.logger?.warn('Suspicious high priority fee in pending tx', {
105
- txHash: tx.hash,
106
- maxPriorityFeePerGas: formatGwei(fee),
107
- maxFeePerGas: formatGwei(tx.maxFeePerGas || 0n),
108
- maxFeePerBlobGas: tx.maxFeePerBlobGas ? formatGwei(tx.maxFeePerBlobGas) : 'N/A',
109
- });
110
- }
111
- return fee;
112
- })
113
- .filter((fee: bigint) => fee > 0n);
114
-
115
- if (pendingFees.length > 0) {
116
- // Use 75th percentile of pending fees to be competitive
117
- const sortedPendingFees = [...pendingFees].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
118
- const percentile75Index = Math.floor((sortedPendingFees.length - 1) * 0.75);
119
- const pendingCompetitiveFee = sortedPendingFees[percentile75Index];
120
-
121
- if (pendingCompetitiveFee > competitiveFee) {
122
- competitiveFee = pendingCompetitiveFee;
123
- }
124
-
125
- this.logger?.debug('Analyzed pending transactions for competitive pricing', {
126
- pendingTxCount: pendingFees.length,
127
- pendingP75: formatGwei(pendingCompetitiveFee),
128
- });
129
- }
130
- }
131
- if (
132
- feeHistoryResult.status === 'fulfilled' &&
133
- feeHistoryResult.value !== null &&
134
- feeHistoryResult.value.reward &&
135
- feeHistoryResult.value.reward.length > 0
136
- ) {
137
- const feeHistory = feeHistoryResult.value;
138
- // Extract 75th percentile fees from each block
139
- const percentile75Fees = feeHistory.reward!.map(rewards => rewards[0] || 0n).filter(fee => fee > 0n);
140
-
141
- if (percentile75Fees.length > 0) {
142
- // Calculate median of the 75th percentile fees across blocks
143
- const medianHistoricalFee = median(percentile75Fees) ?? 0n;
144
-
145
- // Debug: Log suspicious fees from history
146
- if (medianHistoricalFee > 100n * WEI_CONST) {
147
- this.logger?.warn('Suspicious high fee in history', {
148
- historicalMedian: formatGwei(medianHistoricalFee),
149
- allP75Fees: percentile75Fees.map(f => formatGwei(f)),
150
- });
151
- }
152
-
153
- if (medianHistoricalFee > competitiveFee) {
154
- competitiveFee = medianHistoricalFee;
155
- }
156
-
157
- this.logger?.debug('Analyzed fee history for competitive pricing', {
158
- historicalMedian: formatGwei(medianHistoricalFee),
159
- });
160
- }
161
- }
162
-
163
- // Sanity check: cap competitive fee at 100x network estimate to avoid using unrealistic fees
164
- // (e.g., Anvil returns inflated historical fees that don't reflect actual network conditions)
165
- const maxReasonableFee = networkEstimate * 100n;
166
- if (competitiveFee > maxReasonableFee) {
167
- this.logger?.warn('Competitive fee exceeds sanity cap, using capped value', {
168
- competitiveFee: formatGwei(competitiveFee),
169
- networkEstimate: formatGwei(networkEstimate),
170
- cappedTo: formatGwei(maxReasonableFee),
171
- });
172
- competitiveFee = maxReasonableFee;
173
- }
174
-
175
- // Log final decision
176
- if (competitiveFee > networkEstimate) {
177
- this.logger?.debug('Using competitive fee from market analysis', {
178
- networkEstimate: formatGwei(networkEstimate),
179
- competitive: formatGwei(competitiveFee),
180
- });
181
- }
182
-
183
- return competitiveFee;
184
- }
185
-
186
70
  /**
187
71
  * Gets the current gas price with bounds checking
188
72
  */
@@ -194,74 +78,47 @@ export class ReadOnlyL1TxUtils {
194
78
  ): Promise<GasPrice> {
195
79
  const gasConfig = merge(this.config, gasConfigOverrides);
196
80
 
197
- // Make all RPC calls in parallel upfront with retry logic
198
- const latestBlockPromise = this.tryTwice(
199
- () => this.client.getBlock({ blockTag: 'latest' }),
200
- 'Getting latest block',
201
- );
202
- const networkEstimatePromise = this.tryTwice(
203
- () => this.client.estimateMaxPriorityFeePerGas(),
204
- 'Estimating max priority fee per gas',
81
+ // Execute strategy - it handles all RPC calls internally and returns everything we need
82
+ const strategyResult = await retry(
83
+ () =>
84
+ CurrentStrategy.execute(this.client, {
85
+ gasConfig,
86
+ isBlobTx,
87
+ logger: this.logger,
88
+ }),
89
+ 'Executing priority fee strategy',
90
+ makeBackoff(times(2, () => 0)),
91
+ this.logger,
92
+ true,
205
93
  );
206
- const pendingBlockPromise = this.tryTwice(
207
- () => this.client.getBlock({ blockTag: 'pending', includeTransactions: true }),
208
- 'Getting pending block',
209
- );
210
- const feeHistoryPromise = this.tryTwice(
211
- () => this.client.getFeeHistory({ blockCount: HISTORICAL_BLOCK_COUNT, rewardPercentiles: [75] }),
212
- 'Getting fee history',
213
- );
214
- const blobBaseFeePromise = isBlobTx
215
- ? this.tryTwice(() => this.client.getBlobBaseFee(), 'Getting blob base fee')
216
- : null;
217
-
218
- const [latestBlockResult, networkEstimateResult, pendingBlockResult, feeHistoryResult, blobBaseFeeResult] =
219
- await Promise.allSettled([
220
- latestBlockPromise,
221
- networkEstimatePromise,
222
- pendingBlockPromise,
223
- feeHistoryPromise,
224
- blobBaseFeePromise ?? Promise.resolve(0n),
225
- ]);
226
-
227
- // Extract results
228
- const baseFee =
229
- latestBlockResult.status === 'fulfilled' &&
230
- typeof latestBlockResult.value === 'object' &&
231
- latestBlockResult.value.baseFeePerGas
232
- ? latestBlockResult.value.baseFeePerGas
233
- : 0n;
234
-
235
- // Get blob base fee if available
236
- let blobBaseFee = 0n;
237
- if (isBlobTx && blobBaseFeeResult.status === 'fulfilled' && typeof blobBaseFeeResult.value === 'bigint') {
238
- blobBaseFee = blobBaseFeeResult.value;
239
- } else if (isBlobTx) {
94
+
95
+ const { latestBlock, blobBaseFee, priorityFee: strategyPriorityFee } = strategyResult;
96
+
97
+ // Extract base fee from latest block
98
+ const baseFee = latestBlock.baseFeePerGas ?? 0n;
99
+
100
+ // Handle blob base fee
101
+ if (isBlobTx && blobBaseFee === undefined) {
240
102
  this.logger?.warn('Failed to get L1 blob base fee', attempt);
241
103
  }
242
104
 
243
- // Get competitive priority fee
244
- let priorityFee = this.getCompetitivePriorityFee(networkEstimateResult, pendingBlockResult, feeHistoryResult);
105
+ let priorityFee = strategyPriorityFee;
245
106
 
246
- // Apply minimum priority fee as a floor if configured
107
+ // Apply minimum priority fee floor if configured
247
108
  if (gasConfig.minimumPriorityFeePerGas) {
248
- const minimumFee = BigInt(Math.trunc(gasConfig.minimumPriorityFeePerGas * Number(WEI_CONST)));
249
- if (minimumFee > priorityFee) {
250
- this.logger?.debug('Using minimum priority fee as floor', {
251
- minimumPriorityFeePerGas: formatGwei(minimumFee),
252
- competitiveFee: formatGwei(priorityFee),
253
- });
254
- priorityFee = minimumFee;
255
- } else {
256
- this.logger?.debug('Competitive fee exceeds minimum, using competitive fee', {
257
- minimumPriorityFeePerGas: formatGwei(minimumFee),
258
- competitiveFee: formatGwei(priorityFee),
109
+ const minimumPriorityFee = BigInt(Math.trunc(gasConfig.minimumPriorityFeePerGas * Number(WEI_CONST)));
110
+ if (priorityFee < minimumPriorityFee) {
111
+ this.logger?.debug('Applying minimum priority fee floor', {
112
+ calculatedPriorityFee: formatGwei(priorityFee),
113
+ minimumPriorityFeePerGas: gasConfig.minimumPriorityFeePerGas,
114
+ appliedFee: formatGwei(minimumPriorityFee),
259
115
  });
116
+ priorityFee = minimumPriorityFee;
260
117
  }
261
118
  }
262
119
  let maxFeePerGas = baseFee;
263
120
 
264
- let maxFeePerBlobGas = blobBaseFee;
121
+ let maxFeePerBlobGas = blobBaseFee ?? 0n;
265
122
 
266
123
  // Bump base fee so it's valid for next blocks if it stalls
267
124
  const numBlocks = Math.ceil(gasConfig.stallTimeMs! / BLOCK_TIME_MS);
@@ -355,7 +212,7 @@ export class ReadOnlyL1TxUtils {
355
212
  baseFee: formatGwei(baseFee),
356
213
  maxFeePerGas: formatGwei(maxFeePerGas),
357
214
  maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
358
- blobBaseFee: formatGwei(blobBaseFee),
215
+ blobBaseFee: formatGwei(blobBaseFee ?? 0n),
359
216
  maxFeePerBlobGas: formatGwei(maxFeePerBlobGas),
360
217
  },
361
218
  );
@@ -387,11 +244,17 @@ export class ReadOnlyL1TxUtils {
387
244
  ..._blobInputs,
388
245
  maxFeePerBlobGas: gasPrice.maxFeePerBlobGas!,
389
246
  gas: LARGE_GAS_LIMIT,
247
+ blockTag: 'latest',
390
248
  });
391
249
 
392
250
  this.logger?.trace(`Estimated gas for blob tx: ${initialEstimate}`);
393
251
  } else {
394
- initialEstimate = await this.client.estimateGas({ account, ...request, gas: LARGE_GAS_LIMIT });
252
+ initialEstimate = await this.client.estimateGas({
253
+ account,
254
+ ...request,
255
+ gas: LARGE_GAS_LIMIT,
256
+ blockTag: 'latest',
257
+ });
395
258
  this.logger?.trace(`Estimated gas for non-blob tx: ${initialEstimate}`);
396
259
  }
397
260
 
@@ -547,11 +410,4 @@ export class ReadOnlyL1TxUtils {
547
410
  });
548
411
  return bumpedGasLimit;
549
412
  }
550
-
551
- /**
552
- * Helper function to retry RPC calls twice
553
- */
554
- private tryTwice<T>(fn: () => Promise<T>, description: string): Promise<T> {
555
- return retry<T>(fn, description, makeBackoff(times(2, () => 0)), this.logger, true);
556
- }
557
413
  }
package/src/utils.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
8
  type ContractEventName,
9
9
  ContractFunctionRevertedError,
10
10
  type DecodeEventLogReturnType,
11
+ type FormattedTransaction,
11
12
  type Hex,
12
13
  type Log,
13
14
  decodeErrorResult,
@@ -233,3 +234,31 @@ export function tryGetCustomErrorName(err: any) {
233
234
  return undefined;
234
235
  }
235
236
  }
237
+
238
+ /**
239
+ * Type guard to check if a transaction is a blob transaction (EIP-4844).
240
+ * Blob transactions have maxFeePerBlobGas and blobVersionedHashes fields.
241
+ */
242
+ export function isBlobTransaction(tx: FormattedTransaction): tx is FormattedTransaction & {
243
+ maxFeePerBlobGas: bigint;
244
+ blobVersionedHashes: readonly Hex[];
245
+ } {
246
+ return (
247
+ 'maxFeePerBlobGas' in tx &&
248
+ tx.maxFeePerBlobGas !== undefined &&
249
+ 'blobVersionedHashes' in tx &&
250
+ tx.blobVersionedHashes !== undefined
251
+ );
252
+ }
253
+
254
+ /**
255
+ * Calculates a percentile from an array of bigints
256
+ */
257
+ export function calculatePercentile(values: bigint[], percentile: number): bigint {
258
+ if (values.length === 0) {
259
+ return 0n;
260
+ }
261
+ const sorted = [...values].sort((a, b) => (a < b ? -1 : a > b ? 1 : 0));
262
+ const index = Math.ceil((sorted.length - 1) * (percentile / 100));
263
+ return sorted[index];
264
+ }