ic_web3_rs/api/
net.rs

1//! `Net` namespace
2
3use crate::{api::Namespace, helpers::CallFuture, transports::ic_http_client::CallOptions, types::U256, Transport};
4
5/// `Net` namespace
6#[derive(Debug, Clone)]
7pub struct Net<T> {
8    transport: T,
9}
10
11impl<T: Transport> Namespace<T> for Net<T> {
12    fn new(transport: T) -> Self
13    where
14        Self: Sized,
15    {
16        Net { transport }
17    }
18
19    fn transport(&self) -> &T {
20        &self.transport
21    }
22}
23
24impl<T: Transport> Net<T> {
25    /// Returns the network id.
26    pub fn version(&self, options: CallOptions) -> CallFuture<String, T::Out> {
27        CallFuture::new(self.transport.execute("net_version", vec![], options))
28    }
29
30    /// Returns number of peers connected to node.
31    pub fn peer_count(&self, options: CallOptions) -> CallFuture<U256, T::Out> {
32        CallFuture::new(self.transport.execute("net_peerCount", vec![], options))
33    }
34
35    /// Whether the node is listening for network connections
36    pub fn is_listening(&self, options: CallOptions) -> CallFuture<bool, T::Out> {
37        CallFuture::new(self.transport.execute("net_listening", vec![], options))
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::Net;
44    use crate::{api::Namespace, rpc::Value, transports::ic_http_client::CallOptions, types::U256};
45
46    rpc_test! (
47      Net:version,CallOptions::default() => "net_version", Vec::<String>::new();
48      Value::String("Test123".into()) => "Test123"
49    );
50
51    rpc_test! (
52      Net:peer_count,CallOptions::default() => "net_peerCount",Vec::<String>::new();
53      Value::String("0x123".into()) => U256::from(0x123)
54    );
55
56    rpc_test! (
57      Net:is_listening,CallOptions::default() => "net_listening",Vec::<String>::new();
58      Value::Bool(true) => true
59    );
60}