cosmos_client/client/
distribution.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::distribution::v1beta1::{
5    QueryCommunityPoolRequest, QueryCommunityPoolResponse, QueryDelegationRewardsRequest,
6    QueryDelegationRewardsResponse, QueryDelegationTotalRewardsRequest,
7    QueryDelegationTotalRewardsResponse, QueryDelegatorValidatorsRequest,
8    QueryDelegatorValidatorsResponse, QueryDelegatorWithdrawAddressRequest,
9    QueryDelegatorWithdrawAddressResponse, QueryParamsRequest, QueryParamsResponse,
10    QueryValidatorCommissionRequest, QueryValidatorCommissionResponse,
11    QueryValidatorOutstandingRewardsRequest, QueryValidatorOutstandingRewardsResponse,
12    QueryValidatorSlashesRequest, QueryValidatorSlashesResponse,
13};
14use prost::Message;
15use std::rc::Rc;
16use tendermint::abci::Code;
17use tendermint_rpc::{Client, HttpClient};
18
19pub struct Module {
20    rpc: Rc<HttpClient>,
21}
22
23impl Module {
24    pub fn new(rpc: Rc<HttpClient>) -> Self {
25        Module { rpc }
26    }
27
28    /// # Errors
29    ///
30    /// Will return `Err` if :
31    /// - a prost encode / decode fail
32    /// - the json-rpc return an error code
33    /// - if there is some network error
34    pub async fn params(&self) -> Result<QueryParamsResponse, CosmosClient> {
35        let query = QueryParamsRequest {};
36        let query = self
37            .rpc
38            .abci_query(
39                Some("/cosmos.distribution.v1beta1.Query/Params".to_string()),
40                query.encode_to_vec(),
41                None,
42                false,
43            )
44            .await?;
45
46        if query.code != Code::Ok {
47            return Err(RpcError(query.log));
48        }
49        QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
50    }
51
52    /// # Errors
53    ///
54    /// Will return `Err` if :
55    /// - a prost encode / decode fail
56    /// - the json-rpc return an error code
57    /// - if there is some network error
58    pub async fn validator_outstanding_rewards(
59        &self,
60        validator_address: &str,
61    ) -> Result<QueryValidatorOutstandingRewardsResponse, CosmosClient> {
62        let query = QueryValidatorOutstandingRewardsRequest {
63            validator_address: validator_address.to_string(),
64        };
65        let query = self
66            .rpc
67            .abci_query(
68                Some("/cosmos.distribution.v1beta1.Query/ValidatorOutstandingRewards".to_string()),
69                query.encode_to_vec(),
70                None,
71                false,
72            )
73            .await?;
74
75        if query.code != Code::Ok {
76            return Err(RpcError(query.log));
77        }
78        QueryValidatorOutstandingRewardsResponse::decode(query.value.as_slice())
79            .map_err(ProstDecodeError)
80    }
81
82    /// # Errors
83    ///
84    /// Will return `Err` if :
85    /// - a prost encode / decode fail
86    /// - the json-rpc return an error code
87    /// - if there is some network error
88    pub async fn validator_commission(
89        &self,
90        validator_address: &str,
91    ) -> Result<QueryValidatorCommissionResponse, CosmosClient> {
92        let query = QueryValidatorCommissionRequest {
93            validator_address: validator_address.to_string(),
94        };
95        let query = self
96            .rpc
97            .abci_query(
98                Some("/cosmos.distribution.v1beta1.Query/ValidatorCommission".to_string()),
99                query.encode_to_vec(),
100                None,
101                false,
102            )
103            .await?;
104
105        if query.code != Code::Ok {
106            return Err(RpcError(query.log));
107        }
108        QueryValidatorCommissionResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
109    }
110
111    /// # Errors
112    ///
113    /// Will return `Err` if :
114    /// - a prost encode / decode fail
115    /// - the json-rpc return an error code
116    /// - if there is some network error
117    pub async fn validator_slashes(
118        &self,
119        validator_address: &str,
120        starting_height: u64,
121        ending_height: u64,
122        pagination: Option<PageRequest>,
123    ) -> Result<QueryValidatorSlashesResponse, CosmosClient> {
124        let query = QueryValidatorSlashesRequest {
125            validator_address: validator_address.to_string(),
126            starting_height,
127            ending_height,
128            pagination,
129        };
130        let query = self
131            .rpc
132            .abci_query(
133                Some("/cosmos.distribution.v1beta1.Query/ValidatorSlashes".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        QueryValidatorSlashesResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
144    }
145
146    /// # Errors
147    ///
148    /// Will return `Err` if :
149    /// - a prost encode / decode fail
150    /// - the json-rpc return an error code
151    /// - if there is some network error
152    pub async fn delegation_rewards(
153        &self,
154        delegator_address: &str,
155        validator_address: &str,
156    ) -> Result<QueryDelegationRewardsResponse, CosmosClient> {
157        let query = QueryDelegationRewardsRequest {
158            delegator_address: delegator_address.to_string(),
159            validator_address: validator_address.to_string(),
160        };
161        let query = self
162            .rpc
163            .abci_query(
164                Some("/cosmos.distribution.v1beta1.Query/DelegationRewards".to_string()),
165                query.encode_to_vec(),
166                None,
167                false,
168            )
169            .await?;
170
171        if query.code != Code::Ok {
172            return Err(RpcError(query.log));
173        }
174        QueryDelegationRewardsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
175    }
176
177    /// # Errors
178    ///
179    /// Will return `Err` if :
180    /// - a prost encode / decode fail
181    /// - the json-rpc return an error code
182    /// - if there is some network error
183    pub async fn delegation_total_rewards(
184        &self,
185        delegator_address: &str,
186    ) -> Result<QueryDelegationTotalRewardsResponse, CosmosClient> {
187        let query = QueryDelegationTotalRewardsRequest {
188            delegator_address: delegator_address.to_string(),
189        };
190        let query = self
191            .rpc
192            .abci_query(
193                Some("/cosmos.distribution.v1beta1.Query/DelegationTotalRewards".to_string()),
194                query.encode_to_vec(),
195                None,
196                false,
197            )
198            .await?;
199
200        if query.code != Code::Ok {
201            return Err(RpcError(query.log));
202        }
203        QueryDelegationTotalRewardsResponse::decode(query.value.as_slice())
204            .map_err(ProstDecodeError)
205    }
206
207    /// # Errors
208    ///
209    /// Will return `Err` if :
210    /// - a prost encode / decode fail
211    /// - the json-rpc return an error code
212    /// - if there is some network error
213    pub async fn delegator_validators(
214        &self,
215        delegator_addr: &str,
216    ) -> Result<QueryDelegatorValidatorsResponse, CosmosClient> {
217        let query = QueryDelegatorValidatorsRequest {
218            delegator_address: delegator_addr.to_string(),
219        };
220        let query = self
221            .rpc
222            .abci_query(
223                Some("/cosmos.distribution.v1beta1.Query/DelegatorValidators".to_string()),
224                query.encode_to_vec(),
225                None,
226                false,
227            )
228            .await?;
229
230        if query.code != Code::Ok {
231            return Err(RpcError(query.log));
232        }
233        QueryDelegatorValidatorsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
234    }
235
236    /// # Errors
237    ///
238    /// Will return `Err` if :
239    /// - a prost encode / decode fail
240    /// - the json-rpc return an error code
241    /// - if there is some network error
242    pub async fn delegator_withdraw_address(
243        &self,
244        delegator_addr: &str,
245    ) -> Result<QueryDelegatorWithdrawAddressResponse, CosmosClient> {
246        let query = QueryDelegatorWithdrawAddressRequest {
247            delegator_address: delegator_addr.to_string(),
248        };
249        let query = self
250            .rpc
251            .abci_query(
252                Some("/cosmos.distribution.v1beta1.Query/DelegatorWithdrawAddress".to_string()),
253                query.encode_to_vec(),
254                None,
255                false,
256            )
257            .await?;
258
259        if query.code != Code::Ok {
260            return Err(RpcError(query.log));
261        }
262        QueryDelegatorWithdrawAddressResponse::decode(query.value.as_slice())
263            .map_err(ProstDecodeError)
264    }
265
266    /// # Errors
267    ///
268    /// Will return `Err` if :
269    /// - a prost encode / decode fail
270    /// - the json-rpc return an error code
271    /// - if there is some network error
272    pub async fn community_pool(&self) -> Result<QueryCommunityPoolResponse, CosmosClient> {
273        let query = QueryCommunityPoolRequest {};
274        let query = self
275            .rpc
276            .abci_query(
277                Some("/cosmos.distribution.v1beta1.Query/CommunityPool".to_string()),
278                query.encode_to_vec(),
279                None,
280                false,
281            )
282            .await?;
283
284        if query.code != Code::Ok {
285            return Err(RpcError(query.log));
286        }
287        QueryCommunityPoolResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
288    }
289}