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;
12use crate::utils::impl_hidden_constructor;
13
14#[non_exhaustive]
15#[derive(
16    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
17)]
18#[serde(rename_all = "snake_case")]
19pub enum BankQuery {
20    /// This calls into the native bank module for querying the total supply of one denomination.
21    /// It does the same as the SupplyOf call in Cosmos SDK's RPC API.
22    /// Return value is of type SupplyResponse.
23    #[cfg(feature = "cosmwasm_1_1")]
24    Supply { denom: String },
25    /// This calls into the native bank module for one denomination
26    /// Return value is BalanceResponse
27    Balance { address: String, denom: 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(
39    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
40)]
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_hidden_constructor!(SupplyResponse, amount: Coin);
50
51impl QueryResponseType for SupplyResponse {}
52
53#[derive(
54    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
55)]
56#[serde(rename_all = "snake_case")]
57#[non_exhaustive]
58pub struct BalanceResponse {
59    /// Always returns a Coin with the requested denom.
60    /// This may be of 0 amount if no such funds.
61    pub amount: Coin,
62}
63
64impl_hidden_constructor!(BalanceResponse, amount: Coin);
65
66impl QueryResponseType for BalanceResponse {}
67
68#[derive(
69    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
70)]
71#[serde(rename_all = "snake_case")]
72#[non_exhaustive]
73pub struct DenomMetadataResponse {
74    /// The metadata for the queried denom.
75    pub metadata: DenomMetadata,
76}
77
78impl_hidden_constructor!(DenomMetadataResponse, metadata: DenomMetadata);
79
80impl QueryResponseType for DenomMetadataResponse {}
81
82#[derive(
83    Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema, cw_schema::Schemaifier,
84)]
85#[serde(rename_all = "snake_case")]
86#[non_exhaustive]
87pub struct AllDenomMetadataResponse {
88    /// Always returns metadata for all token denoms on the base chain.
89    pub metadata: Vec<DenomMetadata>,
90    pub next_key: Option<Binary>,
91}
92
93impl_hidden_constructor!(
94    AllDenomMetadataResponse,
95    metadata: Vec<DenomMetadata>,
96    next_key: Option<Binary>
97);
98
99impl QueryResponseType for AllDenomMetadataResponse {}
100
101#[cfg(test)]
102mod tests {
103    use super::*;
104
105    #[test]
106    fn private_constructor_works() {
107        let response = BalanceResponse::new(Coin::new(1234u128, "uatom"));
108        assert_eq!(
109            response,
110            BalanceResponse {
111                amount: Coin::new(1234u128, "uatom")
112            }
113        );
114    }
115}