use alloy_primitives::U64;
use jsonrpsee::core::RpcResult as Result;
use reth_network_api::PeersInfo;
use reth_rpc_api::NetApiServer;
use reth_rpc_eth_api::helpers::EthApiSpec;
pub struct NetApi<Net, Eth> {
network: Net,
eth: Eth,
}
impl<Net, Eth> NetApi<Net, Eth> {
pub const fn new(network: Net, eth: Eth) -> Self {
Self { network, eth }
}
}
impl<Net, Eth> NetApiServer for NetApi<Net, Eth>
where
Net: PeersInfo + 'static,
Eth: EthApiSpec + 'static,
{
fn version(&self) -> Result<String> {
Ok(self.eth.chain_id().to::<u64>().to_string())
}
fn peer_count(&self) -> Result<U64> {
Ok(U64::from(self.network.num_connected_peers()))
}
fn is_listening(&self) -> Result<bool> {
Ok(true)
}
}
impl<Net, Eth> std::fmt::Debug for NetApi<Net, Eth> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NetApi").finish_non_exhaustive()
}
}