koios-sdk 0.1.1

A Rust SDK for the Koios Cardano API
Documentation
use crate::{
    error::Result,
    models::network::{CliProtocolParams, Genesis, ParamUpdate, ReserveWithdrawal, Tip, Totals},
    types::EpochNo,
    Client,
};
use urlencoding::encode;

impl Client {
    /// Get the tip info about the latest block seen by chain
    pub async fn get_tip(&self) -> Result<Vec<Tip>> {
        self.get("/tip").await
    }

    /// Get the Genesis parameters used to start specific era on chain
    pub async fn get_genesis(&self) -> Result<Vec<Genesis>> {
        self.get("/genesis").await
    }

    /// Get the circulating utxo, treasury, rewards, supply and reserves in
    /// lovelace for specified epoch, all epochs if empty
    pub async fn get_totals(&self, epoch_no: Option<EpochNo>) -> Result<Vec<Totals>> {
        let endpoint = match epoch_no {
            Some(epoch) => format!("/totals?_epoch_no={}", encode(epoch.value())),
            None => "/totals".to_string(),
        };
        self.get(&endpoint).await
    }

    /// Get all parameter update proposals submitted to the chain starting Shelley era
    pub async fn get_param_updates(&self) -> Result<Vec<ParamUpdate>> {
        self.get("/param_updates").await
    }

    /// Get Current Protocol Parameters as published by cardano-cli
    pub async fn get_cli_protocol_params(&self) -> Result<Vec<CliProtocolParams>> {
        self.get("/cli_protocol_params").await
    }

    /// List of all withdrawals from reserves against stake accounts
    pub async fn get_reserve_withdrawals(&self) -> Result<Vec<ReserveWithdrawal>> {
        self.get("/reserve_withdrawals").await
    }

    /// List of all withdrawals from treasury against stake accounts
    pub async fn get_treasury_withdrawals(&self) -> Result<Vec<ReserveWithdrawal>> {
        self.get("/treasury_withdrawals").await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio;

    #[tokio::test]
    async fn test_get_tip() {
        let client = Client::new().unwrap();
        let result = client.get_tip().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_genesis() {
        let client = Client::new().unwrap();
        let result = client.get_genesis().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_get_totals() {
        let client = Client::new().unwrap();
        let result = client.get_totals(None).await;
        assert!(result.is_ok());

        let result = client.get_totals(Some(EpochNo::new("320"))).await;
        assert!(result.is_ok());
    }

    // Add more tests for other endpoints...
}