Skip to content

Commit

Permalink
Merge pull request #1883 from CosmWasm/1559-non-exhaustive
Browse files Browse the repository at this point in the history
[2.0] Response changes
  • Loading branch information
chipshort authored Nov 8, 2023
2 parents 9a1380a + f6ba5ac commit 0294194
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 50 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions contracts/hackatom/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -269,8 +269,8 @@ fn query_verifier(deps: Deps) -> StdResult<VerifierResponse> {
}

fn query_other_balance(deps: Deps, address: String) -> StdResult<AllBalanceResponse> {
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<RecurseResponse> {
Expand Down
8 changes: 4 additions & 4 deletions contracts/staking/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 12 additions & 13 deletions contracts/staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions contracts/staking/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
8 changes: 5 additions & 3 deletions packages/std/src/query/bank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum BankQuery {
AllDenomMetadata { pagination: Option<PageRequest> },
}

#[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 {
Expand All @@ -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.
Expand All @@ -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<Coin>,
Expand Down
8 changes: 8 additions & 0 deletions packages/std/src/query/distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl QueryResponseType for DelegatorWithdrawAddressResponse {}
/// See <https://github.com/cosmos/cosmos-sdk/blob/c74e2887b0b73e81d48c2f33e6b1020090089ee0/proto/cosmos/distribution/v1beta1/query.proto#L169-L178>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub struct DelegationRewardsResponse {
pub rewards: Vec<DecCoin>,
}
Expand Down Expand Up @@ -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<DecCoin>,
}
impl_response_constructor!(
DelegatorReward,
validator_address: String,
reward: Vec<DecCoin>
);

/// See <https://github.com/cosmos/cosmos-sdk/blob/b0acf60e6c39f7ab023841841fc0b751a12c13ff/proto/cosmos/distribution/v1beta1/query.proto#L212-L220>
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct DelegatorValidatorsResponse {
pub validators: Vec<String>,
}
Expand Down
3 changes: 3 additions & 0 deletions packages/std/src/query/ibc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,23 @@ pub enum IbcQuery {
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct PortIdResponse {
pub port_id: String,
}

impl_response_constructor!(PortIdResponse, port_id: String);

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct ListChannelsResponse {
pub channels: Vec<IbcChannel>,
}

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

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct ChannelResponse {
pub channel: Option<IbcChannel>,
}
Expand Down
69 changes: 69 additions & 0 deletions packages/std/src/query/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -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<Delegation>,
}
Expand All @@ -59,6 +61,7 @@ impl_response_constructor!(AllDelegationsResponse, delegations: Vec<Delegation>)
///
/// 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...)
Expand All @@ -67,6 +70,8 @@ pub struct Delegation {
pub amount: Coin,
}

impl_response_constructor!(Delegation, delegator: Addr, validator: String, amount: Coin);

impl From<FullDelegation> for Delegation {
fn from(full: FullDelegation) -> Self {
Delegation {
Expand All @@ -80,6 +85,7 @@ impl From<FullDelegation> 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<FullDelegation>,
}
Expand All @@ -93,6 +99,7 @@ impl_response_constructor!(DelegationResponse, delegation: Option<FullDelegation
///
/// Instances are created in the querier.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[non_exhaustive]
pub struct FullDelegation {
pub delegator: Addr,
/// A validator address (e.g. cosmosvaloper1...)
Expand All @@ -107,8 +114,40 @@ pub struct FullDelegation {
pub accumulated_rewards: Vec<Coin>,
}

impl_response_constructor!(
FullDelegation,
delegator: Addr,
validator: String,
amount: Coin,
can_redelegate: Coin,
accumulated_rewards: Vec<Coin>
);

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<Coin>,
) -> 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<Validator>,
}
Expand All @@ -119,6 +158,7 @@ impl_response_constructor!(AllValidatorsResponse, validators: Vec<Validator>);

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

/// 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
Expand All @@ -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,
}
}
}
Loading

0 comments on commit 0294194

Please sign in to comment.