cosmwasm_std/query/
bank.rs

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