Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueryResponseType trait and supply Default implementations #1560

Merged
merged 5 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ and this project adheres to
- cosmwasm-std: Upgrade `serde-json-wasm` dependency to 0.5.0 which adds map
support to `to_vec`/`to_binary` and friends.
- cosmwasm-std: Implement `AsRef<[u8]>` for `Binary` and `HexBinary` ([#1550]).
- cosmwasm-std: Add constructor `SupplyResponse::new` ([#1552]).
- cosmwasm-std: Allow constructing `SupplyResponse` via a `Default`
implementation ([#1552], [#1560]).

[#1436]: https://github.com/CosmWasm/cosmwasm/issues/1436
[#1437]: https://github.com/CosmWasm/cosmwasm/issues/1437
Expand All @@ -33,6 +34,7 @@ and this project adheres to
[#1550]: https://github.com/CosmWasm/cosmwasm/issues/1550
[#1552]: https://github.com/CosmWasm/cosmwasm/pull/1552
[#1554]: https://github.com/CosmWasm/cosmwasm/pull/1554
[#1560]: https://github.com/CosmWasm/cosmwasm/pull/1560

### Changed

Expand Down
2 changes: 1 addition & 1 deletion contracts/reflect/schema/raw/query.json
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@
"additionalProperties": false
},
{
"description": "returns a ContractInfoResponse with metadata on the contract from the runtime",
"description": "Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime",
"type": "object",
"required": [
"contract_info"
Expand Down
2 changes: 1 addition & 1 deletion contracts/reflect/schema/reflect.json
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@
"additionalProperties": false
},
{
"description": "returns a ContractInfoResponse with metadata on the contract from the runtime",
"description": "Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime",
"type": "object",
"required": [
"contract_info"
Expand Down
23 changes: 10 additions & 13 deletions packages/std/src/query/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};

use crate::Coin;

use super::query_response::QueryResponseType;

#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand All @@ -22,7 +24,7 @@ pub enum BankQuery {
}

#[cfg(feature = "cosmwasm_1_1")]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub struct SupplyResponse {
Expand All @@ -32,28 +34,23 @@ pub struct SupplyResponse {
}

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

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

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
impl QueryResponseType for BalanceResponse {}

#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct AllBalanceResponse {
/// Returns all non-zero coins held by this account.
pub amount: Vec<Coin>,
}

impl QueryResponseType for AllBalanceResponse {}
1 change: 1 addition & 0 deletions packages/std/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::Empty;

mod bank;
mod ibc;
mod query_response;
mod staking;
mod wasm;

Expand Down
16 changes: 16 additions & 0 deletions packages/std/src/query/query_response.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use serde::de::DeserializeOwned;

/// A marker trait for query response types.
///
/// Those types have in common that they should be `#[non_exhaustive]` in order
/// to allow adding fields in a backwards compatible way. In contracts they are
/// only constructed through deserialization. We want to make it hard for
/// contract developers to construct those types themselves as this is most likely
/// not what they should do.
///
/// In hosts they are constructed as follows:
/// - wasmvm: Go types with the same JSON layout
/// - multi-test/cw-sdk: create a default instance and mutate the fields
///
/// This trait is crate-internal and can change any time.
pub(crate) trait QueryResponseType: Default + DeserializeOwned {}
17 changes: 11 additions & 6 deletions packages/std/src/query/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize};

use crate::Binary;

use super::query_response::QueryResponseType;

#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
Expand All @@ -22,12 +24,12 @@ pub enum WasmQuery {
/// Key is the raw key used in the contracts Storage
key: Binary,
},
/// returns a ContractInfoResponse with metadata on the contract from the runtime
/// Returns a [`ContractInfoResponse`] with metadata on the contract from the runtime
ContractInfo { contract_addr: String },
}

#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)]
pub struct ContractInfoResponse {
pub code_id: u64,
/// address that instantiated this contract
Expand All @@ -40,19 +42,22 @@ pub struct ContractInfoResponse {
pub ibc_port: Option<String>,
}

impl QueryResponseType for ContractInfoResponse {}

impl ContractInfoResponse {
/// Constructor for testing frameworks such as cw-multi-test.
/// This is required because query response types should be #[non_exhaustive].
/// As a contract developer you should not need this constructor since
/// query responses are constructed for you via deserialization.
#[doc(hidden)]
#[deprecated(
note = "Use ContractInfoResponse::default() and mutate the fields you want to set."
)]
pub fn new(code_id: u64, creator: impl Into<String>) -> Self {
Self {
ContractInfoResponse {
code_id,
creator: creator.into(),
admin: None,
pinned: false,
ibc_port: None,
..Default::default()
}
}
}