1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! API client for the [Mintscan] Cosmos explorer by [Cosmostation].
//!
//! [Mintscan]: https://www.mintscan.io/
//! [Cosmostation]: https://www.cosmostation.io/

#![doc(html_root_url = "https://docs.rs/mintscan/0.0.1")]
#![forbid(unsafe_code, clippy::unwrap_used)]
#![warn(missing_docs, rust_2018_idioms, unused_qualifications)]

pub mod coin;
pub mod v1;

mod deserializers;

pub use self::coin::Coin;
pub use tendermint;

use iqhttp::{HttpsClient, Result};

/// Bech32-encoded address.
// TODO(tarcieri): use `cosmos_sdk`'s `AccountId` or upstream it to `tendermint`?
pub type Address = String;

/// Validator rates.
// TODO(tarcieri): real decimal type for this? (e.g. "0.100000000000000000")
pub type Rate = String;

/// Mintscan API client.
pub struct Mintscan {
    /// HTTP client.
    client: HttpsClient,
}

impl Mintscan {
    /// Create a new Mintscan client for the given API hostname
    /// (e.g. `api.cosmostation.io`)
    pub fn new(hostname: impl Into<String>) -> Self {
        Self {
            client: HttpsClient::new(hostname),
        }
    }

    /// Get `/v1/status` endpoint.
    pub async fn status(&self) -> Result<v1::Status> {
        self.client
            .get_json("/v1/status", &Default::default())
            .await
    }

    /// Get `/v1/staking/validator` endpoint.
    ///
    /// Accepts a Bech32-encoded account address for the validator.
    pub async fn validator(&self, addr: impl Into<Address>) -> Result<v1::staking::Validator> {
        // TODO(tarcieri): path construction with proper escaping
        let path = format!("/v1/staking/validator/{}", &addr.into());
        self.client.get_json(&path, &Default::default()).await
    }

    /// Get `/v1/staking/validator/uptime` endpoint.
    ///
    /// Accepts a Bech32-encoded account address for the validator.
    pub async fn validator_uptime(
        &self,
        addr: impl Into<Address>,
    ) -> Result<v1::staking::validator::Uptime> {
        // TODO(tarcieri): path construction with proper escaping
        let path = format!("/v1/staking/validator/uptime/{}", &addr.into());
        self.client.get_json(&path, &Default::default()).await
    }
}

impl From<HttpsClient> for Mintscan {
    fn from(client: HttpsClient) -> Mintscan {
        Mintscan { client }
    }
}