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