reth_rpc/
web3.rs

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