cosmos_client/client/
authz.rs

1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::authz::v1beta1::{
4    QueryGranteeGrantsRequest, QueryGranteeGrantsResponse, QueryGranterGrantsRequest,
5    QueryGranterGrantsResponse, QueryGrantsRequest, QueryGrantsResponse,
6};
7use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
8use prost::Message;
9use std::rc::Rc;
10use tendermint::abci::Code;
11use tendermint_rpc::{Client, HttpClient};
12
13pub struct Module {
14    rpc: Rc<HttpClient>,
15}
16
17impl Module {
18    pub fn new(rpc: Rc<HttpClient>) -> Self {
19        Module { rpc }
20    }
21
22    /// # Errors
23    ///
24    /// Will return `Err` if :
25    /// - a prost encode / decode fail
26    /// - the json-rpc return an error code
27    /// - if there is some network error
28    #[allow(clippy::similar_names)]
29    pub async fn grants(
30        &self,
31        granter: &str,
32        grantee: &str,
33        msg_type_url: &str,
34        pagination: Option<PageRequest>,
35    ) -> Result<QueryGrantsResponse, CosmosClient> {
36        let query = QueryGrantsRequest {
37            granter: granter.to_string(),
38            grantee: grantee.to_string(),
39            msg_type_url: msg_type_url.to_string(),
40            pagination,
41        };
42        let query = self
43            .rpc
44            .abci_query(
45                Some("/cosmos.authz.v1beta1.Query/Grants".to_string()),
46                query.encode_to_vec(),
47                None,
48                false,
49            )
50            .await?;
51
52        if query.code != Code::Ok {
53            return Err(RpcError(query.log));
54        }
55        QueryGrantsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
56    }
57
58    /// # Errors
59    ///
60    /// Will return `Err` if :
61    /// - a prost encode / decode fail
62    /// - the json-rpc return an error code
63    /// - if there is some network error
64    pub async fn granter_grants(
65        &self,
66        granter: &str,
67        pagination: Option<PageRequest>,
68    ) -> Result<QueryGranterGrantsResponse, CosmosClient> {
69        let query = QueryGranterGrantsRequest {
70            granter: granter.to_string(),
71            pagination,
72        };
73        let query = self
74            .rpc
75            .abci_query(
76                Some("/cosmos.authz.v1beta1.Query/GranterGrants".to_string()),
77                query.encode_to_vec(),
78                None,
79                false,
80            )
81            .await?;
82
83        if query.code != Code::Ok {
84            return Err(RpcError(query.log));
85        }
86        QueryGranterGrantsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
87    }
88
89    /// # Errors
90    ///
91    /// Will return `Err` if :
92    /// - a prost encode / decode fail
93    /// - the json-rpc return an error code
94    /// - if there is some network error
95    pub async fn grantee_grants(
96        &self,
97        grantee: &str,
98        pagination: Option<PageRequest>,
99    ) -> Result<QueryGranteeGrantsResponse, CosmosClient> {
100        let query = QueryGranteeGrantsRequest {
101            grantee: grantee.to_string(),
102            pagination,
103        };
104        let query = self
105            .rpc
106            .abci_query(
107                Some("/cosmos.authz.v1beta1.Query/GranteeGrants".to_string()),
108                query.encode_to_vec(),
109                None,
110                false,
111            )
112            .await?;
113
114        if query.code != Code::Ok {
115            return Err(RpcError(query.log));
116        }
117        QueryGranteeGrantsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
118    }
119}