@aztec/ethereum 0.0.0-test.1 → 0.0.1-commit.b655e406

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 (229) hide show
  1. package/dest/account.d.ts +2 -0
  2. package/dest/account.d.ts.map +1 -0
  3. package/dest/account.js +4 -0
  4. package/dest/client.d.ts +5 -3
  5. package/dest/client.d.ts.map +1 -1
  6. package/dest/client.js +16 -2
  7. package/dest/config.d.ts +107 -16
  8. package/dest/config.d.ts.map +1 -1
  9. package/dest/config.js +456 -22
  10. package/dest/contracts/empire_base.d.ts +21 -6
  11. package/dest/contracts/empire_base.d.ts.map +1 -1
  12. package/dest/contracts/empire_base.js +75 -2
  13. package/dest/contracts/empire_slashing_proposer.d.ts +65 -0
  14. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -0
  15. package/dest/contracts/empire_slashing_proposer.js +194 -0
  16. package/dest/contracts/errors.d.ts +7 -0
  17. package/dest/contracts/errors.d.ts.map +1 -0
  18. package/dest/contracts/errors.js +12 -0
  19. package/dest/contracts/fee_asset_handler.d.ts +19 -0
  20. package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
  21. package/dest/contracts/fee_asset_handler.js +57 -0
  22. package/dest/contracts/fee_juice.d.ts +5 -6
  23. package/dest/contracts/fee_juice.d.ts.map +1 -1
  24. package/dest/contracts/fee_juice.js +27 -20
  25. package/dest/contracts/governance.d.ts +36 -25
  26. package/dest/contracts/governance.d.ts.map +1 -1
  27. package/dest/contracts/governance.js +87 -84
  28. package/dest/contracts/governance_proposer.d.ts +13 -11
  29. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  30. package/dest/contracts/governance_proposer.js +32 -18
  31. package/dest/contracts/gse.d.ts +32 -0
  32. package/dest/contracts/gse.d.ts.map +1 -0
  33. package/dest/contracts/gse.js +72 -0
  34. package/dest/contracts/inbox.d.ts +26 -0
  35. package/dest/contracts/inbox.d.ts.map +1 -0
  36. package/dest/contracts/inbox.js +45 -0
  37. package/dest/contracts/index.d.ts +8 -2
  38. package/dest/contracts/index.d.ts.map +1 -1
  39. package/dest/contracts/index.js +8 -2
  40. package/dest/contracts/multicall.d.ts +21 -0
  41. package/dest/contracts/multicall.d.ts.map +1 -0
  42. package/dest/contracts/multicall.js +156 -0
  43. package/dest/contracts/registry.d.ts +9 -4
  44. package/dest/contracts/registry.d.ts.map +1 -1
  45. package/dest/contracts/registry.js +44 -16
  46. package/dest/contracts/rollup.d.ts +202 -29
  47. package/dest/contracts/rollup.d.ts.map +1 -1
  48. package/dest/contracts/rollup.js +500 -55
  49. package/dest/contracts/slasher_contract.d.ts +44 -0
  50. package/dest/contracts/slasher_contract.d.ts.map +1 -0
  51. package/dest/contracts/slasher_contract.js +75 -0
  52. package/dest/contracts/tally_slashing_proposer.d.ts +138 -0
  53. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -0
  54. package/dest/contracts/tally_slashing_proposer.js +313 -0
  55. package/dest/contracts/utils.d.ts +3 -0
  56. package/dest/contracts/utils.d.ts.map +1 -0
  57. package/dest/contracts/utils.js +11 -0
  58. package/dest/deploy_l1_contracts.d.ts +128 -21112
  59. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  60. package/dest/deploy_l1_contracts.js +1204 -418
  61. package/dest/eth-signer/eth-signer.d.ts +21 -0
  62. package/dest/eth-signer/eth-signer.d.ts.map +1 -0
  63. package/dest/eth-signer/eth-signer.js +5 -0
  64. package/dest/eth-signer/index.d.ts +2 -0
  65. package/dest/eth-signer/index.d.ts.map +1 -0
  66. package/dest/eth-signer/index.js +1 -0
  67. package/dest/index.d.ts +6 -2
  68. package/dest/index.d.ts.map +1 -1
  69. package/dest/index.js +6 -2
  70. package/dest/l1_artifacts.d.ts +76184 -0
  71. package/dest/l1_artifacts.d.ts.map +1 -0
  72. package/dest/l1_artifacts.js +166 -0
  73. package/dest/l1_contract_addresses.d.ts +21 -1
  74. package/dest/l1_contract_addresses.d.ts.map +1 -1
  75. package/dest/l1_contract_addresses.js +22 -18
  76. package/dest/l1_reader.d.ts +1 -1
  77. package/dest/l1_reader.d.ts.map +1 -1
  78. package/dest/l1_reader.js +8 -8
  79. package/dest/l1_tx_utils/config.d.ts +59 -0
  80. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  81. package/dest/l1_tx_utils/config.js +73 -0
  82. package/dest/l1_tx_utils/constants.d.ts +6 -0
  83. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  84. package/dest/l1_tx_utils/constants.js +14 -0
  85. package/dest/l1_tx_utils/factory.d.ts +24 -0
  86. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  87. package/dest/l1_tx_utils/factory.js +12 -0
  88. package/dest/l1_tx_utils/index.d.ts +10 -0
  89. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  90. package/dest/l1_tx_utils/index.js +10 -0
  91. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  92. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  93. package/dest/l1_tx_utils/interfaces.js +4 -0
  94. package/dest/l1_tx_utils/l1_tx_utils.d.ts +95 -0
  95. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  96. package/dest/l1_tx_utils/l1_tx_utils.js +610 -0
  97. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts +26 -0
  98. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts.map +1 -0
  99. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.js +26 -0
  100. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +94 -0
  101. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  102. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +431 -0
  103. package/dest/l1_tx_utils/signer.d.ts +4 -0
  104. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  105. package/dest/l1_tx_utils/signer.js +16 -0
  106. package/dest/l1_tx_utils/types.d.ts +67 -0
  107. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  108. package/dest/l1_tx_utils/types.js +26 -0
  109. package/dest/l1_tx_utils/utils.d.ts +4 -0
  110. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  111. package/dest/l1_tx_utils/utils.js +14 -0
  112. package/dest/l1_types.d.ts +6 -0
  113. package/dest/l1_types.d.ts.map +1 -0
  114. package/dest/l1_types.js +1 -0
  115. package/dest/publisher_manager.d.ts +15 -0
  116. package/dest/publisher_manager.d.ts.map +1 -0
  117. package/dest/publisher_manager.js +88 -0
  118. package/dest/queries.d.ts +3 -1
  119. package/dest/queries.d.ts.map +1 -1
  120. package/dest/queries.js +51 -12
  121. package/dest/test/chain_monitor.d.ts +72 -0
  122. package/dest/test/chain_monitor.d.ts.map +1 -0
  123. package/dest/test/chain_monitor.js +216 -0
  124. package/dest/test/delayed_tx_utils.d.ts +7 -2
  125. package/dest/test/delayed_tx_utils.d.ts.map +1 -1
  126. package/dest/test/delayed_tx_utils.js +13 -6
  127. package/dest/{eth_cheat_codes.d.ts → test/eth_cheat_codes.d.ts} +87 -13
  128. package/dest/test/eth_cheat_codes.d.ts.map +1 -0
  129. package/dest/test/eth_cheat_codes.js +552 -0
  130. package/dest/test/eth_cheat_codes_with_state.d.ts +1 -1
  131. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -1
  132. package/dest/test/eth_cheat_codes_with_state.js +1 -1
  133. package/dest/test/index.d.ts +3 -0
  134. package/dest/test/index.d.ts.map +1 -1
  135. package/dest/test/index.js +3 -0
  136. package/dest/test/rollup_cheat_codes.d.ts +86 -0
  137. package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
  138. package/dest/test/rollup_cheat_codes.js +268 -0
  139. package/dest/test/start_anvil.d.ts +5 -0
  140. package/dest/test/start_anvil.d.ts.map +1 -1
  141. package/dest/test/start_anvil.js +15 -7
  142. package/dest/test/tx_delayer.d.ts +17 -6
  143. package/dest/test/tx_delayer.d.ts.map +1 -1
  144. package/dest/test/tx_delayer.js +95 -19
  145. package/dest/test/upgrade_utils.d.ts +5 -4
  146. package/dest/test/upgrade_utils.d.ts.map +1 -1
  147. package/dest/test/upgrade_utils.js +23 -16
  148. package/dest/types.d.ts +6 -7
  149. package/dest/types.d.ts.map +1 -1
  150. package/dest/types.js +3 -1
  151. package/dest/utils.d.ts +1 -0
  152. package/dest/utils.d.ts.map +1 -1
  153. package/dest/utils.js +43 -88
  154. package/dest/zkPassportVerifierAddress.d.ts +15 -0
  155. package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
  156. package/dest/zkPassportVerifierAddress.js +11 -0
  157. package/package.json +24 -16
  158. package/src/account.ts +5 -0
  159. package/src/client.ts +42 -4
  160. package/src/config.ts +584 -31
  161. package/src/contracts/empire_base.ts +75 -6
  162. package/src/contracts/empire_slashing_proposer.ts +259 -0
  163. package/src/contracts/errors.ts +13 -0
  164. package/src/contracts/fee_asset_handler.ts +63 -0
  165. package/src/contracts/fee_juice.ts +29 -15
  166. package/src/contracts/governance.ts +80 -77
  167. package/src/contracts/governance_proposer.ts +60 -24
  168. package/src/contracts/gse.ts +88 -0
  169. package/src/contracts/inbox.ts +63 -0
  170. package/src/contracts/index.ts +8 -2
  171. package/src/contracts/multicall.ts +155 -0
  172. package/src/contracts/registry.ts +51 -26
  173. package/src/contracts/rollup.ts +585 -56
  174. package/src/contracts/slasher_contract.ts +89 -0
  175. package/src/contracts/tally_slashing_proposer.ts +315 -0
  176. package/src/contracts/utils.ts +14 -0
  177. package/src/deploy_l1_contracts.ts +1467 -566
  178. package/src/eth-signer/eth-signer.ts +25 -0
  179. package/src/eth-signer/index.ts +1 -0
  180. package/src/index.ts +6 -2
  181. package/src/l1_artifacts.ts +254 -0
  182. package/src/l1_contract_addresses.ts +32 -19
  183. package/src/l1_reader.ts +9 -9
  184. package/src/l1_tx_utils/README.md +177 -0
  185. package/src/l1_tx_utils/config.ts +140 -0
  186. package/src/l1_tx_utils/constants.ts +18 -0
  187. package/src/l1_tx_utils/factory.ts +64 -0
  188. package/src/l1_tx_utils/index.ts +12 -0
  189. package/src/l1_tx_utils/interfaces.ts +86 -0
  190. package/src/l1_tx_utils/l1_tx_utils.ts +718 -0
  191. package/src/l1_tx_utils/l1_tx_utils_with_blobs.ts +77 -0
  192. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +559 -0
  193. package/src/l1_tx_utils/signer.ts +28 -0
  194. package/src/l1_tx_utils/types.ts +85 -0
  195. package/src/l1_tx_utils/utils.ts +16 -0
  196. package/src/l1_types.ts +6 -0
  197. package/src/publisher_manager.ts +106 -0
  198. package/src/queries.ts +70 -15
  199. package/src/test/chain_monitor.ts +243 -0
  200. package/src/test/delayed_tx_utils.ts +34 -6
  201. package/src/test/eth_cheat_codes.ts +582 -0
  202. package/src/test/eth_cheat_codes_with_state.ts +1 -1
  203. package/src/test/index.ts +3 -0
  204. package/src/test/rollup_cheat_codes.ts +310 -0
  205. package/src/test/start_anvil.ts +20 -5
  206. package/src/test/tx_delayer.ts +127 -26
  207. package/src/test/upgrade_utils.ts +30 -21
  208. package/src/types.ts +10 -8
  209. package/src/utils.ts +49 -90
  210. package/src/zkPassportVerifierAddress.ts +15 -0
  211. package/dest/contracts/forwarder.d.ts +0 -24
  212. package/dest/contracts/forwarder.d.ts.map +0 -1
  213. package/dest/contracts/forwarder.js +0 -101
  214. package/dest/contracts/slashing_proposer.d.ts +0 -21
  215. package/dest/contracts/slashing_proposer.d.ts.map +0 -1
  216. package/dest/contracts/slashing_proposer.js +0 -47
  217. package/dest/eth_cheat_codes.d.ts.map +0 -1
  218. package/dest/eth_cheat_codes.js +0 -303
  219. package/dest/l1_tx_utils.d.ts +0 -192
  220. package/dest/l1_tx_utils.d.ts.map +0 -1
  221. package/dest/l1_tx_utils.js +0 -641
  222. package/dest/l1_tx_utils_with_blobs.d.ts +0 -12
  223. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  224. package/dest/l1_tx_utils_with_blobs.js +0 -64
  225. package/src/contracts/forwarder.ts +0 -132
  226. package/src/contracts/slashing_proposer.ts +0 -51
  227. package/src/eth_cheat_codes.ts +0 -314
  228. package/src/l1_tx_utils.ts +0 -847
  229. package/src/l1_tx_utils_with_blobs.ts +0 -86
@@ -0,0 +1,77 @@
1
+ import { Blob } from '@aztec/blob-lib';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import type { Logger } from '@aztec/foundation/log';
4
+ import { DateProvider } from '@aztec/foundation/timer';
5
+
6
+ import type { TransactionSerializable } from 'viem';
7
+
8
+ import type { EthSigner } from '../eth-signer/eth-signer.js';
9
+ import type { ExtendedViemWalletClient, ViemClient } from '../types.js';
10
+ import type { L1TxUtilsConfig } from './config.js';
11
+ import type { IL1TxMetrics, IL1TxStore } from './interfaces.js';
12
+ import { L1TxUtils } from './l1_tx_utils.js';
13
+ import { createViemSigner } from './signer.js';
14
+ import type { L1BlobInputs, SigningCallback } from './types.js';
15
+
16
+ /** Extends L1TxUtils with the capability to cancel blobs. This needs to be a separate class so we don't require a dependency on blob-lib unnecessarily. */
17
+ export class L1TxUtilsWithBlobs extends L1TxUtils {
18
+ /** Makes empty blob inputs for the cancellation tx. */
19
+ protected override makeEmptyBlobInputs(maxFeePerBlobGas: bigint): Required<L1BlobInputs> {
20
+ const blobData = new Uint8Array(131072).fill(0);
21
+ const kzg = Blob.getViemKzgInstance();
22
+ return { blobs: [blobData], kzg, maxFeePerBlobGas };
23
+ }
24
+ }
25
+
26
+ export function createL1TxUtilsWithBlobsFromViemWallet(
27
+ client: ExtendedViemWalletClient,
28
+ deps: {
29
+ logger?: Logger;
30
+ dateProvider?: DateProvider;
31
+ store?: IL1TxStore;
32
+ metrics?: IL1TxMetrics;
33
+ } = {},
34
+ config: Partial<L1TxUtilsConfig> = {},
35
+ debugMaxGasLimit: boolean = false,
36
+ ) {
37
+ return new L1TxUtilsWithBlobs(
38
+ client,
39
+ EthAddress.fromString(client.account.address),
40
+ createViemSigner(client),
41
+ deps.logger,
42
+ deps.dateProvider,
43
+ config,
44
+ debugMaxGasLimit,
45
+ deps.store,
46
+ deps.metrics,
47
+ );
48
+ }
49
+
50
+ export function createL1TxUtilsWithBlobsFromEthSigner(
51
+ client: ViemClient,
52
+ signer: EthSigner,
53
+ deps: {
54
+ logger?: Logger;
55
+ dateProvider?: DateProvider;
56
+ store?: IL1TxStore;
57
+ metrics?: IL1TxMetrics;
58
+ } = {},
59
+ config: Partial<L1TxUtilsConfig> = {},
60
+ debugMaxGasLimit: boolean = false,
61
+ ) {
62
+ const callback: SigningCallback = async (transaction: TransactionSerializable, _signingAddress) => {
63
+ return (await signer.signTransaction(transaction)).toViemTransactionSignature();
64
+ };
65
+
66
+ return new L1TxUtilsWithBlobs(
67
+ client,
68
+ signer.address,
69
+ callback,
70
+ deps.logger,
71
+ deps.dateProvider,
72
+ config,
73
+ debugMaxGasLimit,
74
+ deps.store,
75
+ deps.metrics,
76
+ );
77
+ }
@@ -0,0 +1,559 @@
1
+ import { getKeys, median, merge, pick, times } from '@aztec/foundation/collection';
2
+ import { type Logger, createLogger } from '@aztec/foundation/log';
3
+ import { makeBackoff, retry } from '@aztec/foundation/retry';
4
+ import { DateProvider } from '@aztec/foundation/timer';
5
+ import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
6
+
7
+ import pickBy from 'lodash.pickby';
8
+ import {
9
+ type Abi,
10
+ type Account,
11
+ type BaseError,
12
+ type BlockOverrides,
13
+ type ContractFunctionExecutionError,
14
+ type Hex,
15
+ MethodNotFoundRpcError,
16
+ MethodNotSupportedRpcError,
17
+ type StateOverride,
18
+ decodeErrorResult,
19
+ formatGwei,
20
+ getContractError,
21
+ hexToBytes,
22
+ } from 'viem';
23
+
24
+ import type { ViemClient } from '../types.js';
25
+ import { type L1TxUtilsConfig, defaultL1TxUtilsConfig, l1TxUtilsConfigMappings } from './config.js';
26
+ import {
27
+ BLOCK_TIME_MS,
28
+ LARGE_GAS_LIMIT,
29
+ MIN_BLOB_REPLACEMENT_BUMP_PERCENTAGE,
30
+ MIN_REPLACEMENT_BUMP_PERCENTAGE,
31
+ WEI_CONST,
32
+ } from './constants.js';
33
+ import type { GasPrice, L1BlobInputs, L1TxRequest, TransactionStats } from './types.js';
34
+ import { getCalldataGasUsage, tryGetCustomErrorNameContractFunction } from './utils.js';
35
+
36
+ const HISTORICAL_BLOCK_COUNT = 5;
37
+
38
+ export class ReadOnlyL1TxUtils {
39
+ public config: Required<L1TxUtilsConfig>;
40
+ protected interrupted = false;
41
+
42
+ constructor(
43
+ public client: ViemClient,
44
+ protected logger: Logger = createLogger('ethereum:readonly-l1-utils'),
45
+ public readonly dateProvider: DateProvider,
46
+ config?: Partial<L1TxUtilsConfig>,
47
+ protected debugMaxGasLimit: boolean = false,
48
+ ) {
49
+ this.config = merge(defaultL1TxUtilsConfig, pick(config || {}, ...getKeys(l1TxUtilsConfigMappings)));
50
+ }
51
+
52
+ public interrupt() {
53
+ this.interrupted = true;
54
+ }
55
+
56
+ public restart() {
57
+ this.interrupted = false;
58
+ }
59
+
60
+ public getBlock() {
61
+ return this.client.getBlock();
62
+ }
63
+
64
+ public getBlockNumber() {
65
+ return this.client.getBlockNumber();
66
+ }
67
+
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
+ /**
187
+ * Gets the current gas price with bounds checking
188
+ */
189
+ public async getGasPrice(
190
+ gasConfigOverrides?: L1TxUtilsConfig,
191
+ isBlobTx: boolean = false,
192
+ attempt: number = 0,
193
+ previousGasPrice?: typeof attempt extends 0 ? never : GasPrice,
194
+ ): Promise<GasPrice> {
195
+ const gasConfig = merge(this.config, gasConfigOverrides);
196
+
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 = gasConfig.fixedPriorityFeePerGas
203
+ ? null
204
+ : this.tryTwice(() => this.client.estimateMaxPriorityFeePerGas(), 'Estimating max priority fee per gas');
205
+ const pendingBlockPromise = gasConfig.fixedPriorityFeePerGas
206
+ ? null
207
+ : this.tryTwice(
208
+ () => this.client.getBlock({ blockTag: 'pending', includeTransactions: true }),
209
+ 'Getting pending block',
210
+ );
211
+ const feeHistoryPromise = gasConfig.fixedPriorityFeePerGas
212
+ ? null
213
+ : this.tryTwice(
214
+ () => this.client.getFeeHistory({ blockCount: HISTORICAL_BLOCK_COUNT, rewardPercentiles: [75] }),
215
+ 'Getting fee history',
216
+ );
217
+ const blobBaseFeePromise = isBlobTx
218
+ ? this.tryTwice(() => this.client.getBlobBaseFee(), 'Getting blob base fee')
219
+ : null;
220
+
221
+ const [latestBlockResult, networkEstimateResult, pendingBlockResult, feeHistoryResult, blobBaseFeeResult] =
222
+ await Promise.allSettled([
223
+ latestBlockPromise,
224
+ networkEstimatePromise ?? Promise.resolve(0n),
225
+ pendingBlockPromise ?? Promise.resolve(null),
226
+ feeHistoryPromise ?? Promise.resolve(null),
227
+ blobBaseFeePromise ?? Promise.resolve(0n),
228
+ ]);
229
+
230
+ // Extract results
231
+ const baseFee =
232
+ latestBlockResult.status === 'fulfilled' &&
233
+ typeof latestBlockResult.value === 'object' &&
234
+ latestBlockResult.value.baseFeePerGas
235
+ ? latestBlockResult.value.baseFeePerGas
236
+ : 0n;
237
+
238
+ // Get blob base fee if available
239
+ let blobBaseFee = 0n;
240
+ if (isBlobTx && blobBaseFeeResult.status === 'fulfilled' && typeof blobBaseFeeResult.value === 'bigint') {
241
+ blobBaseFee = blobBaseFeeResult.value;
242
+ } else if (isBlobTx) {
243
+ this.logger?.warn('Failed to get L1 blob base fee', attempt);
244
+ }
245
+
246
+ let priorityFee: bigint;
247
+ if (gasConfig.fixedPriorityFeePerGas) {
248
+ this.logger?.debug('Using fixed priority fee per L1 gas', {
249
+ fixedPriorityFeePerGas: gasConfig.fixedPriorityFeePerGas,
250
+ });
251
+ // try to maintain precision up to 1000000 wei
252
+ priorityFee = BigInt(gasConfig.fixedPriorityFeePerGas * 1_000_000) * (WEI_CONST / 1_000_000n);
253
+ } else {
254
+ // Get competitive priority fee (includes network estimate + analysis)
255
+ priorityFee = this.getCompetitivePriorityFee(networkEstimateResult, pendingBlockResult, feeHistoryResult);
256
+ }
257
+ let maxFeePerGas = baseFee;
258
+
259
+ let maxFeePerBlobGas = blobBaseFee;
260
+
261
+ // Bump base fee so it's valid for next blocks if it stalls
262
+ const numBlocks = Math.ceil(gasConfig.stallTimeMs! / BLOCK_TIME_MS);
263
+ for (let i = 0; i < numBlocks; i++) {
264
+ // each block can go up 12.5% from previous baseFee
265
+ maxFeePerGas = (maxFeePerGas * (1_000n + 125n)) / 1_000n;
266
+ // same for blob gas fee
267
+ maxFeePerBlobGas = (maxFeePerBlobGas * (1_000n + 125n)) / 1_000n;
268
+ }
269
+
270
+ if (attempt > 0) {
271
+ const configBump =
272
+ gasConfig.priorityFeeRetryBumpPercentage ?? defaultL1TxUtilsConfig.priorityFeeRetryBumpPercentage!;
273
+
274
+ // if this is a blob tx, we have to use the blob bump percentage
275
+ const minBumpPercentage = isBlobTx ? MIN_BLOB_REPLACEMENT_BUMP_PERCENTAGE : MIN_REPLACEMENT_BUMP_PERCENTAGE;
276
+
277
+ const bumpPercentage = configBump > minBumpPercentage ? configBump : minBumpPercentage;
278
+ // Calculate minimum required fees based on previous attempt
279
+ // multiply by 100 & divide by 100 to maintain some precision
280
+ const minPriorityFee =
281
+ (previousGasPrice!.maxPriorityFeePerGas * (100_00n + BigInt(bumpPercentage * 1_00))) / 100_00n;
282
+ const minMaxFee = (previousGasPrice!.maxFeePerGas * (100_00n + BigInt(bumpPercentage * 1_00))) / 100_00n;
283
+
284
+ let competitivePriorityFee = priorityFee;
285
+ if (!gasConfig.fixedPriorityFeePerGas) {
286
+ // Apply bump percentage to competitive fee
287
+ competitivePriorityFee = (priorityFee * (100_00n + BigInt(configBump * 1_00))) / 100_00n;
288
+
289
+ this.logger?.debug(`Speed-up attempt ${attempt}: using competitive fee strategy`, {
290
+ networkEstimate: formatGwei(priorityFee),
291
+ competitiveFee: formatGwei(competitivePriorityFee),
292
+ minRequired: formatGwei(minPriorityFee),
293
+ bumpPercentage: configBump,
294
+ });
295
+ }
296
+
297
+ // Use maximum between competitive fee and minimum required bump
298
+ const finalPriorityFee = competitivePriorityFee > minPriorityFee ? competitivePriorityFee : minPriorityFee;
299
+ const feeSource = finalPriorityFee === competitivePriorityFee ? 'competitive' : 'minimum-bump';
300
+
301
+ priorityFee = finalPriorityFee;
302
+ // Add the final priority fee to maxFeePerGas
303
+ maxFeePerGas += finalPriorityFee;
304
+ maxFeePerGas = maxFeePerGas > minMaxFee ? maxFeePerGas : minMaxFee;
305
+
306
+ if (!gasConfig.fixedPriorityFeePerGas) {
307
+ this.logger?.debug(`Speed-up fee decision: using ${feeSource} fee`, {
308
+ finalPriorityFee: formatGwei(finalPriorityFee),
309
+ });
310
+ }
311
+ } else {
312
+ // First attempt: apply configured bump percentage to competitive fee
313
+ // multiply by 100 & divide by 100 to maintain some precision
314
+ if (!gasConfig.fixedPriorityFeePerGas) {
315
+ priorityFee = (priorityFee * (100_00n + BigInt((gasConfig.priorityFeeBumpPercentage || 0) * 1_00))) / 100_00n;
316
+ this.logger?.debug('Initial transaction: using competitive fee from market analysis', {
317
+ networkEstimate: formatGwei(priorityFee),
318
+ });
319
+ }
320
+ maxFeePerGas += priorityFee;
321
+ }
322
+
323
+ // maxGwei and maxBlobGwei are hard limits
324
+ const effectiveMaxGwei = gasConfig.maxGwei! * WEI_CONST;
325
+ const effectiveMaxBlobGwei = gasConfig.maxBlobGwei! * WEI_CONST;
326
+
327
+ // Ensure we don't exceed maxGwei
328
+ if (effectiveMaxGwei > 0n) {
329
+ maxFeePerGas = maxFeePerGas > effectiveMaxGwei ? effectiveMaxGwei : maxFeePerGas;
330
+ }
331
+
332
+ // Ensure we don't exceed maxBlobGwei
333
+ if (maxFeePerBlobGas && effectiveMaxBlobGwei > 0n) {
334
+ maxFeePerBlobGas = maxFeePerBlobGas > effectiveMaxBlobGwei ? effectiveMaxBlobGwei : maxFeePerBlobGas;
335
+ }
336
+
337
+ // Ensure priority fee doesn't exceed max fee
338
+ const maxPriorityFeePerGas = priorityFee > maxFeePerGas ? maxFeePerGas : priorityFee;
339
+
340
+ if (attempt > 0 && previousGasPrice?.maxFeePerBlobGas) {
341
+ const bumpPercentage =
342
+ gasConfig.priorityFeeRetryBumpPercentage! > MIN_BLOB_REPLACEMENT_BUMP_PERCENTAGE
343
+ ? gasConfig.priorityFeeRetryBumpPercentage!
344
+ : MIN_BLOB_REPLACEMENT_BUMP_PERCENTAGE;
345
+
346
+ // calculate min blob fee based on previous attempt
347
+ const minBlobFee = (previousGasPrice.maxFeePerBlobGas * (100_00n + BigInt(bumpPercentage * 1_00))) / 100_00n;
348
+
349
+ // use max between current network values and min required values
350
+ maxFeePerBlobGas = maxFeePerBlobGas > minBlobFee ? maxFeePerBlobGas : minBlobFee;
351
+ }
352
+
353
+ this.logger?.trace(
354
+ `Computed L1 gas price max fee ${formatGwei(maxFeePerGas)} and max priority fee ${formatGwei(maxPriorityFeePerGas)}`,
355
+ {
356
+ attempt,
357
+ baseFee: formatGwei(baseFee),
358
+ maxFeePerGas: formatGwei(maxFeePerGas),
359
+ maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
360
+ blobBaseFee: formatGwei(blobBaseFee),
361
+ maxFeePerBlobGas: formatGwei(maxFeePerBlobGas),
362
+ },
363
+ );
364
+
365
+ return {
366
+ maxFeePerGas,
367
+ maxPriorityFeePerGas,
368
+ ...(maxFeePerBlobGas && { maxFeePerBlobGas: maxFeePerBlobGas }),
369
+ };
370
+ }
371
+
372
+ /**
373
+ * Estimates gas and adds buffer
374
+ */
375
+ public async estimateGas(
376
+ account: Account | Hex,
377
+ request: L1TxRequest,
378
+ _gasConfig?: L1TxUtilsConfig,
379
+ _blobInputs?: L1BlobInputs,
380
+ ): Promise<bigint> {
381
+ const gasConfig = { ...this.config, ..._gasConfig };
382
+ let initialEstimate = 0n;
383
+ if (_blobInputs) {
384
+ // @note requests with blobs also require maxFeePerBlobGas to be set
385
+ const gasPrice = await this.getGasPrice(gasConfig, true, 0);
386
+ initialEstimate = await this.client.estimateGas({
387
+ account,
388
+ ...request,
389
+ ..._blobInputs,
390
+ maxFeePerBlobGas: gasPrice.maxFeePerBlobGas!,
391
+ gas: LARGE_GAS_LIMIT,
392
+ });
393
+
394
+ this.logger?.trace(`Estimated gas for blob tx: ${initialEstimate}`);
395
+ } else {
396
+ initialEstimate = await this.client.estimateGas({ account, ...request, gas: LARGE_GAS_LIMIT });
397
+ this.logger?.trace(`Estimated gas for non-blob tx: ${initialEstimate}`);
398
+ }
399
+
400
+ // Add buffer based on either fixed amount or percentage
401
+ const withBuffer = this.bumpGasLimit(initialEstimate, gasConfig);
402
+
403
+ return withBuffer;
404
+ }
405
+
406
+ async getTransactionStats(txHash: string): Promise<TransactionStats | undefined> {
407
+ const tx = await this.client.getTransaction({ hash: txHash as Hex });
408
+ if (!tx) {
409
+ return undefined;
410
+ }
411
+ const calldata = hexToBytes(tx.input);
412
+ return {
413
+ sender: tx.from.toString(),
414
+ transactionHash: tx.hash,
415
+ calldataSize: calldata.length,
416
+ calldataGas: getCalldataGasUsage(calldata),
417
+ };
418
+ }
419
+
420
+ public async tryGetErrorFromRevertedTx(
421
+ data: Hex,
422
+ args: {
423
+ args: readonly any[];
424
+ functionName: string;
425
+ abi: Abi;
426
+ address: Hex;
427
+ },
428
+ blobInputs: (L1BlobInputs & { maxFeePerBlobGas: bigint }) | undefined,
429
+ stateOverride: StateOverride = [],
430
+ ) {
431
+ try {
432
+ await this.client.simulateContract({
433
+ ...args,
434
+ account: this.client.account,
435
+ stateOverride,
436
+ });
437
+ this.logger?.trace('Simulated blob tx', { blobInputs });
438
+ // If the above passes, we have a blob error. We cannot simulate blob txs, and failed txs no longer throw errors.
439
+ // Strangely, the only way to throw the revert reason as an error and provide blobs is prepareTransactionRequest.
440
+ // See: https://github.com/wevm/viem/issues/2075
441
+ // This throws a EstimateGasExecutionError with the custom error information:
442
+ const request = blobInputs
443
+ ? {
444
+ account: this.client.account,
445
+ to: args.address,
446
+ data,
447
+ blobs: blobInputs.blobs,
448
+ kzg: blobInputs.kzg,
449
+ maxFeePerBlobGas: blobInputs.maxFeePerBlobGas,
450
+ }
451
+ : {
452
+ account: this.client.account,
453
+ to: args.address,
454
+ data,
455
+ };
456
+ this.logger?.trace('Preparing tx', { request });
457
+ await this.client.prepareTransactionRequest(request);
458
+ this.logger?.trace('Prepared tx');
459
+ return undefined;
460
+ } catch (simulationErr: any) {
461
+ // If we don't have a ContractFunctionExecutionError, we have a blob related error => use getContractError to get the error msg.
462
+ const contractErr =
463
+ simulationErr.name === 'ContractFunctionExecutionError'
464
+ ? simulationErr
465
+ : getContractError(simulationErr as BaseError, {
466
+ args: [],
467
+ abi: args.abi,
468
+ functionName: args.functionName,
469
+ address: args.address,
470
+ });
471
+ if (contractErr.name === 'ContractFunctionExecutionError') {
472
+ const execErr = contractErr as ContractFunctionExecutionError;
473
+ return tryGetCustomErrorNameContractFunction(execErr);
474
+ }
475
+ this.logger?.error(`Error getting error from simulation`, simulationErr);
476
+ }
477
+ }
478
+
479
+ public async simulate(
480
+ request: L1TxRequest & { gas?: bigint; from?: Hex },
481
+ blockOverrides: BlockOverrides<bigint, number> = {},
482
+ stateOverrides: StateOverride = [],
483
+ abi: Abi = RollupAbi,
484
+ _gasConfig?: L1TxUtilsConfig & { fallbackGasEstimate?: bigint },
485
+ ): Promise<{ gasUsed: bigint; result: `0x${string}` }> {
486
+ const gasConfig = { ...this.config, ..._gasConfig };
487
+
488
+ const call: any = {
489
+ to: request.to!,
490
+ data: request.data,
491
+ ...(request.from && { from: request.from }),
492
+ };
493
+
494
+ return await this._simulate(call, blockOverrides, stateOverrides, gasConfig, abi);
495
+ }
496
+
497
+ protected async _simulate(
498
+ call: any,
499
+ blockOverrides: BlockOverrides<bigint, number> = {},
500
+ stateOverrides: StateOverride = [],
501
+ gasConfig: L1TxUtilsConfig & { fallbackGasEstimate?: bigint },
502
+ abi: Abi,
503
+ ) {
504
+ try {
505
+ const result = await this.client.simulateBlocks({
506
+ validation: false,
507
+ blocks: [
508
+ {
509
+ blockOverrides,
510
+ stateOverrides,
511
+ calls: [call],
512
+ },
513
+ ],
514
+ });
515
+
516
+ if (result[0].calls[0].status === 'failure') {
517
+ this.logger?.error('L1 transaction simulation failed', result[0].calls[0].error);
518
+ const decodedError = decodeErrorResult({ abi, data: result[0].calls[0].data });
519
+
520
+ throw new Error(
521
+ `L1 transaction simulation failed with error ${decodedError.errorName}(${decodedError.args?.join(',')})`,
522
+ );
523
+ }
524
+ this.logger?.debug(`L1 transaction simulation succeeded`, { ...result[0].calls[0] });
525
+ return { gasUsed: result[0].gasUsed, result: result[0].calls[0].data as `0x${string}` };
526
+ } catch (err) {
527
+ if (err instanceof MethodNotFoundRpcError || err instanceof MethodNotSupportedRpcError) {
528
+ if (gasConfig.fallbackGasEstimate) {
529
+ this.logger?.warn(
530
+ `Node does not support eth_simulateV1 API. Using fallback gas estimate: ${gasConfig.fallbackGasEstimate}`,
531
+ );
532
+ return { gasUsed: gasConfig.fallbackGasEstimate, result: '0x' as `0x${string}` };
533
+ }
534
+ this.logger?.error('Node does not support eth_simulateV1 API');
535
+ }
536
+ throw err;
537
+ }
538
+ }
539
+
540
+ public bumpGasLimit(gasLimit: bigint, _gasConfig?: L1TxUtilsConfig): bigint {
541
+ const gasConfig = { ...this.config, ..._gasConfig };
542
+ const bumpedGasLimit = gasLimit + (gasLimit * BigInt((gasConfig?.gasLimitBufferPercentage || 0) * 1_00)) / 100_00n;
543
+
544
+ const cleanGasConfig = pickBy(gasConfig, (_, key) => key in l1TxUtilsConfigMappings);
545
+ this.logger?.trace(`Bumping gas limit from ${gasLimit} to ${bumpedGasLimit}`, {
546
+ gasLimit,
547
+ gasConfig: cleanGasConfig,
548
+ bumpedGasLimit,
549
+ });
550
+ return bumpedGasLimit;
551
+ }
552
+
553
+ /**
554
+ * Helper function to retry RPC calls twice
555
+ */
556
+ private tryTwice<T>(fn: () => Promise<T>, description: string): Promise<T> {
557
+ return retry<T>(fn, description, makeBackoff(times(2, () => 0)), this.logger, true);
558
+ }
559
+ }
@@ -0,0 +1,28 @@
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import type { ViemTransactionSignature } from '@aztec/foundation/eth-signature';
3
+
4
+ import { type TransactionSerializable, type WalletClient, parseTransaction } from 'viem';
5
+
6
+ import type { SigningCallback } from './types.js';
7
+
8
+ export function createViemSigner(client: WalletClient) {
9
+ const signer: SigningCallback = async (
10
+ tx: TransactionSerializable,
11
+ _address: EthAddress,
12
+ ): Promise<ViemTransactionSignature> => {
13
+ const signedTx = await client.signTransaction(tx as any);
14
+
15
+ const parsed = parseTransaction(signedTx);
16
+
17
+ if (!parsed.r || !parsed.s || (parsed.yParity !== 0 && parsed.yParity !== 1)) {
18
+ throw new Error('Failed to extract signature from viem signed transaction');
19
+ }
20
+
21
+ return {
22
+ r: parsed.r,
23
+ s: parsed.s,
24
+ yParity: parsed.yParity,
25
+ };
26
+ };
27
+ return signer;
28
+ }