Skip to content

Commit

Permalink
Tests for CheckPassedProposals
Browse files Browse the repository at this point in the history
  • Loading branch information
nlipartiia-hacken committed Dec 22, 2022
1 parent acbcf41 commit 9f2d7ee
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 3 deletions.
385 changes: 384 additions & 1 deletion contracts/cw-rules/src/tests/daodao.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::{to_binary, Addr, Binary, Uint128};
use cw20::Cw20Coin;
use cw_multi_test::{next_block, App, Executor};
use cw_rules_core::types::{CheckProposalStatus, Status};
use cw_rules_core::types::{CheckPassedProposals, CheckProposalStatus, Status};
use cwd_core::state::ProposalModule;
use cwd_proposal_multiple::proposal::MultipleChoiceProposal;
use cwd_proposal_single::proposal::SingleChoiceProposal;
Expand Down Expand Up @@ -665,3 +665,386 @@ fn test_dao_multiple_proposal_ready() {
}
);
}

#[test]
fn test_single_check_passed_proposals() {
let mut app = App::default();
let code_id = app.store_code(cw_rules_contract());

let instantiate = InstantiateMsg {};
let contract_addr = app
.instantiate_contract(
code_id,
Addr::unchecked(CREATOR_ADDR),
&instantiate,
&[],
"cw-rules",
None,
)
.unwrap();

let proposal_module_code_id = app.store_code(single_proposal_contract());
let threshold = Threshold::AbsolutePercentage {
percentage: PercentageThreshold::Majority {},
};
let max_voting_period = cw_utils::Duration::Height(6);
let instantiate_govmod = cwd_proposal_single::msg::InstantiateMsg {
threshold,
max_voting_period,
min_voting_period: None,
only_members_execute: false,
allow_revoting: false,
close_proposal_on_execution_failure: true,
pre_propose_info: cwd_voting::pre_propose::PreProposeInfo::AnyoneMayPropose {},
};
let governance_addr = instantiate_with_staking_active_threshold(
&mut app,
proposal_module_code_id,
to_binary(&instantiate_govmod).unwrap(),
None,
None,
);
let governance_modules: Vec<ProposalModule> = app
.wrap()
.query_wasm_smart(
governance_addr,
&cwd_core::msg::QueryMsg::ProposalModules {
start_after: None,
limit: None,
},
)
.unwrap();

assert_eq!(governance_modules.len(), 1);
let govmod_single = governance_modules.into_iter().next().unwrap().address;

let govmod_config: cwd_proposal_single::state::Config = app
.wrap()
.query_wasm_smart(
govmod_single.clone(),
&cwd_proposal_single::msg::QueryMsg::Config {},
)
.unwrap();
let dao = govmod_config.dao;
let voting_module: Addr = app
.wrap()
.query_wasm_smart(dao, &cwd_core::msg::QueryMsg::VotingModule {})
.unwrap();
let staking_contract: Addr = app
.wrap()
.query_wasm_smart(
voting_module.clone(),
&cwd_voting_cw20_staked::msg::QueryMsg::StakingContract {},
)
.unwrap();
let token_contract: Addr = app
.wrap()
.query_wasm_smart(
voting_module,
&cwd_interface::voting::Query::TokenContract {},
)
.unwrap();

// Stake some tokens so we can propose
let msg = cw20::Cw20ExecuteMsg::Send {
contract: staking_contract.to_string(),
amount: Uint128::new(2000),
msg: to_binary(&cw20_stake::msg::ReceiveMsg::Stake {}).unwrap(),
};
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
token_contract.clone(),
&msg,
&[],
)
.unwrap();
app.update_block(next_block);

// Create 100 new proposals
for num in 1..101 {
let mut title = "Cron".to_string();
title.push(num.into());
let mut description = "Cat".to_string();
description.push(num.into());
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_single::msg::ExecuteMsg::Propose {
title,
description,
msgs: vec![],
proposer: None,
},
&[],
)
.unwrap();
}

// Neither proposal has passed
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();
assert!(!res.result);

// Approve even proposals
for num in 1..51 {
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_single::msg::ExecuteMsg::Vote {
proposal_id: 2 * num as u64,
vote: Vote::Yes,
},
&[],
)
.unwrap();
}

// Query passed proposals and execute them
for num in 1..51 {
let index: u64 = 2 * num;

// Check that CheckPassedProposals returns index as the number of the first passed proposal
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();
assert_eq!(
res,
QueryResponse {
result: true,
data: to_binary(&index).unwrap()
}
);

// Execute the proposal
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_single::msg::ExecuteMsg::Execute { proposal_id: index },
&[],
)
.unwrap();
}

// All passed proposals are executed
// There shouldn't be any passed proposals
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();
assert!(!res.result,);
}

#[test]
fn test_multiple_check_passed_proposals() {
let mut app = App::default();
let code_id = app.store_code(cw_rules_contract());
let instantiate = InstantiateMsg {};
let contract_addr = app
.instantiate_contract(
code_id,
Addr::unchecked(CREATOR_ADDR),
&instantiate,
&[],
"cw-rules",
None,
)
.unwrap();

let proposal_module_code_id = app.store_code(multiple_proposal_contract());
let voting_strategy = VotingStrategy::SingleChoice {
quorum: PercentageThreshold::Majority {},
};
let max_voting_period = cw_utils::Duration::Height(6);
let instantiate_govmod = cwd_proposal_multiple::msg::InstantiateMsg {
voting_strategy,
max_voting_period,
min_voting_period: None,
only_members_execute: false,
allow_revoting: false,
close_proposal_on_execution_failure: true,
pre_propose_info: cwd_voting::pre_propose::PreProposeInfo::AnyoneMayPropose {},
};
let governance_addr = instantiate_with_staking_active_threshold(
&mut app,
proposal_module_code_id,
to_binary(&instantiate_govmod).unwrap(),
None,
None,
);
let governance_modules: Vec<ProposalModule> = app
.wrap()
.query_wasm_smart(
governance_addr,
&cwd_core::msg::QueryMsg::ProposalModules {
start_after: None,
limit: None,
},
)
.unwrap();

assert_eq!(governance_modules.len(), 1);
let govmod_single = governance_modules.into_iter().next().unwrap().address;

let govmod_config: cwd_proposal_multiple::state::Config = app
.wrap()
.query_wasm_smart(
govmod_single.clone(),
&cwd_proposal_multiple::msg::QueryMsg::Config {},
)
.unwrap();
let dao = govmod_config.dao;
let voting_module: Addr = app
.wrap()
.query_wasm_smart(dao, &cwd_core::msg::QueryMsg::VotingModule {})
.unwrap();
let staking_contract: Addr = app
.wrap()
.query_wasm_smart(
voting_module.clone(),
&cwd_voting_cw20_staked::msg::QueryMsg::StakingContract {},
)
.unwrap();
let token_contract: Addr = app
.wrap()
.query_wasm_smart(
voting_module,
&cwd_interface::voting::Query::TokenContract {},
)
.unwrap();

// Stake some tokens so we can propose
let msg = cw20::Cw20ExecuteMsg::Send {
contract: staking_contract.to_string(),
amount: Uint128::new(2000),
msg: to_binary(&cw20_stake::msg::ReceiveMsg::Stake {}).unwrap(),
};
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
token_contract.clone(),
&msg,
&[],
)
.unwrap();
app.update_block(next_block);

// Create 100 new proposals
for num in 1..101 {
let mut title = "Cron".to_string();
title.push(num.into());
let mut description = "Cat".to_string();
description.push(num.into());
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_multiple::msg::ExecuteMsg::Propose {
title,
description,
choices: MultipleChoiceOptions {
options: vec![
MultipleChoiceOption {
description: "a".to_string(),
msgs: None,
},
MultipleChoiceOption {
description: "b".to_string(),
msgs: None,
},
],
},
proposer: None,
},
&[],
)
.unwrap();
}

// Neither proposal has passed
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();
assert!(!res.result,);

// Vote on even proposals
for num in 1..51 {
app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_multiple::msg::ExecuteMsg::Vote {
proposal_id: 2 * num as u64,
vote: MultipleChoiceVote { option_id: 0 },
},
&[],
)
.unwrap();
}

// Query passed proposals and execute them
for num in 1..51 {
let index: u64 = 2 * num;

// Check that CheckPassedProposals returns index as the number of the first passed proposal
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();

// Execute the proposal
assert_eq!(
res,
QueryResponse {
result: true,
data: to_binary(&index).unwrap()
}
);

app.execute_contract(
Addr::unchecked(CREATOR_ADDR),
govmod_single.clone(),
&cwd_proposal_multiple::msg::ExecuteMsg::Execute { proposal_id: index },
&[],
)
.unwrap();
}

// All passed proposals are executed
// There shouldn't be any passed proposals
let res: QueryResponse<Binary> = app
.wrap()
.query_wasm_smart(
contract_addr.clone(),
&QueryMsg::CheckPassedProposals(CheckPassedProposals {
dao_address: govmod_single.to_string(),
}),
)
.unwrap();
assert!(!res.result,);
}
Loading

0 comments on commit 9f2d7ee

Please sign in to comment.