Skip to content

Commit

Permalink
added test tube tests for instantiate2
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jul 17, 2024
1 parent 2216c70 commit 72c72fd
Show file tree
Hide file tree
Showing 9 changed files with 637 additions and 1 deletion.
8 changes: 8 additions & 0 deletions Cargo.lock

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

14 changes: 13 additions & 1 deletion contracts/external/cw-admin-factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ crate-type = ["cdylib", "rlib"]
backtraces = ["cosmwasm-std/backtraces"]
# use library feature to disable all instantiate/execute/query exports
library = []
# use test tube feature to enable test-tube integration tests, for example
# cargo test --features "test-tube"
test-tube = []
# when writing tests you may wish to enable test-tube as a default feature
# default = ["test-tube"]

[dependencies]
cosmwasm-std = { workspace = true, features = ["cosmwasm_1_2"] }
Expand All @@ -26,7 +31,14 @@ cw-utils = { workspace = true }

[dev-dependencies]
cosmwasm-schema = { workspace = true }
cw-admin-factory = { workspace = true }
cw-multi-test = { workspace = true }
cw20-base = { workspace = true, features = ["library"] }
cw4 = { workspace = true }
dao-dao-core = { workspace = true, features = ["library"] }
dao-interface = { workspace = true }
cw20-base = { workspace = true, features = ["library"] }
dao-proposal-single = { workspace = true }
dao-testing = { workspace = true }
dao-voting = { workspace = true }
dao-voting-cw4 = { workspace = true }
osmosis-test-tube = { workspace = true }
214 changes: 214 additions & 0 deletions contracts/external/cw-admin-factory/src/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
use cosmwasm_std::{
instantiate2_address, testing::mock_dependencies, to_json_binary, Api, Binary, Coin, Decimal,
};
use cw_utils::Duration;
use dao_interface::state::{Admin, ModuleInstantiateInfo};
use dao_testing::test_tube::{
cw4_group::Cw4Group, cw_admin_factory::CwAdminFactory, dao_dao_core::DaoCore,
dao_proposal_single::DaoProposalSingle, dao_voting_cw4::DaoVotingCw4,
};
use dao_voting::{
pre_propose::PreProposeInfo,
threshold::{PercentageThreshold, Threshold},
};
use osmosis_test_tube::{
osmosis_std::types::cosmwasm::wasm::v1::{
QueryCodeRequest, QueryCodeResponse, QueryContractInfoRequest, QueryContractInfoResponse,
},
Account, OsmosisTestApp, Runner, RunnerError,
};

use cw_admin_factory::msg::ExecuteMsg;

use crate::ContractError;

#[test]
fn test_set_self_admin_instantiate2() {
let app = OsmosisTestApp::new();
let accounts = app
.init_accounts(&[Coin::new(1000000000000000u128, "uosmo")], 10)
.unwrap();

let cw_admin_factory = CwAdminFactory::new(&app, None, &accounts[0], &[]).unwrap();
let dao_dao_core_id = DaoCore::upload(&app, &accounts[0]).unwrap();
let cw4_group_id = Cw4Group::upload(&app, &accounts[0]).unwrap();
let dao_voting_cw4_id = DaoVotingCw4::upload(&app, &accounts[0]).unwrap();
let proposal_single_id = DaoProposalSingle::upload(&app, &accounts[0]).unwrap();

// Get DAO core checksum
let dao_core_checksum = app
.query::<QueryCodeRequest, QueryCodeResponse>(
"/cosmwasm.wasm.v1.Query/Code",
&QueryCodeRequest {
code_id: dao_dao_core_id,
},
)
.unwrap()
.code_info
.unwrap()
.data_hash;

let msg = dao_interface::msg::InstantiateMsg {
dao_uri: None,
admin: None,
name: "DAO DAO".to_string(),
description: "A DAO that makes DAO tooling".to_string(),
image_url: None,
automatically_add_cw20s: false,
automatically_add_cw721s: false,
voting_module_instantiate_info: ModuleInstantiateInfo {
code_id: dao_voting_cw4_id,
msg: to_json_binary(&dao_voting_cw4::msg::InstantiateMsg {
group_contract: dao_voting_cw4::msg::GroupContract::New {
cw4_group_code_id: cw4_group_id,
initial_members: vec![cw4::Member {
addr: accounts[0].address(),
weight: 1,
}],
},
})
.unwrap(),
admin: Some(Admin::CoreModule {}),
funds: vec![],
label: "DAO DAO Voting Module".to_string(),
},
proposal_modules_instantiate_info: vec![ModuleInstantiateInfo {
code_id: proposal_single_id,
msg: to_json_binary(&dao_proposal_single::msg::InstantiateMsg {
min_voting_period: None,
threshold: Threshold::ThresholdQuorum {
threshold: PercentageThreshold::Majority {},
quorum: PercentageThreshold::Percent(Decimal::percent(35)),
},
max_voting_period: Duration::Time(432000),
allow_revoting: false,
only_members_execute: true,
close_proposal_on_execution_failure: false,
pre_propose_info: PreProposeInfo::AnyoneMayPropose {},
veto: None,
})
.unwrap(),
admin: Some(Admin::CoreModule {}),
funds: vec![],
label: "DAO DAO Proposal Module".to_string(),
}],
initial_items: None,
};

let salt = Binary::from("salt".as_bytes());
let res = cw_admin_factory
.execute(
&ExecuteMsg::Instantiate2ContractWithSelfAdmin {
instantiate_msg: to_json_binary(&msg).unwrap(),
code_id: dao_dao_core_id,
label: "first".to_string(),
salt: salt.clone(),
expect: None,
},
&[],
&accounts[0],
)
.unwrap();
let instantiate_event = &res.events[2];
assert_eq!(instantiate_event.ty, "instantiate");
let core_addr = instantiate_event.attributes[0].value.clone();

// Check that admin of core address is itself
let core_admin = app
.query::<QueryContractInfoRequest, QueryContractInfoResponse>(
"/cosmwasm.wasm.v1.Query/ContractInfo",
&QueryContractInfoRequest {
address: core_addr.clone(),
},
)
.unwrap()
.contract_info
.unwrap()
.admin;
assert_eq!(core_admin, core_addr.clone());

let deps = mock_dependencies();

// Check that the address matches the predicted address
let canonical_factory = deps
.api
.addr_canonicalize(&cw_admin_factory.contract_addr)
.unwrap();
let expected_addr =
instantiate2_address(&dao_core_checksum, &canonical_factory, salt.as_slice()).unwrap();
let canonical_core = deps.api.addr_canonicalize(&core_addr).unwrap();
assert_eq!(canonical_core, expected_addr);

// Check that it succeeds when expect matches.
let salt = Binary::from("salt_two".as_bytes());
let expected_addr = deps
.api
.addr_humanize(
&instantiate2_address(&dao_core_checksum, &canonical_factory, salt.as_slice()).unwrap(),
)
.unwrap();
let res = cw_admin_factory
.execute(
&ExecuteMsg::Instantiate2ContractWithSelfAdmin {
instantiate_msg: to_json_binary(&msg).unwrap(),
code_id: dao_dao_core_id,
label: "second".to_string(),
salt: salt.clone(),
expect: Some(expected_addr.to_string()),
},
&[],
&accounts[0],
)
.unwrap();
let instantiate_event = &res.events[2];
assert_eq!(instantiate_event.ty, "instantiate");
let core_addr = instantiate_event.attributes[0].value.clone();
assert_eq!(core_addr, expected_addr);

// Check that admin of core address is itself
let core_admin = app
.query::<QueryContractInfoRequest, QueryContractInfoResponse>(
"/cosmwasm.wasm.v1.Query/ContractInfo",
&QueryContractInfoRequest {
address: core_addr.clone(),
},
)
.unwrap()
.contract_info
.unwrap()
.admin;
assert_eq!(core_admin, core_addr.clone());

// Check that it fails when expect does not match.
let salt = Binary::from("salt_mismatch".as_bytes());
let actual_addr = deps
.api
.addr_humanize(
&instantiate2_address(&dao_core_checksum, &canonical_factory, salt.as_slice()).unwrap(),
)
.unwrap();
let err = cw_admin_factory
.execute(
&ExecuteMsg::Instantiate2ContractWithSelfAdmin {
instantiate_msg: to_json_binary(&msg).unwrap(),
code_id: dao_dao_core_id,
label: "third".to_string(),
salt: salt.clone(),
expect: Some("wrong".to_string()),
},
&[],
&accounts[0],
)
.unwrap_err();

assert_eq!(
err,
RunnerError::ExecuteError {
msg: ContractError::UnexpectedContractAddress {
expected: "wrong".to_string(),
actual: actual_addr.to_string(),
}
.to_string()
},
);
}
7 changes: 7 additions & 0 deletions contracts/external/cw-admin-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@ pub mod state;
#[cfg(test)]
mod tests;

// Integrationg tests using an actual chain binary, requires
// the "test-tube" feature to be enabled
// cargo test --features test-tube
#[cfg(test)]
#[cfg(feature = "test-tube")]
mod integration_tests;

pub use crate::error::ContractError;
1 change: 1 addition & 0 deletions packages/dao-testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rand = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

cw-admin-factory = { workspace = true }
cw-core-v1 = { workspace = true, features = ["library"] }
cw-hooks = { workspace = true }
cw-proposal-single-v1 = { workspace = true }
Expand Down
Loading

0 comments on commit 72c72fd

Please sign in to comment.