cosmos_client/client/
bank.rs

1use crate::error::CosmosClient;
2use crate::error::CosmosClient::{ProstDecodeError, RpcError};
3use cosmos_sdk_proto::cosmos::bank::v1beta1::{
4    QueryAllBalancesRequest, QueryAllBalancesResponse, QueryBalanceRequest, QueryBalanceResponse,
5    QueryDenomMetadataRequest, QueryDenomMetadataResponse, QueryDenomsMetadataRequest,
6    QueryDenomsMetadataResponse, QueryParamsRequest, QueryParamsResponse,
7    QuerySpendableBalancesRequest, QuerySpendableBalancesResponse, QuerySupplyOfRequest,
8    QuerySupplyOfResponse, QueryTotalSupplyRequest, QueryTotalSupplyResponse,
9};
10use cosmos_sdk_proto::cosmos::base::query::v1beta1::PageRequest;
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    /// # Errors
26    ///
27    /// Will return `Err` if :
28    /// - a prost encode / decode fail
29    /// - the json-rpc return an error code
30    /// - if there is some network error
31    pub async fn balance(
32        &self,
33        address: &str,
34        denom: &str,
35    ) -> Result<QueryBalanceResponse, CosmosClient> {
36        let query = QueryBalanceRequest {
37            address: address.to_string(),
38            denom: denom.to_string(),
39        };
40        let query = self
41            .rpc
42            .abci_query(
43                Some("/cosmos.bank.v1beta1.Query/Balance".to_string()),
44                query.encode_to_vec(),
45                None,
46                false,
47            )
48            .await?;
49
50        if query.code != Code::Ok {
51            return Err(RpcError(query.log));
52        }
53        QueryBalanceResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
54    }
55
56    /// # Errors
57    ///
58    /// Will return `Err` if :
59    /// - a prost encode / decode fail
60    /// - the json-rpc return an error code
61    /// - if there is some network error
62    pub async fn all_balances(
63        &self,
64        address: &str,
65        pagination: Option<PageRequest>,
66    ) -> Result<QueryAllBalancesResponse, CosmosClient> {
67        let query = QueryAllBalancesRequest {
68            address: address.to_string(),
69            pagination,
70        };
71        let query = self
72            .rpc
73            .abci_query(
74                Some("/cosmos.bank.v1beta1.Query/AllBalances".to_string()),
75                query.encode_to_vec(),
76                None,
77                false,
78            )
79            .await?;
80
81        if query.code != Code::Ok {
82            return Err(RpcError(query.log));
83        }
84        QueryAllBalancesResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
85    }
86
87    /// # Errors
88    ///
89    /// Will return `Err` if :
90    /// - a prost encode / decode fail
91    /// - the json-rpc return an error code
92    /// - if there is some network error
93    pub async fn spendable_balances(
94        &self,
95        address: &str,
96        pagination: Option<PageRequest>,
97    ) -> Result<QuerySpendableBalancesResponse, CosmosClient> {
98        let query = QuerySpendableBalancesRequest {
99            address: address.to_string(),
100            pagination,
101        };
102        let query = self
103            .rpc
104            .abci_query(
105                Some("/cosmos.bank.v1beta1.Query/SpendableBalances".to_string()),
106                query.encode_to_vec(),
107                None,
108                false,
109            )
110            .await?;
111
112        if query.code != Code::Ok {
113            return Err(RpcError(query.log));
114        }
115        QuerySpendableBalancesResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
116    }
117
118    /// # Errors
119    ///
120    /// Will return `Err` if :
121    /// - a prost encode / decode fail
122    /// - the json-rpc return an error code
123    /// - if there is some network error
124    pub async fn total_supply(
125        &self,
126        pagination: Option<PageRequest>,
127    ) -> Result<QueryTotalSupplyResponse, CosmosClient> {
128        let query = QueryTotalSupplyRequest { pagination };
129        let query = self
130            .rpc
131            .abci_query(
132                Some("/cosmos.bank.v1beta1.Query/TotalSupply".to_string()),
133                query.encode_to_vec(),
134                None,
135                false,
136            )
137            .await?;
138
139        if query.code != Code::Ok {
140            return Err(RpcError(query.log));
141        }
142        QueryTotalSupplyResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
143    }
144
145    /// # Errors
146    ///
147    /// Will return `Err` if :
148    /// - a prost encode / decode fail
149    /// - the json-rpc return an error code
150    /// - if there is some network error
151    pub async fn supply_of(&self, denom: &str) -> Result<QuerySupplyOfResponse, CosmosClient> {
152        let query = QuerySupplyOfRequest {
153            denom: denom.to_string(),
154        };
155        let query = self
156            .rpc
157            .abci_query(
158                Some("/cosmos.bank.v1beta1.Query/SupplyOf".to_string()),
159                query.encode_to_vec(),
160                None,
161                false,
162            )
163            .await?;
164
165        if query.code != Code::Ok {
166            return Err(RpcError(query.log));
167        }
168        QuerySupplyOfResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
169    }
170
171    /// # Errors
172    ///
173    /// Will return `Err` if :
174    /// - a prost encode / decode fail
175    /// - the json-rpc return an error code
176    /// - if there is some network error
177    pub async fn params(&self) -> Result<QueryParamsResponse, CosmosClient> {
178        let query = QueryParamsRequest {};
179        let query = self
180            .rpc
181            .abci_query(
182                Some("/cosmos.bank.v1beta1.Query/Params".to_string()),
183                query.encode_to_vec(),
184                None,
185                false,
186            )
187            .await?;
188
189        if query.code != Code::Ok {
190            return Err(RpcError(query.log));
191        }
192        QueryParamsResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
193    }
194
195    /// # Errors
196    ///
197    /// Will return `Err` if :
198    /// - a prost encode / decode fail
199    /// - the json-rpc return an error code
200    /// - if there is some network error
201    pub async fn denom_metadata(
202        &self,
203        denom: &str,
204    ) -> Result<QueryDenomMetadataResponse, CosmosClient> {
205        let query = QueryDenomMetadataRequest {
206            denom: denom.to_string(),
207        };
208        let query = self
209            .rpc
210            .abci_query(
211                Some("/cosmos.bank.v1beta1.Query/DenomMetadata".to_string()),
212                query.encode_to_vec(),
213                None,
214                false,
215            )
216            .await?;
217
218        if query.code != Code::Ok {
219            return Err(RpcError(query.log));
220        }
221        QueryDenomMetadataResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
222    }
223
224    /// # Errors
225    ///
226    /// Will return `Err` if :
227    /// - a prost encode / decode fail
228    /// - the json-rpc return an error code
229    /// - if there is some network error
230    pub async fn denoms_metadata(
231        &self,
232        pagination: Option<PageRequest>,
233    ) -> Result<QueryDenomsMetadataResponse, CosmosClient> {
234        let query = QueryDenomsMetadataRequest { pagination };
235        let query = self
236            .rpc
237            .abci_query(
238                Some("/cosmos.bank.v1beta1.Query/DenomsMetadata".to_string()),
239                query.encode_to_vec(),
240                None,
241                false,
242            )
243            .await?;
244
245        if query.code != Code::Ok {
246            return Err(RpcError(query.log));
247        }
248        QueryDenomsMetadataResponse::decode(query.value.as_slice()).map_err(ProstDecodeError)
249    }
250}