Skip to main content

reth_rpc/
web3.rs

1use alloy_primitives::{keccak256, Bytes, B256};
2use jsonrpsee::core::RpcResult;
3use reth_network_api::NetworkInfo;
4use reth_rpc_api::Web3ApiServer;
5use reth_rpc_server_types::ToRpcResult;
6
7/// `web3` API implementation.
8///
9/// This type provides the functionality for handling `web3` related requests.
10pub struct Web3Api<N> {
11    /// An interface to interact with the network
12    network: N,
13}
14
15impl<N> Web3Api<N> {
16    /// Creates a new instance of `Web3Api`.
17    pub const fn new(network: N) -> Self {
18        Self { network }
19    }
20}
21
22impl<N> Web3ApiServer for Web3Api<N>
23where
24    N: NetworkInfo + 'static,
25{
26    /// Handler for `web3_clientVersion`
27    async fn client_version(&self) -> RpcResult<String> {
28        let status = self.network.network_status().await.to_rpc_result()?;
29        Ok(status.client_version)
30    }
31
32    /// Handler for `web3_sha3`
33    fn sha3(&self, input: Bytes) -> RpcResult<B256> {
34        Ok(keccak256(input))
35    }
36}
37
38impl<N> std::fmt::Debug for Web3Api<N> {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        f.debug_struct("Web3Api").finish_non_exhaustive()
41    }
42}