diff --git a/CHANGELOG.md b/CHANGELOG.md index 2439396833..37e8d1c7f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,10 +34,19 @@ and this project adheres to - cosmwasm-std: Upgrade to `serde-json-wasm` 1.0. This means `u128` and `i128` are now serialized as numbers instead of strings. Use `Uint128` and `Int128` instead. ([#1939]) +- cosmwasm-std: Make `BalanceResponse`, `AllBalanceResponse`, + `DelegationRewardsResponse`, `DelegatorReward`, `DelegatorValidatorsResponse`, + `PortIdResponse`, `ListChannelsResponse`, `ChannelResponse`, + `BondedDenomResponse`, `AllDelegationsResponse`, `Delegation`, + `DelegationResponse`, `FullDelegation`, `AllValidatorsResponse`, + `ValidatorResponse` and `Validator` non-exhaustive. Add `Validator::create` + and `FullDelegation::create` to allow creating them in a stable way. Use + `Addr` type for `ContractInfoResponse::{creator, admin}`. ([#1883]) [#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874 [#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876 [#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879 +[#1883]: https://github.com/CosmWasm/cosmwasm/pull/1883 [#1884]: https://github.com/CosmWasm/cosmwasm/pull/1884 [#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898 [#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902 diff --git a/contracts/hackatom/src/contract.rs b/contracts/hackatom/src/contract.rs index f62ffcb319..d99eb0753a 100644 --- a/contracts/hackatom/src/contract.rs +++ b/contracts/hackatom/src/contract.rs @@ -2,8 +2,8 @@ use sha2::{Digest, Sha256}; use cosmwasm_std::{ entry_point, from_json, to_json_binary, to_json_vec, Addr, AllBalanceResponse, Api, BankMsg, - CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse, Response, - StdError, StdResult, WasmMsg, WasmQuery, + BankQuery, CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse, + Response, StdError, StdResult, WasmMsg, WasmQuery, }; use crate::errors::HackError; @@ -269,8 +269,8 @@ fn query_verifier(deps: Deps) -> StdResult { } fn query_other_balance(deps: Deps, address: String) -> StdResult { - let amount = deps.querier.query_all_balances(address)?; - Ok(AllBalanceResponse { amount }) + deps.querier + .query(&BankQuery::AllBalances { address }.into()) } fn query_recurse(deps: Deps, depth: u32, work: u32, contract: Addr) -> StdResult { diff --git a/contracts/staking/Cargo.lock b/contracts/staking/Cargo.lock index 939c78f4e7..adf45172da 100644 --- a/contracts/staking/Cargo.lock +++ b/contracts/staking/Cargo.lock @@ -857,9 +857,9 @@ dependencies = [ [[package]] name = "itoa" -version = "0.4.7" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" +checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "js-sys" @@ -1358,9 +1358,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.64" +version = "1.0.107" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" +checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65" dependencies = [ "itoa", "ryu", diff --git a/contracts/staking/src/contract.rs b/contracts/staking/src/contract.rs index ab70170ea3..f5ee407776 100644 --- a/contracts/staking/src/contract.rs +++ b/contracts/staking/src/contract.rs @@ -412,23 +412,22 @@ mod tests { use std::str::FromStr; fn sample_validator(addr: &str) -> Validator { - Validator { - address: addr.to_owned(), - commission: Decimal::percent(3), - max_commission: Decimal::percent(10), - max_change_rate: Decimal::percent(1), - } + Validator::create( + addr.to_owned(), + Decimal::percent(3), + Decimal::percent(10), + Decimal::percent(1), + ) } fn sample_delegation(validator_addr: &str, amount: Coin) -> FullDelegation { - let can_redelegate = amount.clone(); - FullDelegation { - validator: validator_addr.to_owned(), - delegator: Addr::unchecked(MOCK_CONTRACT_ADDR), + FullDelegation::create( + Addr::unchecked(MOCK_CONTRACT_ADDR), + validator_addr.to_owned(), + amount.clone(), amount, - can_redelegate, - accumulated_rewards: Vec::new(), - } + vec![], + ) } fn set_validator(querier: &mut MockQuerier) { diff --git a/contracts/staking/tests/integration.rs b/contracts/staking/tests/integration.rs index cca65a9db4..23355ba932 100644 --- a/contracts/staking/tests/integration.rs +++ b/contracts/staking/tests/integration.rs @@ -34,12 +34,12 @@ static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/st // static WASM: &[u8] = include_bytes!("../contract.wasm"); fn sample_validator(addr: &str) -> Validator { - Validator { - address: addr.to_owned(), - commission: Decimal::percent(3), - max_commission: Decimal::percent(10), - max_change_rate: Decimal::percent(1), - } + Validator::create( + addr.to_owned(), + Decimal::percent(3), + Decimal::percent(10), + Decimal::percent(1), + ) } #[test] diff --git a/packages/std/src/query/bank.rs b/packages/std/src/query/bank.rs index 7980760f4a..d8ecb3baec 100644 --- a/packages/std/src/query/bank.rs +++ b/packages/std/src/query/bank.rs @@ -35,7 +35,7 @@ pub enum BankQuery { AllDenomMetadata { pagination: Option }, } -#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] #[non_exhaustive] pub struct SupplyResponse { @@ -48,8 +48,9 @@ impl_response_constructor!(SupplyResponse, amount: Coin); impl QueryResponseType for SupplyResponse {} -#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct BalanceResponse { /// Always returns a Coin with the requested denom. /// This may be of 0 amount if no such funds. @@ -60,8 +61,9 @@ impl_response_constructor!(BalanceResponse, amount: Coin); impl QueryResponseType for BalanceResponse {} -#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct AllBalanceResponse { /// Returns all non-zero coins held by this account. pub amount: Vec, diff --git a/packages/std/src/query/distribution.rs b/packages/std/src/query/distribution.rs index 4776c81e08..5dbdfec2e4 100644 --- a/packages/std/src/query/distribution.rs +++ b/packages/std/src/query/distribution.rs @@ -39,6 +39,7 @@ impl QueryResponseType for DelegatorWithdrawAddressResponse {} /// See #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct DelegationRewardsResponse { pub rewards: Vec, } @@ -91,13 +92,20 @@ impl_response_constructor!( impl QueryResponseType for DelegationTotalRewardsResponse {} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct DelegatorReward { pub validator_address: String, pub reward: Vec, } +impl_response_constructor!( + DelegatorReward, + validator_address: String, + reward: Vec +); /// See #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct DelegatorValidatorsResponse { pub validators: Vec, } diff --git a/packages/std/src/query/ibc.rs b/packages/std/src/query/ibc.rs index e8d6ff81e7..511c61274e 100644 --- a/packages/std/src/query/ibc.rs +++ b/packages/std/src/query/ibc.rs @@ -31,6 +31,7 @@ pub enum IbcQuery { } #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct PortIdResponse { pub port_id: String, } @@ -38,6 +39,7 @@ pub struct PortIdResponse { impl_response_constructor!(PortIdResponse, port_id: String); #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct ListChannelsResponse { pub channels: Vec, } @@ -45,6 +47,7 @@ pub struct ListChannelsResponse { impl_response_constructor!(ListChannelsResponse, channels: Vec); #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct ChannelResponse { pub channel: Option, } diff --git a/packages/std/src/query/staking.rs b/packages/std/src/query/staking.rs index 08222f3cbd..b8104b8f12 100644 --- a/packages/std/src/query/staking.rs +++ b/packages/std/src/query/staking.rs @@ -36,6 +36,7 @@ pub enum StakingQuery { /// BondedDenomResponse is data format returned from StakingRequest::BondedDenom query #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct BondedDenomResponse { pub denom: String, } @@ -47,6 +48,7 @@ impl_response_constructor!(BondedDenomResponse, denom: String); /// DelegationsResponse is data format returned from StakingRequest::AllDelegations query #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct AllDelegationsResponse { pub delegations: Vec, } @@ -59,6 +61,7 @@ impl_response_constructor!(AllDelegationsResponse, delegations: Vec) /// /// Instances are created in the querier. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct Delegation { pub delegator: Addr, /// A validator address (e.g. cosmosvaloper1...) @@ -67,6 +70,8 @@ pub struct Delegation { pub amount: Coin, } +impl_response_constructor!(Delegation, delegator: Addr, validator: String, amount: Coin); + impl From for Delegation { fn from(full: FullDelegation) -> Self { Delegation { @@ -80,6 +85,7 @@ impl From for Delegation { /// DelegationResponse is data format returned from StakingRequest::Delegation query #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] #[serde(rename_all = "snake_case")] +#[non_exhaustive] pub struct DelegationResponse { pub delegation: Option, } @@ -93,6 +99,7 @@ impl_response_constructor!(DelegationResponse, delegation: Option, } +impl_response_constructor!( + FullDelegation, + delegator: Addr, + validator: String, + amount: Coin, + can_redelegate: Coin, + accumulated_rewards: Vec +); + +impl FullDelegation { + /// Creates a new delegation. + /// + /// If fields get added to the [`FullDelegation`] struct in the future, this constructor will + /// provide default values for them, but these default values may not be sensible. + pub fn create( + delegator: Addr, + validator: String, + amount: Coin, + can_redelegate: Coin, + accumulated_rewards: Vec, + ) -> Self { + Self { + delegator, + validator, + amount, + can_redelegate, + accumulated_rewards, + } + } +} + /// The data format returned from StakingRequest::AllValidators query #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct AllValidatorsResponse { pub validators: Vec, } @@ -119,6 +158,7 @@ impl_response_constructor!(AllValidatorsResponse, validators: Vec); /// The data format returned from StakingRequest::Validator query #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct ValidatorResponse { pub validator: Option, } @@ -129,6 +169,7 @@ impl_response_constructor!(ValidatorResponse, validator: Option); /// Instances are created in the querier. #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] +#[non_exhaustive] pub struct Validator { /// The operator address of the validator (e.g. cosmosvaloper1...). /// See https://github.com/cosmos/cosmos-sdk/blob/v0.47.4/proto/cosmos/staking/v1beta1/staking.proto#L95-L96 @@ -142,3 +183,31 @@ pub struct Validator { /// The maximum daily increase of the commission pub max_change_rate: Decimal, } + +impl_response_constructor!( + Validator, + address: String, + commission: Decimal, + max_commission: Decimal, + max_change_rate: Decimal +); + +impl Validator { + /// Creates a new validator. + /// + /// If fields get added to the [`Validator`] struct in the future, this constructor will + /// provide default values for them, but these default values may not be sensible. + pub fn create( + address: String, + commission: Decimal, + max_commission: Decimal, + max_change_rate: Decimal, + ) -> Self { + Self { + address, + commission, + max_commission, + max_change_rate, + } + } +} diff --git a/packages/std/src/query/wasm.rs b/packages/std/src/query/wasm.rs index 936100835d..91a36cac40 100644 --- a/packages/std/src/query/wasm.rs +++ b/packages/std/src/query/wasm.rs @@ -1,7 +1,7 @@ use schemars::JsonSchema; use serde::{Deserialize, Serialize}; -use crate::{Binary, HexBinary}; +use crate::{Addr, Binary, HexBinary}; use super::query_response::QueryResponseType; @@ -32,13 +32,13 @@ pub enum WasmQuery { } #[non_exhaustive] -#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct ContractInfoResponse { pub code_id: u64, /// address that instantiated this contract - pub creator: String, + pub creator: Addr, /// admin who can run migrations (if any) - pub admin: Option, + pub admin: Option, /// if set, the contract is pinned to the cache, and thus uses less gas when called pub pinned: bool, /// set if this contract has bound an IBC port @@ -50,8 +50,8 @@ impl QueryResponseType for ContractInfoResponse {} impl_response_constructor!( ContractInfoResponse, code_id: u64, - creator: String, - admin: Option, + creator: Addr, + admin: Option, pinned: bool, ibc_port: Option ); @@ -64,11 +64,11 @@ impl_response_constructor!( /// [CodeInfo]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/types.proto#L62-L72 /// [CodeInfoResponse]: https://github.com/CosmWasm/wasmd/blob/v0.30.0/proto/cosmwasm/wasm/v1/query.proto#L184-L199 #[non_exhaustive] -#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq, Eq, JsonSchema)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)] pub struct CodeInfoResponse { pub code_id: u64, /// The address that initially stored the code - pub creator: String, + pub creator: Addr, /// The hash of the Wasm blob pub checksum: HexBinary, } @@ -76,7 +76,7 @@ pub struct CodeInfoResponse { impl_response_constructor!( CodeInfoResponse, code_id: u64, - creator: String, + creator: Addr, checksum: HexBinary ); @@ -114,8 +114,8 @@ mod tests { fn contract_info_response_serialization() { let response = ContractInfoResponse { code_id: 67, - creator: "jane".to_string(), - admin: Some("king".to_string()), + creator: Addr::unchecked("jane"), + admin: Some(Addr::unchecked("king")), pinned: true, ibc_port: Some("wasm.123".to_string()), }; @@ -133,7 +133,7 @@ mod tests { let response = CodeInfoResponse { code_id: 67, - creator: "jane".to_string(), + creator: Addr::unchecked("jane"), checksum: HexBinary::from_hex( "f7bb7b18fb01bbf425cf4ed2cd4b7fb26a019a7fc75a4dc87e8a0b768c501f00", ) diff --git a/packages/std/src/testing/mock.rs b/packages/std/src/testing/mock.rs index 2db41403d5..1aaad06daf 100644 --- a/packages/std/src/testing/mock.rs +++ b/packages/std/src/testing/mock.rs @@ -2216,7 +2216,7 @@ mod tests { if addr == constract1 { let response = ContractInfoResponse { code_id: 4, - creator: "lalala".into(), + creator: Addr::unchecked("lalala"), admin: None, pinned: false, ibc_port: None, @@ -2235,7 +2235,7 @@ mod tests { if code_id == 4 { let response = CodeInfoResponse { code_id, - creator: "lalala".into(), + creator: Addr::unchecked("lalala"), checksum: HexBinary::from_hex( "84cf20810fd429caf58898c3210fcb71759a27becddae08dbde8668ea2f4725d", ) diff --git a/packages/std/src/traits.rs b/packages/std/src/traits.rs index 12f9824c0c..2ddc669add 100644 --- a/packages/std/src/traits.rs +++ b/packages/std/src/traits.rs @@ -567,7 +567,7 @@ mod tests { fn mock_resp() -> ContractInfoResponse { ContractInfoResponse { code_id: 0, - creator: "creator".to_string(), + creator: Addr::unchecked("creator"), admin: None, pinned: false, ibc_port: None, @@ -598,7 +598,7 @@ mod tests { fn mock_resp() -> ContractInfoResponse { ContractInfoResponse { code_id: 0, - creator: "creator".to_string(), + creator: Addr::unchecked("creator"), admin: None, pinned: false, ibc_port: None, diff --git a/packages/vm/src/instance.rs b/packages/vm/src/instance.rs index b400731de8..b5f26c875e 100644 --- a/packages/vm/src/instance.rs +++ b/packages/vm/src/instance.rs @@ -970,7 +970,7 @@ mod tests { .unwrap() .unwrap() .unwrap(); - let BalanceResponse { amount } = from_json(response).unwrap(); + let BalanceResponse { amount, .. } = from_json(response).unwrap(); assert_eq!(amount.amount.u128(), 8000); assert_eq!(amount.denom, "silver"); Ok(()) @@ -991,7 +991,7 @@ mod tests { .unwrap() .unwrap() .unwrap(); - let AllBalanceResponse { amount } = from_json(response).unwrap(); + let AllBalanceResponse { amount, .. } = from_json(response).unwrap(); assert_eq!(amount.len(), 2); assert_eq!(amount[0].amount.u128(), 10000); assert_eq!(amount[0].denom, "gold"); @@ -1026,7 +1026,7 @@ mod tests { .unwrap() .unwrap() .unwrap(); - let BalanceResponse { amount } = from_json(response).unwrap(); + let BalanceResponse { amount, .. } = from_json(response).unwrap(); assert_eq!(amount.amount.u128(), 500); Ok(()) }) @@ -1055,7 +1055,7 @@ mod tests { .unwrap() .unwrap() .unwrap(); - let BalanceResponse { amount } = from_json(response).unwrap(); + let BalanceResponse { amount, .. } = from_json(response).unwrap(); assert_eq!(amount.amount.u128(), 8000); Ok(()) })