Skip to main content

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