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(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
16#[serde(rename_all = "snake_case")]
17pub enum BankQuery {
18    /// This calls into the native bank module for querying the total supply of one denomination.
19    /// It does the same as the SupplyOf call in Cosmos SDK's RPC API.
20    /// Return value is of type SupplyResponse.
21    #[cfg(feature = "cosmwasm_1_1")]
22    Supply { denom: String },
23    /// This calls into the native bank module for one denomination
24    /// Return value is BalanceResponse
25    Balance { address: String, denom: String },
26    /// This calls into the native bank module for querying metadata for a specific bank token.
27    /// Return value is DenomMetadataResponse
28    #[cfg(feature = "cosmwasm_1_3")]
29    DenomMetadata { denom: String },
30    /// This calls into the native bank module for querying metadata for all bank tokens that have a metadata entry.
31    /// Return value is AllDenomMetadataResponse
32    #[cfg(feature = "cosmwasm_1_3")]
33    AllDenomMetadata { pagination: Option<PageRequest> },
34}
35
36#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
37#[serde(rename_all = "snake_case")]
38#[non_exhaustive]
39pub struct SupplyResponse {
40    /// Always returns a Coin with the requested denom.
41    /// This will be of zero amount if the denom does not exist.
42    pub amount: Coin,
43}
44
45impl_hidden_constructor!(SupplyResponse, amount: Coin);
46
47impl QueryResponseType for SupplyResponse {}
48
49#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
50#[serde(rename_all = "snake_case")]
51#[non_exhaustive]
52pub struct BalanceResponse {
53    /// Always returns a Coin with the requested denom.
54    /// This may be of 0 amount if no such funds.
55    pub amount: Coin,
56}
57
58impl_hidden_constructor!(BalanceResponse, amount: Coin);
59
60impl QueryResponseType for BalanceResponse {}
61
62#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
63#[serde(rename_all = "snake_case")]
64#[non_exhaustive]
65pub struct DenomMetadataResponse {
66    /// The metadata for the queried denom.
67    pub metadata: DenomMetadata,
68}
69
70impl_hidden_constructor!(DenomMetadataResponse, metadata: DenomMetadata);
71
72impl QueryResponseType for DenomMetadataResponse {}
73
74#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
75#[serde(rename_all = "snake_case")]
76#[non_exhaustive]
77pub struct AllDenomMetadataResponse {
78    /// Always returns metadata for all token denoms on the base chain.
79    pub metadata: Vec<DenomMetadata>,
80    pub next_key: Option<Binary>,
81}
82
83impl_hidden_constructor!(
84    AllDenomMetadataResponse,
85    metadata: Vec<DenomMetadata>,
86    next_key: Option<Binary>
87);
88
89impl QueryResponseType for AllDenomMetadataResponse {}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn private_constructor_works() {
97        let response = BalanceResponse::new(Coin::new(1234u128, "uatom"));
98        assert_eq!(
99            response,
100            BalanceResponse {
101                amount: Coin::new(1234u128, "uatom")
102            }
103        );
104    }
105}