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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crate::error::CosmosClientError;
use cosmos_sdk_proto::cosmos::authz::v1beta1::{
    QueryGranteeGrantsRequest, QueryGranteeGrantsResponse, QueryGranterGrantsRequest,
    QueryGranterGrantsResponse, QueryGrantsRequest, QueryGrantsResponse,
};
use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
use prost::Message;
use std::rc::Rc;
use tendermint_rpc::{Client, HttpClient};

pub struct AuthzModule {
    rpc: Rc<HttpClient>,
}

impl AuthzModule {
    pub fn new(rpc: Rc<HttpClient>) -> Self {
        AuthzModule { rpc }
    }

    pub async fn grants(
        &self,
        granter: &str,
        grantee: &str,
        msg_type_url: &str,
        pagination: Option<PageRequest>,
    ) -> Result<QueryGrantsResponse, CosmosClientError> {
        let query = QueryGrantsRequest {
            granter: granter.to_string(),
            grantee: grantee.to_string(),
            msg_type_url: msg_type_url.to_string(),
            pagination,
        };
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.authz.v1beta1.Query/Grants".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QueryGrantsResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }

    pub async fn granter_grants(
        &self,
        granter: &str,
        pagination: Option<PageRequest>,
    ) -> Result<QueryGranterGrantsResponse, CosmosClientError> {
        let query = QueryGranterGrantsRequest {
            granter: granter.to_string(),
            pagination,
        };
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.authz.v1beta1.Query/GranterGrants".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QueryGranterGrantsResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }

    pub async fn grantee_grants(
        &self,
        grantee: &str,
        pagination: Option<PageRequest>,
    ) -> Result<QueryGranteeGrantsResponse, CosmosClientError> {
        let query = QueryGranteeGrantsRequest {
            grantee: grantee.to_string(),
            pagination,
        };
        let query = self
            .rpc
            .abci_query(
                Some("/cosmos.authz.v1beta1.Query/GranteeGrants".to_string()),
                query.encode_to_vec(),
                None,
                false,
            )
            .await
            .unwrap();

        let resp = QueryGranteeGrantsResponse::decode(query.value.as_slice())?;
        Ok(resp)
    }
}