cosmwasm_std/query/mod.rs
1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "stargate")]
5use crate::Binary;
6use crate::Empty;
7
8/// Implements a hidden constructor for query responses.
9macro_rules! impl_response_constructor {
10 ( $response:ty, $( $field: ident : $t: ty),* ) => {
11 impl $response {
12 /// Constructor for testing frameworks such as cw-multi-test.
13 /// This is required because query response types should be #[non_exhaustive].
14 /// As a contract developer you should not need this constructor since
15 /// query responses are constructed for you via deserialization.
16 ///
17 /// Warning: This can change in breaking ways in minor versions.
18 #[doc(hidden)]
19 #[allow(dead_code)]
20 pub fn new($( $field: $t),*) -> Self {
21 Self { $( $field ),* }
22 }
23 }
24 };
25}
26
27mod bank;
28mod distribution;
29mod ibc;
30mod query_response;
31mod staking;
32mod wasm;
33
34pub use bank::*;
35pub use distribution::*;
36pub use ibc::*;
37pub use staking::*;
38pub use wasm::*;
39
40#[non_exhaustive]
41#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
42#[serde(rename_all = "snake_case")]
43pub enum QueryRequest<C> {
44 Bank(BankQuery),
45 Custom(C),
46 #[cfg(feature = "staking")]
47 Staking(StakingQuery),
48 #[cfg(feature = "cosmwasm_1_3")]
49 Distribution(DistributionQuery),
50 /// A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data.
51 /// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md).
52 /// The response is protobuf encoded data directly without a JSON response wrapper.
53 /// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
54 #[cfg(feature = "stargate")]
55 Stargate {
56 /// this is the fully qualified service path used for routing,
57 /// eg. custom/cosmos_sdk.x.bank.v1.Query/QueryBalance
58 path: String,
59 /// this is the expected protobuf message type (not any), binary encoded
60 data: Binary,
61 },
62 #[cfg(feature = "stargate")]
63 Ibc(IbcQuery),
64 Wasm(WasmQuery),
65}
66
67/// A trait that is required to avoid conflicts with other query types like BankQuery and WasmQuery
68/// in generic implementations.
69/// You need to implement it in your custom query type.
70///
71/// # Examples
72///
73/// ```
74/// # use cosmwasm_std::CustomQuery;
75/// # use schemars::JsonSchema;
76/// # use serde::{Deserialize, Serialize};
77/// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
78/// #[serde(rename_all = "snake_case")]
79/// pub enum MyCustomQuery {
80/// Ping {},
81/// Capitalized { text: String },
82/// }
83///
84/// impl CustomQuery for MyCustomQuery {}
85/// ```
86pub trait CustomQuery: Serialize + Clone {}
87// We require `Clone` because `Clone` in `QueryRequest<C>` is only derived for
88// `C: Clone` and we want consistent behaviour for all `QueryRequest<C>`
89
90impl CustomQuery for Empty {}
91
92impl<C: CustomQuery> From<BankQuery> for QueryRequest<C> {
93 fn from(msg: BankQuery) -> Self {
94 QueryRequest::Bank(msg)
95 }
96}
97
98impl<C: CustomQuery> From<C> for QueryRequest<C> {
99 fn from(msg: C) -> Self {
100 QueryRequest::Custom(msg)
101 }
102}
103
104#[cfg(feature = "staking")]
105impl<C: CustomQuery> From<StakingQuery> for QueryRequest<C> {
106 fn from(msg: StakingQuery) -> Self {
107 QueryRequest::Staking(msg)
108 }
109}
110
111impl<C: CustomQuery> From<WasmQuery> for QueryRequest<C> {
112 fn from(msg: WasmQuery) -> Self {
113 QueryRequest::Wasm(msg)
114 }
115}
116
117#[cfg(feature = "stargate")]
118impl<C: CustomQuery> From<IbcQuery> for QueryRequest<C> {
119 fn from(msg: IbcQuery) -> Self {
120 QueryRequest::Ibc(msg)
121 }
122}
123
124#[cfg(feature = "cosmwasm_1_3")]
125impl<C: CustomQuery> From<DistributionQuery> for QueryRequest<C> {
126 fn from(msg: DistributionQuery) -> Self {
127 QueryRequest::Distribution(msg)
128 }
129}