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