Skip to content

Commit 082b508

Browse files
committed
Split query module into multiple files
1 parent 1f43164 commit 082b508

File tree

6 files changed

+270
-241
lines changed

6 files changed

+270
-241
lines changed

packages/std/src/query.rs

-241
This file was deleted.

packages/std/src/query/bank.rs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use schemars::JsonSchema;
2+
use serde::{Deserialize, Serialize};
3+
4+
use crate::Coin;
5+
6+
#[non_exhaustive]
7+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
8+
#[serde(rename_all = "snake_case")]
9+
pub enum BankQuery {
10+
/// This calls into the native bank module for one denomination
11+
/// Return value is BalanceResponse
12+
Balance { address: String, denom: String },
13+
/// This calls into the native bank module for all denominations.
14+
/// Note that this may be much more expensive than Balance and should be avoided if possible.
15+
/// Return value is AllBalanceResponse.
16+
AllBalances { address: String },
17+
}
18+
19+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
20+
#[serde(rename_all = "snake_case")]
21+
pub struct BalanceResponse {
22+
/// Always returns a Coin with the requested denom.
23+
/// This may be of 0 amount if no such funds.
24+
pub amount: Coin,
25+
}
26+
27+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
28+
#[serde(rename_all = "snake_case")]
29+
pub struct AllBalanceResponse {
30+
/// Returns all non-zero coins held by this account.
31+
pub amount: Vec<Coin>,
32+
}

packages/std/src/query/mod.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)