secret_cosmwasm_std/query/
mod.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "stargate")]
5use crate::Binary;
6use crate::Empty;
7
8mod bank;
9mod ibc;
10mod staking;
11mod wasm;
12
13#[cfg(feature = "cosmwasm_1_1")]
14pub use bank::SupplyResponse;
15pub use bank::{AllBalanceResponse, BalanceResponse, BankQuery};
16#[cfg(feature = "stargate")]
17pub use ibc::{ChannelResponse, IbcQuery, ListChannelsResponse, PortIdResponse};
18#[cfg(feature = "staking")]
19pub use staking::{
20    AllDelegationsResponse, AllValidatorsResponse, BondedDenomResponse, Delegation,
21    DelegationResponse, FullDelegation, StakingQuery, Validator, ValidatorResponse,
22};
23pub use wasm::{ContractInfoResponse, WasmQuery};
24
25#[non_exhaustive]
26#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
27#[serde(rename_all = "snake_case")]
28pub enum QueryRequest<C> {
29    Bank(BankQuery),
30    Custom(C),
31    #[cfg(feature = "staking")]
32    Staking(StakingQuery),
33    /// A Stargate query is encoded the same way as abci_query, with path and protobuf encoded request data.
34    /// The format is defined in [ADR-21](https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-021-protobuf-query-encoding.md).
35    /// The response is protobuf encoded data directly without a JSON response wrapper.
36    /// The caller is responsible for compiling the proper protobuf definitions for both requests and responses.
37    #[cfg(feature = "stargate")]
38    Stargate {
39        /// this is the fully qualified service path used for routing,
40        /// e.g. "/cosmos.bank.v1beta1.Query.Balance"
41        /// NOTE: the path can be changed after a chain upgrade
42        path: String,
43        /// this is the expected protobuf message type (not any), binary encoded
44        data: Binary,
45    },
46    #[cfg(feature = "stargate")]
47    Ibc(IbcQuery),
48    Wasm(WasmQuery),
49}
50
51/// A trait that is required to avoid conflicts with other query types like BankQuery and WasmQuery
52/// in generic implementations.
53/// You need to implement it in your custom query type.
54///
55/// # Examples
56///
57/// ```
58/// # use secret_cosmwasm_std::CustomQuery;
59/// # use schemars::JsonSchema;
60/// # use serde::{Deserialize, Serialize};
61/// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
62/// #[serde(rename_all = "snake_case")]
63/// pub enum MyCustomQuery {
64///     Ping {},
65///     Capitalized { text: String },
66/// }
67///
68/// impl CustomQuery for MyCustomQuery {}
69/// ```
70pub trait CustomQuery: Serialize + Clone {}
71// We require `Clone` because `Clone` in `QueryRequest<C>` is only derived for
72// `C: Clone` and we want consistent behaviour for all `QueryRequest<C>`
73
74impl CustomQuery for Empty {}
75
76impl<C: CustomQuery> From<BankQuery> for QueryRequest<C> {
77    fn from(msg: BankQuery) -> Self {
78        QueryRequest::Bank(msg)
79    }
80}
81
82impl<C: CustomQuery> From<C> for QueryRequest<C> {
83    fn from(msg: C) -> Self {
84        QueryRequest::Custom(msg)
85    }
86}
87
88#[cfg(feature = "staking")]
89impl<C: CustomQuery> From<StakingQuery> for QueryRequest<C> {
90    fn from(msg: StakingQuery) -> Self {
91        QueryRequest::Staking(msg)
92    }
93}
94
95impl<C: CustomQuery> From<WasmQuery> for QueryRequest<C> {
96    fn from(msg: WasmQuery) -> Self {
97        QueryRequest::Wasm(msg)
98    }
99}
100
101#[cfg(feature = "stargate")]
102impl<C: CustomQuery> From<IbcQuery> for QueryRequest<C> {
103    fn from(msg: IbcQuery) -> Self {
104        QueryRequest::Ibc(msg)
105    }
106}