alloy_rpc_types/
rpc.rs

1//! Types for the `rpc` API.
2
3use alloy_primitives::map::HashMap;
4use serde::{Deserialize, Serialize};
5
6/// Represents the `rpc_modules` response, which returns the
7/// list of all available modules on that transport and their version
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
9#[serde(transparent)]
10pub struct RpcModules {
11    module_map: HashMap<String, String>,
12}
13
14impl RpcModules {
15    /// Create a new instance of `RPCModules`
16    pub const fn new(module_map: HashMap<String, String>) -> Self {
17        Self { module_map }
18    }
19
20    /// Consumes self and returns the inner hashmap mapping module names to their versions
21    pub fn into_modules(self) -> HashMap<String, String> {
22        self.module_map
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use similar_asserts::assert_eq;
30    #[test]
31    fn test_parse_module_versions_roundtrip() {
32        let s = r#"{"txpool":"1.0","trace":"1.0","eth":"1.0","web3":"1.0","net":"1.0"}"#;
33        let module_map = HashMap::from_iter([
34            ("txpool".to_owned(), "1.0".to_owned()),
35            ("trace".to_owned(), "1.0".to_owned()),
36            ("eth".to_owned(), "1.0".to_owned()),
37            ("web3".to_owned(), "1.0".to_owned()),
38            ("net".to_owned(), "1.0".to_owned()),
39        ]);
40        let m = RpcModules::new(module_map);
41        let de_serialized: RpcModules = serde_json::from_str(s).unwrap();
42        assert_eq!(de_serialized, m);
43    }
44}