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

Use cw_serde where appropriate #215

Merged
merged 1 commit into from
Sep 24, 2024
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cosmwasm_2_1 = ["cosmwasm_2_0", "cosmwasm-std/cosmwasm_2_1"]
[dependencies]
anyhow = "1.0.89"
bech32 = "0.11.0"
cosmwasm-schema = "2.1.3"
cosmwasm-std = "2.1.4"
cw-storage-plus = "2.0.0"
cw-utils = "2.0.0"
Expand Down
26 changes: 12 additions & 14 deletions src/test_helpers/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,48 @@
//! Additionally, it bypasses all events and attributes send to it.

use crate::{Contract, ContractWrapper};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, Attribute, Binary, CustomMsg, Deps, DepsMut, Empty, Env, Event, MessageInfo,
Reply, Response, StdError, SubMsg, SubMsgResponse, SubMsgResult,
};
use cw_utils::{parse_execute_response_data, parse_instantiate_response_data};
use schemars::JsonSchema;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt::Debug;
use serde::de::DeserializeOwned;

// Choosing a reply id less than ECHO_EXECUTE_BASE_ID indicates an Instantiate message reply by convention.
// An Execute message reply otherwise.
pub const EXECUTE_REPLY_BASE_ID: u64 = i64::MAX as u64;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cw_serde]
#[derive(Default)]
pub struct Message<ExecC>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
ExecC: CustomMsg + 'static,
{
pub data: Option<String>,
pub sub_msg: Vec<SubMsg<ExecC>>,
pub attributes: Vec<Attribute>,
pub events: Vec<Event>,
}

// This can take some data... but happy to accept {}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[cw_serde]
#[derive(Default)]
pub struct InitMessage<ExecC>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
ExecC: CustomMsg + 'static,
{
pub data: Option<String>,
pub sub_msg: Option<Vec<SubMsg<ExecC>>>,
}

#[allow(clippy::unnecessary_wraps)]
fn instantiate<ExecC>(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InitMessage<ExecC>,
) -> Result<Response<ExecC>, StdError>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
ExecC: CustomMsg + 'static,
{
let mut res = Response::new();
if let Some(data) = msg.data {
Expand All @@ -58,15 +57,14 @@ where
Ok(res)
}

#[allow(clippy::unnecessary_wraps)]
fn execute<ExecC>(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: Message<ExecC>,
) -> Result<Response<ExecC>, StdError>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
ExecC: CustomMsg + 'static,
{
let mut resp = Response::new();

Expand All @@ -84,12 +82,12 @@ fn query(_deps: Deps, _env: Env, msg: Empty) -> Result<Binary, StdError> {
to_json_binary(&msg)
}

#[allow(clippy::unnecessary_wraps, deprecated)]
fn reply<ExecC>(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response<ExecC>, StdError>
where
ExecC: Debug + PartialEq + Clone + JsonSchema + 'static,
ExecC: CustomMsg + 'static,
{
let res = Response::new();
#[allow(deprecated)]
if let Reply {
id,
result: SubMsgResult::Ok(SubMsgResponse {
Expand Down
10 changes: 4 additions & 6 deletions src/test_helpers/hackatom.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
//! Simplified contract which when executed releases the funds to beneficiary

use crate::{Contract, ContractWrapper};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, BankMsg, Binary, CustomMsg, Deps, DepsMut, Empty, Env, MessageInfo, Response,
StdError,
};
use cw_storage_plus::Item;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cw_serde]
pub struct InstantiateMsg {
pub beneficiary: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cw_serde]
pub struct MigrateMsg {
// just use some other string, so we see there are other types
pub new_guy: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum QueryMsg {
// returns InstantiateMsg
Beneficiary {},
Expand Down
6 changes: 3 additions & 3 deletions src/test_helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#![cfg(test)]

use cosmwasm_schema::cw_serde;
use cosmwasm_std::CustomMsg;
use cw_storage_plus::Item;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

pub mod caller;
pub mod echo;
Expand All @@ -19,7 +18,8 @@ pub mod reflect;
pub mod stargate;

/// Custom message for testing purposes.
#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[cw_serde]
#[derive(Default)]
#[serde(rename = "snake_case")]
pub enum CustomHelperMsg {
SetName {
Expand Down
11 changes: 5 additions & 6 deletions src/test_helpers/payout.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
use crate::test_helpers::COUNT;
use crate::{Contract, ContractWrapper};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, BankMsg, Binary, Coin, CustomMsg, Deps, DepsMut, Empty, Env, MessageInfo,
Response, StdError,
};
use cw_storage_plus::Item;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cw_serde]
pub struct InstantiateMessage {
pub payout: Coin,
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cw_serde]
pub struct SudoMsg {
pub set_count: u32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cw_serde]
pub enum QueryMsg {
Count {},
Payout {},
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cw_serde]
pub struct CountResponse {
pub count: u32,
}
Expand Down
6 changes: 3 additions & 3 deletions src/test_helpers/reflect.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
use crate::test_helpers::{payout, CustomHelperMsg, COUNT};
use crate::{Contract, ContractWrapper};
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, Reply, Response,
StdError, SubMsg,
};
use cw_storage_plus::Map;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[cw_serde]
pub struct Message {
pub messages: Vec<SubMsg<CustomHelperMsg>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cw_serde]
pub enum QueryMsg {
Count {},
Reply { id: u64 },
Expand Down
3 changes: 0 additions & 3 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
mod test_app;
mod test_custom_handler;
mod test_error;
#[cfg(feature = "stargate")]
mod test_gov;
#[cfg(feature = "stargate")]
mod test_ibc;
#[cfg(feature = "stargate")]
mod test_stargate;
2 changes: 2 additions & 0 deletions src/tests/test_gov.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "stargate")]

use crate::test_helpers::gov;
use crate::{no_init, App, AppBuilder, Executor, GovAcceptingModule};
use cosmwasm_std::Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/tests/test_ibc.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "stargate")]

use crate::test_helpers::ibc;
use crate::{no_init, App, AppBuilder, Executor, IbcAcceptingModule};
use cosmwasm_std::Empty;
Expand Down
2 changes: 2 additions & 0 deletions src/tests/test_stargate.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "stargate")]

use crate::test_helpers::stargate;
use crate::{no_init, App, AppBuilder, Executor, StargateAccepting};
use cosmwasm_std::Empty;
Expand Down
7 changes: 3 additions & 4 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ mod test_wasm;
mod test_contracts {

pub mod counter {
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{
to_json_binary, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response, StdError,
WasmMsg,
};
use cw_multi_test::{Contract, ContractWrapper};
use cw_storage_plus::Item;
use serde::{Deserialize, Serialize};

const COUNTER: Item<u64> = Item::new("counter");

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum CounterQueryMsg {
Counter {},
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cw_serde]
pub struct CounterResponseMsg {
pub value: u64,
}
Expand Down
9 changes: 3 additions & 6 deletions tests/test_bank/test_init_balance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Coin, CustomMsg, CustomQuery, Uint128};
use cw_multi_test::{custom_app, App, AppBuilder, BasicApp};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

const USER: &str = "user";
const DENOM: &str = "denom";
Expand Down Expand Up @@ -52,15 +51,13 @@ fn initializing_balance_without_builder_should_work() {

#[test]
fn initializing_balance_custom_app_should_work() {
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename = "snake_case")]
#[cw_serde]
pub enum CustomHelperMsg {
HelperMsg,
}
impl CustomMsg for CustomHelperMsg {}

#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
#[serde(rename = "snake_case")]
#[cw_serde]
pub enum CustomHelperQuery {
HelperQuery,
}
Expand Down