1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
4use cosmos_sdk_proto::cosmos::gov::v1beta1::{
5 QueryDepositRequest, QueryDepositResponse, QueryDepositsRequest, QueryDepositsResponse,
6 QueryParamsRequest, QueryParamsResponse, QueryProposalRequest, QueryProposalResponse,
7 QueryProposalsRequest, QueryProposalsResponse, QueryTallyResultRequest,
8 QueryTallyResultResponse, QueryVoteRequest, QueryVoteResponse, QueryVotesRequest,
9 QueryVotesResponse,
10};
11use prost::Message;
12use std::rc::Rc;
13use tendermint::abci::Code;
14use tendermint_rpc::{Client, HttpClient};
15
16pub struct Module {
17 rpc: Rc<HttpClient>,
18}
19
20impl Module {
21 pub fn new(rpc: Rc<HttpClient>) -> Self {
22 Module { rpc }
23 }
24
25 pub async fn proposal(&self, proposal_id: u64) -> Result<QueryProposalResponse, CosmosClient> {
32 let query = QueryProposalRequest { proposal_id };
33 let query = self
34 .rpc
35 .abci_query(
36 Some("/cosmos.gov.v1beta1.Query/Proposal".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 QueryProposalResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
47 }
48
49 pub async fn proposals(
56 &self,
57 proposal_status: i32,
58 voter: &str,
59 depositor: &str,
60 pagination: Option<PageRequest>,
61 ) -> Result<QueryProposalsResponse, CosmosClient> {
62 let query = QueryProposalsRequest {
63 proposal_status,
64 voter: voter.to_string(),
65 depositor: depositor.to_string(),
66 pagination,
67 };
68 let query = self
69 .rpc
70 .abci_query(
71 Some("/cosmos.gov.v1beta1.Query/Proposals".to_string()),
72 query.encode_to_vec(),
73 None,
74 false,
75 )
76 .await?;
77
78 if query.code != Code::Ok {
79 return Err(RpcError(query.log));
80 }
81 QueryProposalsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
82 }
83
84 pub async fn vote(
91 &self,
92 proposal_id: u64,
93 voter: &str,
94 ) -> Result<QueryVoteResponse, CosmosClient> {
95 let query = QueryVoteRequest {
96 proposal_id,
97 voter: voter.to_string(),
98 };
99 let query = self
100 .rpc
101 .abci_query(
102 Some("/cosmos.gov.v1beta1.Query/Vote".to_string()),
103 query.encode_to_vec(),
104 None,
105 false,
106 )
107 .await?;
108
109 if query.code != Code::Ok {
110 return Err(RpcError(query.log));
111 }
112 QueryVoteResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
113 }
114
115 pub async fn votes(
122 &self,
123 proposal_id: u64,
124 pagination: Option<PageRequest>,
125 ) -> Result<QueryVotesResponse, CosmosClient> {
126 let query = QueryVotesRequest {
127 proposal_id,
128 pagination,
129 };
130 let query = self
131 .rpc
132 .abci_query(
133 Some("/cosmos.gov.v1beta1.Query/Votes".to_string()),
134 query.encode_to_vec(),
135 None,
136 false,
137 )
138 .await?;
139
140 if query.code != Code::Ok {
141 return Err(RpcError(query.log));
142 }
143 QueryVotesResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
144 }
145
146 pub async fn params(&self, params_type: &str) -> Result<QueryParamsResponse, CosmosClient> {
153 let query = QueryParamsRequest {
154 params_type: params_type.to_string(),
155 };
156 let query = self
157 .rpc
158 .abci_query(
159 Some("/cosmos.gov.v1beta1.Query/Params".to_string()),
160 query.encode_to_vec(),
161 None,
162 false,
163 )
164 .await?;
165
166 if query.code != Code::Ok {
167 return Err(RpcError(query.log));
168 }
169 QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
170 }
171
172 pub async fn deposit(
179 &self,
180 proposal_id: u64,
181 depositor: &str,
182 ) -> Result<QueryDepositResponse, CosmosClient> {
183 let query = QueryDepositRequest {
184 proposal_id,
185 depositor: depositor.to_string(),
186 };
187 let query = self
188 .rpc
189 .abci_query(
190 Some("/cosmos.gov.v1beta1.Query/Deposit".to_string()),
191 query.encode_to_vec(),
192 None,
193 false,
194 )
195 .await?;
196
197 if query.code != Code::Ok {
198 return Err(RpcError(query.log));
199 }
200 QueryDepositResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
201 }
202
203 pub async fn deposits(
210 &self,
211 proposal_id: u64,
212 pagination: Option<PageRequest>,
213 ) -> Result<QueryDepositsResponse, CosmosClient> {
214 let query = QueryDepositsRequest {
215 proposal_id,
216 pagination,
217 };
218 let query = self
219 .rpc
220 .abci_query(
221 Some("/cosmos.gov.v1beta1.Query/Deposits".to_string()),
222 query.encode_to_vec(),
223 None,
224 false,
225 )
226 .await?;
227
228 if query.code != Code::Ok {
229 return Err(RpcError(query.log));
230 }
231 QueryDepositsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
232 }
233
234 pub async fn tally_result(
241 &self,
242 proposal_id: u64,
243 ) -> Result<QueryTallyResultResponse, CosmosClient> {
244 let query = QueryTallyResultRequest { proposal_id };
245 let query = self
246 .rpc
247 .abci_query(
248 Some("/cosmos.gov.v1beta1.Query/TallyResult".to_string()),
249 query.encode_to_vec(),
250 None,
251 false,
252 )
253 .await?;
254
255 if query.code != Code::Ok {
256 return Err(RpcError(query.log));
257 }
258 QueryTallyResultResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
259 }
260}