Skip to content

Commit 0294194

Browse files
authored
Merge pull request #1883 from CosmWasm/1559-non-exhaustive
[2.0] Response changes
2 parents 9a1380a + f6ba5ac commit 0294194

File tree

13 files changed

+140
-50
lines changed

13 files changed

+140
-50
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,19 @@ and this project adheres to
3434
- cosmwasm-std: Upgrade to `serde-json-wasm` 1.0. This means `u128` and `i128`
3535
are now serialized as numbers instead of strings. Use `Uint128` and `Int128`
3636
instead. ([#1939])
37+
- cosmwasm-std: Make `BalanceResponse`, `AllBalanceResponse`,
38+
`DelegationRewardsResponse`, `DelegatorReward`, `DelegatorValidatorsResponse`,
39+
`PortIdResponse`, `ListChannelsResponse`, `ChannelResponse`,
40+
`BondedDenomResponse`, `AllDelegationsResponse`, `Delegation`,
41+
`DelegationResponse`, `FullDelegation`, `AllValidatorsResponse`,
42+
`ValidatorResponse` and `Validator` non-exhaustive. Add `Validator::create`
43+
and `FullDelegation::create` to allow creating them in a stable way. Use
44+
`Addr` type for `ContractInfoResponse::{creator, admin}`. ([#1883])
3745

3846
[#1874]: https://github.com/CosmWasm/cosmwasm/pull/1874
3947
[#1876]: https://github.com/CosmWasm/cosmwasm/pull/1876
4048
[#1879]: https://github.com/CosmWasm/cosmwasm/pull/1879
49+
[#1883]: https://github.com/CosmWasm/cosmwasm/pull/1883
4150
[#1884]: https://github.com/CosmWasm/cosmwasm/pull/1884
4251
[#1898]: https://github.com/CosmWasm/cosmwasm/pull/1898
4352
[#1902]: https://github.com/CosmWasm/cosmwasm/pull/1902

contracts/hackatom/src/contract.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use sha2::{Digest, Sha256};
22

33
use cosmwasm_std::{
44
entry_point, from_json, to_json_binary, to_json_vec, Addr, AllBalanceResponse, Api, BankMsg,
5-
CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse, Response,
6-
StdError, StdResult, WasmMsg, WasmQuery,
5+
BankQuery, CanonicalAddr, Deps, DepsMut, Env, Event, MessageInfo, QueryRequest, QueryResponse,
6+
Response, StdError, StdResult, WasmMsg, WasmQuery,
77
};
88

99
use crate::errors::HackError;
@@ -269,8 +269,8 @@ fn query_verifier(deps: Deps) -> StdResult<VerifierResponse> {
269269
}
270270

271271
fn query_other_balance(deps: Deps, address: String) -> StdResult<AllBalanceResponse> {
272-
let amount = deps.querier.query_all_balances(address)?;
273-
Ok(AllBalanceResponse { amount })
272+
deps.querier
273+
.query(&BankQuery::AllBalances { address }.into())
274274
}
275275

276276
fn query_recurse(deps: Deps, depth: u32, work: u32, contract: Addr) -> StdResult<RecurseResponse> {

contracts/staking/Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/staking/src/contract.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -412,23 +412,22 @@ mod tests {
412412
use std::str::FromStr;
413413

414414
fn sample_validator(addr: &str) -> Validator {
415-
Validator {
416-
address: addr.to_owned(),
417-
commission: Decimal::percent(3),
418-
max_commission: Decimal::percent(10),
419-
max_change_rate: Decimal::percent(1),
420-
}
415+
Validator::create(
416+
addr.to_owned(),
417+
Decimal::percent(3),
418+
Decimal::percent(10),
419+
Decimal::percent(1),
420+
)
421421
}
422422

423423
fn sample_delegation(validator_addr: &str, amount: Coin) -> FullDelegation {
424-
let can_redelegate = amount.clone();
425-
FullDelegation {
426-
validator: validator_addr.to_owned(),
427-
delegator: Addr::unchecked(MOCK_CONTRACT_ADDR),
424+
FullDelegation::create(
425+
Addr::unchecked(MOCK_CONTRACT_ADDR),
426+
validator_addr.to_owned(),
427+
amount.clone(),
428428
amount,
429-
can_redelegate,
430-
accumulated_rewards: Vec::new(),
431-
}
429+
vec![],
430+
)
432431
}
433432

434433
fn set_validator(querier: &mut MockQuerier) {

contracts/staking/tests/integration.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ static WASM: &[u8] = include_bytes!("../target/wasm32-unknown-unknown/release/st
3434
// static WASM: &[u8] = include_bytes!("../contract.wasm");
3535

3636
fn sample_validator(addr: &str) -> Validator {
37-
Validator {
38-
address: addr.to_owned(),
39-
commission: Decimal::percent(3),
40-
max_commission: Decimal::percent(10),
41-
max_change_rate: Decimal::percent(1),
42-
}
37+
Validator::create(
38+
addr.to_owned(),
39+
Decimal::percent(3),
40+
Decimal::percent(10),
41+
Decimal::percent(1),
42+
)
4343
}
4444

4545
#[test]

packages/std/src/query/bank.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub enum BankQuery {
3535
AllDenomMetadata { pagination: Option<PageRequest> },
3636
}
3737

38-
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
38+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
3939
#[serde(rename_all = "snake_case")]
4040
#[non_exhaustive]
4141
pub struct SupplyResponse {
@@ -48,8 +48,9 @@ impl_response_constructor!(SupplyResponse, amount: Coin);
4848

4949
impl QueryResponseType for SupplyResponse {}
5050

51-
#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq, Eq, JsonSchema)]
51+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
5252
#[serde(rename_all = "snake_case")]
53+
#[non_exhaustive]
5354
pub struct BalanceResponse {
5455
/// Always returns a Coin with the requested denom.
5556
/// This may be of 0 amount if no such funds.
@@ -60,8 +61,9 @@ impl_response_constructor!(BalanceResponse, amount: Coin);
6061

6162
impl QueryResponseType for BalanceResponse {}
6263

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

packages/std/src/query/distribution.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl QueryResponseType for DelegatorWithdrawAddressResponse {}
3939
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L169-L178>
4040
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
4141
#[serde(rename_all = "snake_case")]
42+
#[non_exhaustive]
4243
pub struct DelegationRewardsResponse {
4344
pub rewards: Vec<DecCoin>,
4445
}
@@ -91,13 +92,20 @@ impl_response_constructor!(
9192
impl QueryResponseType for DelegationTotalRewardsResponse {}
9293

9394
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
95+
#[non_exhaustive]
9496
pub struct DelegatorReward {
9597
pub validator_address: String,
9698
pub reward: Vec<DecCoin>,
9799
}
100+
impl_response_constructor!(
101+
DelegatorReward,
102+
validator_address: String,
103+
reward: Vec<DecCoin>
104+
);
98105

99106
/// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L212-L220>
100107
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
108+
#[non_exhaustive]
101109
pub struct DelegatorValidatorsResponse {
102110
pub validators: Vec<String>,
103111
}

packages/std/src/query/ibc.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,23 @@ pub enum IbcQuery {
3131
}
3232

3333
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
34+
#[non_exhaustive]
3435
pub struct PortIdResponse {
3536
pub port_id: String,
3637
}
3738

3839
impl_response_constructor!(PortIdResponse, port_id: String);
3940

4041
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
42+
#[non_exhaustive]
4143
pub struct ListChannelsResponse {
4244
pub channels: Vec<IbcChannel>,
4345
}
4446

4547
impl_response_constructor!(ListChannelsResponse, channels: Vec<IbcChannel>);
4648

4749
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
50+
#[non_exhaustive]
4851
pub struct ChannelResponse {
4952
pub channel: Option<IbcChannel>,
5053
}

packages/std/src/query/staking.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ pub enum StakingQuery {
3636
/// BondedDenomResponse is data format returned from StakingRequest::BondedDenom query
3737
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
3838
#[serde(rename_all = "snake_case")]
39+
#[non_exhaustive]
3940
pub struct BondedDenomResponse {
4041
pub denom: String,
4142
}
@@ -47,6 +48,7 @@ impl_response_constructor!(BondedDenomResponse, denom: String);
4748
/// DelegationsResponse is data format returned from StakingRequest::AllDelegations query
4849
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
4950
#[serde(rename_all = "snake_case")]
51+
#[non_exhaustive]
5052
pub struct AllDelegationsResponse {
5153
pub delegations: Vec<Delegation>,
5254
}
@@ -59,6 +61,7 @@ impl_response_constructor!(AllDelegationsResponse, delegations: Vec<Delegation>)
5961
///
6062
/// Instances are created in the querier.
6163
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
64+
#[non_exhaustive]
6265
pub struct Delegation {
6366
pub delegator: Addr,
6467
/// A validator address (e.g. cosmosvaloper1...)
@@ -67,6 +70,8 @@ pub struct Delegation {
6770
pub amount: Coin,
6871
}
6972

73+
impl_response_constructor!(Delegation, delegator: Addr, validator: String, amount: Coin);
74+
7075
impl From<FullDelegation> for Delegation {
7176
fn from(full: FullDelegation) -> Self {
7277
Delegation {
@@ -80,6 +85,7 @@ impl From<FullDelegation> for Delegation {
8085
/// DelegationResponse is data format returned from StakingRequest::Delegation query
8186
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
8287
#[serde(rename_all = "snake_case")]
88+
#[non_exhaustive]
8389
pub struct DelegationResponse {
8490
pub delegation: Option<FullDelegation>,
8591
}
@@ -93,6 +99,7 @@ impl_response_constructor!(DelegationResponse, delegation: Option<FullDelegation
9399
///
94100
/// Instances are created in the querier.
95101
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
102+
#[non_exhaustive]
96103
pub struct FullDelegation {
97104
pub delegator: Addr,
98105
/// A validator address (e.g. cosmosvaloper1...)
@@ -107,8 +114,40 @@ pub struct FullDelegation {
107114
pub accumulated_rewards: Vec<Coin>,
108115
}
109116

117+
impl_response_constructor!(
118+
FullDelegation,
119+
delegator: Addr,
120+
validator: String,
121+
amount: Coin,
122+
can_redelegate: Coin,
123+
accumulated_rewards: Vec<Coin>
124+
);
125+
126+
impl FullDelegation {
127+
/// Creates a new delegation.
128+
///
129+
/// If fields get added to the [`FullDelegation`] struct in the future, this constructor will
130+
/// provide default values for them, but these default values may not be sensible.
131+
pub fn create(
132+
delegator: Addr,
133+
validator: String,
134+
amount: Coin,
135+
can_redelegate: Coin,
136+
accumulated_rewards: Vec<Coin>,
137+
) -> Self {
138+
Self {
139+
delegator,
140+
validator,
141+
amount,
142+
can_redelegate,
143+
accumulated_rewards,
144+
}
145+
}
146+
}
147+
110148
/// The data format returned from StakingRequest::AllValidators query
111149
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
150+
#[non_exhaustive]
112151
pub struct AllValidatorsResponse {
113152
pub validators: Vec<Validator>,
114153
}
@@ -119,6 +158,7 @@ impl_response_constructor!(AllValidatorsResponse, validators: Vec<Validator>);
119158

120159
/// The data format returned from StakingRequest::Validator query
121160
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
161+
#[non_exhaustive]
122162
pub struct ValidatorResponse {
123163
pub validator: Option<Validator>,
124164
}
@@ -129,6 +169,7 @@ impl_response_constructor!(ValidatorResponse, validator: Option<Validator>);
129169

130170
/// Instances are created in the querier.
131171
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
172+
#[non_exhaustive]
132173
pub struct Validator {
133174
/// The operator address of the validator (e.g. cosmosvaloper1...).
134175
/// 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 {
142183
/// The maximum daily increase of the commission
143184
pub max_change_rate: Decimal,
144185
}
186+
187+
impl_response_constructor!(
188+
Validator,
189+
address: String,
190+
commission: Decimal,
191+
max_commission: Decimal,
192+
max_change_rate: Decimal
193+
);
194+
195+
impl Validator {
196+
/// Creates a new validator.
197+
///
198+
/// If fields get added to the [`Validator`] struct in the future, this constructor will
199+
/// provide default values for them, but these default values may not be sensible.
200+
pub fn create(
201+
address: String,
202+
commission: Decimal,
203+
max_commission: Decimal,
204+
max_change_rate: Decimal,
205+
) -> Self {
206+
Self {
207+
address,
208+
commission,
209+
max_commission,
210+
max_change_rate,
211+
}
212+
}
213+
}

0 commit comments

Comments
 (0)