onemoney_protocol/api/states.rs
1//! State-related API operations.
2
3use crate::client::Client;
4use crate::client::config::api_path;
5use crate::client::config::endpoints::states::LATEST_EPOCH_CHECKPOINT;
6use crate::{Result, responses::LatestStateResponse};
7
8impl Client {
9 /// Get the latest epoch and checkpoint information.
10 ///
11 /// This is commonly used when creating transactions to get the required
12 /// recent_epoch and recent_checkpoint values.
13 ///
14 /// # Returns
15 ///
16 /// The latest state information.
17 ///
18 /// # Example
19 ///
20 /// ```rust,no_run
21 /// use onemoney_protocol::Client;
22 ///
23 /// #[tokio::main]
24 /// async fn main() -> Result<(), Box<dyn std::error::Error>> {
25 /// let client = Client::mainnet()?;
26 ///
27 /// let state = client.get_latest_epoch_checkpoint().await?;
28 /// println!("Latest state: epoch {} checkpoint {}", state.epoch, state.checkpoint);
29 ///
30 /// // Use in transaction payload
31 /// let recent_epoch = state.epoch;
32 /// let recent_checkpoint = state.checkpoint;
33 ///
34 /// Ok(())
35 /// }
36 /// ```
37 pub async fn get_latest_epoch_checkpoint(&self) -> Result<LatestStateResponse> {
38 self.get(&api_path(LATEST_EPOCH_CHECKPOINT)).await
39 }
40}
41
42#[cfg(test)]
43mod tests {
44 use super::*;
45
46 #[test]
47 fn test_latest_state_response_structure() {
48 let state = LatestStateResponse {
49 epoch: 123,
50 checkpoint: 456,
51 };
52
53 let json = serde_json::to_string(&state).expect("Test data should be valid");
54 let deserialized: LatestStateResponse =
55 serde_json::from_str(&json).expect("Test data should be valid");
56
57 assert_eq!(state.epoch, deserialized.epoch);
58 assert_eq!(state.checkpoint, deserialized.checkpoint);
59 }
60}