Skip to content

Commit

Permalink
proposal condorcet testcases implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-1857 committed Apr 10, 2024
1 parent bcd06eb commit 87d4d2e
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 349 deletions.
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 contracts/proposal/dao-proposal-condorcet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ schemars ={ workspace = true }
secret-toolkit ={ workspace = true }
secret-cw-controllers ={ workspace = true }
shade-protocol ={ workspace = true }
query_auth ={ workspace = true }

[dev-dependencies]
cosmwasm-schema = { workspace = true }
Expand Down
35 changes: 11 additions & 24 deletions contracts/proposal/dao-proposal-condorcet/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Propose { choices, key } => execute_propose(deps, env, info, choices, key),
ExecuteMsg::Propose { auth, choices } => execute_propose(deps, env, info, choices, auth),
ExecuteMsg::Vote {
proposal_id,
vote,
key,
} => execute_vote(deps, env, info, proposal_id, vote, key),
ExecuteMsg::Execute { proposal_id, key } => {
execute_execute(deps, env, info, proposal_id, key)
auth,
} => execute_vote(deps, env, info, proposal_id, vote, auth),
ExecuteMsg::Execute { proposal_id, auth } => {
execute_execute(deps, env, info, proposal_id, auth)
}
ExecuteMsg::Close { proposal_id } => execute_close(deps, env, info, proposal_id),

Expand All @@ -117,14 +117,13 @@ fn execute_propose(
env: Env,
info: MessageInfo,
choices: Vec<Choice>,
key: String,
auth: Auth,
) -> Result<Response, ContractError> {
let dao = DAO.load(deps.storage)?;
let config = CONFIG.load(deps.storage)?;
let auth = Auth::ViewingKey {
key,
address: info.sender.clone().to_string(),
};
if choices.is_empty() {
return Err(ContractError::ZeroChoices {});
}
let sender_voting_power = get_voting_power(
deps.as_ref(),
dao.code_hash.clone(),
Expand All @@ -139,10 +138,6 @@ fn execute_propose(
let id = next_proposal_id(deps.storage)?;
let total_power = get_total_power(deps.as_ref(), dao.code_hash.clone(), &dao.addr, None)?;

if choices.is_empty() {
return Err(ContractError::ZeroChoices {});
}

let none_of_the_above = Choice { msgs: vec![] };
let mut choices = choices;
choices.push(none_of_the_above);
Expand Down Expand Up @@ -171,13 +166,9 @@ fn execute_vote(
info: MessageInfo,
proposal_id: u32,
vote: Vec<u32>,
key: String,
auth: Auth,
) -> Result<Response, ContractError> {
let tally = TALLY.get(deps.storage, &proposal_id);
let auth = Auth::ViewingKey {
key,
address: info.sender.clone().to_string(),
};
let sender_power = get_voting_power(
deps.as_ref(),
DAO.load(deps.storage)?.code_hash.clone(),
Expand Down Expand Up @@ -211,14 +202,10 @@ fn execute_execute(
env: Env,
info: MessageInfo,
proposal_id: u32,
key: String,
auth: Auth,
) -> Result<Response, ContractError> {
let tally = TALLY.get(deps.storage, &proposal_id);
let dao = DAO.load(deps.storage)?;
let auth = Auth::ViewingKey {
key,
address: info.sender.clone().to_string(),
};
let sender_power = get_voting_power(
deps.as_ref(),
dao.code_hash.clone(),
Expand Down
4 changes: 2 additions & 2 deletions contracts/proposal/dao-proposal-condorcet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ pub mod proposal;
pub mod state;
pub mod tally;

// #[cfg(test)]
// mod testing;
#[cfg(test)]
mod testing;

pub mod vote;

Expand Down
7 changes: 4 additions & 3 deletions contracts/proposal/dao-proposal-condorcet/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{CosmosMsg, Empty};

use dao_dao_macros::proposal_module_query;
use shade_protocol::basic_staking::Auth;

use crate::config::UncheckedConfig;

Expand All @@ -15,17 +16,17 @@ pub struct Choice {
#[cw_serde]
pub enum ExecuteMsg {
Propose {
auth: Auth,
choices: Vec<Choice>,
key: String,
},
Vote {
auth: Auth,
proposal_id: u32,
vote: Vec<u32>,
key: String,
},
Execute {
auth: Auth,
proposal_id: u32,
key: String,
},
Close {
proposal_id: u32,
Expand Down
75 changes: 75 additions & 0 deletions contracts/proposal/dao-proposal-condorcet/src/testing/contracts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use cosmwasm_std::{from_binary, ContractInfo, Empty, MessageInfo};
use secret_multi_test::{App, Contract, ContractWrapper, Executor};

pub fn _cw4_group_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
cw4_group::contract::execute,
cw4_group::contract::instantiate,
cw4_group::contract::query,
);
Box::new(contract)
}

pub fn proposal_condorcet_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
crate::contract::execute,
crate::contract::instantiate,
crate::contract::query,
)
.with_reply(crate::contract::reply);
Box::new(contract)
}

pub fn _dao_dao_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
dao_dao_core::contract::execute,
dao_dao_core::contract::instantiate,
dao_dao_core::contract::query,
)
.with_reply(dao_dao_core::contract::reply)
.with_migrate(dao_dao_core::contract::migrate);
Box::new(contract)
}

pub fn _dao_voting_cw4_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
dao_voting_cw4::contract::execute,
dao_voting_cw4::contract::instantiate,
dao_voting_cw4::contract::query,
)
.with_reply(dao_voting_cw4::contract::reply);
Box::new(contract)
}

pub fn query_auth_contract() -> Box<dyn Contract<Empty>> {
let contract = ContractWrapper::new(
query_auth::contract::execute,
query_auth::contract::instantiate,
query_auth::contract::query,
);
Box::new(contract)
}

pub(crate) fn create_viewing_key(
app: &mut App,
contract_info: ContractInfo,
info: MessageInfo,
) -> String {
let msg = shade_protocol::contract_interfaces::query_auth::ExecuteMsg::CreateViewingKey {
entropy: "entropy".to_string(),
padding: None,
};
let res = app
.execute_contract(info.sender, &contract_info, &msg, &[])
.unwrap();
let mut viewing_key = String::new();
let data: shade_protocol::contract_interfaces::query_auth::ExecuteAnswer =
from_binary(&res.data.unwrap()).unwrap();
if let shade_protocol::contract_interfaces::query_auth::ExecuteAnswer::CreateViewingKey {
key,
} = data
{
viewing_key = key;
};
viewing_key
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use cosmwasm_std::Decimal;
use cw_utils::Duration;
use cosmwasm_std::{to_binary, Addr, ContractInfo, Decimal};
use dao_voting::threshold::PercentageThreshold;
use secret_multi_test::{App, Executor};
use secret_utils::Duration;

use crate::config::UncheckedConfig;

use super::suite::SuiteBuilder;
use super::{contracts::query_auth_contract, suite::SuiteBuilder};

#[test]
fn test_instantiation() {
Expand All @@ -24,6 +25,7 @@ fn test_instantiate_conflicting_proposal_durations() {
voting_period: Duration::Height(10),
min_voting_period: Some(Duration::Height(11)),
close_proposals_on_execution_failure: true,
dao_code_hash: "dao_code_hash".to_string(),
})
.build();
}
Expand All @@ -38,6 +40,7 @@ fn test_instantiate_conflicting_duration_types() {
voting_period: Duration::Height(10),
min_voting_period: Some(Duration::Time(9)),
close_proposals_on_execution_failure: true,
dao_code_hash: "dao_code_hash".to_string(),
})
.build();
}
Expand All @@ -49,13 +52,36 @@ fn test_instantiate_open_til_expiry() {
voting_period: Duration::Height(10),
min_voting_period: Some(Duration::Height(10)),
close_proposals_on_execution_failure: true,
dao_code_hash: "dao_code_hash".to_string(),
})
.build();
SuiteBuilder::with_config(UncheckedConfig {
quorum: PercentageThreshold::Percent(Decimal::percent(15)),
voting_period: Duration::Time(10),
min_voting_period: Some(Duration::Time(10)),
close_proposals_on_execution_failure: true,
dao_code_hash: "dao_code_hash".to_string(),
})
.build();
}

pub(crate) fn instantiate_query_auth(app: &mut App) -> ContractInfo {
let query_auth_info = app.store_code(query_auth_contract());
let msg = shade_protocol::contract_interfaces::query_auth::InstantiateMsg {
admin_auth: shade_protocol::Contract {
address: Addr::unchecked("admin_contract"),
code_hash: "code_hash".to_string(),
},
prng_seed: to_binary("seed").unwrap(),
};

app.instantiate_contract(
query_auth_info,
Addr::unchecked("creator"),
&msg,
&[],
"query_auth",
None,
)
.unwrap()
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod contracts;
mod instantiation;
mod proposals;
mod suite;
Expand Down
Loading

0 comments on commit 87d4d2e

Please sign in to comment.