Skip to main content

cw_orch_daemon/queriers/
feegrant.rs

1use crate::{cosmos_modules, error::DaemonError, Daemon};
2use cosmrs::proto::cosmos::base::query::v1beta1::PageRequest;
3use cosmwasm_std::Addr;
4use cw_orch_core::environment::{Querier, QuerierGetter};
5use tokio::runtime::Handle;
6use tonic::transport::Channel;
7
8/// Querier for the Cosmos Gov module
9/// All the async function are prefixed with `_`
10pub struct FeeGrant {
11    pub channel: Channel,
12    pub rt_handle: Option<Handle>,
13}
14
15impl FeeGrant {
16    pub fn new(daemon: &Daemon) -> Self {
17        Self {
18            channel: daemon.channel(),
19            rt_handle: Some(daemon.rt_handle.clone()),
20        }
21    }
22
23    pub fn new_async(channel: Channel) -> Self {
24        Self {
25            channel,
26            rt_handle: None,
27        }
28    }
29}
30
31impl Querier for FeeGrant {
32    type Error = DaemonError;
33}
34
35impl QuerierGetter<FeeGrant> for Daemon {
36    fn querier(&self) -> FeeGrant {
37        FeeGrant::new(self)
38    }
39}
40
41impl FeeGrant {
42    /// Query all allowances granted to the grantee address by a granter address
43    pub async fn _allowance(
44        &self,
45        granter: &Addr,
46        grantee: &Addr,
47    ) -> Result<cosmos_modules::feegrant::Grant, DaemonError> {
48        let allowance: cosmos_modules::feegrant::QueryAllowanceResponse = cosmos_query!(
49            self,
50            feegrant,
51            allowance,
52            QueryAllowanceRequest {
53                granter: granter.to_string(),
54                grantee: grantee.to_string(),
55            }
56        );
57        Ok(allowance.allowance.unwrap())
58    }
59
60    /// Query allowances for grantee address with a given pagination
61    ///
62    /// see [PageRequest] for pagination
63    pub async fn _allowances(
64        &self,
65        grantee: &Addr,
66        pagination: Option<PageRequest>,
67    ) -> Result<Vec<cosmos_modules::feegrant::Grant>, DaemonError> {
68        let allowances: cosmos_modules::feegrant::QueryAllowancesResponse = cosmos_query!(
69            self,
70            feegrant,
71            allowances,
72            QueryAllowancesRequest {
73                grantee: grantee.to_string(),
74                pagination: pagination
75            }
76        );
77        Ok(allowances.allowances)
78    }
79}