Skip to main content

cw_orch_daemon/queriers/
gov.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 Gov {
11    pub channel: Channel,
12    pub rt_handle: Option<Handle>,
13}
14
15impl Gov {
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 Gov {
32    type Error = DaemonError;
33}
34
35impl QuerierGetter<Gov> for Daemon {
36    fn querier(&self) -> Gov {
37        Gov::new(self)
38    }
39}
40
41impl Gov {
42    /// Query proposal details by proposal id
43    pub async fn _proposal(
44        &self,
45        proposal_id: u64,
46    ) -> Result<cosmos_modules::gov::Proposal, DaemonError> {
47        let proposal: cosmos_modules::gov::QueryProposalResponse = cosmos_query!(
48            self,
49            gov,
50            proposal,
51            QueryProposalRequest {
52                proposal_id: proposal_id,
53            }
54        );
55        Ok(proposal.proposal.unwrap())
56    }
57
58    /// Query proposals based on given status
59    ///
60    /// see [PageRequest] for pagination
61    pub async fn _proposals(
62        &self,
63        proposal_status: GovProposalStatus,
64        voter: &Addr,
65        depositor: &Addr,
66        pagination: Option<PageRequest>,
67    ) -> Result<cosmos_modules::gov::QueryProposalsResponse, DaemonError> {
68        let proposals: cosmos_modules::gov::QueryProposalsResponse = cosmos_query!(
69            self,
70            gov,
71            proposals,
72            QueryProposalsRequest {
73                proposal_status: proposal_status as i32,
74                voter: voter.to_string(),
75                depositor: depositor.to_string(),
76                pagination: pagination
77            }
78        );
79        Ok(proposals)
80    }
81
82    /// Query voted information based on proposal_id for voter address
83    pub async fn _vote(
84        &self,
85        proposal_id: u64,
86        voter: &Addr,
87    ) -> Result<cosmos_modules::gov::Vote, DaemonError> {
88        let vote: cosmos_modules::gov::QueryVoteResponse = cosmos_query!(
89            self,
90            gov,
91            vote,
92            QueryVoteRequest {
93                proposal_id: proposal_id,
94                voter: voter.to_string()
95            }
96        );
97        Ok(vote.vote.unwrap())
98    }
99
100    /// Query votes of a given proposal
101    ///
102    /// see [PageRequest] for pagination
103    pub async fn _votes(
104        &self,
105        proposal_id: impl Into<u64>,
106        pagination: Option<PageRequest>,
107    ) -> Result<cosmos_modules::gov::QueryVotesResponse, DaemonError> {
108        let votes: cosmos_modules::gov::QueryVotesResponse = cosmos_query!(
109            self,
110            gov,
111            votes,
112            QueryVotesRequest {
113                proposal_id: proposal_id.into(),
114                pagination: pagination
115            }
116        );
117        Ok(votes)
118    }
119
120    /// Query all parameters of the gov module
121    pub async fn _params(
122        &self,
123        params_type: impl Into<String>,
124    ) -> Result<cosmos_modules::gov::QueryParamsResponse, DaemonError> {
125        let params: cosmos_modules::gov::QueryParamsResponse = cosmos_query!(
126            self,
127            gov,
128            params,
129            QueryParamsRequest {
130                params_type: params_type.into()
131            }
132        );
133        Ok(params)
134    }
135
136    /// Query deposit information using proposal_id and depositor address
137    pub async fn _deposit(
138        &self,
139        proposal_id: u64,
140        depositor: impl Into<String>,
141    ) -> Result<cosmos_modules::gov::Deposit, DaemonError> {
142        let deposit: cosmos_modules::gov::QueryDepositResponse = cosmos_query!(
143            self,
144            gov,
145            deposit,
146            QueryDepositRequest {
147                proposal_id: proposal_id,
148                depositor: depositor.into()
149            }
150        );
151        Ok(deposit.deposit.unwrap())
152    }
153
154    /// Query deposits of a proposal
155    ///
156    /// see [PageRequest] for pagination
157    pub async fn _deposits(
158        &self,
159        proposal_id: u64,
160        pagination: Option<PageRequest>,
161    ) -> Result<cosmos_modules::gov::QueryDepositsResponse, DaemonError> {
162        let deposits: cosmos_modules::gov::QueryDepositsResponse = cosmos_query!(
163            self,
164            gov,
165            deposits,
166            QueryDepositsRequest {
167                proposal_id: proposal_id,
168                pagination: pagination
169            }
170        );
171        Ok(deposits)
172    }
173
174    /// TallyResult queries the tally of a proposal vote.
175    pub async fn _tally_result(
176        &mut self,
177        proposal_id: u64,
178    ) -> Result<cosmos_modules::gov::TallyResult, DaemonError> {
179        let tally_result: cosmos_modules::gov::QueryTallyResultResponse = cosmos_query!(
180            self,
181            gov,
182            tally_result,
183            QueryTallyResultRequest {
184                proposal_id: proposal_id,
185            }
186        );
187        Ok(tally_result.tally.unwrap())
188    }
189}
190
191/// Proposal status
192#[allow(missing_docs)]
193pub enum GovProposalStatus {
194    Unspecified = 0,
195    DepositPeriod = 1,
196    VotingPeriod = 2,
197    Passed = 3,
198    Rejected = 4,
199    Failed = 5,
200}