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