anvil_zksync_api_decl/namespaces/anvil.rs
1use anvil_zksync_types::api::{DetailedTransaction, ResetRequest};
2use anvil_zksync_types::Numeric;
3use jsonrpsee::core::RpcResult;
4use jsonrpsee::proc_macros::rpc;
5use zksync_types::api::Block;
6use zksync_types::web3::Bytes;
7use zksync_types::{Address, H256, U256, U64};
8
9#[rpc(server, namespace = "anvil")]
10pub trait AnvilNamespace {
11 /// Create a buffer that represents all state on the chain, which can be loaded to separate
12 /// process by calling `anvil_loadState`.
13 ///
14 /// # Arguments
15 ///
16 /// * `preserve_historical_states` - Whether to preserve historical states
17 ///
18 /// # Returns
19 /// Buffer representing the chain state.
20 #[method(name = "dumpState", aliases = ["hardhat_dumpState"])]
21 async fn dump_state(&self, preserve_historical_states: Option<bool>) -> RpcResult<Bytes>;
22
23 /// Append chain state buffer to current chain. Will overwrite any conflicting addresses or
24 /// storage.
25 ///
26 /// # Arguments
27 ///
28 /// * `bytes` - Buffer containing the chain state
29 ///
30 /// # Returns
31 /// `true` if a snapshot was reverted, otherwise `false`.
32 #[method(name = "loadState", aliases = ["hardhat_loadState"])]
33 async fn load_state(&self, bytes: Bytes) -> RpcResult<bool>;
34
35 /// Mines a single block in the same way as `evm_mine` but returns extra fields.
36 ///
37 /// # Returns
38 /// Freshly mined block's representation along with extra fields.
39 #[method(name = "mine_detailed", aliases = ["evm_mine_detailed"])]
40 async fn mine_detailed(&self) -> RpcResult<Block<DetailedTransaction>>;
41
42 /// Sets the fork RPC url. Assumes the underlying chain is the same as before.
43 ///
44 /// # Arguments
45 ///
46 /// * `url` - Fork's new URL
47 #[method(name = "setRpcUrl")]
48 async fn set_rpc_url(&self, url: String) -> RpcResult<()>;
49
50 /// Sets the base fee of the next block.
51 ///
52 /// # Arguments
53 ///
54 /// * `base_fee` - Value to be set as base fee for the next block
55 #[method(name = "setNextBlockBaseFeePerGas", aliases = ["hardhat_setNextBlockBaseFeePerGas"])]
56 async fn set_next_block_base_fee_per_gas(&self, base_fee: U256) -> RpcResult<()>;
57
58 /// Removes a transaction from the pool.
59 ///
60 /// # Arguments
61 ///
62 /// * `hash` - Hash of the transaction to be removed from the pool
63 ///
64 /// # Returns
65 /// `Some(hash)` if transaction was in the pool before being removed, `None` otherwise
66 #[method(name = "dropTransaction", aliases = ["hardhat_dropTransaction"])]
67 async fn drop_transaction(&self, hash: H256) -> RpcResult<Option<H256>>;
68
69 /// Remove all transactions from the pool.
70 #[method(name = "dropAllTransactions", aliases = ["hardhat_dropAllTransactions"])]
71 async fn drop_all_transactions(&self) -> RpcResult<()>;
72
73 /// Remove all transactions from the pool by sender address.
74 ///
75 /// # Arguments
76 ///
77 /// * `address` - Sender which transactions should be removed from the pool
78 #[method(name = "removePoolTransactions")]
79 async fn remove_pool_transactions(&self, address: Address) -> RpcResult<()>;
80
81 /// Gets node's auto mining status.
82 ///
83 /// # Returns
84 /// `true` if auto mining is enabled, `false` otherwise
85 #[method(name = "getAutomine", aliases = ["hardhat_getAutomine"])]
86 async fn get_auto_mine(&self) -> RpcResult<bool>;
87
88 /// Enables or disables, based on the single boolean argument, the automatic mining of new
89 /// blocks with each new transaction submitted to the network.
90 ///
91 /// # Arguments
92 ///
93 /// * `enable` - if `true` automatic mining will be enabled, disabled otherwise
94 #[method(name = "setAutomine", aliases = ["evm_setAutomine"])]
95 async fn set_auto_mine(&self, enable: bool) -> RpcResult<()>;
96
97 /// Sets the mining behavior to interval with the given interval (seconds).
98 ///
99 /// # Arguments
100 ///
101 /// * `seconds` - Frequency of automatic block production (in seconds)
102 #[method(name = "setIntervalMining", aliases = ["evm_setIntervalMining"])]
103 async fn set_interval_mining(&self, seconds: u64) -> RpcResult<()>;
104
105 /// Sets the block timestamp interval. All future blocks' timestamps will
106 /// have the provided amount of seconds in-between of them. Does not affect
107 /// the block production interval.
108 ///
109 /// # Arguments
110 ///
111 /// * `seconds` - The interval between two consecutive blocks (in seconds)
112 #[method(name = "setBlockTimestampInterval")]
113 async fn set_block_timestamp_interval(&self, seconds: u64) -> RpcResult<()>;
114
115 /// Removes the block timestamp interval if it exists.
116 ///
117 /// # Returns
118 /// `true` if an existing interval was removed, `false` otherwise
119 #[method(name = "removeBlockTimestampInterval")]
120 async fn remove_block_timestamp_interval(&self) -> RpcResult<bool>;
121
122 /// Set the minimum gas price for the node. Unsupported for ZKsync as it is only relevant for
123 /// pre-EIP1559 chains.
124 ///
125 /// # Arguments
126 ///
127 /// * `gas` - The minimum gas price to be set
128 #[method(name = "setMinGasPrice", aliases = ["hardhat_setMinGasPrice"])]
129 async fn set_min_gas_price(&self, gas: U256) -> RpcResult<()>;
130
131 /// Enable or disable logging.
132 ///
133 /// # Arguments
134 ///
135 /// * `enable` - if `true` logging will be enabled, disabled otherwise
136 #[method(name = "setLoggingEnabled", aliases = ["hardhat_setLoggingEnabled"])]
137 async fn set_logging_enabled(&self, enable: bool) -> RpcResult<()>;
138
139 /// Snapshot the state of the blockchain at the current block. Takes no parameters. Returns the id of the snapshot
140 /// that was created. A snapshot can only be reverted once. After a successful `anvil_revert`, the same snapshot id cannot
141 /// be used again. Consider creating a new snapshot after each `anvil_revert` if you need to revert to the same
142 /// point multiple times.
143 ///
144 /// # Returns
145 /// The `U64` identifier for this snapshot.
146 #[method(name = "snapshot", aliases = ["evm_snapshot"])]
147 async fn snapshot(&self) -> RpcResult<U64>;
148
149 /// Revert the state of the blockchain to a previous snapshot. Takes a single parameter,
150 /// which is the snapshot id to revert to. This deletes the given snapshot, as well as any snapshots
151 /// taken after (e.g.: reverting to id 0x1 will delete snapshots with ids 0x1, 0x2, etc.)
152 ///
153 /// # Arguments
154 ///
155 /// * `id` - The snapshot id to revert
156 ///
157 /// # Returns
158 /// `true` if a snapshot was reverted, otherwise `false`.
159 #[method(name = "revert", aliases = ["evm_revert"])]
160 async fn revert(&self, id: U64) -> RpcResult<bool>;
161
162 /// Set the current timestamp for the node.
163 /// Warning: This will allow you to move backwards in time, which may cause new blocks to appear to be
164 /// mined before old blocks. This will result in an invalid state.
165 ///
166 /// # Arguments
167 ///
168 /// * `time` - The timestamp to set the time to
169 ///
170 /// # Returns
171 /// The difference between the current timestamp and the new timestamp.
172 #[method(name = "setTime", aliases = ["evm_setTime"])]
173 async fn set_time(&self, timestamp: Numeric) -> RpcResult<i128>;
174
175 /// Increase the current timestamp for the node
176 ///
177 /// # Arguments
178 ///
179 /// * `seconds` - The number of seconds to increase time by
180 ///
181 /// # Returns
182 /// The applied time delta to the current timestamp in seconds.
183 #[method(name = "increaseTime", aliases = ["evm_increaseTime"])]
184 async fn increase_time(&self, seconds: Numeric) -> RpcResult<u64>;
185
186 /// Set timestamp for the next block. The timestamp must be in future.
187 ///
188 /// # Arguments
189 ///
190 /// * `timestamp` - The timestamp to set the time to
191 #[method(name = "setNextBlockTimestamp", aliases = ["evm_setNextBlockTimestamp"])]
192 async fn set_next_block_timestamp(&self, timestamp: Numeric) -> RpcResult<()>;
193
194 /// Sets auto impersonation status.
195 ///
196 /// # Arguments
197 ///
198 /// * `enabled` - `true` makes every account impersonated, `false` disables this behavior
199 ///
200 /// # Returns
201 ///
202 /// A `BoxFuture` containing a `Result` representing the success of the operation.
203 #[method(name = "autoImpersonateAccount", aliases = ["hardhat_autoImpersonateAccount"])]
204 async fn auto_impersonate_account(&self, enabled: bool) -> RpcResult<()>;
205
206 /// Sets the balance of the given address to the given balance.
207 ///
208 /// # Arguments
209 ///
210 /// * `address` - The `Address` whose balance will be edited
211 /// * `balance` - The new balance to set for the given address, in wei
212 ///
213 /// # Returns
214 ///
215 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
216 #[method(name = "setBalance", aliases = ["hardhat_setBalance"])]
217 async fn set_balance(&self, address: Address, balance: U256) -> RpcResult<bool>;
218
219 /// Modifies an account's nonce by overwriting it.
220 ///
221 /// # Arguments
222 ///
223 /// * `address` - The `Address` whose nonce is to be changed
224 /// * `nonce` - The new nonce
225 ///
226 /// # Returns
227 ///
228 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
229 #[method(name = "setNonce", aliases = ["hardhat_setNonce", "evm_setAccountNonce"])]
230 async fn set_nonce(&self, address: Address, nonce: U256) -> RpcResult<bool>;
231
232 /// Sometimes you may want to advance the latest block number of the network by a large number of blocks.
233 /// One way to do this would be to call the evm_mine RPC method multiple times, but this is too slow if you want to mine thousands of blocks.
234 /// The `anvil_mine` method can mine any number of blocks at once, in constant time. (It exhibits the same performance no matter how many blocks are mined.)
235 ///
236 /// # Arguments
237 ///
238 /// * `num_blocks` - The number of blocks to mine, defaults to 1
239 /// * `interval` - The interval between the timestamps of each block, in seconds, and it also defaults to 1
240 ///
241 /// # Returns
242 ///
243 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
244 #[method(name = "mine", aliases = ["hardhat_mine"])]
245 async fn anvil_mine(&self, num_blocks: Option<U64>, interval: Option<U64>) -> RpcResult<()>;
246
247 /// Reset the state of the network back to a fresh forked state, or disable forking.
248 ///
249 /// # Arguments
250 ///
251 /// * `reset_spec` - The requested state, defaults to resetting the current network.
252 ///
253 /// # Returns
254 ///
255 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
256 #[method(name = "reset", aliases = ["hardhat_reset"])]
257 async fn reset_network(&self, reset_spec: Option<ResetRequest>) -> RpcResult<bool>;
258
259 /// anvil-zksync allows transactions impersonating specific account and contract addresses.
260 /// To impersonate an account use this method, passing the address to impersonate as its parameter.
261 /// After calling this method, any transactions with this sender will be executed without verification.
262 /// Multiple addresses can be impersonated at once.
263 ///
264 /// # Arguments
265 ///
266 /// * `address` - The address to impersonate
267 ///
268 /// # Returns
269 ///
270 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
271 #[method(name = "impersonateAccount", aliases = ["hardhat_impersonateAccount"])]
272 async fn impersonate_account(&self, address: Address) -> RpcResult<()>;
273
274 /// Use this method to stop impersonating an account after having previously used `anvil_impersonateAccount`
275 /// The method returns `true` if the account was being impersonated and `false` otherwise.
276 ///
277 /// # Arguments
278 ///
279 /// * `address` - The address to stop impersonating.
280 ///
281 /// # Returns
282 ///
283 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
284 #[method(name = "stopImpersonatingAccount", aliases = ["hardhat_stopImpersonatingAccount"])]
285 async fn stop_impersonating_account(&self, address: Address) -> RpcResult<()>;
286
287 /// Modifies the bytecode stored at an account's address.
288 ///
289 /// # Arguments
290 ///
291 /// * `address` - The address where the given code should be stored.
292 /// * `code` - The code to be stored.
293 ///
294 /// # Returns
295 ///
296 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
297 #[method(name = "setCode", aliases = ["hardhat_setCode"])]
298 async fn set_code(&self, address: Address, code: String) -> RpcResult<()>;
299
300 /// Directly modifies the storage of a contract at a specified slot.
301 ///
302 /// # Arguments
303 ///
304 /// * `address` - The contract address whose storage is to be modified.
305 /// * `slot` - The storage slot to modify.
306 /// * `value` - The value to be set at the specified slot.
307 ///
308 /// # Returns
309 ///
310 /// A `BoxFuture` containing a `Result` with a `bool` representing the success of the operation.
311 #[method(name = "setStorageAt", aliases = ["hardhat_setStorageAt"])]
312 async fn set_storage_at(&self, address: Address, slot: U256, value: U256) -> RpcResult<bool>;
313
314 /// Sets the chain id.
315 ///
316 /// # Arguments
317 ///
318 /// * `id` - The chain id to be set.
319 #[method(name = "setChainId")]
320 async fn set_chain_id(&self, id: u32) -> RpcResult<()>;
321}