From 16c53db3e6a518fbb15d5886061899feec5a988d Mon Sep 17 00:00:00 2001 From: Shane Vitarana Date: Tue, 31 Aug 2021 16:19:06 -0400 Subject: [PATCH 01/12] Make cw721-base queries public --- contracts/cw721-base/src/contract.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/contracts/cw721-base/src/contract.rs b/contracts/cw721-base/src/contract.rs index f2be6b800..8ceefee81 100644 --- a/contracts/cw721-base/src/contract.rs +++ b/contracts/cw721-base/src/contract.rs @@ -388,23 +388,23 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult { } } -fn query_minter(deps: Deps) -> StdResult { +pub fn query_minter(deps: Deps) -> StdResult { let minter_addr = MINTER.load(deps.storage)?; Ok(MinterResponse { minter: minter_addr.to_string(), }) } -fn query_contract_info(deps: Deps) -> StdResult { +pub fn query_contract_info(deps: Deps) -> StdResult { CONTRACT_INFO.load(deps.storage) } -fn query_num_tokens(deps: Deps) -> StdResult { +pub fn query_num_tokens(deps: Deps) -> StdResult { let count = num_tokens(deps.storage)?; Ok(NumTokensResponse { count }) } -fn query_nft_info(deps: Deps, token_id: String) -> StdResult { +pub fn query_nft_info(deps: Deps, token_id: String) -> StdResult { let info = tokens().load(deps.storage, &token_id)?; Ok(NftInfoResponse { name: info.name, @@ -413,7 +413,7 @@ fn query_nft_info(deps: Deps, token_id: String) -> StdResult { }) } -fn query_owner_of( +pub fn query_owner_of( deps: Deps, env: Env, token_id: String, @@ -429,7 +429,7 @@ fn query_owner_of( const DEFAULT_LIMIT: u32 = 10; const MAX_LIMIT: u32 = 30; -fn query_all_approvals( +pub fn query_all_approvals( deps: Deps, env: Env, owner: String, @@ -459,7 +459,7 @@ fn parse_approval(item: StdResult>) -> StdResult, @@ -482,7 +482,7 @@ fn query_tokens( Ok(TokensResponse { tokens }) } -fn query_all_tokens( +pub fn query_all_tokens( deps: Deps, start_after: Option, limit: Option, @@ -499,7 +499,7 @@ fn query_all_tokens( Ok(TokensResponse { tokens: tokens? }) } -fn query_all_nft_info( +pub fn query_all_nft_info( deps: Deps, env: Env, token_id: String, From e5e9e1b653372136b56a1298406eaf29ec587c87 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Mon, 6 Sep 2021 09:22:39 +0200 Subject: [PATCH 02/12] Remove unnecessary empty test --- packages/cw1/src/lib.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/packages/cw1/src/lib.rs b/packages/cw1/src/lib.rs index 4d5fbfadd..a21f8502e 100644 --- a/packages/cw1/src/lib.rs +++ b/packages/cw1/src/lib.rs @@ -5,11 +5,3 @@ pub mod query; pub use crate::helpers::Cw1Contract; pub use crate::msg::Cw1ExecuteMsg; pub use crate::query::{CanExecuteResponse, Cw1QueryMsg}; - -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - // test me - } -} From 9eb8b0cbcce038c270162ed4f538c9d337bcc4a5 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Mon, 6 Sep 2021 14:06:17 +0200 Subject: [PATCH 03/12] Create a base for multitest --- Cargo.lock | 128 ++++++++++++++------- contracts/cw1-whitelist/Cargo.toml | 1 + contracts/cw1-whitelist/multitest.rs | 1 + contracts/cw1-whitelist/multitest/suite.rs | 13 +++ contracts/cw1-whitelist/src/lib.rs | 2 + 5 files changed, 102 insertions(+), 43 deletions(-) create mode 100644 contracts/cw1-whitelist/multitest.rs create mode 100644 contracts/cw1-whitelist/multitest/suite.rs diff --git a/Cargo.lock b/Cargo.lock index bccf218e5..4408ed201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -238,8 +238,8 @@ name = "cw-controllers" version = "0.8.1" dependencies = [ "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "schemars", "serde", "thiserror", @@ -252,8 +252,8 @@ dependencies = [ "anyhow", "cosmwasm-std", "cosmwasm-storage", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "derivative", "itertools", "prost", @@ -262,21 +262,62 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cw-multi-test" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ecca04ea07d99e8cb7031c58ec5a7d8f581d597621353f1ecbee9e2829169d27" +dependencies = [ + "anyhow", + "cosmwasm-std", + "cosmwasm-storage", + "cw-storage-plus 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cw0 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "itertools", + "prost", + "schemars", + "serde", + "thiserror", +] + +[[package]] +name = "cw-storage-plus" +version = "0.8.1" +dependencies = [ + "cosmwasm-std", + "schemars", + "serde", +] + [[package]] name = "cw-storage-plus" version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1e867b9972b83b32e00e878dfbff48299ba26618dabeb19b9c56fae176dc225" +dependencies = [ + "cosmwasm-std", + "schemars", + "serde", +] + +[[package]] +name = "cw0" +version = "0.8.1" dependencies = [ "cosmwasm-std", + "cw-storage-plus 0.8.1", "schemars", "serde", + "thiserror", ] [[package]] name = "cw0" version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c497f885a40918a02df7d938c81809965fa05cfc21b3dc591e9950237b5de0a9" dependencies = [ "cosmwasm-std", - "cw-storage-plus", "schemars", "serde", "thiserror", @@ -298,8 +339,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw1", "cw1-whitelist", "cw2", @@ -314,8 +355,9 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-multi-test 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw1", "cw2", "schemars", @@ -329,7 +371,7 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw0", + "cw0 0.8.1", "schemars", "serde", ] @@ -340,8 +382,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw1155", "cw2", "schemars", @@ -354,7 +396,7 @@ name = "cw2" version = "0.8.1" dependencies = [ "cosmwasm-std", - "cw-storage-plus", + "cw-storage-plus 0.8.1", "schemars", "serde", ] @@ -365,7 +407,7 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw0", + "cw0 0.8.1", "schemars", "serde", ] @@ -376,8 +418,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "hex 0.3.2", @@ -393,8 +435,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "schemars", @@ -408,8 +450,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "cw20-base", @@ -427,9 +469,9 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-multi-test", - "cw-storage-plus", - "cw0", + "cw-multi-test 0.8.1", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "cw20-base", @@ -444,8 +486,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "schemars", @@ -459,8 +501,8 @@ version = "0.7.0" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "hex 0.4.3", @@ -478,8 +520,8 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-controllers", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "cw20-base", @@ -494,7 +536,7 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw0", + "cw0 0.8.1", "schemars", "serde", ] @@ -505,9 +547,9 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-multi-test", - "cw-storage-plus", - "cw0", + "cw-multi-test 0.8.1", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "cw20-base", @@ -523,9 +565,9 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-multi-test", - "cw-storage-plus", - "cw0", + "cw-multi-test 0.8.1", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw3", "cw4", @@ -552,8 +594,8 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-controllers", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw4", "schemars", @@ -568,8 +610,8 @@ dependencies = [ "cosmwasm-schema", "cosmwasm-std", "cw-controllers", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw20", "cw4", @@ -584,7 +626,7 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw0", + "cw0 0.8.1", "schemars", "serde", ] @@ -595,8 +637,8 @@ version = "0.8.1" dependencies = [ "cosmwasm-schema", "cosmwasm-std", - "cw-storage-plus", - "cw0", + "cw-storage-plus 0.8.1", + "cw0 0.8.1", "cw2", "cw721", "schemars", diff --git a/contracts/cw1-whitelist/Cargo.toml b/contracts/cw1-whitelist/Cargo.toml index 0bc76e2bd..75bef945d 100644 --- a/contracts/cw1-whitelist/Cargo.toml +++ b/contracts/cw1-whitelist/Cargo.toml @@ -30,3 +30,4 @@ thiserror = { version = "1.0.23" } [dev-dependencies] cosmwasm-schema = { version = "0.16.0" } +cw-multi-test = "0.8.1" diff --git a/contracts/cw1-whitelist/multitest.rs b/contracts/cw1-whitelist/multitest.rs new file mode 100644 index 000000000..7a4d0e3b2 --- /dev/null +++ b/contracts/cw1-whitelist/multitest.rs @@ -0,0 +1 @@ +mod suite; diff --git a/contracts/cw1-whitelist/multitest/suite.rs b/contracts/cw1-whitelist/multitest/suite.rs new file mode 100644 index 000000000..63e3f229f --- /dev/null +++ b/contracts/cw1-whitelist/multitest/suite.rs @@ -0,0 +1,13 @@ +use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; +use cosmwasm_std::{to_binary, Addr, Binary, Empty, Response, StdError, Uint128}; +use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; + +fn mock_app() -> App { + let env = mock_env(); + let api = MockApi::default(); + let bank = BankKeeper::new(); + let storage = MockStorage::new(); + + App::new(api, env.block, bank, storage) +} + diff --git a/contracts/cw1-whitelist/src/lib.rs b/contracts/cw1-whitelist/src/lib.rs index e5ff7237e..74709ad6d 100644 --- a/contracts/cw1-whitelist/src/lib.rs +++ b/contracts/cw1-whitelist/src/lib.rs @@ -2,5 +2,7 @@ pub mod contract; pub mod error; pub mod msg; pub mod state; +#[cfg(test)] +mod multitest; pub use crate::error::ContractError; From d7e44cd66bc3060f77549d20e095485b58f9d570 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Mon, 6 Sep 2021 16:01:55 +0200 Subject: [PATCH 04/12] Create a multitest suite with inital implementations --- Cargo.lock | 2 + contracts/cw1-whitelist/Cargo.toml | 2 + contracts/cw1-whitelist/multitest.rs | 1 - contracts/cw1-whitelist/multitest/suite.rs | 13 --- contracts/cw1-whitelist/src/multitest.rs | 116 +++++++++++++++++++++ 5 files changed, 120 insertions(+), 14 deletions(-) delete mode 100644 contracts/cw1-whitelist/multitest.rs delete mode 100644 contracts/cw1-whitelist/multitest/suite.rs create mode 100644 contracts/cw1-whitelist/src/multitest.rs diff --git a/Cargo.lock b/Cargo.lock index 4408ed201..001fc328a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -353,6 +353,7 @@ dependencies = [ name = "cw1-whitelist" version = "0.8.1" dependencies = [ + "anyhow", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -360,6 +361,7 @@ dependencies = [ "cw0 0.8.1", "cw1", "cw2", + "derivative", "schemars", "serde", "thiserror", diff --git a/contracts/cw1-whitelist/Cargo.toml b/contracts/cw1-whitelist/Cargo.toml index 75bef945d..dbaf69259 100644 --- a/contracts/cw1-whitelist/Cargo.toml +++ b/contracts/cw1-whitelist/Cargo.toml @@ -29,5 +29,7 @@ serde = { version = "1.0.103", default-features = false, features = ["derive"] } thiserror = { version = "1.0.23" } [dev-dependencies] +anyhow = "1" cosmwasm-schema = { version = "0.16.0" } cw-multi-test = "0.8.1" +derivative = "2" diff --git a/contracts/cw1-whitelist/multitest.rs b/contracts/cw1-whitelist/multitest.rs deleted file mode 100644 index 7a4d0e3b2..000000000 --- a/contracts/cw1-whitelist/multitest.rs +++ /dev/null @@ -1 +0,0 @@ -mod suite; diff --git a/contracts/cw1-whitelist/multitest/suite.rs b/contracts/cw1-whitelist/multitest/suite.rs deleted file mode 100644 index 63e3f229f..000000000 --- a/contracts/cw1-whitelist/multitest/suite.rs +++ /dev/null @@ -1,13 +0,0 @@ -use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{to_binary, Addr, Binary, Empty, Response, StdError, Uint128}; -use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; - -fn mock_app() -> App { - let env = mock_env(); - let api = MockApi::default(); - let bank = BankKeeper::new(); - let storage = MockStorage::new(); - - App::new(api, env.block, bank, storage) -} - diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs new file mode 100644 index 000000000..6203e4b45 --- /dev/null +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -0,0 +1,116 @@ +use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; +use cw_multi_test::{App, BankKeeper, Contract, ContractWrapper, Executor}; +use cosmwasm_std::{Addr, Empty}; +use anyhow::Result; +use cw1::Cw1Contract; +use derivative::Derivative; + +fn mock_app() -> App { + let env = mock_env(); + let api = MockApi::default(); + let bank = BankKeeper::new(); + let storage = MockStorage::new(); + + App::new(api, env.block, bank, storage) +} + +fn contract_cw1() -> Box> { + let contract = ContractWrapper::new( + crate::contract::execute, + crate::contract::instantiate, + crate::contract::query, + ); + Box::new(contract) +} + +/// Configuration of single member +struct MemberConfig { + /// Member address + addr: String, +} + +#[derive(Derivative)] +#[derivative(Debug)] +pub struct Suite { + /// Application mock + #[derivative(Debug = "ignore")] + pub app: App, + /// Special account for performing administrative execution + pub owner: Addr, + /// Members of whitelist + pub members: Vec, + /// cw1 whitelist contract address + pub whitelist: Cw1Contract, +} + +#[derive(Default)] +pub struct Config { + /// Initial members + members: Vec, +} + +impl Config { + pub fn new() -> Self { + Self::default() + } + + pub fn with_member(mut self, addr: &str) -> Self { + self.members.push(MemberConfig { + addr: addr.to_owned(), + }); + + self + } + + pub fn init(self, admins: Vec) -> Result { + let mut app = mock_app(); + let owner = Addr::unchecked("owner"); + let cw1_id = app.store_code(contract_cw1()); + + let members: Vec<_> = self + .members + .into_iter() + .map(|member| -> Result<_> { + let member = MemberConfig { + addr: member.addr.to_string(), + }; + Ok(member) + }) + .collect::>>()?; + + let whitelist = app + .instantiate_contract( + cw1_id, + owner.clone(), + &crate::msg::InstantiateMsg { + admins, + mutable: false, + }, + &[], + "Whitelist", + None, + ) + .unwrap(); + + let members = members + .into_iter() + .map(|member| Addr::unchecked(member.addr)) + .collect(); + + Ok(Suite { + app, + owner, + members, + whitelist: Cw1Contract(whitelist), + }) + } +} + +#[test] +fn execute_freeze() { + let _suite = Config::new() + .with_member("member1") + .with_member("member2") + .init(vec!["member1".to_owned()]) + .unwrap(); +} From 0f141068a558e0c1a30ba90d85b1d4b44370871d Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 12:33:41 +0200 Subject: [PATCH 05/12] Add freeze msg call implementation --- contracts/cw1-whitelist/src/lib.rs | 2 +- contracts/cw1-whitelist/src/multitest.rs | 31 +++++++++++++++++------- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/contracts/cw1-whitelist/src/lib.rs b/contracts/cw1-whitelist/src/lib.rs index 74709ad6d..a8c27c273 100644 --- a/contracts/cw1-whitelist/src/lib.rs +++ b/contracts/cw1-whitelist/src/lib.rs @@ -1,8 +1,8 @@ pub mod contract; pub mod error; pub mod msg; -pub mod state; #[cfg(test)] mod multitest; +pub mod state; pub use crate::error::ContractError; diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs index 6203e4b45..2191e74fd 100644 --- a/contracts/cw1-whitelist/src/multitest.rs +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -1,8 +1,8 @@ +use anyhow::{anyhow, Result}; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cw_multi_test::{App, BankKeeper, Contract, ContractWrapper, Executor}; -use cosmwasm_std::{Addr, Empty}; -use anyhow::Result; +use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, WasmMsg}; use cw1::Cw1Contract; +use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; use derivative::Derivative; fn mock_app() -> App { @@ -43,6 +43,22 @@ pub struct Suite { pub whitelist: Cw1Contract, } +impl Suite { + pub fn freeze(&mut self, addr: &Addr) -> Result { + let freeze_msg: crate::msg::ExecuteMsg = crate::msg::ExecuteMsg::Freeze {}; + let execute: crate::msg::ExecuteMsg = crate::msg::ExecuteMsg::Execute { + msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: addr.to_string(), + msg: to_binary(&freeze_msg)?, + funds: vec![], + })], + }; + self.app + .execute_contract(self.owner.clone(), self.whitelist.addr(), &execute, &[]) + .map_err(|err| anyhow!(err)) + } +} + #[derive(Default)] pub struct Config { /// Initial members @@ -62,7 +78,7 @@ impl Config { self } - pub fn init(self, admins: Vec) -> Result { + pub fn init(self, admins: Vec, mutable: bool) -> Result { let mut app = mock_app(); let owner = Addr::unchecked("owner"); let cw1_id = app.store_code(contract_cw1()); @@ -82,10 +98,7 @@ impl Config { .instantiate_contract( cw1_id, owner.clone(), - &crate::msg::InstantiateMsg { - admins, - mutable: false, - }, + &crate::msg::InstantiateMsg { admins, mutable }, &[], "Whitelist", None, @@ -111,6 +124,6 @@ fn execute_freeze() { let _suite = Config::new() .with_member("member1") .with_member("member2") - .init(vec!["member1".to_owned()]) + .init(vec!["member1".to_owned()], true) .unwrap(); } From b67173e963bcd073870f6bdc59be947c78caf326 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 12:59:02 +0200 Subject: [PATCH 06/12] Simplify member struct --- contracts/cw1-whitelist/src/multitest.rs | 34 +++++++++++------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs index 2191e74fd..7bb522bb1 100644 --- a/contracts/cw1-whitelist/src/multitest.rs +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -23,12 +23,6 @@ fn contract_cw1() -> Box> { Box::new(contract) } -/// Configuration of single member -struct MemberConfig { - /// Member address - addr: String, -} - #[derive(Derivative)] #[derivative(Debug)] pub struct Suite { @@ -62,7 +56,7 @@ impl Suite { #[derive(Default)] pub struct Config { /// Initial members - members: Vec, + members: Vec, } impl Config { @@ -71,25 +65,21 @@ impl Config { } pub fn with_member(mut self, addr: &str) -> Self { - self.members.push(MemberConfig { - addr: addr.to_owned(), - }); + self.members.push(Addr::unchecked(addr)); self } pub fn init(self, admins: Vec, mutable: bool) -> Result { let mut app = mock_app(); - let owner = Addr::unchecked("owner"); + let owner = Addr::unchecked(admins[0].clone()); let cw1_id = app.store_code(contract_cw1()); let members: Vec<_> = self .members .into_iter() - .map(|member| -> Result<_> { - let member = MemberConfig { - addr: member.addr.to_string(), - }; + .map(|address| -> Result<_> { + let member = address.to_string(); Ok(member) }) .collect::>>()?; @@ -107,7 +97,7 @@ impl Config { let members = members .into_iter() - .map(|member| Addr::unchecked(member.addr)) + .map(|address| Addr::unchecked(address)) .collect(); Ok(Suite { @@ -121,9 +111,17 @@ impl Config { #[test] fn execute_freeze() { - let _suite = Config::new() + let mut suite1 = Config::new() .with_member("member1") - .with_member("member2") .init(vec!["member1".to_owned()], true) .unwrap(); + + let mut suite2 = Config::new() + .with_member("member2") + .init(vec!["member2".to_owned()], true) + .unwrap(); + + let member = suite1.members[0].clone(); + + let err_freeze = suite1.freeze(&suite2.members[0]).unwrap_err(); } From f2ef6a2366122fd2bc5678a134fdf5a79500ea1d Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 15:25:36 +0200 Subject: [PATCH 07/12] Working test case; add assert_matches in couple places --- Cargo.lock | 7 +++ contracts/cw1-whitelist/Cargo.toml | 1 + contracts/cw1-whitelist/src/multitest.rs | 64 +++++++++++++++++------- 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 001fc328a..bfd3f5f5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "assert_matches" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b34d609dfbaf33d6889b2b7106d3ca345eacad44200913df5ba02bfd31d2ba9" + [[package]] name = "autocfg" version = "1.0.1" @@ -354,6 +360,7 @@ name = "cw1-whitelist" version = "0.8.1" dependencies = [ "anyhow", + "assert_matches", "cosmwasm-schema", "cosmwasm-std", "cw-multi-test 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/contracts/cw1-whitelist/Cargo.toml b/contracts/cw1-whitelist/Cargo.toml index dbaf69259..ad95d50d8 100644 --- a/contracts/cw1-whitelist/Cargo.toml +++ b/contracts/cw1-whitelist/Cargo.toml @@ -30,6 +30,7 @@ thiserror = { version = "1.0.23" } [dev-dependencies] anyhow = "1" +assert_matches = "1" cosmwasm-schema = { version = "0.16.0" } cw-multi-test = "0.8.1" derivative = "2" diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs index 7bb522bb1..6f4a5322d 100644 --- a/contracts/cw1-whitelist/src/multitest.rs +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -1,6 +1,7 @@ use anyhow::{anyhow, Result}; +use assert_matches::assert_matches; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, WasmMsg}; +use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, QueryRequest, WasmMsg, WasmQuery}; use cw1::Cw1Contract; use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; use derivative::Derivative; @@ -48,7 +49,12 @@ impl Suite { })], }; self.app - .execute_contract(self.owner.clone(), self.whitelist.addr(), &execute, &[]) + .execute_contract( + self.members[0].clone(), + self.whitelist.addr(), + &execute, + &[], + ) .map_err(|err| anyhow!(err)) } } @@ -75,15 +81,6 @@ impl Config { let owner = Addr::unchecked(admins[0].clone()); let cw1_id = app.store_code(contract_cw1()); - let members: Vec<_> = self - .members - .into_iter() - .map(|address| -> Result<_> { - let member = address.to_string(); - Ok(member) - }) - .collect::>>()?; - let whitelist = app .instantiate_contract( cw1_id, @@ -95,7 +92,8 @@ impl Config { ) .unwrap(); - let members = members + let members = self + .members .into_iter() .map(|address| Addr::unchecked(address)) .collect(); @@ -111,17 +109,45 @@ impl Config { #[test] fn execute_freeze() { - let mut suite1 = Config::new() + let mut suite = Config::new() .with_member("member1") .init(vec!["member1".to_owned()], true) .unwrap(); - let mut suite2 = Config::new() - .with_member("member2") - .init(vec!["member2".to_owned()], true) + let cw1_id = suite.app.store_code(contract_cw1()); + let freezable_whitelist = suite + .app + .instantiate_contract( + cw1_id, + Addr::unchecked("member1"), + &crate::msg::InstantiateMsg { + admins: vec![suite.whitelist.0.to_string()], + mutable: true, + }, + &[], + "Whitelist", + None, + ) .unwrap(); - let member = suite1.members[0].clone(); - - let err_freeze = suite1.freeze(&suite2.members[0]).unwrap_err(); + assert_ne!(freezable_whitelist, suite.whitelist.0); + assert_matches!(suite.freeze(&freezable_whitelist), Ok(_)); + + let query_msg: crate::msg::QueryMsg = crate::msg::QueryMsg::AdminList {}; + let query_request = QueryRequest::Wasm(WasmQuery::Smart { + contract_addr: freezable_whitelist.to_string(), + msg: to_binary(&query_msg).unwrap(), + }); + + let resposne = suite.app.wrap().query(&query_request); + assert_matches!( + resposne, + Ok( + crate::msg::AdminListResponse { + admins: _, + mutable + }) => { + assert!(!mutable) + } + ); } From a538576ce2dfa42a4fa82a1bc9edc01b52dddff4 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 16:22:40 +0200 Subject: [PATCH 08/12] Refactoring --- contracts/cw1-whitelist/src/multitest.rs | 113 +++++++++-------------- 1 file changed, 46 insertions(+), 67 deletions(-) diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs index 6f4a5322d..d75b7be49 100644 --- a/contracts/cw1-whitelist/src/multitest.rs +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -1,3 +1,4 @@ +use crate::msg::{AdminListResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use anyhow::{anyhow, Result}; use assert_matches::assert_matches; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; @@ -30,97 +31,74 @@ pub struct Suite { /// Application mock #[derivative(Debug = "ignore")] pub app: App, - /// Special account for performing administrative execution + /// Sender of instantiated contract pub owner: Addr, /// Members of whitelist - pub members: Vec, + pub admins: Vec, /// cw1 whitelist contract address pub whitelist: Cw1Contract, } impl Suite { - pub fn freeze(&mut self, addr: &Addr) -> Result { - let freeze_msg: crate::msg::ExecuteMsg = crate::msg::ExecuteMsg::Freeze {}; - let execute: crate::msg::ExecuteMsg = crate::msg::ExecuteMsg::Execute { - msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: addr.to_string(), - msg: to_binary(&freeze_msg)?, - funds: vec![], - })], - }; - self.app - .execute_contract( - self.members[0].clone(), - self.whitelist.addr(), - &execute, - &[], - ) - .map_err(|err| anyhow!(err)) - } -} - -#[derive(Default)] -pub struct Config { - /// Initial members - members: Vec, -} - -impl Config { - pub fn new() -> Self { - Self::default() - } - - pub fn with_member(mut self, addr: &str) -> Self { - self.members.push(Addr::unchecked(addr)); - - self - } - - pub fn init(self, admins: Vec, mutable: bool) -> Result { + pub fn init(ads: Vec, mutable: bool) -> Result { let mut app = mock_app(); - let owner = Addr::unchecked(admins[0].clone()); + let owner = Addr::unchecked(ads[0].clone()); let cw1_id = app.store_code(contract_cw1()); + let admins = ads + .iter() + .map(|address| Addr::unchecked(address)) + .collect::>(); + let whitelist = app .instantiate_contract( cw1_id, owner.clone(), - &crate::msg::InstantiateMsg { admins, mutable }, + &InstantiateMsg { + admins: ads, + mutable, + }, &[], "Whitelist", None, ) .unwrap(); - let members = self - .members - .into_iter() - .map(|address| Addr::unchecked(address)) - .collect(); - Ok(Suite { app, owner, - members, + admins, whitelist: Cw1Contract(whitelist), }) } + + pub fn freeze(&mut self, sender: Addr, contract_addr: &Addr) -> Result { + let freeze_msg: ExecuteMsg = ExecuteMsg::Freeze {}; + let execute: ExecuteMsg = ExecuteMsg::Execute { + msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { + contract_addr: contract_addr.to_string(), + msg: to_binary(&freeze_msg)?, + funds: vec![], + })], + }; + self.app + .execute_contract(sender, self.whitelist.addr(), &execute, &[]) + .map_err(|err| anyhow!(err)) + } } #[test] fn execute_freeze() { - let mut suite = Config::new() - .with_member("member1") - .init(vec!["member1".to_owned()], true) - .unwrap(); + let owner = Addr::unchecked("owner"); + let mut suite = Suite::init(vec![owner.to_string()], true).unwrap(); let cw1_id = suite.app.store_code(contract_cw1()); - let freezable_whitelist = suite + let second_contract = suite .app .instantiate_contract( cw1_id, - Addr::unchecked("member1"), - &crate::msg::InstantiateMsg { + owner.clone(), + &InstantiateMsg { admins: vec![suite.whitelist.0.to_string()], mutable: true, }, @@ -130,20 +108,21 @@ fn execute_freeze() { ) .unwrap(); - assert_ne!(freezable_whitelist, suite.whitelist.0); - assert_matches!(suite.freeze(&freezable_whitelist), Ok(_)); - - let query_msg: crate::msg::QueryMsg = crate::msg::QueryMsg::AdminList {}; - let query_request = QueryRequest::Wasm(WasmQuery::Smart { - contract_addr: freezable_whitelist.to_string(), - msg: to_binary(&query_msg).unwrap(), - }); + assert_ne!(second_contract, suite.whitelist.0); + assert_matches!(suite.freeze(owner.clone(), &second_contract), Ok(_)); - let resposne = suite.app.wrap().query(&query_request); + let query_msg: QueryMsg = QueryMsg::AdminList {}; assert_matches!( - resposne, + suite + .app + .wrap() + .query(&QueryRequest::Wasm(WasmQuery::Smart { + contract_addr: second_contract.to_string(), + msg: to_binary(&query_msg).unwrap(), + }) + ), Ok( - crate::msg::AdminListResponse { + AdminListResponse { admins: _, mutable }) => { From 675b20d69a64cca550773a237c6ff0eafce0107b Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 16:47:27 +0200 Subject: [PATCH 09/12] Removal of most of unnecessary stuff --- Cargo.lock | 7 +++++ contracts/cw1-whitelist/Cargo.toml | 1 + contracts/cw1-whitelist/src/multitest.rs | 39 +++++++++++------------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bfd3f5f5e..cb0e080e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,6 +369,7 @@ dependencies = [ "cw1", "cw2", "derivative", + "lazy_static", "schemars", "serde", "thiserror", @@ -888,6 +889,12 @@ dependencies = [ "sha2 0.9.6", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.101" diff --git a/contracts/cw1-whitelist/Cargo.toml b/contracts/cw1-whitelist/Cargo.toml index ad95d50d8..698f72068 100644 --- a/contracts/cw1-whitelist/Cargo.toml +++ b/contracts/cw1-whitelist/Cargo.toml @@ -34,3 +34,4 @@ assert_matches = "1" cosmwasm-schema = { version = "0.16.0" } cw-multi-test = "0.8.1" derivative = "2" +lazy_static = "1.4" diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/multitest.rs index d75b7be49..11f557759 100644 --- a/contracts/cw1-whitelist/src/multitest.rs +++ b/contracts/cw1-whitelist/src/multitest.rs @@ -6,6 +6,11 @@ use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, QueryRequest, WasmMsg, Was use cw1::Cw1Contract; use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; use derivative::Derivative; +use lazy_static::lazy_static; + +lazy_static! { + static ref OWNER: String = "owner".to_string(); +} fn mock_app() -> App { let env = mock_env(); @@ -31,31 +36,21 @@ pub struct Suite { /// Application mock #[derivative(Debug = "ignore")] pub app: App, - /// Sender of instantiated contract - pub owner: Addr, - /// Members of whitelist - pub admins: Vec, /// cw1 whitelist contract address pub whitelist: Cw1Contract, } impl Suite { - pub fn init(ads: Vec, mutable: bool) -> Result { + pub fn init(mutable: bool) -> Result { let mut app = mock_app(); - let owner = Addr::unchecked(ads[0].clone()); let cw1_id = app.store_code(contract_cw1()); - let admins = ads - .iter() - .map(|address| Addr::unchecked(address)) - .collect::>(); - let whitelist = app .instantiate_contract( cw1_id, - owner.clone(), + Addr::unchecked(OWNER.clone()), &InstantiateMsg { - admins: ads, + admins: vec![OWNER.clone()], mutable, }, &[], @@ -66,13 +61,11 @@ impl Suite { Ok(Suite { app, - owner, - admins, whitelist: Cw1Contract(whitelist), }) } - pub fn freeze(&mut self, sender: Addr, contract_addr: &Addr) -> Result { + pub fn freeze(&mut self, contract_addr: &Addr) -> Result { let freeze_msg: ExecuteMsg = ExecuteMsg::Freeze {}; let execute: ExecuteMsg = ExecuteMsg::Execute { msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { @@ -82,22 +75,26 @@ impl Suite { })], }; self.app - .execute_contract(sender, self.whitelist.addr(), &execute, &[]) + .execute_contract( + Addr::unchecked(OWNER.clone()), + self.whitelist.addr(), + &execute, + &[], + ) .map_err(|err| anyhow!(err)) } } #[test] fn execute_freeze() { - let owner = Addr::unchecked("owner"); - let mut suite = Suite::init(vec![owner.to_string()], true).unwrap(); + let mut suite = Suite::init(true).unwrap(); let cw1_id = suite.app.store_code(contract_cw1()); let second_contract = suite .app .instantiate_contract( cw1_id, - owner.clone(), + Addr::unchecked(OWNER.clone()), &InstantiateMsg { admins: vec![suite.whitelist.0.to_string()], mutable: true, @@ -109,7 +106,7 @@ fn execute_freeze() { .unwrap(); assert_ne!(second_contract, suite.whitelist.0); - assert_matches!(suite.freeze(owner.clone(), &second_contract), Ok(_)); + assert_matches!(suite.freeze(&second_contract), Ok(_)); let query_msg: QueryMsg = QueryMsg::AdminList {}; assert_matches!( From 182b4f7fb57d0127e3070039220b90add083abb8 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Tue, 7 Sep 2021 16:52:32 +0200 Subject: [PATCH 10/12] Rename multitest.rs file to integration_tests.rs --- .../cw1-whitelist/src/{multitest.rs => integration_tests.rs} | 0 contracts/cw1-whitelist/src/lib.rs | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) rename contracts/cw1-whitelist/src/{multitest.rs => integration_tests.rs} (100%) diff --git a/contracts/cw1-whitelist/src/multitest.rs b/contracts/cw1-whitelist/src/integration_tests.rs similarity index 100% rename from contracts/cw1-whitelist/src/multitest.rs rename to contracts/cw1-whitelist/src/integration_tests.rs diff --git a/contracts/cw1-whitelist/src/lib.rs b/contracts/cw1-whitelist/src/lib.rs index a8c27c273..83686a729 100644 --- a/contracts/cw1-whitelist/src/lib.rs +++ b/contracts/cw1-whitelist/src/lib.rs @@ -1,8 +1,8 @@ pub mod contract; pub mod error; -pub mod msg; #[cfg(test)] -mod multitest; +mod integration_tests; +pub mod msg; pub mod state; pub use crate::error::ContractError; From 0a662cb6a6ad0cf0a692584c6c0584984d0be84b Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Wed, 8 Sep 2021 10:18:26 +0200 Subject: [PATCH 11/12] Respond to some of the comments; Remove lazy_static --- Cargo.lock | 7 ---- contracts/cw1-whitelist/Cargo.toml | 1 - .../cw1-whitelist/src/integration_tests.rs | 36 ++++++++++--------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb0e080e9..bfd3f5f5e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -369,7 +369,6 @@ dependencies = [ "cw1", "cw2", "derivative", - "lazy_static", "schemars", "serde", "thiserror", @@ -889,12 +888,6 @@ dependencies = [ "sha2 0.9.6", ] -[[package]] -name = "lazy_static" -version = "1.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" - [[package]] name = "libc" version = "0.2.101" diff --git a/contracts/cw1-whitelist/Cargo.toml b/contracts/cw1-whitelist/Cargo.toml index 698f72068..ad95d50d8 100644 --- a/contracts/cw1-whitelist/Cargo.toml +++ b/contracts/cw1-whitelist/Cargo.toml @@ -34,4 +34,3 @@ assert_matches = "1" cosmwasm-schema = { version = "0.16.0" } cw-multi-test = "0.8.1" derivative = "2" -lazy_static = "1.4" diff --git a/contracts/cw1-whitelist/src/integration_tests.rs b/contracts/cw1-whitelist/src/integration_tests.rs index 11f557759..f7ff6274a 100644 --- a/contracts/cw1-whitelist/src/integration_tests.rs +++ b/contracts/cw1-whitelist/src/integration_tests.rs @@ -6,11 +6,7 @@ use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, QueryRequest, WasmMsg, Was use cw1::Cw1Contract; use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; use derivative::Derivative; -use lazy_static::lazy_static; - -lazy_static! { - static ref OWNER: String = "owner".to_string(); -} +use serde::{de::DeserializeOwned, Serialize}; fn mock_app() -> App { let env = mock_env(); @@ -38,19 +34,22 @@ pub struct Suite { pub app: App, /// cw1 whitelist contract address pub whitelist: Cw1Contract, + /// Special account + pub owner: String, } impl Suite { pub fn init(mutable: bool) -> Result { let mut app = mock_app(); let cw1_id = app.store_code(contract_cw1()); + let owner = "owner".to_owned(); let whitelist = app .instantiate_contract( cw1_id, - Addr::unchecked(OWNER.clone()), + Addr::unchecked(owner.clone()), &InstantiateMsg { - admins: vec![OWNER.clone()], + admins: vec![owner.clone()], mutable, }, &[], @@ -62,21 +61,24 @@ impl Suite { Ok(Suite { app, whitelist: Cw1Contract(whitelist), + owner, }) } - pub fn freeze(&mut self, contract_addr: &Addr) -> Result { - let freeze_msg: ExecuteMsg = ExecuteMsg::Freeze {}; + pub fn execute(&mut self, contract_addr: &Addr, msg: M) -> Result + where + M: Serialize + DeserializeOwned, + { let execute: ExecuteMsg = ExecuteMsg::Execute { msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { contract_addr: contract_addr.to_string(), - msg: to_binary(&freeze_msg)?, + msg: to_binary(&msg)?, funds: vec![], })], }; self.app .execute_contract( - Addr::unchecked(OWNER.clone()), + Addr::unchecked(self.owner.clone()), self.whitelist.addr(), &execute, &[], @@ -86,15 +88,16 @@ impl Suite { } #[test] -fn execute_freeze() { +fn proxy_freeze_message() { let mut suite = Suite::init(true).unwrap(); + let owner = "owner".to_string(); let cw1_id = suite.app.store_code(contract_cw1()); let second_contract = suite .app .instantiate_contract( cw1_id, - Addr::unchecked(OWNER.clone()), + Addr::unchecked(owner), &InstantiateMsg { admins: vec![suite.whitelist.0.to_string()], mutable: true, @@ -106,7 +109,8 @@ fn execute_freeze() { .unwrap(); assert_ne!(second_contract, suite.whitelist.0); - assert_matches!(suite.freeze(&second_contract), Ok(_)); + let freeze_msg: ExecuteMsg = ExecuteMsg::Freeze {}; + assert_matches!(suite.execute(&second_contract, freeze_msg), Ok(_)); let query_msg: QueryMsg = QueryMsg::AdminList {}; assert_matches!( @@ -120,8 +124,8 @@ fn execute_freeze() { ), Ok( AdminListResponse { - admins: _, - mutable + mutable, + .. }) => { assert!(!mutable) } From 99a057004891429ed1e87ebd22bf82f90eddb153 Mon Sep 17 00:00:00 2001 From: Jakub Bogucki Date: Wed, 8 Sep 2021 11:14:06 +0200 Subject: [PATCH 12/12] Extract execute and query to helper functions --- .../cw1-whitelist/src/integration_tests.rs | 93 +++++++++---------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/contracts/cw1-whitelist/src/integration_tests.rs b/contracts/cw1-whitelist/src/integration_tests.rs index f7ff6274a..91265c7aa 100644 --- a/contracts/cw1-whitelist/src/integration_tests.rs +++ b/contracts/cw1-whitelist/src/integration_tests.rs @@ -2,7 +2,7 @@ use crate::msg::{AdminListResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; use anyhow::{anyhow, Result}; use assert_matches::assert_matches; use cosmwasm_std::testing::{mock_env, MockApi, MockStorage}; -use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, QueryRequest, WasmMsg, WasmQuery}; +use cosmwasm_std::{to_binary, Addr, CosmosMsg, Empty, QueryRequest, StdError, WasmMsg, WasmQuery}; use cw1::Cw1Contract; use cw_multi_test::{App, AppResponse, BankKeeper, Contract, ContractWrapper, Executor}; use derivative::Derivative; @@ -31,47 +31,49 @@ fn contract_cw1() -> Box> { pub struct Suite { /// Application mock #[derivative(Debug = "ignore")] - pub app: App, - /// cw1 whitelist contract address - pub whitelist: Cw1Contract, + app: App, /// Special account pub owner: String, + /// ID of stored code for cw1 contract + cw1_id: u64, } impl Suite { - pub fn init(mutable: bool) -> Result { + pub fn init() -> Result { let mut app = mock_app(); - let cw1_id = app.store_code(contract_cw1()); let owner = "owner".to_owned(); + let cw1_id = app.store_code(contract_cw1()); + + Ok(Suite { app, owner, cw1_id }) + } - let whitelist = app + pub fn instantiate_cw1_contract(&mut self, admins: Vec, mutable: bool) -> Cw1Contract { + let contract = self + .app .instantiate_contract( - cw1_id, - Addr::unchecked(owner.clone()), - &InstantiateMsg { - admins: vec![owner.clone()], - mutable, - }, + self.cw1_id, + Addr::unchecked(self.owner.clone()), + &InstantiateMsg { admins, mutable }, &[], "Whitelist", None, ) .unwrap(); - - Ok(Suite { - app, - whitelist: Cw1Contract(whitelist), - owner, - }) + Cw1Contract(contract) } - pub fn execute(&mut self, contract_addr: &Addr, msg: M) -> Result + pub fn execute( + &mut self, + sender_contract: Addr, + target_contract: &Addr, + msg: M, + ) -> Result where M: Serialize + DeserializeOwned, { let execute: ExecuteMsg = ExecuteMsg::Execute { msgs: vec![CosmosMsg::Wasm(WasmMsg::Execute { - contract_addr: contract_addr.to_string(), + contract_addr: target_contract.to_string(), msg: to_binary(&msg)?, funds: vec![], })], @@ -79,49 +81,42 @@ impl Suite { self.app .execute_contract( Addr::unchecked(self.owner.clone()), - self.whitelist.addr(), + sender_contract, &execute, &[], ) .map_err(|err| anyhow!(err)) } + + pub fn query(&self, target_contract: Addr, msg: M) -> Result + where + M: Serialize + DeserializeOwned, + { + self.app.wrap().query(&QueryRequest::Wasm(WasmQuery::Smart { + contract_addr: target_contract.to_string(), + msg: to_binary(&msg).unwrap(), + })) + } } #[test] fn proxy_freeze_message() { - let mut suite = Suite::init(true).unwrap(); + let mut suite = Suite::init().unwrap(); - let owner = "owner".to_string(); - let cw1_id = suite.app.store_code(contract_cw1()); - let second_contract = suite - .app - .instantiate_contract( - cw1_id, - Addr::unchecked(owner), - &InstantiateMsg { - admins: vec![suite.whitelist.0.to_string()], - mutable: true, - }, - &[], - "Whitelist", - None, - ) - .unwrap(); + let first_contract = suite.instantiate_cw1_contract(vec![suite.owner.clone()], true); + let second_contract = + suite.instantiate_cw1_contract(vec![first_contract.addr().to_string()], true); + assert_ne!(second_contract, first_contract); - assert_ne!(second_contract, suite.whitelist.0); let freeze_msg: ExecuteMsg = ExecuteMsg::Freeze {}; - assert_matches!(suite.execute(&second_contract, freeze_msg), Ok(_)); + assert_matches!( + suite.execute(first_contract.addr(), &second_contract.addr(), freeze_msg), + Ok(_) + ); let query_msg: QueryMsg = QueryMsg::AdminList {}; assert_matches!( - suite - .app - .wrap() - .query(&QueryRequest::Wasm(WasmQuery::Smart { - contract_addr: second_contract.to_string(), - msg: to_binary(&query_msg).unwrap(), - }) - ), + suite.query(second_contract.addr(), query_msg), Ok( AdminListResponse { mutable,