reth_rpc_api/
anvil.rs

1use jsonrpsee::{core::RpcResult, proc_macros::rpc};
2
3use alloy_primitives::{Address, Bytes, B256, U256};
4use alloy_rpc_types_anvil::{Forking, Metadata, MineOptions, NodeInfo};
5use alloy_rpc_types_eth::Block;
6
7/// Anvil rpc interface.
8/// https://book.getfoundry.sh/reference/anvil/#custom-methods
9#[cfg_attr(not(feature = "client"), rpc(server, namespace = "anvil"))]
10#[cfg_attr(feature = "client", rpc(server, client, namespace = "anvil"))]
11pub trait AnvilApi {
12    /// Sends transactions impersonating specific account and contract addresses.
13    #[method(name = "impersonateAccount")]
14    async fn anvil_impersonate_account(&self, address: Address) -> RpcResult<()>;
15
16    /// Stops impersonating an account if previously set with `anvil_impersonateAccount`.
17    #[method(name = "stopImpersonatingAccount")]
18    async fn anvil_stop_impersonating_account(&self, address: Address) -> RpcResult<()>;
19
20    /// If set to true will make every account impersonated.
21    #[method(name = "autoImpersonateAccount")]
22    async fn anvil_auto_impersonate_account(&self, enabled: bool) -> RpcResult<()>;
23
24    /// Returns `true` if auto mining is enabled, and `false`.
25    #[method(name = "getAutomine")]
26    async fn anvil_get_automine(&self) -> RpcResult<bool>;
27
28    /// Mines a series of blocks.
29    #[method(name = "mine")]
30    async fn anvil_mine(&self, blocks: Option<U256>, interval: Option<U256>) -> RpcResult<()>;
31
32    /// Enables or disables, based on the single boolean argument, the automatic mining of new
33    /// blocks with each new transaction submitted to the network.
34    #[method(name = "setAutomine")]
35    async fn anvil_set_automine(&self, enabled: bool) -> RpcResult<()>;
36
37    /// Sets the mining behavior to interval with the given interval (seconds).
38    #[method(name = "setIntervalMining")]
39    async fn anvil_set_interval_mining(&self, interval: u64) -> RpcResult<()>;
40
41    /// Removes transactions from the pool.
42    #[method(name = "anvil_dropTransaction")]
43    async fn anvil_drop_transaction(&self, tx_hash: B256) -> RpcResult<Option<B256>>;
44
45    /// Resets the fork to a fresh forked state, and optionally update the fork config.
46    ///
47    /// If `forking` is `None` then this will disable forking entirely.
48    #[method(name = "reset")]
49    async fn anvil_reset(&self, fork: Option<Forking>) -> RpcResult<()>;
50
51    /// Sets the backend rpc url.
52    #[method(name = "setRpcUrl")]
53    async fn anvil_set_rpc_url(&self, url: String) -> RpcResult<()>;
54
55    /// Modifies the balance of an account.
56    #[method(name = "setBalance")]
57    async fn anvil_set_balance(&self, address: Address, balance: U256) -> RpcResult<()>;
58
59    /// Sets the code of a contract.
60    #[method(name = "setCode")]
61    async fn anvil_set_code(&self, address: Address, code: Bytes) -> RpcResult<()>;
62
63    /// Sets the nonce of an address.
64    #[method(name = "setNonce")]
65    async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> RpcResult<()>;
66
67    /// Writes a single slot of the account's storage.
68    #[method(name = "setStorageAt")]
69    async fn anvil_set_storage_at(
70        &self,
71        address: Address,
72        slot: U256,
73        value: B256,
74    ) -> RpcResult<bool>;
75
76    /// Sets the coinbase address.
77    #[method(name = "setCoinbase")]
78    async fn anvil_set_coinbase(&self, address: Address) -> RpcResult<()>;
79
80    /// Sets the chain id.
81    #[method(name = "setChainId")]
82    async fn anvil_set_chain_id(&self, chain_id: u64) -> RpcResult<()>;
83
84    /// Enables or disable logging.
85    #[method(name = "setLoggingEnabled")]
86    async fn anvil_set_logging_enabled(&self, enabled: bool) -> RpcResult<()>;
87
88    ///  Sets the minimum gas price for the node.
89    #[method(name = "setMinGasPrice")]
90    async fn anvil_set_min_gas_price(&self, gas_price: U256) -> RpcResult<()>;
91
92    /// Sets the base fee of the next block.
93    #[method(name = "setNextBlockBaseFeePerGas")]
94    async fn anvil_set_next_block_base_fee_per_gas(&self, base_fee: U256) -> RpcResult<()>;
95
96    /// Sets the minimum gas price for the node.
97    #[method(name = "setTime")]
98    async fn anvil_set_time(&self, timestamp: u64) -> RpcResult<u64>;
99
100    /// Creates a buffer that represents all state on the chain, which can be loaded to separate
101    /// process by calling `anvil_loadState`.
102    #[method(name = "dumpState")]
103    async fn anvil_dump_state(&self) -> RpcResult<Bytes>;
104
105    /// Append chain state buffer to current chain. Will overwrite any conflicting addresses or
106    /// storage.
107    #[method(name = "loadState")]
108    async fn anvil_load_state(&self, state: Bytes) -> RpcResult<bool>;
109
110    /// Retrieves the Anvil node configuration params.
111    #[method(name = "nodeInfo")]
112    async fn anvil_node_info(&self) -> RpcResult<NodeInfo>;
113
114    /// Retrieves metadata about the Anvil instance.
115    #[method(name = "metadata")]
116    async fn anvil_metadata(&self) -> RpcResult<Metadata>;
117
118    /// Snapshot the state of the blockchain at the current block.
119    #[method(name = "snapshot")]
120    async fn anvil_snapshot(&self) -> RpcResult<U256>;
121
122    /// Revert the state of the blockchain to a previous snapshot.
123    /// Takes a single parameter, which is the snapshot id to revert to.
124    #[method(name = "revert")]
125    async fn anvil_revert(&self, id: U256) -> RpcResult<bool>;
126
127    /// Jump forward in time by the given amount of time, in seconds.
128    #[method(name = "increaseTime")]
129    async fn anvil_increase_time(&self, seconds: U256) -> RpcResult<i64>;
130
131    /// Similar to `evm_increaseTime` but takes the exact timestamp that you want in the next block.
132    #[method(name = "setNextBlockTimestamp")]
133    async fn anvil_set_next_block_timestamp(&self, seconds: u64) -> RpcResult<()>;
134
135    /// Sets the next block gas limit.
136    #[method(name = "setBlockGasLimit")]
137    async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> RpcResult<bool>;
138
139    /// Sets an interval for the block timestamp.
140    #[method(name = "setBlockTimestampInterval")]
141    async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> RpcResult<()>;
142
143    /// Sets an interval for the block timestamp.
144    #[method(name = "removeBlockTimestampInterval")]
145    async fn anvil_remove_block_timestamp_interval(&self) -> RpcResult<bool>;
146
147    /// Mine blocks, instantly and return the mined blocks.
148    ///
149    /// This will mine the blocks regardless of the configured mining mode.
150    ///
151    /// **Note**: This behaves exactly as `evm_mine` but returns different output, for
152    /// compatibility reasons, this is a separate call since `evm_mine` is not an anvil original.
153    /// and `ganache` may change the `0x0` placeholder.
154    #[method(name = "mine_detailed")] // This method requires using `snake_case`.
155    async fn anvil_mine_detailed(&self, opts: Option<MineOptions>) -> RpcResult<Vec<Block>>;
156
157    /// Turn on call traces for transactions that are returned to the user when they execute a
158    /// transaction (instead of just txhash/receipt).
159    #[method(name = "enableTraces")]
160    async fn anvil_enable_traces(&self) -> RpcResult<()>;
161
162    /// Removes all transactions for that address from the transaction pool.
163    #[method(name = "removePoolTransactions")]
164    async fn anvil_remove_pool_transactions(&self, address: Address) -> RpcResult<()>;
165}