Skip to content

Commit a3f9ad7

Browse files
committed
Add QueryResponseType trait
1 parent abc9bcc commit a3f9ad7

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

packages/std/src/query/bank.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
33

44
use crate::Coin;
55

6+
use super::query_response::QueryResponseType;
7+
68
#[non_exhaustive]
79
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
810
#[serde(rename_all = "snake_case")]
@@ -22,7 +24,7 @@ pub enum BankQuery {
2224
}
2325

2426
#[cfg(feature = "cosmwasm_1_1")]
25-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
27+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
2628
#[serde(rename_all = "snake_case")]
2729
#[non_exhaustive]
2830
pub struct SupplyResponse {
@@ -32,28 +34,23 @@ pub struct SupplyResponse {
3234
}
3335

3436
#[cfg(feature = "cosmwasm_1_1")]
35-
impl SupplyResponse {
36-
/// Constructor for testing frameworks such as cw-multi-test.
37-
/// This is required because query response types should be #[non_exhaustive].
38-
/// As a contract developer you should not need this constructor since
39-
/// query responses are constructed for you via deserialization.
40-
#[doc(hidden)]
41-
pub fn new(amount: Coin) -> Self {
42-
Self { amount }
43-
}
44-
}
37+
impl QueryResponseType for SupplyResponse {}
4538

46-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
39+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
4740
#[serde(rename_all = "snake_case")]
4841
pub struct BalanceResponse {
4942
/// Always returns a Coin with the requested denom.
5043
/// This may be of 0 amount if no such funds.
5144
pub amount: Coin,
5245
}
5346

54-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
47+
impl QueryResponseType for BalanceResponse {}
48+
49+
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
5550
#[serde(rename_all = "snake_case")]
5651
pub struct AllBalanceResponse {
5752
/// Returns all non-zero coins held by this account.
5853
pub amount: Vec<Coin>,
5954
}
55+
56+
impl QueryResponseType for AllBalanceResponse {}

packages/std/src/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::Empty;
77

88
mod bank;
99
mod ibc;
10+
mod query_response;
1011
mod staking;
1112
mod wasm;
1213

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use serde::de::DeserializeOwned;
2+
3+
/// A marker trait for query response types.
4+
///
5+
/// Those types have in common that they should be `#[non_exhaustive]` in order
6+
/// to allow adding fields in a backwards compatible way. In contracts they are
7+
/// only constructed through deserialization. We want to make it hard for
8+
/// contract developers to construct those types themselves as this is most likely
9+
/// not what they should do.
10+
///
11+
/// In hosts they are constructed as follows:
12+
/// - wasmvm: Go types with the same JSON layout
13+
/// - multi-test/cw-sdk: create a default instance and mutate the fields
14+
///
15+
/// This trait is crate-internal and can change any time.
16+
pub(crate) trait QueryResponseType: Default + DeserializeOwned {}

packages/std/src/query/wasm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};
33

44
use crate::Binary;
55

6+
use super::query_response::QueryResponseType;
7+
68
#[non_exhaustive]
79
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
810
#[serde(rename_all = "snake_case")]
@@ -27,7 +29,7 @@ pub enum WasmQuery {
2729
}
2830

2931
#[non_exhaustive]
30-
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
32+
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
3133
pub struct ContractInfoResponse {
3234
pub code_id: u64,
3335
/// address that instantiated this contract
@@ -40,6 +42,8 @@ pub struct ContractInfoResponse {
4042
pub ibc_port: Option<String>,
4143
}
4244

45+
impl QueryResponseType for ContractInfoResponse {}
46+
4347
impl ContractInfoResponse {
4448
/// Constructor for testing frameworks such as cw-multi-test.
4549
/// This is required because query response types should be #[non_exhaustive].

0 commit comments

Comments
 (0)