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    AllBalances { address: String },
29    /// This calls into the native bank module for querying metadata for a specific bank token.
30    /// Return value is DenomMetadataResponse
31    #[cfg(feature = "cosmwasm_1_3")]
32    DenomMetadata { denom: String },
33    /// This calls into the native bank module for querying metadata for all bank tokens that have a metadata entry.
34    /// Return value is AllDenomMetadataResponse
35    #[cfg(feature = "cosmwasm_1_3")]
36    AllDenomMetadata { pagination: Option<PageRequest> },
37}
38
39#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
40#[serde(rename_all = "snake_case")]
41#[non_exhaustive]
42pub struct SupplyResponse {
43    /// Always returns a Coin with the requested denom.
44    /// This will be of zero amount if the denom does not exist.
45    pub amount: Coin,
46}
47
48impl_response_constructor!(SupplyResponse, amount: Coin);
49
50impl QueryResponseType for SupplyResponse {}
51
52#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
53#[serde(rename_all = "snake_case")]
54#[non_exhaustive]
55pub struct BalanceResponse {
56    /// Always returns a Coin with the requested denom.
57    /// This may be of 0 amount if no such funds.
58    pub amount: Coin,
59}
60
61impl_response_constructor!(BalanceResponse, amount: Coin);
62
63impl QueryResponseType for BalanceResponse {}
64
65#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
66#[serde(rename_all = "snake_case")]
67#[non_exhaustive]
68pub struct AllBalanceResponse {
69    /// Returns all non-zero coins held by this account.
70    pub amount: Vec<Coin>,
71}
72
73impl_response_constructor!(AllBalanceResponse, amount: Vec<Coin>);
74
75impl QueryResponseType for AllBalanceResponse {}
76
77#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
78#[serde(rename_all = "snake_case")]
79#[non_exhaustive]
80pub struct DenomMetadataResponse {
81    /// The metadata for the queried denom.
82    pub metadata: DenomMetadata,
83}
84
85impl_response_constructor!(DenomMetadataResponse, metadata: DenomMetadata);
86
87impl QueryResponseType for DenomMetadataResponse {}
88
89#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
90#[serde(rename_all = "snake_case")]
91#[non_exhaustive]
92pub struct AllDenomMetadataResponse {
93    /// Always returns metadata for all token denoms on the base chain.
94    pub metadata: Vec<DenomMetadata>,
95    pub next_key: Option<Binary>,
96}
97
98impl_response_constructor!(
99    AllDenomMetadataResponse,
100    metadata: Vec<DenomMetadata>,
101    next_key: Option<Binary>
102);
103
104impl QueryResponseType for AllDenomMetadataResponse {}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn private_constructor_works() {
112        let response = AllBalanceResponse::new(vec![Coin::new(1234u128, "uatom")]);
113        assert_eq!(
114            response,
115            AllBalanceResponse {
116                amount: vec![Coin::new(1234u128, "uatom")]
117            }
118        );
119    }
120}