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

Generic module types #425

Merged
merged 10 commits into from
Sep 19, 2021
16 changes: 8 additions & 8 deletions contracts/cw20-escrow/src/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

use cosmwasm_std::{coins, to_binary, Addr, Empty, Uint128};
use cw20::{Cw20Coin, Cw20Contract, Cw20ExecuteMsg};
use cw_multi_test::{App, AppBuilder, Contract, ContractWrapper, Executor};
use cw_multi_test::{App, Contract, ContractWrapper, Executor};

use crate::msg::{CreateMsg, DetailsResponse, ExecuteMsg, InstantiateMsg, QueryMsg, ReceiveMsg};

fn mock_app() -> App {
AppBuilder::new().build()
}

pub fn contract_escrow() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
crate::contract::execute,
Expand All @@ -31,12 +27,16 @@ pub fn contract_cw20() -> Box<dyn Contract<Empty>> {
#[test]
// receive cw20 tokens and release upon approval
fn escrow_happy_path_cw20_tokens() {
let mut router = mock_app();

// set personal balance
let owner = Addr::unchecked("owner");
let init_funds = coins(2000, "btc");
router.init_bank_balance(&owner, init_funds).unwrap();

let mut router = App::new(|router, _, storage| {
router
.bank
.init_balance(storage, &owner, init_funds)
.unwrap();
});

// set up cw20 contract with some tokens
let cw20_id = router.store_code(contract_cw20());
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw3-fixed-multisig/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use cw0::Duration;
use cw20::{BalanceResponse, MinterResponse};
use cw20_base::msg::QueryMsg;
use cw3::Vote;
use cw_multi_test::{App, AppBuilder, Contract, ContractWrapper, Executor};
use cw_multi_test::{App, Contract, ContractWrapper, Executor};

fn mock_app() -> App {
AppBuilder::new().build()
App::default()
}

pub fn contract_cw3_fixed_multisig() -> Box<dyn Contract<Empty>> {
Expand Down
113 changes: 47 additions & 66 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,13 @@ mod tests {
Box::new(contract)
}

fn mock_app() -> App {
AppBuilder::new().build()
fn mock_app(init_funds: &[Coin]) -> App {
AppBuilder::new().build(|router, _, storage| {
router
.bank
.init_balance(storage, &Addr::unchecked(OWNER), init_funds.to_vec())
.unwrap();
})
}

// uploads code and returns address of group contract
Expand Down Expand Up @@ -560,7 +565,8 @@ mod tests {

// Bonus: set some funds on the multisig contract for future proposals
if !init_funds.is_empty() {
app.init_bank_balance(&flex_addr, init_funds).unwrap();
app.send_tokens(Addr::unchecked(OWNER), flex_addr.clone(), &init_funds)
.unwrap();
}
(flex_addr, group_addr)
}
Expand Down Expand Up @@ -588,7 +594,7 @@ mod tests {

#[test]
fn test_instantiate_works() {
let mut app = mock_app();
let mut app = mock_app(&[]);

// make a simple group
let group_addr = instantiate_group(&mut app, vec![member(OWNER, 1)]);
Expand Down Expand Up @@ -684,17 +690,13 @@ mod tests {

#[test]
fn test_propose_works() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 4;
let voting_period = Duration::Time(2000000);
let (flex_addr, _) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
false,
);
let (flex_addr, _) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, false);

let proposal = pay_somebody_proposal();
// Only voters can propose
Expand Down Expand Up @@ -793,17 +795,13 @@ mod tests {

#[test]
fn test_proposal_queries() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 3;
let voting_period = Duration::Time(2000000);
let (flex_addr, _) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
false,
);
let (flex_addr, _) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, false);

// create proposal with 1 vote power
let proposal = pay_somebody_proposal();
Expand Down Expand Up @@ -889,17 +887,13 @@ mod tests {

#[test]
fn test_vote_works() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 3;
let voting_period = Duration::Time(2000000);
let (flex_addr, _) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
false,
);
let (flex_addr, _) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, false);

// create proposal with 0 vote power
let proposal = pay_somebody_proposal();
Expand Down Expand Up @@ -1041,17 +1035,13 @@ mod tests {

#[test]
fn test_execute_works() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 3;
let voting_period = Duration::Time(2000000);
let (flex_addr, _) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
true,
);
let (flex_addr, _) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, true);

// ensure we have cash to cover the proposal
let contract_bal = app.wrap().query_balance(&flex_addr, "BTC").unwrap();
Expand Down Expand Up @@ -1134,17 +1124,13 @@ mod tests {

#[test]
fn test_close_works() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 3;
let voting_period = Duration::Height(2000000);
let (flex_addr, _) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
true,
);
let (flex_addr, _) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, true);

// create proposal with 0 vote power
let proposal = pay_somebody_proposal();
Expand Down Expand Up @@ -1187,17 +1173,13 @@ mod tests {
// uses the power from the beginning of the voting period
#[test]
fn execute_group_changes_from_external() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 4;
let voting_period = Duration::Time(20000);
let (flex_addr, group_addr) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
false,
);
let (flex_addr, group_addr) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, false);

// VOTER1 starts a proposal to send some tokens (1/4 votes)
let proposal = pay_somebody_proposal();
Expand Down Expand Up @@ -1316,17 +1298,13 @@ mod tests {
// trigger the action
#[test]
fn execute_group_changes_from_proposal() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

let required_weight = 4;
let voting_period = Duration::Time(20000);
let (flex_addr, group_addr) = setup_test_case_fixed(
&mut app,
required_weight,
voting_period,
coins(10, "BTC"),
true,
);
let (flex_addr, group_addr) =
setup_test_case_fixed(&mut app, required_weight, voting_period, init_funds, true);

// Start a proposal to remove VOTER3 from the set
let update_msg = Cw4GroupContract::new(group_addr)
Expand Down Expand Up @@ -1436,7 +1414,8 @@ mod tests {
// uses the power from the beginning of the voting period
#[test]
fn percentage_handles_group_changes() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

// 33% required, which is 5 of the initial 15
let voting_period = Duration::Time(20000);
Expand All @@ -1446,7 +1425,7 @@ mod tests {
percentage: Decimal::percent(33),
},
voting_period,
coins(10, "BTC"),
init_funds,
false,
);

Expand Down Expand Up @@ -1516,7 +1495,8 @@ mod tests {
// uses the power from the beginning of the voting period
#[test]
fn quorum_handles_group_changes() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

// 33% required for quora, which is 5 of the initial 15
// 50% yes required to pass early (8 of the initial 15)
Expand All @@ -1528,7 +1508,7 @@ mod tests {
quorum: Decimal::percent(33),
},
voting_period,
coins(10, "BTC"),
init_funds,
false,
);

Expand Down Expand Up @@ -1584,7 +1564,8 @@ mod tests {

#[test]
fn quorum_enforced_even_if_absolute_threshold_met() {
let mut app = mock_app();
let init_funds = coins(10, "BTC");
let mut app = mock_app(&init_funds);

// 33% required for quora, which is 5 of the initial 15
// 50% yes required to pass early (8 of the initial 15)
Expand All @@ -1597,7 +1578,7 @@ mod tests {
quorum: Decimal::percent(80),
},
voting_period,
coins(10, "BTC"),
init_funds,
false,
);

Expand Down
Loading