cosmos_client/client/
mint.rs

1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::mint::v1beta1::{
4    QueryAnnualProvisionsRequest, QueryAnnualProvisionsResponse, QueryInflationRequest,
5    QueryInflationResponse, QueryParamsRequest, QueryParamsResponse,
6};
7use prost::Message;
8use std::rc::Rc;
9use tendermint::abci::Code;
10use tendermint_rpc::{Client, HttpClient};
11
12pub struct Module {
13    rpc: Rc<HttpClient>,
14}
15
16impl Module {
17    pub fn new(rpc: Rc<HttpClient>) -> Self {
18        Module { rpc }
19    }
20
21    /// # Errors
22    ///
23    /// Will return `Err` if :
24    /// - a prost encode / decode fail
25    /// - the json-rpc return an error code
26    /// - if there is some network error
27    pub async fn annual_provisions(&self) -> Result<QueryAnnualProvisionsResponse, CosmosClient> {
28        let query = QueryAnnualProvisionsRequest {};
29        let query = self
30            .rpc
31            .abci_query(
32                Some("/cosmos.mint.v1beta1.Query/AnnualProvisions".to_string()),
33                query.encode_to_vec(),
34                None,
35                false,
36            )
37            .await?;
38
39        if query.code != Code::Ok {
40            return Err(RpcError(query.log));
41        }
42        QueryAnnualProvisionsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
43    }
44
45    /// # Errors
46    ///
47    /// Will return `Err` if :
48    /// - a prost encode / decode fail
49    /// - the json-rpc return an error code
50    /// - if there is some network error
51    pub async fn inflation(&self) -> Result<QueryInflationResponse, CosmosClient> {
52        let query = QueryInflationRequest {};
53        let query = self
54            .rpc
55            .abci_query(
56                Some("/cosmos.mint.v1beta1.Query/Inflation".to_string()),
57                query.encode_to_vec(),
58                None,
59                false,
60            )
61            .await?;
62
63        if query.code != Code::Ok {
64            return Err(RpcError(query.log));
65        }
66        QueryInflationResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
67    }
68
69    /// # Errors
70    ///
71    /// Will return `Err` if :
72    /// - a prost encode / decode fail
73    /// - the json-rpc return an error code
74    /// - if there is some network error
75    pub async fn params(&self) -> Result<QueryParamsResponse, CosmosClient> {
76        let query = QueryParamsRequest {};
77        let query = self
78            .rpc
79            .abci_query(
80                Some("/cosmos.mint.v1beta1.Query/Params".to_string()),
81                query.encode_to_vec(),
82                None,
83                false,
84            )
85            .await?;
86
87        if query.code != Code::Ok {
88            return Err(RpcError(query.log));
89        }
90        QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
91    }
92}