1use jsonrpsee::{core::RpcResult, proc_macros::rpc};
23use alloy_primitives::{Address, Bytes, B256, U256};
4use alloy_rpc_types_anvil::{Forking, Metadata, MineOptions, NodeInfo};
5use alloy_rpc_types_eth::Block;
67/// 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")]
14async fn anvil_impersonate_account(&self, address: Address) -> RpcResult<()>;
1516/// Stops impersonating an account if previously set with `anvil_impersonateAccount`.
17#[method(name = "stopImpersonatingAccount")]
18async fn anvil_stop_impersonating_account(&self, address: Address) -> RpcResult<()>;
1920/// If set to true will make every account impersonated.
21#[method(name = "autoImpersonateAccount")]
22async fn anvil_auto_impersonate_account(&self, enabled: bool) -> RpcResult<()>;
2324/// Returns `true` if auto mining is enabled, and `false`.
25#[method(name = "getAutomine")]
26async fn anvil_get_automine(&self) -> RpcResult<bool>;
2728/// Mines a series of blocks.
29#[method(name = "mine")]
30async fn anvil_mine(&self, blocks: Option<U256>, interval: Option<U256>) -> RpcResult<()>;
3132/// 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")]
35async fn anvil_set_automine(&self, enabled: bool) -> RpcResult<()>;
3637/// Sets the mining behavior to interval with the given interval (seconds).
38#[method(name = "setIntervalMining")]
39async fn anvil_set_interval_mining(&self, interval: u64) -> RpcResult<()>;
4041/// Removes transactions from the pool.
42#[method(name = "anvil_dropTransaction")]
43async fn anvil_drop_transaction(&self, tx_hash: B256) -> RpcResult<Option<B256>>;
4445/// 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")]
49async fn anvil_reset(&self, fork: Option<Forking>) -> RpcResult<()>;
5051/// Sets the backend rpc url.
52#[method(name = "setRpcUrl")]
53async fn anvil_set_rpc_url(&self, url: String) -> RpcResult<()>;
5455/// Modifies the balance of an account.
56#[method(name = "setBalance")]
57async fn anvil_set_balance(&self, address: Address, balance: U256) -> RpcResult<()>;
5859/// Sets the code of a contract.
60#[method(name = "setCode")]
61async fn anvil_set_code(&self, address: Address, code: Bytes) -> RpcResult<()>;
6263/// Sets the nonce of an address.
64#[method(name = "setNonce")]
65async fn anvil_set_nonce(&self, address: Address, nonce: U256) -> RpcResult<()>;
6667/// Writes a single slot of the account's storage.
68#[method(name = "setStorageAt")]
69async fn anvil_set_storage_at(
70&self,
71 address: Address,
72 slot: U256,
73 value: B256,
74 ) -> RpcResult<bool>;
7576/// Sets the coinbase address.
77#[method(name = "setCoinbase")]
78async fn anvil_set_coinbase(&self, address: Address) -> RpcResult<()>;
7980/// Sets the chain id.
81#[method(name = "setChainId")]
82async fn anvil_set_chain_id(&self, chain_id: u64) -> RpcResult<()>;
8384/// Enables or disable logging.
85#[method(name = "setLoggingEnabled")]
86async fn anvil_set_logging_enabled(&self, enabled: bool) -> RpcResult<()>;
8788/// Sets the minimum gas price for the node.
89#[method(name = "setMinGasPrice")]
90async fn anvil_set_min_gas_price(&self, gas_price: U256) -> RpcResult<()>;
9192/// Sets the base fee of the next block.
93#[method(name = "setNextBlockBaseFeePerGas")]
94async fn anvil_set_next_block_base_fee_per_gas(&self, base_fee: U256) -> RpcResult<()>;
9596/// Sets the minimum gas price for the node.
97#[method(name = "setTime")]
98async fn anvil_set_time(&self, timestamp: u64) -> RpcResult<u64>;
99100/// 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")]
103async fn anvil_dump_state(&self) -> RpcResult<Bytes>;
104105/// Append chain state buffer to current chain. Will overwrite any conflicting addresses or
106 /// storage.
107#[method(name = "loadState")]
108async fn anvil_load_state(&self, state: Bytes) -> RpcResult<bool>;
109110/// Retrieves the Anvil node configuration params.
111#[method(name = "nodeInfo")]
112async fn anvil_node_info(&self) -> RpcResult<NodeInfo>;
113114/// Retrieves metadata about the Anvil instance.
115#[method(name = "metadata")]
116async fn anvil_metadata(&self) -> RpcResult<Metadata>;
117118/// Snapshot the state of the blockchain at the current block.
119#[method(name = "snapshot")]
120async fn anvil_snapshot(&self) -> RpcResult<U256>;
121122/// 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")]
125async fn anvil_revert(&self, id: U256) -> RpcResult<bool>;
126127/// Jump forward in time by the given amount of time, in seconds.
128#[method(name = "increaseTime")]
129async fn anvil_increase_time(&self, seconds: U256) -> RpcResult<i64>;
130131/// Similar to `evm_increaseTime` but takes the exact timestamp that you want in the next block.
132#[method(name = "setNextBlockTimestamp")]
133async fn anvil_set_next_block_timestamp(&self, seconds: u64) -> RpcResult<()>;
134135/// Sets the next block gas limit.
136#[method(name = "setBlockGasLimit")]
137async fn anvil_set_block_gas_limit(&self, gas_limit: U256) -> RpcResult<bool>;
138139/// Sets an interval for the block timestamp.
140#[method(name = "setBlockTimestampInterval")]
141async fn anvil_set_block_timestamp_interval(&self, seconds: u64) -> RpcResult<()>;
142143/// Sets an interval for the block timestamp.
144#[method(name = "removeBlockTimestampInterval")]
145async fn anvil_remove_block_timestamp_interval(&self) -> RpcResult<bool>;
146147/// 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`.
155async fn anvil_mine_detailed(&self, opts: Option<MineOptions>) -> RpcResult<Vec<Block>>;
156157/// 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")]
160async fn anvil_enable_traces(&self) -> RpcResult<()>;
161162/// Removes all transactions for that address from the transaction pool.
163#[method(name = "removePoolTransactions")]
164async fn anvil_remove_pool_transactions(&self, address: Address) -> RpcResult<()>;
165}