cosmwasm_std/query/mod.rs
1// needed because the derive macros on QueryRequest use the deprecated `Stargate` variant
2#![allow(deprecated)]
3
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use crate::prelude::*;
8use crate::Binary;
9use crate::Empty;
10
11/// Implements a hidden constructor for query responses.
12macro_rules! impl_response_constructor {
13 ( $response:ty, $( $field: ident : $t: ty),* ) => {
14 impl $response {
15 /// Constructor for testing frameworks such as cw-multi-test.
16 /// This is required because query response types should be #[non_exhaustive].
17 /// As a contract developer you should not need this constructor since
18 /// query responses are constructed for you via deserialization.
19 ///
20 /// Warning: This can change in breaking ways in minor versions.
21 #[doc(hidden)]
22 #[allow(dead_code)]
23 pub fn new($( $field: $t),*) -> Self {
24 Self { $( $field ),* }
25 }
26 }
27 };
28}
29
30mod bank;
31mod distribution;
32mod ibc;
33mod query_response;
34mod staking;
35mod wasm;
36
37pub use bank::*;
38pub use distribution::*;
39pub use ibc::*;
40pub use staking::*;
41pub use wasm::*;
42
43#[non_exhaustive]
44#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
45#[serde(rename_all = "snake_case")]
46pub enum QueryRequest<C = Empty> {
47 Bank(BankQuery),
48 Custom(C),
49 #[cfg(feature = "staking")]
50 Staking(StakingQuery),
51 #[cfg(feature = "cosmwasm_1_3")]
52 Distribution(DistributionQuery),
53 /// A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data.
54 /// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md).
55 /// The response is protobuf encoded data directly without a JSON response wrapper.
56 /// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
57 #[cfg(feature = "stargate")]
58 #[deprecated = "Please use the GrpcQuery instead"]
59 Stargate {
60 /// this is the fully qualified service path used for routing,
61 /// eg. "/cosmos_sdk.x.bank.v1.Query/QueryBalance"
62 path: String,
63 /// this is the expected protobuf message type (not any), binary encoded
64 data: Binary,
65 },
66 #[cfg(feature = "stargate")]
67 Ibc(IbcQuery),
68 Wasm(WasmQuery),
69 #[cfg(feature = "cosmwasm_2_0")]
70 Grpc(GrpcQuery),
71}
72
73/// Queries the chain using a grpc query.
74/// This allows to query information that is not exposed in our API.
75/// The chain needs to allowlist the supported queries.
76/// The drawback of this query is that you have to handle the protobuf encoding and decoding yourself.
77///
78/// The returned data is protobuf encoded. The protobuf type depends on the query.
79/// Because of this, using it with the [`query`](crate::QuerierWrapper::query) function will result
80/// in a deserialization error.
81/// Use [`raw_query`](crate::Querier::raw_query) or [`query_grpc`](crate::QuerierWrapper::query_grpc)
82/// instead.
83///
84/// To find the path, as well as the request and response types,
85/// you can query the chain's gRPC endpoint using a tool like
86/// [grpcurl](https://github.com/fullstorydev/grpcurl).
87#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
88pub struct GrpcQuery {
89 /// The fully qualified endpoint path used for routing.
90 /// It follows the format `/service_path/method_name`,
91 /// eg. "/cosmos.authz.v1beta1.Query/Grants"
92 pub path: String,
93 /// The expected protobuf message type (not [Any](https://protobuf.dev/programming-guides/proto3/#any)), binary encoded
94 pub data: Binary,
95}
96
97/// A trait that is required to avoid conflicts with other query types like BankQuery and WasmQuery
98/// in generic implementations.
99/// You need to implement it in your custom query type.
100///
101/// # Examples
102///
103/// ```
104/// # use cosmwasm_std::CustomQuery;
105/// # use schemars::JsonSchema;
106/// # use serde::{Deserialize, Serialize};
107/// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
108/// #[serde(rename_all = "snake_case")]
109/// pub enum MyCustomQuery {
110/// Ping {},
111/// Capitalized { text: String },
112/// }
113///
114/// impl CustomQuery for MyCustomQuery {}
115/// ```
116pub trait CustomQuery: Serialize + Clone {}
117// We require `Clone` because `Clone` in `QueryRequest<C>` is only derived for
118// `C: Clone` and we want consistent behaviour for all `QueryRequest<C>`
119
120impl CustomQuery for Empty {}
121
122impl<C: CustomQuery> From<BankQuery> for QueryRequest<C> {
123 fn from(msg: BankQuery) -> Self {
124 QueryRequest::Bank(msg)
125 }
126}
127
128impl<C: CustomQuery> From<C> for QueryRequest<C> {
129 fn from(msg: C) -> Self {
130 QueryRequest::Custom(msg)
131 }
132}
133
134#[cfg(feature = "staking")]
135impl<C: CustomQuery> From<StakingQuery> for QueryRequest<C> {
136 fn from(msg: StakingQuery) -> Self {
137 QueryRequest::Staking(msg)
138 }
139}
140
141impl<C: CustomQuery> From<WasmQuery> for QueryRequest<C> {
142 fn from(msg: WasmQuery) -> Self {
143 QueryRequest::Wasm(msg)
144 }
145}
146
147#[cfg(feature = "cosmwasm_2_0")]
148impl<C: CustomQuery> From<GrpcQuery> for QueryRequest<C> {
149 fn from(msg: GrpcQuery) -> Self {
150 QueryRequest::Grpc(msg)
151 }
152}
153
154#[cfg(feature = "stargate")]
155impl<C: CustomQuery> From<IbcQuery> for QueryRequest<C> {
156 fn from(msg: IbcQuery) -> Self {
157 QueryRequest::Ibc(msg)
158 }
159}
160
161#[cfg(feature = "cosmwasm_1_3")]
162impl<C: CustomQuery> From<DistributionQuery> for QueryRequest<C> {
163 fn from(msg: DistributionQuery) -> Self {
164 QueryRequest::Distribution(msg)
165 }
166}