koios_sdk/api/
network.rs

1use crate::{
2    error::Result,
3    models::network::{CliProtocolParams, Genesis, ParamUpdate, ReserveWithdrawal, Tip, Totals},
4    types::EpochNo,
5    Client,
6};
7use urlencoding::encode;
8
9impl Client {
10    /// Get the tip info about the latest block seen by chain
11    pub async fn get_tip(&self) -> Result<Vec<Tip>> {
12        self.get("/tip").await
13    }
14
15    /// Get the Genesis parameters used to start specific era on chain
16    pub async fn get_genesis(&self) -> Result<Vec<Genesis>> {
17        self.get("/genesis").await
18    }
19
20    /// Get the circulating utxo, treasury, rewards, supply and reserves in
21    /// lovelace for specified epoch, all epochs if empty
22    pub async fn get_totals(&self, epoch_no: Option<EpochNo>) -> Result<Vec<Totals>> {
23        let endpoint = match epoch_no {
24            Some(epoch) => format!("/totals?_epoch_no={}", encode(epoch.value())),
25            None => "/totals".to_string(),
26        };
27        self.get(&endpoint).await
28    }
29
30    /// Get all parameter update proposals submitted to the chain starting Shelley era
31    pub async fn get_param_updates(&self) -> Result<Vec<ParamUpdate>> {
32        self.get("/param_updates").await
33    }
34
35    /// Get Current Protocol Parameters as published by cardano-cli
36    pub async fn get_cli_protocol_params(&self) -> Result<Vec<CliProtocolParams>> {
37        self.get("/cli_protocol_params").await
38    }
39
40    /// List of all withdrawals from reserves against stake accounts
41    pub async fn get_reserve_withdrawals(&self) -> Result<Vec<ReserveWithdrawal>> {
42        self.get("/reserve_withdrawals").await
43    }
44
45    /// List of all withdrawals from treasury against stake accounts
46    pub async fn get_treasury_withdrawals(&self) -> Result<Vec<ReserveWithdrawal>> {
47        self.get("/treasury_withdrawals").await
48    }
49}
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54    use tokio;
55
56    #[tokio::test]
57    async fn test_get_tip() {
58        let client = Client::new().unwrap();
59        let result = client.get_tip().await;
60        assert!(result.is_ok());
61    }
62
63    #[tokio::test]
64    async fn test_get_genesis() {
65        let client = Client::new().unwrap();
66        let result = client.get_genesis().await;
67        assert!(result.is_ok());
68    }
69
70    #[tokio::test]
71    async fn test_get_totals() {
72        let client = Client::new().unwrap();
73        let result = client.get_totals(None).await;
74        assert!(result.is_ok());
75
76        let result = client.get_totals(Some(EpochNo::new("320"))).await;
77        assert!(result.is_ok());
78    }
79
80    // Add more tests for other endpoints...
81}