onemoney_protocol/api/
chains.rs

1//! Chain-related API operations.
2
3use crate::Result;
4use crate::client::Client;
5use crate::client::config::api_path;
6use crate::client::config::endpoints::chains::CHAIN_ID;
7use crate::responses::ChainIdResponse;
8
9impl Client {
10    /// Get the current chain ID.
11    ///
12    /// # Returns
13    ///
14    /// The chain ID.
15    ///
16    /// # Example
17    ///
18    /// ```rust,no_run
19    /// use onemoney_protocol::Client;
20    ///
21    /// #[tokio::main]
22    /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
23    ///     let client = Client::mainnet()?;
24    ///
25    ///     let chain_id = client.get_chain_id().await?;
26    ///     println!("Current chain ID: {}", chain_id);
27    ///
28    ///     Ok(())
29    /// }
30    /// ```
31    pub async fn get_chain_id(&self) -> Result<u64> {
32        let response: ChainIdResponse = self.get(&api_path(CHAIN_ID)).await?;
33        Ok(response.chain_id)
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn test_chain_id_response_structure() {
43        // Test that ChainIdResponse can be serialized/deserialized
44        let chain_id_response = ChainIdResponse { chain_id: 1212101 };
45
46        let json = serde_json::to_string(&chain_id_response).expect("Test data should be valid");
47        let deserialized: ChainIdResponse =
48            serde_json::from_str(&json).expect("Test data should be valid");
49
50        assert_eq!(chain_id_response.chain_id, deserialized.chain_id);
51    }
52}