cosmos_client/client/
params.rs

1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::params::v1beta1::{QueryParamsRequest, QueryParamsResponse};
4use prost::Message;
5use std::rc::Rc;
6use tendermint::abci::Code;
7use tendermint_rpc::{Client, HttpClient};
8
9pub struct Module {
10    rpc: Rc<HttpClient>,
11}
12
13impl Module {
14    pub fn new(rpc: Rc<HttpClient>) -> Self {
15        Module { rpc }
16    }
17
18    /// # Errors
19    ///
20    /// Will return `Err` if :
21    /// - a prost encode / decode fail
22    /// - the json-rpc return an error code
23    /// - if there is some network error
24    pub async fn params(
25        &self,
26        subspace: &str,
27        key: &str,
28    ) -> Result<QueryParamsResponse, CosmosClient> {
29        let query = QueryParamsRequest {
30            subspace: subspace.to_string(),
31            key: key.to_string(),
32        };
33        let query = self
34            .rpc
35            .abci_query(
36                Some("/cosmos.params.v1beta1.Query/Params".to_string()),
37                query.encode_to_vec(),
38                None,
39                false,
40            )
41            .await?;
42
43        if query.code != Code::Ok {
44            return Err(RpcError(query.log));
45        }
46        QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
47    }
48}