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

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 (285) 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/chain.d.ts +1 -1
  5. package/dest/client.d.ts +28 -4
  6. package/dest/client.d.ts.map +1 -1
  7. package/dest/client.js +73 -3
  8. package/dest/config.d.ts +70 -23
  9. package/dest/config.d.ts.map +1 -1
  10. package/dest/config.js +151 -34
  11. package/dest/constants.d.ts +1 -1
  12. package/dest/contracts/chain_state_override.d.ts +78 -0
  13. package/dest/contracts/chain_state_override.d.ts.map +1 -0
  14. package/dest/contracts/chain_state_override.js +137 -0
  15. package/dest/contracts/empire_base.d.ts +25 -8
  16. package/dest/contracts/empire_base.d.ts.map +1 -1
  17. package/dest/contracts/empire_base.js +75 -2
  18. package/dest/contracts/errors.d.ts +7 -0
  19. package/dest/contracts/errors.d.ts.map +1 -0
  20. package/dest/contracts/errors.js +12 -0
  21. package/dest/contracts/fee_asset_handler.d.ts +20 -0
  22. package/dest/contracts/fee_asset_handler.d.ts.map +1 -0
  23. package/dest/contracts/fee_asset_handler.js +59 -0
  24. package/dest/contracts/fee_asset_price_oracle.d.ts +105 -0
  25. package/dest/contracts/fee_asset_price_oracle.d.ts.map +1 -0
  26. package/dest/contracts/fee_asset_price_oracle.js +656 -0
  27. package/dest/contracts/fee_juice.d.ts +6 -7
  28. package/dest/contracts/fee_juice.d.ts.map +1 -1
  29. package/dest/contracts/fee_juice.js +27 -20
  30. package/dest/contracts/governance.d.ts +131 -43
  31. package/dest/contracts/governance.d.ts.map +1 -1
  32. package/dest/contracts/governance.js +281 -87
  33. package/dest/contracts/governance_proposer.d.ts +49 -13
  34. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  35. package/dest/contracts/governance_proposer.js +483 -26
  36. package/dest/contracts/gse.d.ts +32 -0
  37. package/dest/contracts/gse.d.ts.map +1 -0
  38. package/dest/contracts/gse.js +72 -0
  39. package/dest/contracts/inbox.d.ts +47 -0
  40. package/dest/contracts/inbox.d.ts.map +1 -0
  41. package/dest/contracts/inbox.js +85 -0
  42. package/dest/contracts/index.d.ts +11 -2
  43. package/dest/contracts/index.d.ts.map +1 -1
  44. package/dest/contracts/index.js +10 -1
  45. package/dest/contracts/log.d.ts +13 -0
  46. package/dest/contracts/log.d.ts.map +1 -0
  47. package/dest/contracts/log.js +1 -0
  48. package/dest/contracts/multicall.d.ts +136 -0
  49. package/dest/contracts/multicall.d.ts.map +1 -0
  50. package/dest/contracts/multicall.js +264 -0
  51. package/dest/contracts/outbox.d.ts +64 -0
  52. package/dest/contracts/outbox.d.ts.map +1 -0
  53. package/dest/contracts/outbox.js +113 -0
  54. package/dest/contracts/registry.d.ts +12 -5
  55. package/dest/contracts/registry.d.ts.map +1 -1
  56. package/dest/contracts/registry.js +74 -17
  57. package/dest/contracts/rollup.d.ts +5526 -52
  58. package/dest/contracts/rollup.d.ts.map +1 -1
  59. package/dest/contracts/rollup.js +1446 -118
  60. package/dest/contracts/slasher_contract.d.ts +44 -0
  61. package/dest/contracts/slasher_contract.d.ts.map +1 -0
  62. package/dest/contracts/slasher_contract.js +75 -0
  63. package/dest/contracts/slashing_proposer.d.ts +133 -15
  64. package/dest/contracts/slashing_proposer.d.ts.map +1 -1
  65. package/dest/contracts/slashing_proposer.js +293 -22
  66. package/dest/contracts/utils.d.ts +3 -0
  67. package/dest/contracts/utils.d.ts.map +1 -0
  68. package/dest/contracts/utils.js +11 -0
  69. package/dest/deploy_aztec_l1_contracts.d.ts +256 -0
  70. package/dest/deploy_aztec_l1_contracts.d.ts.map +1 -0
  71. package/dest/deploy_aztec_l1_contracts.js +411 -0
  72. package/dest/deploy_l1_contract.d.ts +68 -0
  73. package/dest/deploy_l1_contract.d.ts.map +1 -0
  74. package/dest/deploy_l1_contract.js +312 -0
  75. package/dest/eth-signer/eth-signer.d.ts +21 -0
  76. package/dest/eth-signer/eth-signer.d.ts.map +1 -0
  77. package/dest/eth-signer/eth-signer.js +5 -0
  78. package/dest/eth-signer/index.d.ts +2 -0
  79. package/dest/eth-signer/index.d.ts.map +1 -0
  80. package/dest/eth-signer/index.js +1 -0
  81. package/dest/forwarder_proxy.d.ts +32 -0
  82. package/dest/forwarder_proxy.d.ts.map +1 -0
  83. package/dest/forwarder_proxy.js +93 -0
  84. package/dest/generated/l1-contracts-defaults.d.ts +30 -0
  85. package/dest/generated/l1-contracts-defaults.d.ts.map +1 -0
  86. package/dest/generated/l1-contracts-defaults.js +30 -0
  87. package/dest/l1_artifacts.d.ts +79270 -0
  88. package/dest/l1_artifacts.d.ts.map +1 -0
  89. package/dest/l1_artifacts.js +151 -0
  90. package/dest/l1_contract_addresses.d.ts +61 -45
  91. package/dest/l1_contract_addresses.d.ts.map +1 -1
  92. package/dest/l1_contract_addresses.js +99 -76
  93. package/dest/l1_reader.d.ts +8 -6
  94. package/dest/l1_reader.d.ts.map +1 -1
  95. package/dest/l1_reader.js +20 -12
  96. package/dest/l1_tx_utils/config.d.ts +65 -0
  97. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  98. package/dest/l1_tx_utils/config.js +109 -0
  99. package/dest/l1_tx_utils/constants.d.ts +12 -0
  100. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  101. package/dest/l1_tx_utils/constants.js +39 -0
  102. package/dest/l1_tx_utils/factory.d.ts +32 -0
  103. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  104. package/dest/l1_tx_utils/factory.js +22 -0
  105. package/dest/l1_tx_utils/fee-strategies/index.d.ts +10 -0
  106. package/dest/l1_tx_utils/fee-strategies/index.d.ts.map +1 -0
  107. package/dest/l1_tx_utils/fee-strategies/index.js +12 -0
  108. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts +8 -0
  109. package/dest/l1_tx_utils/fee-strategies/p75_competitive.d.ts.map +1 -0
  110. package/dest/l1_tx_utils/fee-strategies/p75_competitive.js +129 -0
  111. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts +23 -0
  112. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.d.ts.map +1 -0
  113. package/dest/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.js +191 -0
  114. package/dest/l1_tx_utils/fee-strategies/types.d.ts +51 -0
  115. package/dest/l1_tx_utils/fee-strategies/types.d.ts.map +1 -0
  116. package/dest/l1_tx_utils/fee-strategies/types.js +3 -0
  117. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts +41 -0
  118. package/dest/l1_tx_utils/forwarder_l1_tx_utils.d.ts.map +1 -0
  119. package/dest/l1_tx_utils/forwarder_l1_tx_utils.js +42 -0
  120. package/dest/l1_tx_utils/index-blobs.d.ts +3 -0
  121. package/dest/l1_tx_utils/index-blobs.d.ts.map +1 -0
  122. package/dest/l1_tx_utils/index-blobs.js +2 -0
  123. package/dest/l1_tx_utils/index.d.ts +13 -0
  124. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  125. package/dest/l1_tx_utils/index.js +13 -0
  126. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  127. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  128. package/dest/l1_tx_utils/interfaces.js +4 -0
  129. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts +233 -0
  130. package/dest/l1_tx_utils/l1_fee_analyzer.d.ts.map +1 -0
  131. package/dest/l1_tx_utils/l1_fee_analyzer.js +506 -0
  132. package/dest/l1_tx_utils/l1_tx_utils.d.ts +113 -0
  133. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  134. package/dest/l1_tx_utils/l1_tx_utils.js +658 -0
  135. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +83 -0
  136. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  137. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +328 -0
  138. package/dest/l1_tx_utils/signer.d.ts +4 -0
  139. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  140. package/dest/l1_tx_utils/signer.js +16 -0
  141. package/dest/l1_tx_utils/tx_delayer.d.ts +58 -0
  142. package/dest/l1_tx_utils/tx_delayer.d.ts.map +1 -0
  143. package/dest/l1_tx_utils/tx_delayer.js +223 -0
  144. package/dest/l1_tx_utils/types.d.ts +67 -0
  145. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  146. package/dest/l1_tx_utils/types.js +26 -0
  147. package/dest/l1_tx_utils/utils.d.ts +4 -0
  148. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  149. package/dest/l1_tx_utils/utils.js +14 -0
  150. package/dest/l1_types.d.ts +6 -0
  151. package/dest/l1_types.d.ts.map +1 -0
  152. package/dest/l1_types.js +1 -0
  153. package/dest/publisher_manager.d.ts +30 -0
  154. package/dest/publisher_manager.d.ts.map +1 -0
  155. package/dest/publisher_manager.js +162 -0
  156. package/dest/queries.d.ts +17 -4
  157. package/dest/queries.d.ts.map +1 -1
  158. package/dest/queries.js +120 -12
  159. package/dest/test/chain_monitor.d.ts +95 -0
  160. package/dest/test/chain_monitor.d.ts.map +1 -0
  161. package/dest/test/chain_monitor.js +249 -0
  162. package/dest/test/eth_cheat_codes.d.ts +237 -0
  163. package/dest/test/eth_cheat_codes.d.ts.map +1 -0
  164. package/dest/test/eth_cheat_codes.js +573 -0
  165. package/dest/test/eth_cheat_codes_with_state.d.ts +2 -2
  166. package/dest/test/eth_cheat_codes_with_state.d.ts.map +1 -1
  167. package/dest/test/eth_cheat_codes_with_state.js +1 -1
  168. package/dest/test/index.d.ts +4 -3
  169. package/dest/test/index.d.ts.map +1 -1
  170. package/dest/test/index.js +3 -2
  171. package/dest/test/rollup_cheat_codes.d.ts +118 -0
  172. package/dest/test/rollup_cheat_codes.d.ts.map +1 -0
  173. package/dest/test/rollup_cheat_codes.js +344 -0
  174. package/dest/test/start_anvil.d.ts +30 -2
  175. package/dest/test/start_anvil.d.ts.map +1 -1
  176. package/dest/test/start_anvil.js +147 -24
  177. package/dest/test/upgrade_utils.d.ts +6 -5
  178. package/dest/test/upgrade_utils.d.ts.map +1 -1
  179. package/dest/test/upgrade_utils.js +23 -16
  180. package/dest/types.d.ts +62 -8
  181. package/dest/types.d.ts.map +1 -1
  182. package/dest/types.js +3 -1
  183. package/dest/utils.d.ts +25 -4
  184. package/dest/utils.d.ts.map +1 -1
  185. package/dest/utils.js +142 -91
  186. package/dest/zkPassportVerifierAddress.d.ts +15 -0
  187. package/dest/zkPassportVerifierAddress.d.ts.map +1 -0
  188. package/dest/zkPassportVerifierAddress.js +11 -0
  189. package/package.json +52 -26
  190. package/src/account.ts +5 -0
  191. package/src/client.ts +107 -5
  192. package/src/config.ts +218 -43
  193. package/src/contracts/README.md +157 -0
  194. package/src/contracts/chain_state_override.ts +200 -0
  195. package/src/contracts/empire_base.ts +77 -7
  196. package/src/contracts/errors.ts +13 -0
  197. package/src/contracts/fee_asset_handler.ts +66 -0
  198. package/src/contracts/fee_asset_price_oracle.ts +285 -0
  199. package/src/contracts/fee_juice.ts +29 -15
  200. package/src/contracts/governance.ts +343 -74
  201. package/src/contracts/governance_proposer.ts +128 -25
  202. package/src/contracts/gse.ts +88 -0
  203. package/src/contracts/inbox.ts +123 -0
  204. package/src/contracts/index.ts +10 -1
  205. package/src/contracts/log.ts +13 -0
  206. package/src/contracts/multicall.ts +296 -0
  207. package/src/contracts/outbox.ts +132 -0
  208. package/src/contracts/registry.ts +81 -26
  209. package/src/contracts/rollup.ts +1246 -90
  210. package/src/contracts/slasher_contract.ts +89 -0
  211. package/src/contracts/slashing_proposer.ts +296 -27
  212. package/src/contracts/utils.ts +14 -0
  213. package/src/deploy_aztec_l1_contracts.ts +646 -0
  214. package/src/deploy_l1_contract.ts +362 -0
  215. package/src/eth-signer/eth-signer.ts +25 -0
  216. package/src/eth-signer/index.ts +1 -0
  217. package/src/forwarder_proxy.ts +108 -0
  218. package/src/generated/l1-contracts-defaults.ts +32 -0
  219. package/src/l1_artifacts.ts +231 -0
  220. package/src/l1_contract_addresses.ts +121 -80
  221. package/src/l1_reader.ts +31 -16
  222. package/src/l1_tx_utils/README.md +177 -0
  223. package/src/l1_tx_utils/config.ts +181 -0
  224. package/src/l1_tx_utils/constants.ts +29 -0
  225. package/src/l1_tx_utils/factory.ts +64 -0
  226. package/src/l1_tx_utils/fee-strategies/index.ts +22 -0
  227. package/src/l1_tx_utils/fee-strategies/p75_competitive.ts +163 -0
  228. package/src/l1_tx_utils/fee-strategies/p75_competitive_blob_txs_only.ts +245 -0
  229. package/src/l1_tx_utils/fee-strategies/types.ts +56 -0
  230. package/src/l1_tx_utils/forwarder_l1_tx_utils.ts +108 -0
  231. package/src/l1_tx_utils/index-blobs.ts +2 -0
  232. package/src/l1_tx_utils/index.ts +15 -0
  233. package/src/l1_tx_utils/interfaces.ts +86 -0
  234. package/src/l1_tx_utils/l1_fee_analyzer.ts +803 -0
  235. package/src/l1_tx_utils/l1_tx_utils.ts +789 -0
  236. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +424 -0
  237. package/src/l1_tx_utils/signer.ts +28 -0
  238. package/src/l1_tx_utils/tx_delayer.ts +303 -0
  239. package/src/l1_tx_utils/types.ts +85 -0
  240. package/src/l1_tx_utils/utils.ts +16 -0
  241. package/src/l1_types.ts +6 -0
  242. package/src/publisher_manager.ts +203 -0
  243. package/src/queries.ts +149 -17
  244. package/src/test/chain_monitor.ts +307 -0
  245. package/src/test/eth_cheat_codes.ts +600 -0
  246. package/src/test/eth_cheat_codes_with_state.ts +1 -1
  247. package/src/test/index.ts +3 -2
  248. package/src/test/rollup_cheat_codes.ts +388 -0
  249. package/src/test/start_anvil.ts +189 -22
  250. package/src/test/upgrade_utils.ts +30 -21
  251. package/src/types.ts +71 -7
  252. package/src/utils.ts +164 -96
  253. package/src/zkPassportVerifierAddress.ts +15 -0
  254. package/dest/contracts/forwarder.d.ts +0 -24
  255. package/dest/contracts/forwarder.d.ts.map +0 -1
  256. package/dest/contracts/forwarder.js +0 -101
  257. package/dest/deploy_l1_contracts.d.ts +0 -21210
  258. package/dest/deploy_l1_contracts.d.ts.map +0 -1
  259. package/dest/deploy_l1_contracts.js +0 -687
  260. package/dest/eth_cheat_codes.d.ts +0 -147
  261. package/dest/eth_cheat_codes.d.ts.map +0 -1
  262. package/dest/eth_cheat_codes.js +0 -303
  263. package/dest/index.d.ts +0 -14
  264. package/dest/index.d.ts.map +0 -1
  265. package/dest/index.js +0 -13
  266. package/dest/l1_tx_utils.d.ts +0 -192
  267. package/dest/l1_tx_utils.d.ts.map +0 -1
  268. package/dest/l1_tx_utils.js +0 -641
  269. package/dest/l1_tx_utils_with_blobs.d.ts +0 -12
  270. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  271. package/dest/l1_tx_utils_with_blobs.js +0 -64
  272. package/dest/test/delayed_tx_utils.d.ts +0 -8
  273. package/dest/test/delayed_tx_utils.d.ts.map +0 -1
  274. package/dest/test/delayed_tx_utils.js +0 -21
  275. package/dest/test/tx_delayer.d.ts +0 -25
  276. package/dest/test/tx_delayer.d.ts.map +0 -1
  277. package/dest/test/tx_delayer.js +0 -116
  278. package/src/contracts/forwarder.ts +0 -132
  279. package/src/deploy_l1_contracts.ts +0 -948
  280. package/src/eth_cheat_codes.ts +0 -314
  281. package/src/index.ts +0 -13
  282. package/src/l1_tx_utils.ts +0 -847
  283. package/src/l1_tx_utils_with_blobs.ts +0 -86
  284. package/src/test/delayed_tx_utils.ts +0 -24
  285. package/src/test/tx_delayer.ts +0 -163
@@ -0,0 +1,600 @@
1
+ import { toBigIntBE, toHex } from '@aztec/foundation/bigint-buffer';
2
+ import { keccak256 } from '@aztec/foundation/crypto/keccak';
3
+ import { EthAddress } from '@aztec/foundation/eth-address';
4
+ import { jsonStringify } from '@aztec/foundation/json-rpc';
5
+ import { createLogger } from '@aztec/foundation/log';
6
+ import { pluralize } from '@aztec/foundation/string';
7
+ import type { DateProvider, TestDateProvider } from '@aztec/foundation/timer';
8
+
9
+ import { type Chain, type Hex, type Transaction, createPublicClient, fallback, hexToNumber, http } from 'viem';
10
+ import { foundry } from 'viem/chains';
11
+
12
+ import type { ViemPublicClient } from '../types.js';
13
+
14
+ /**
15
+ * A class that provides utility functions for interacting with ethereum (L1).
16
+ */
17
+ export class EthCheatCodes {
18
+ public readonly publicClient: ViemPublicClient;
19
+ constructor(
20
+ /**
21
+ * The RPC URL to use for interacting with the chain
22
+ */
23
+ public rpcUrls: string[],
24
+ /**
25
+ * The date provider to use for time operations
26
+ */
27
+ public dateProvider: DateProvider | TestDateProvider,
28
+ /**
29
+ * The logger to use for the eth cheatcodes
30
+ */
31
+ public logger = createLogger('ethereum:cheat_codes'),
32
+ /**
33
+ * The chain configuration provided to Anvil
34
+ */
35
+ public chain: Chain = foundry,
36
+ ) {
37
+ this.publicClient = createPublicClient({
38
+ transport: fallback(this.rpcUrls.map(url => http(url, { batch: false }))),
39
+ chain: chain,
40
+ });
41
+ }
42
+
43
+ public rpcCall(method: string, params: any[]) {
44
+ this.logger.debug(`Calling ${method} with params: ${jsonStringify(params)} on ${this.rpcUrls.join(', ')}`);
45
+ return this.doRpcCall(method, params);
46
+ }
47
+
48
+ private async doRpcCall(method: string, params: any[]) {
49
+ return (await this.publicClient.transport.request({
50
+ method,
51
+ params,
52
+ })) as any;
53
+ }
54
+
55
+ /**
56
+ * Get the auto mine status of the underlying chain
57
+ * @returns True if automine is on, false otherwise
58
+ */
59
+ public async isAutoMining(): Promise<boolean> {
60
+ try {
61
+ const res = await this.doRpcCall('anvil_getAutomine', []);
62
+ return res;
63
+ } catch (err) {
64
+ this.logger.error(`Calling "anvil_getAutomine" failed with:`, err);
65
+ }
66
+ return false;
67
+ }
68
+
69
+ /**
70
+ * Get the current blocknumber
71
+ * @returns The current block number
72
+ */
73
+ public async blockNumber(): Promise<number> {
74
+ const res = await this.doRpcCall('eth_blockNumber', []);
75
+ return parseInt(res, 16);
76
+ }
77
+
78
+ /**
79
+ * Get the current chainId
80
+ * @returns The current chainId
81
+ */
82
+ public async chainId(): Promise<number> {
83
+ const res = await this.doRpcCall('eth_chainId', []);
84
+ return parseInt(res, 16);
85
+ }
86
+
87
+ /**
88
+ * Get the timestamp of the latest mined L1 block.
89
+ * Note: this is NOT the current time — it's the discrete timestamp of the last block.
90
+ * Between blocks, the actual chain time advances but no new block reflects it.
91
+ * @returns The latest block timestamp in seconds
92
+ */
93
+ public async lastBlockTimestamp(): Promise<number> {
94
+ const res = await this.doRpcCall('eth_getBlockByNumber', ['latest', true]);
95
+ return parseInt(res.timestamp, 16);
96
+ }
97
+
98
+ /**
99
+ * Get the timestamp of the pending L1 block — the block that anvil will mine next.
100
+ * Reflects any prior `setNextBlockTimestamp` call or the configured block interval.
101
+ * @returns The pending block timestamp in seconds.
102
+ */
103
+ public async nextBlockTimestamp(): Promise<number> {
104
+ const res = await this.doRpcCall('eth_getBlockByNumber', ['pending', false]);
105
+ return parseInt(res.timestamp, 16);
106
+ }
107
+
108
+ /**
109
+ * Advance the chain by a number of blocks
110
+ * @param numberOfBlocks - The number of blocks to mine
111
+ */
112
+ public async mine(numberOfBlocks: number | bigint = 1): Promise<void> {
113
+ await this.doMine(Number(numberOfBlocks));
114
+ this.logger.warn(`Mined ${numberOfBlocks} L1 blocks`);
115
+ }
116
+
117
+ private async doMine(numberOfBlocks = 1): Promise<void> {
118
+ try {
119
+ await this.doRpcCall('hardhat_mine', [numberOfBlocks]);
120
+ } catch (err) {
121
+ throw new Error(`Error mining: ${err}`);
122
+ }
123
+ }
124
+
125
+ /**
126
+ * Mines a single block with evm_mine
127
+ */
128
+ public async evmMine(): Promise<void> {
129
+ try {
130
+ await this.doRpcCall('evm_mine', []);
131
+ this.logger.warn(`Mined 1 L1 block with evm_mine`);
132
+ } catch (err) {
133
+ throw new Error(`Error mining: ${err}`);
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Set the balance of an account
139
+ * @param account - The account to set the balance for
140
+ * @param balance - The balance to set
141
+ */
142
+ public async setBalance(account: EthAddress | Hex, balance: bigint): Promise<void> {
143
+ try {
144
+ await this.rpcCall('anvil_setBalance', [account.toString(), toHex(balance)]);
145
+ } catch (err) {
146
+ throw new Error(`Error setting balance for ${account}: ${err}`);
147
+ }
148
+ this.logger.warn(`Set balance for ${account} to ${balance}`);
149
+ }
150
+
151
+ public async getBalance(account: EthAddress | Hex): Promise<bigint> {
152
+ const res = await this.doRpcCall('eth_getBalance', [account.toString(), 'latest']);
153
+ return BigInt(res);
154
+ }
155
+
156
+ /**
157
+ * Set the interval between successive blocks (block time). This does NOT enable interval mining.
158
+ * @param interval - The interval to use between blocks
159
+ */
160
+ public async setBlockInterval(interval: number): Promise<void> {
161
+ try {
162
+ await this.rpcCall('anvil_setBlockTimestampInterval', [interval]);
163
+ } catch (err) {
164
+ throw new Error(`Error setting block interval: ${err}`);
165
+ }
166
+ this.logger.warn(`Set L1 block interval to ${interval}`);
167
+ }
168
+
169
+ /**
170
+ * Set the next block base fee per gas
171
+ * @param baseFee - The base fee to set
172
+ */
173
+ public async setNextBlockBaseFeePerGas(baseFee: bigint | number): Promise<void> {
174
+ try {
175
+ await this.rpcCall('anvil_setNextBlockBaseFeePerGas', [baseFee.toString()]);
176
+ } catch (err) {
177
+ throw new Error(`Error setting next block base fee per gas: ${err}`);
178
+ }
179
+ this.logger.warn(`Set L1 next block base fee per gas to ${baseFee}`);
180
+ }
181
+
182
+ /**
183
+ * Get interval mining if set.
184
+ * @param seconds - The interval to use between blocks
185
+ */
186
+ public getIntervalMining(): Promise<number | null> {
187
+ try {
188
+ return this.doRpcCall('anvil_getIntervalMining', []);
189
+ } catch (err) {
190
+ throw new Error(`Error getting interval mining: ${err}`);
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Enable interval mining at the given interval (block time)
196
+ * @param seconds - The interval to use between blocks
197
+ */
198
+ public async setIntervalMining(seconds: number, opts: { silent?: boolean } = {}): Promise<void> {
199
+ try {
200
+ await this.rpcCall('anvil_setIntervalMining', [seconds]);
201
+ } catch (err) {
202
+ throw new Error(`Error setting interval mining: ${err}`);
203
+ }
204
+ if (!opts.silent) {
205
+ this.logger.warn(`Set L1 interval mining to ${seconds} seconds`);
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Set the automine status of the underlying anvil chain
211
+ * @param automine - The automine status to set
212
+ */
213
+ public async setAutomine(automine: boolean, opts: { silent?: boolean } = {}): Promise<void> {
214
+ try {
215
+ await this.rpcCall('anvil_setAutomine', [automine]);
216
+ } catch (err) {
217
+ throw new Error(`Error setting automine: ${err}`);
218
+ }
219
+ if (!opts.silent) {
220
+ this.logger.warn(`Set L1 automine to ${automine}`);
221
+ }
222
+ }
223
+
224
+ /**
225
+ * Drop a transaction from the mempool
226
+ * @param txHash - The transaction hash
227
+ */
228
+ public async dropTransaction(txHash: Hex): Promise<void> {
229
+ try {
230
+ await this.rpcCall('anvil_dropTransaction', [txHash]);
231
+ } catch (err) {
232
+ throw new Error(`Error dropping transaction: ${err}`);
233
+ }
234
+ this.logger.warn(`Dropped transaction ${txHash}`);
235
+ }
236
+
237
+ /**
238
+ * Set the next block timestamp
239
+ * @param timestamp - The timestamp to set the next block to
240
+ */
241
+ public async setNextBlockTimestamp(timestamp: number | Date): Promise<void> {
242
+ try {
243
+ await this.rpcCall('evm_setNextBlockTimestamp', [
244
+ timestamp instanceof Date ? Math.floor(timestamp.getTime() / 1000) : timestamp,
245
+ ]);
246
+ } catch (err: any) {
247
+ throw new Error(`Error setting next block timestamp: ${err.message}`);
248
+ }
249
+ this.logger.warn(`Set L1 next block timestamp to ${timestamp}`);
250
+ }
251
+
252
+ /**
253
+ * Set the next block timestamp and mines the block.
254
+ * Optionally resets interval mining so the next block is mined in `blockInterval` seconds from now.
255
+ * Always updates the injected date provider to follow L1 time.
256
+ * @param timestamp - The timestamp to set the next block to
257
+ */
258
+ public async warp(
259
+ timestamp: number | bigint,
260
+ opts: { silent?: boolean; resetBlockInterval?: boolean } = {},
261
+ ): Promise<void> {
262
+ let blockInterval: number | null = null;
263
+ try {
264
+ // Load current block interval and disable it
265
+ if (opts.resetBlockInterval) {
266
+ blockInterval = await this.getIntervalMining();
267
+ if (blockInterval !== null) {
268
+ await this.setIntervalMining(0, { silent: true });
269
+ }
270
+ }
271
+ // Set the timestamp of the next block to be mined
272
+ await this.rpcCall('evm_setNextBlockTimestamp', [Number(timestamp)]);
273
+ // And mine a block so the timestamp goes into effect now
274
+ await this.doMine();
275
+ // Update the injected date provider so it follows L1 time
276
+ if ('setTime' in this.dateProvider) {
277
+ this.dateProvider.setTime(Number(timestamp) * 1000);
278
+ }
279
+ } catch (err) {
280
+ throw new Error(`Error warping: ${err}`);
281
+ } finally {
282
+ // Restore interval mining so the next block is mined in `blockInterval` seconds from this one
283
+ if (opts.resetBlockInterval && blockInterval !== null && blockInterval > 0) {
284
+ await this.setIntervalMining(blockInterval, { silent: true });
285
+ }
286
+ }
287
+ if (!opts.silent) {
288
+ this.logger.warn(`Warped L1 timestamp to ${timestamp}`);
289
+ }
290
+ }
291
+
292
+ /**
293
+ * Load the value at a storage slot of a contract address on eth
294
+ * @param contract - The contract address
295
+ * @param slot - The storage slot
296
+ * @returns - The value at the storage slot
297
+ */
298
+ public async load(contract: EthAddress, slot: bigint): Promise<bigint> {
299
+ const res = await this.rpcCall('eth_getStorageAt', [contract.toString(), toHex(slot), 'latest']);
300
+ return BigInt(res);
301
+ }
302
+
303
+ /**
304
+ * Set the value at a storage slot of a contract address on eth
305
+ * @param contract - The contract address
306
+ * @param slot - The storage slot
307
+ * @param value - The value to set the storage slot to
308
+ */
309
+ public async store(
310
+ contract: EthAddress,
311
+ slot: bigint,
312
+ value: bigint,
313
+ opts: { silent?: boolean } = {},
314
+ ): Promise<void> {
315
+ // for the rpc call, we need to change value to be a 32 byte hex string.
316
+ try {
317
+ await this.rpcCall('hardhat_setStorageAt', [contract.toString(), toHex(slot), toHex(value, true)]);
318
+ } catch (err) {
319
+ throw new Error(`Error setting storage for contract ${contract} at ${slot}: ${err}`);
320
+ }
321
+ if (!opts.silent) {
322
+ this.logger.warn(`Set L1 storage for contract ${contract} at ${slot} to ${value}`);
323
+ }
324
+ }
325
+
326
+ /**
327
+ * Computes the slot value for a given map and key.
328
+ * @param baseSlot - The base slot of the map (specified in Aztec.nr contract)
329
+ * @param key - The key to lookup in the map
330
+ * @returns The storage slot of the value in the map
331
+ */
332
+ public keccak256(baseSlot: bigint, key: bigint): bigint {
333
+ // abi encode (removing the 0x) - concat key and baseSlot (both padded to 32 bytes)
334
+ const abiEncoded = toHex(key, true).substring(2) + toHex(baseSlot, true).substring(2);
335
+ return toBigIntBE(keccak256(Buffer.from(abiEncoded, 'hex')));
336
+ }
337
+
338
+ /**
339
+ * Send transactions impersonating an externally owned account or contract.
340
+ * @param who - The address to impersonate
341
+ */
342
+ public async startImpersonating(who: EthAddress | Hex): Promise<void> {
343
+ try {
344
+ // Since the `who` impersonated will sometimes be a contract without funds, we fund it if needed.
345
+ if ((await this.getBalance(who)) === 0n) {
346
+ await this.setBalance(who, 10n * 10n ** 18n);
347
+ }
348
+
349
+ await this.rpcCall('hardhat_impersonateAccount', [who.toString()]);
350
+ } catch (err) {
351
+ throw new Error(`Error impersonating ${who}: ${err}`);
352
+ }
353
+ this.logger.warn(`Impersonating ${who}`);
354
+ }
355
+
356
+ /**
357
+ * Stop impersonating an account that you are currently impersonating.
358
+ * @param who - The address to stop impersonating
359
+ */
360
+ public async stopImpersonating(who: EthAddress | Hex): Promise<void> {
361
+ try {
362
+ await this.rpcCall('hardhat_stopImpersonatingAccount', [who.toString()]);
363
+ } catch (err) {
364
+ throw new Error(`Error when stopping the impersonation of ${who}: ${err}`);
365
+ }
366
+ this.logger.warn(`Stopped impersonating ${who}`);
367
+ }
368
+
369
+ /**
370
+ * Set the bytecode for a contract
371
+ * @param contract - The contract address
372
+ * @param bytecode - The bytecode to set
373
+ */
374
+ public async etch(contract: EthAddress, bytecode: `0x${string}`): Promise<void> {
375
+ try {
376
+ await this.rpcCall('hardhat_setCode', [contract.toString(), bytecode]);
377
+ } catch (err) {
378
+ throw new Error(`Error setting bytecode for ${contract}: ${err}`);
379
+ }
380
+ this.logger.warn(`Set bytecode for ${contract} to ${bytecode}`);
381
+ }
382
+
383
+ /**
384
+ * Get the bytecode for a contract
385
+ * @param contract - The contract address
386
+ * @returns The bytecode for the contract
387
+ */
388
+ public async getBytecode(contract: EthAddress): Promise<`0x${string}`> {
389
+ return await this.doRpcCall('eth_getCode', [contract.toString(), 'latest']);
390
+ }
391
+
392
+ /**
393
+ * Get the raw transaction object for a given transaction hash
394
+ * @param txHash - The transaction hash
395
+ * @returns The raw transaction
396
+ */
397
+ public async getRawTransaction(txHash: Hex): Promise<`0x${string}`> {
398
+ return await this.doRpcCall('debug_getRawTransaction', [txHash]);
399
+ }
400
+
401
+ /**
402
+ * Get the trace for a given transaction hash
403
+ * @param txHash - The transaction hash
404
+ * @returns The trace
405
+ */
406
+ public async debugTraceTransaction(txHash: Hex): Promise<any> {
407
+ return await this.doRpcCall('debug_traceTransaction', [txHash]);
408
+ }
409
+
410
+ /**
411
+ * Triggers a reorg of the given depth, removing those blocks from the chain.
412
+ * @param depth - The depth of the reorg
413
+ */
414
+ public reorg(depth: number): Promise<void> {
415
+ return this.execWithPausedAnvil(() => {
416
+ return this.rpcCall('anvil_rollback', [depth]);
417
+ });
418
+ }
419
+
420
+ /**
421
+ * Causes Anvil to reorg until the given block number is the new tip
422
+ * @param blockNumber - The block number that's going to be the new tip
423
+ */
424
+ public reorgTo(blockNumber: number): Promise<void> {
425
+ if (blockNumber <= 0) {
426
+ throw new Error(`Can't reorg to block before genesis: ${blockNumber}`);
427
+ }
428
+
429
+ return this.execWithPausedAnvil(async () => {
430
+ const currentTip = await this.publicClient.getBlockNumber();
431
+ if (currentTip < BigInt(blockNumber)) {
432
+ this.logger.warn(
433
+ `Can't call anvil_rollback, chain tip is behind target block: ${currentTip} < ${BigInt(blockNumber)}`,
434
+ );
435
+ return;
436
+ }
437
+
438
+ const depth = Number(currentTip - BigInt(blockNumber) + 1n);
439
+ await this.rpcCall('anvil_rollback', [depth]);
440
+ this.logger.warn(`Reorged L1 chain to block number ${blockNumber} (depth ${depth})`);
441
+ });
442
+ }
443
+
444
+ /**
445
+ * Triggers a reorg of the given depth, optionally replacing it with new blocks.
446
+ * The resulting block height will be the same as the original chain.
447
+ * @param depth - The depth of the reorg
448
+ * @param newBlocks - The blocks to replace the old ones with, each represented as a list of txs.
449
+ */
450
+ public async reorgWithReplacement(
451
+ depth: number,
452
+ newBlocks: (Hex | { to: EthAddress | Hex; input?: Hex; from?: EthAddress | Hex; value?: number | bigint })[][] = [],
453
+ ): Promise<void> {
454
+ this.logger.verbose(`Preparing L1 reorg with depth ${depth}`);
455
+ try {
456
+ await this.rpcCall('anvil_reorg', [
457
+ depth,
458
+ newBlocks.flatMap((txs, index) => txs.map(tx => [typeof tx === 'string' ? tx : { value: 0, ...tx }, index])),
459
+ ]);
460
+ } catch (err) {
461
+ throw new Error(`Error reorging: ${err}`);
462
+ }
463
+ this.logger.warn(`Reorged L1 chain with depth ${depth} and ${newBlocks.length} new blocks`, { depth, newBlocks });
464
+ }
465
+
466
+ public traceTransaction(txHash: Hex): Promise<any> {
467
+ return this.doRpcCall('trace_transaction', [txHash]);
468
+ }
469
+
470
+ public async getTxPoolStatus(): Promise<{ pending: number; queued: number }> {
471
+ const { pending, queued } = await this.doRpcCall('txpool_status', []);
472
+ return { pending: hexToNumber(pending), queued: hexToNumber(queued) };
473
+ }
474
+
475
+ public async getTxPoolContents(): Promise<TxPoolTransaction[]> {
476
+ const txpoolContent = await this.doRpcCall('txpool_content', []);
477
+ return mapTxPoolContent(txpoolContent);
478
+ }
479
+
480
+ /**
481
+ * Mines an empty block by temporarily removing all pending transactions from the mempool,
482
+ * mining a block, and then re-adding the transactions back to the pool.
483
+ */
484
+ public async mineEmptyBlock(blockCount: number = 1): Promise<void> {
485
+ await this.execWithPausedAnvil(async () => {
486
+ // Get all pending and queued transactions from the pool
487
+ const txs = await this.getTxPoolContents();
488
+
489
+ this.logger.debug(`Found ${txs.length} transactions in pool`);
490
+
491
+ // Get raw transactions before dropping them
492
+ const rawTxs: Hex[] = [];
493
+ for (const tx of txs) {
494
+ try {
495
+ const rawTx = await this.doRpcCall('debug_getRawTransaction', [tx.hash]);
496
+ if (rawTx) {
497
+ rawTxs.push(rawTx);
498
+ this.logger.debug(`Got raw tx for ${tx.hash}`);
499
+ } else {
500
+ this.logger.warn(`No raw tx found for ${tx.hash}`);
501
+ }
502
+ } catch {
503
+ this.logger.warn(`Failed to get raw transaction for ${tx.hash}`);
504
+ }
505
+ }
506
+
507
+ this.logger.debug(`Retrieved ${rawTxs.length} raw transactions`);
508
+
509
+ // Drop all transactions from the mempool
510
+ await this.doRpcCall('anvil_dropAllTransactions', []);
511
+
512
+ // Mine an empty block
513
+ await this.doMine(blockCount);
514
+
515
+ // Re-add the transactions to the pool
516
+ for (const rawTx of rawTxs) {
517
+ try {
518
+ const txHash = await this.doRpcCall('eth_sendRawTransaction', [rawTx]);
519
+ this.logger.debug(`Re-added transaction ${txHash}`);
520
+ } catch (err) {
521
+ this.logger.warn(`Failed to re-add transaction: ${err}`);
522
+ }
523
+ }
524
+
525
+ if (rawTxs.length !== txs.length) {
526
+ this.logger.warn(`Failed to add all txs back: had ${txs.length} but re-added ${rawTxs.length}`);
527
+ }
528
+ });
529
+
530
+ this.logger.warn(`Mined ${blockCount} empty L1 ${pluralize('block', blockCount)}`);
531
+ }
532
+
533
+ public async execWithPausedAnvil<T>(fn: () => Promise<T>): Promise<T> {
534
+ const [blockInterval, wasAutoMining] = await Promise.all([this.getIntervalMining(), this.isAutoMining()]);
535
+ try {
536
+ if (blockInterval !== null) {
537
+ await this.setIntervalMining(0, { silent: true });
538
+ }
539
+
540
+ if (wasAutoMining) {
541
+ await this.setAutomine(false, { silent: true });
542
+ }
543
+
544
+ return await fn();
545
+ } finally {
546
+ try {
547
+ // restore automine if necessary
548
+ if (wasAutoMining) {
549
+ await this.setAutomine(true, { silent: true });
550
+ }
551
+ } catch (err) {
552
+ this.logger.warn(`Failed to reenable automining: ${err}`);
553
+ }
554
+
555
+ try {
556
+ // restore automine if necessary
557
+ if (blockInterval !== null) {
558
+ await this.setIntervalMining(blockInterval, { silent: true });
559
+ }
560
+ } catch (err) {
561
+ this.logger.warn(`Failed to reenable interval mining: ${err}`);
562
+ }
563
+ }
564
+ }
565
+
566
+ public async syncDateProvider() {
567
+ const timestamp = await this.lastBlockTimestamp();
568
+ if ('setTime' in this.dateProvider) {
569
+ this.dateProvider.setTime(timestamp * 1000);
570
+ }
571
+ }
572
+ }
573
+
574
+ type TxPoolState = 'pending' | 'queued';
575
+
576
+ interface TxPoolContent {
577
+ pending: Record<Hex, Record<string, Transaction>>;
578
+ queued: Record<Hex, Record<string, Transaction>>;
579
+ }
580
+
581
+ export type TxPoolTransaction = Transaction & {
582
+ poolState: TxPoolState;
583
+ };
584
+
585
+ function mapTxPoolContent(content: TxPoolContent): TxPoolTransaction[] {
586
+ const result: TxPoolTransaction[] = [];
587
+
588
+ const processPool = (pool: Record<Hex, Record<string, Transaction>>, poolState: TxPoolState) => {
589
+ for (const txsByNonce of Object.values(pool)) {
590
+ for (const tx of Object.values(txsByNonce)) {
591
+ result.push({ ...tx, poolState });
592
+ }
593
+ }
594
+ };
595
+
596
+ processPool(content.pending, 'pending');
597
+ processPool(content.queued, 'queued');
598
+
599
+ return result;
600
+ }
@@ -1,6 +1,6 @@
1
1
  import fs from 'fs';
2
2
 
3
- import { EthCheatCodes } from '../eth_cheat_codes.js';
3
+ import { EthCheatCodes } from './eth_cheat_codes.js';
4
4
 
5
5
  /**
6
6
  * A class that provides utility functions for interacting with ethereum (L1) dumping/loading state to/from a file.
package/src/test/index.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from './delayed_tx_utils.js';
1
+ export * from './eth_cheat_codes.js';
2
2
  export * from './eth_cheat_codes_with_state.js';
3
3
  export * from './start_anvil.js';
4
- export * from './tx_delayer.js';
5
4
  export * from './upgrade_utils.js';
5
+ export * from './chain_monitor.js';
6
+ export * from './rollup_cheat_codes.js';