cosmos_client/client/
staking.rs

1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
4use cosmos_sdk_proto::cosmos::staking::v1beta1::{
5    QueryDelegationRequest, QueryDelegationResponse, QueryDelegatorDelegationsRequest,
6    QueryDelegatorDelegationsResponse, QueryDelegatorUnbondingDelegationsRequest,
7    QueryDelegatorUnbondingDelegationsResponse, QueryDelegatorValidatorRequest,
8    QueryDelegatorValidatorResponse, QueryDelegatorValidatorsResponse, QueryHistoricalInfoRequest,
9    QueryHistoricalInfoResponse, QueryParamsRequest, QueryParamsResponse, QueryPoolRequest,
10    QueryPoolResponse, QueryRedelegationsRequest, QueryRedelegationsResponse,
11    QueryUnbondingDelegationRequest, QueryUnbondingDelegationResponse,
12    QueryValidatorDelegationsRequest, QueryValidatorDelegationsResponse, QueryValidatorRequest,
13    QueryValidatorResponse, QueryValidatorUnbondingDelegationsRequest,
14    QueryValidatorUnbondingDelegationsResponse, QueryValidatorsRequest, QueryValidatorsResponse,
15};
16use prost::Message;
17use std::rc::Rc;
18use tendermint::abci::Code;
19use tendermint_rpc::{Client, HttpClient};
20
21pub struct Module {
22    rpc: Rc<HttpClient>,
23}
24
25impl Module {
26    pub fn new(rpc: Rc<HttpClient>) -> Self {
27        Module { rpc }
28    }
29
30    /// # Errors
31    ///
32    /// Will return `Err` if :
33    /// - a prost encode / decode fail
34    /// - the json-rpc return an error code
35    /// - if there is some network error
36    pub async fn validator(
37        &self,
38        validator_addr: &str,
39    ) -> Result<QueryValidatorResponse, CosmosClient> {
40        let query = QueryValidatorRequest {
41            validator_addr: validator_addr.to_string(),
42        };
43        let query = self
44            .rpc
45            .abci_query(
46                Some("/cosmos.staking.v1beta1.Query/Validators".to_string()),
47                query.encode_to_vec(),
48                None,
49                false,
50            )
51            .await?;
52
53        if query.code != Code::Ok {
54            return Err(RpcError(query.log));
55        }
56        QueryValidatorResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
57    }
58
59    /// # Errors
60    ///
61    /// Will return `Err` if :
62    /// - a prost encode / decode fail
63    /// - the json-rpc return an error code
64    /// - if there is some network error
65    pub async fn validators(
66        &self,
67        status: &str,
68        pagination: Option<PageRequest>,
69    ) -> Result<QueryValidatorsResponse, CosmosClient> {
70        let query = QueryValidatorsRequest {
71            status: status.to_string(),
72            pagination,
73        };
74        let query = self
75            .rpc
76            .abci_query(
77                Some("/cosmos.staking.v1beta1.Query/Validator".to_string()),
78                query.encode_to_vec(),
79                None,
80                false,
81            )
82            .await?;
83
84        if query.code != Code::Ok {
85            return Err(RpcError(query.log));
86        }
87        QueryValidatorsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
88    }
89
90    /// # Errors
91    ///
92    /// Will return `Err` if :
93    /// - a prost encode / decode fail
94    /// - the json-rpc return an error code
95    /// - if there is some network error
96    pub async fn validator_delegations(
97        &self,
98        validator_addr: &str,
99        pagination: Option<PageRequest>,
100    ) -> Result<QueryValidatorDelegationsResponse, CosmosClient> {
101        let query = QueryValidatorDelegationsRequest {
102            validator_addr: validator_addr.to_string(),
103            pagination,
104        };
105        let query = self
106            .rpc
107            .abci_query(
108                Some("/cosmos.staking.v1beta1.Query/ValidatorDelegations".to_string()),
109                query.encode_to_vec(),
110                None,
111                false,
112            )
113            .await?;
114
115        if query.code != Code::Ok {
116            return Err(RpcError(query.log));
117        }
118        QueryValidatorDelegationsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
119    }
120
121    /// # Errors
122    ///
123    /// Will return `Err` if :
124    /// - a prost encode / decode fail
125    /// - the json-rpc return an error code
126    /// - if there is some network error
127    pub async fn validator_unbonding_delegations(
128        &self,
129        validator_addr: &str,
130        pagination: Option<PageRequest>,
131    ) -> Result<QueryValidatorUnbondingDelegationsResponse, CosmosClient> {
132        let query = QueryValidatorUnbondingDelegationsRequest {
133            validator_addr: validator_addr.to_string(),
134            pagination,
135        };
136        let query = self
137            .rpc
138            .abci_query(
139                Some("/cosmos.staking.v1beta1.Query/ValidatorUnbondingDelegations".to_string()),
140                query.encode_to_vec(),
141                None,
142                false,
143            )
144            .await?;
145
146        if query.code != Code::Ok {
147            return Err(RpcError(query.log));
148        }
149        QueryValidatorUnbondingDelegationsResponse::decode(query.value.as_slice())
150            .map_err(ProstDecodeError)
151    }
152
153    /// # Errors
154    ///
155    /// Will return `Err` if :
156    /// - a prost encode / decode fail
157    /// - the json-rpc return an error code
158    /// - if there is some network error
159    pub async fn delegation(
160        &self,
161        delegator_addr: &str,
162        validator_addr: &str,
163    ) -> Result<QueryDelegationResponse, CosmosClient> {
164        let query = QueryDelegationRequest {
165            delegator_addr: delegator_addr.to_string(),
166            validator_addr: validator_addr.to_string(),
167        };
168        let query = self
169            .rpc
170            .abci_query(
171                Some("/cosmos.staking.v1beta1.Query/Delegation".to_string()),
172                query.encode_to_vec(),
173                None,
174                false,
175            )
176            .await?;
177
178        if query.code != Code::Ok {
179            return Err(RpcError(query.log));
180        }
181        QueryDelegationResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
182    }
183
184    /// # Errors
185    ///
186    /// Will return `Err` if :
187    /// - a prost encode / decode fail
188    /// - the json-rpc return an error code
189    /// - if there is some network error
190    pub async fn unbonding_delegation(
191        &self,
192        delegator_addr: &str,
193        validator_addr: &str,
194    ) -> Result<QueryUnbondingDelegationResponse, CosmosClient> {
195        let query = QueryUnbondingDelegationRequest {
196            delegator_addr: delegator_addr.to_string(),
197            validator_addr: validator_addr.to_string(),
198        };
199        let query = self
200            .rpc
201            .abci_query(
202                Some("/cosmos.staking.v1beta1.Query/UnbondingDelegation".to_string()),
203                query.encode_to_vec(),
204                None,
205                false,
206            )
207            .await?;
208
209        if query.code != Code::Ok {
210            return Err(RpcError(query.log));
211        }
212        QueryUnbondingDelegationResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
213    }
214
215    /// # Errors
216    ///
217    /// Will return `Err` if :
218    /// - a prost encode / decode fail
219    /// - the json-rpc return an error code
220    /// - if there is some network error
221    pub async fn delegator_delegations(
222        &self,
223        delegator_addr: &str,
224        pagination: Option<PageRequest>,
225    ) -> Result<QueryDelegatorDelegationsResponse, CosmosClient> {
226        let query = QueryDelegatorDelegationsRequest {
227            delegator_addr: delegator_addr.to_string(),
228            pagination,
229        };
230        let query = self
231            .rpc
232            .abci_query(
233                Some("/cosmos.staking.v1beta1.Query/DelegatorDelegations".to_string()),
234                query.encode_to_vec(),
235                None,
236                false,
237            )
238            .await?;
239
240        if query.code != Code::Ok {
241            return Err(RpcError(query.log));
242        }
243        QueryDelegatorDelegationsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
244    }
245
246    /// # Errors
247    ///
248    /// Will return `Err` if :
249    /// - a prost encode / decode fail
250    /// - the json-rpc return an error code
251    /// - if there is some network error
252    pub async fn delegator_unbonding_delegations(
253        &self,
254        delegator_addr: &str,
255        pagination: Option<PageRequest>,
256    ) -> Result<QueryDelegatorUnbondingDelegationsResponse, CosmosClient> {
257        let query = QueryDelegatorUnbondingDelegationsRequest {
258            delegator_addr: delegator_addr.to_string(),
259            pagination,
260        };
261        let query = self
262            .rpc
263            .abci_query(
264                Some("/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations".to_string()),
265                query.encode_to_vec(),
266                None,
267                false,
268            )
269            .await?;
270
271        if query.code != Code::Ok {
272            return Err(RpcError(query.log));
273        }
274        QueryDelegatorUnbondingDelegationsResponse::decode(query.value.as_slice())
275            .map_err(ProstDecodeError)
276    }
277
278    /// # Errors
279    ///
280    /// Will return `Err` if :
281    /// - a prost encode / decode fail
282    /// - the json-rpc return an error code
283    /// - if there is some network error
284    pub async fn redelegations(
285        &self,
286        delegator_addr: &str,
287        src_validator_addr: &str,
288        dst_validator_addr: &str,
289        pagination: Option<PageRequest>,
290    ) -> Result<QueryRedelegationsResponse, CosmosClient> {
291        let query = QueryRedelegationsRequest {
292            delegator_addr: delegator_addr.to_string(),
293            src_validator_addr: src_validator_addr.to_string(),
294            dst_validator_addr: dst_validator_addr.to_string(),
295            pagination,
296        };
297        let query = self
298            .rpc
299            .abci_query(
300                Some("/cosmos.staking.v1beta1.Query/Redelegations".to_string()),
301                query.encode_to_vec(),
302                None,
303                false,
304            )
305            .await?;
306
307        if query.code != Code::Ok {
308            return Err(RpcError(query.log));
309        }
310        QueryRedelegationsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
311    }
312
313    /// # Errors
314    ///
315    /// Will return `Err` if :
316    /// - a prost encode / decode fail
317    /// - the json-rpc return an error code
318    /// - if there is some network error
319    pub async fn delegator_validators(
320        &self,
321        delegator_addr: &str,
322        pagination: Option<PageRequest>,
323    ) -> Result<QueryDelegatorValidatorsResponse, CosmosClient> {
324        let query = QueryDelegatorDelegationsRequest {
325            delegator_addr: delegator_addr.to_string(),
326            pagination,
327        };
328        let query = self
329            .rpc
330            .abci_query(
331                Some("/cosmos.staking.v1beta1.Query/DelegatorValidators".to_string()),
332                query.encode_to_vec(),
333                None,
334                false,
335            )
336            .await?;
337
338        if query.code != Code::Ok {
339            return Err(RpcError(query.log));
340        }
341        QueryDelegatorValidatorsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
342    }
343
344    /// # Errors
345    ///
346    /// Will return `Err` if :
347    /// - a prost encode / decode fail
348    /// - the json-rpc return an error code
349    /// - if there is some network error
350    pub async fn delegator_validator(
351        &self,
352        delegator_addr: &str,
353        validator_addr: &str,
354    ) -> Result<QueryDelegatorValidatorResponse, CosmosClient> {
355        let query = QueryDelegatorValidatorRequest {
356            delegator_addr: delegator_addr.to_string(),
357            validator_addr: validator_addr.to_string(),
358        };
359        let query = self
360            .rpc
361            .abci_query(
362                Some("/cosmos.staking.v1beta1.Query/DelegatorValidator".to_string()),
363                query.encode_to_vec(),
364                None,
365                false,
366            )
367            .await?;
368
369        if query.code != Code::Ok {
370            return Err(RpcError(query.log));
371        }
372        QueryDelegatorValidatorResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
373    }
374
375    /// # Errors
376    ///
377    /// Will return `Err` if :
378    /// - a prost encode / decode fail
379    /// - the json-rpc return an error code
380    /// - if there is some network error
381    pub async fn historical_info(
382        &self,
383        height: i64,
384    ) -> Result<QueryHistoricalInfoResponse, CosmosClient> {
385        let query = QueryHistoricalInfoRequest { height };
386        let query = self
387            .rpc
388            .abci_query(
389                Some("/cosmos.staking.v1beta1.Query/HistoricalInfo".to_string()),
390                query.encode_to_vec(),
391                None,
392                false,
393            )
394            .await?;
395
396        if query.code != Code::Ok {
397            return Err(RpcError(query.log));
398        }
399        QueryHistoricalInfoResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
400    }
401
402    /// # Errors
403    ///
404    /// Will return `Err` if :
405    /// - a prost encode / decode fail
406    /// - the json-rpc return an error code
407    /// - if there is some network error
408    pub async fn pool(&self) -> Result<QueryPoolResponse, CosmosClient> {
409        let query = QueryPoolRequest {};
410        let query = self
411            .rpc
412            .abci_query(
413                Some("/cosmos.staking.v1beta1.Query/Pool".to_string()),
414                query.encode_to_vec(),
415                None,
416                false,
417            )
418            .await?;
419
420        if query.code != Code::Ok {
421            return Err(RpcError(query.log));
422        }
423        QueryPoolResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
424    }
425
426    /// # Errors
427    ///
428    /// Will return `Err` if :
429    /// - a prost encode / decode fail
430    /// - the json-rpc return an error code
431    /// - if there is some network error
432    pub async fn params(&self) -> Result<QueryParamsResponse, CosmosClient> {
433        let query = QueryParamsRequest {};
434        let query = self
435            .rpc
436            .abci_query(
437                Some("/cosmos.staking.v1beta1.Query/Params".to_string()),
438                query.encode_to_vec(),
439                None,
440                false,
441            )
442            .await?;
443
444        if query.code != Code::Ok {
445            return Err(RpcError(query.log));
446        }
447        QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
448    }
449}