cosmwasm_std/query/
bank.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::Coin;
5
6use crate::prelude::*;
7#[cfg(feature = "cosmwasm_1_3")]
8use crate::PageRequest;
9use crate::{Binary, DenomMetadata};
10
11use super::query_response::QueryResponseType;
12
13#[non_exhaustive]
14#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
15#[serde(rename_all = "snake_case")]
16pub enum BankQuery {
17    /// This calls into the native bank module for querying the total supply of one denomination.
18    /// It does the same as the SupplyOf call in Cosmos SDK's RPC API.
19    /// Return value is of type SupplyResponse.
20    #[cfg(feature = "cosmwasm_1_1")]
21    Supply { denom: String },
22    /// This calls into the native bank module for one denomination
23    /// Return value is BalanceResponse
24    Balance { address: String, denom: String },
25    /// This calls into the native bank module for all denominations.
26    /// Note that this may be much more expensive than Balance and should be avoided if possible.
27    /// Return value is AllBalanceResponse.
28    #[deprecated = "Returns a potentially unbound number of results. If you think you have a valid usecase, please open an issue."]
29    AllBalances { address: String },
30    /// This calls into the native bank module for querying metadata for a specific bank token.
31    /// Return value is DenomMetadataResponse
32    #[cfg(feature = "cosmwasm_1_3")]
33    DenomMetadata { denom: String },
34    /// This calls into the native bank module for querying metadata for all bank tokens that have a metadata entry.
35    /// Return value is AllDenomMetadataResponse
36    #[cfg(feature = "cosmwasm_1_3")]
37    AllDenomMetadata { pagination: Option<PageRequest> },
38}
39
40#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
41#[serde(rename_all = "snake_case")]
42#[non_exhaustive]
43pub struct SupplyResponse {
44    /// Always returns a Coin with the requested denom.
45    /// This will be of zero amount if the denom does not exist.
46    pub amount: Coin,
47}
48
49impl_response_constructor!(SupplyResponse, amount: Coin);
50
51impl QueryResponseType for SupplyResponse {}
52
53#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
54#[serde(rename_all = "snake_case")]
55#[non_exhaustive]
56pub struct BalanceResponse {
57    /// Always returns a Coin with the requested denom.
58    /// This may be of 0 amount if no such funds.
59    pub amount: Coin,
60}
61
62impl_response_constructor!(BalanceResponse, amount: Coin);
63
64impl QueryResponseType for BalanceResponse {}
65
66#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
67#[serde(rename_all = "snake_case")]
68#[non_exhaustive]
69pub struct AllBalanceResponse {
70    /// Returns all non-zero coins held by this account.
71    pub amount: Vec<Coin>,
72}
73
74impl_response_constructor!(AllBalanceResponse, amount: Vec<Coin>);
75
76impl QueryResponseType for AllBalanceResponse {}
77
78#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
79#[serde(rename_all = "snake_case")]
80#[non_exhaustive]
81pub struct DenomMetadataResponse {
82    /// The metadata for the queried denom.
83    pub metadata: DenomMetadata,
84}
85
86impl_response_constructor!(DenomMetadataResponse, metadata: DenomMetadata);
87
88impl QueryResponseType for DenomMetadataResponse {}
89
90#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
91#[serde(rename_all = "snake_case")]
92#[non_exhaustive]
93pub struct AllDenomMetadataResponse {
94    /// Always returns metadata for all token denoms on the base chain.
95    pub metadata: Vec<DenomMetadata>,
96    pub next_key: Option<Binary>,
97}
98
99impl_response_constructor!(
100    AllDenomMetadataResponse,
101    metadata: Vec<DenomMetadata>,
102    next_key: Option<Binary>
103);
104
105impl QueryResponseType for AllDenomMetadataResponse {}
106
107#[cfg(test)]
108mod tests {
109    use super::*;
110
111    #[test]
112    fn private_constructor_works() {
113        let response = AllBalanceResponse::new(vec![Coin::new(1234u128, "uatom")]);
114        assert_eq!(
115            response,
116            AllBalanceResponse {
117                amount: vec![Coin::new(1234u128, "uatom")]
118            }
119        );
120    }
121}