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

Add active threshold functionality to CW4 voting module, closes #781 #819

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ fn setup_default_test(
amount: Uint128::new(8),
},
]),
None,
);
let proposal_modules: Vec<ProposalModule> = app
.wrap()
Expand Down Expand Up @@ -1385,6 +1386,7 @@ fn test_instantiate_with_zero_native_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down Expand Up @@ -1450,6 +1452,7 @@ fn test_instantiate_with_zero_cw20_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down
2 changes: 2 additions & 0 deletions contracts/pre-propose/dao-pre-propose-approver/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ fn setup_default_test(
amount: Uint128::new(8),
},
]),
None,
);
let proposal_modules: Vec<ProposalModule> = app
.wrap()
Expand Down Expand Up @@ -255,6 +256,7 @@ fn setup_default_test(
amount: Uint128::new(8),
},
]),
None,
);
let proposal_modules: Vec<ProposalModule> = app
.wrap()
Expand Down
3 changes: 3 additions & 0 deletions contracts/pre-propose/dao-pre-propose-multiple/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ fn setup_default_test(
amount: Uint128::new(8),
},
]),
None,
);
let proposal_modules: Vec<ProposalModule> = app
.wrap()
Expand Down Expand Up @@ -1088,6 +1089,7 @@ fn test_instantiate_with_zero_native_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down Expand Up @@ -1151,6 +1153,7 @@ fn test_instantiate_with_zero_cw20_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down
3 changes: 3 additions & 0 deletions contracts/pre-propose/dao-pre-propose-single/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ fn setup_default_test(
amount: Uint128::new(8),
},
]),
None,
);
let proposal_modules: Vec<ProposalModule> = app
.wrap()
Expand Down Expand Up @@ -1024,6 +1025,7 @@ fn test_instantiate_with_zero_native_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down Expand Up @@ -1087,6 +1089,7 @@ fn test_instantiate_with_zero_cw20_deposit() {
amount: Uint128::new(8),
},
]),
None,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dao_interface::{
use dao_testing::contracts::{
cw4_group_contract, dao_dao_contract, dao_voting_cw4_contract, proposal_condorcet_contract,
};
use dao_voting::threshold::PercentageThreshold;
use dao_voting::threshold::{ActiveThreshold, PercentageThreshold};
use dao_voting_cw4::msg::GroupContract;

use crate::{
Expand All @@ -30,6 +30,7 @@ pub(crate) struct SuiteBuilder {
pub instantiate: InstantiateMsg,
with_proposal: Option<u32>,
with_voters: Vec<(String, u64)>,
active_threshold: Option<ActiveThreshold>,
}

impl Default for SuiteBuilder {
Expand All @@ -43,6 +44,7 @@ impl Default for SuiteBuilder {
},
with_proposal: None,
with_voters: vec![("sender".to_string(), 10)],
active_threshold: None,
}
}
}
Expand Down Expand Up @@ -93,6 +95,7 @@ impl SuiteBuilder {
cw4_group_code_id: cw4_id,
initial_members,
},
active_threshold: self.active_threshold.clone(),
})
.unwrap(),
admin: Some(Admin::CoreModule {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ pub fn _instantiate_with_cw4_groups_governance(
app: &mut App,
proposal_module_instantiate: InstantiateMsg,
initial_weights: Option<Vec<Cw20Coin>>,
active_threshold: Option<dao_voting::threshold::ActiveThreshold>,
) -> Addr {
let proposal_module_code_id = app.store_code(proposal_multiple_contract());
let cw4_id = app.store_code(cw4_group_contract());
Expand Down Expand Up @@ -813,6 +814,7 @@ pub fn _instantiate_with_cw4_groups_governance(
cw4_group_code_id: cw4_id,
initial_members: initial_weights,
},
active_threshold,
})
.unwrap(),
admin: Some(Admin::CoreModule {}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ pub(crate) fn instantiate_with_cw4_groups_governance(
cw4_group_code_id: cw4_id,
initial_members: initial_weights,
},
active_threshold: None,
})
.unwrap(),
admin: Some(Admin::CoreModule {}),
Expand Down
11 changes: 11 additions & 0 deletions contracts/proposal/dao-proposal-single/src/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ fn test_timelocked_proposal_veto_expired_timelock() -> anyhow::Result<()> {
},
]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1141,6 +1142,7 @@ fn test_timelocked_proposal_execute_no_early_exec() -> anyhow::Result<()> {
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1225,6 +1227,7 @@ fn test_timelocked_proposal_execute_early() -> anyhow::Result<()> {
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1315,6 +1318,7 @@ fn test_timelocked_proposal_execute_active_timelock_unauthorized() -> anyhow::Re
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1406,6 +1410,7 @@ fn test_timelocked_proposal_execute_expired_timelock_not_vetoer() -> anyhow::Res
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1492,6 +1497,7 @@ fn test_proposal_message_timelock_veto() -> anyhow::Result<()> {
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1617,6 +1623,7 @@ fn test_proposal_message_timelock_early_execution() -> anyhow::Result<()> {
},
]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1704,6 +1711,7 @@ fn test_proposal_message_timelock_veto_before_passed() {
},
]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1777,6 +1785,7 @@ fn test_veto_only_members_execute_proposal() -> anyhow::Result<()> {
amount: Uint128::new(85),
}]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -1902,6 +1911,7 @@ fn test_proposal_cant_close_after_expiry_is_passed() {
},
]),
);

let proposal_module = query_single_proposal_module(&app, &core_addr);
let gov_token = query_dao_token(&app, &core_addr);

Expand Down Expand Up @@ -2629,6 +2639,7 @@ fn test_min_duration_same_as_proposal_duration() {
},
]),
);

let gov_token = query_dao_token(&app, &core_addr);
let proposal_module = query_single_proposal_module(&app, &core_addr);

Expand Down
1 change: 1 addition & 0 deletions contracts/voting/dao-voting-cw4/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dao-dao-macros = { workspace = true }
dao-interface = { workspace = true }
cw4 = { workspace = true }
cw4-group = { workspace = true }
dao-voting = { workspace = true }

[dev-dependencies]
cw-multi-test = { workspace = true }
104 changes: 98 additions & 6 deletions contracts/voting/dao-voting-cw4/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ use cw2::{get_contract_version, set_contract_version, ContractVersion};
use cw4::{MemberListResponse, MemberResponse, TotalWeightResponse};
use cw_utils::parse_reply_instantiate_data;

use dao_interface::voting::IsActiveResponse;
use dao_voting::threshold::ActiveThreshold;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, GroupContract, InstantiateMsg, MigrateMsg, QueryMsg};
use crate::state::{DAO, GROUP_CONTRACT};
use crate::state::{ACTIVE_THRESHOLD, DAO, GROUP_CONTRACT};

pub(crate) const CONTRACT_NAME: &str = "crates.io:dao-voting-cw4";
pub(crate) const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -26,6 +29,19 @@ pub fn instantiate(
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;

// Validate and save the active threshold if provided
if let Some(threshold) = msg.active_threshold {
if let ActiveThreshold::AbsoluteCount { count } = threshold {
if count > Uint128::zero() {
ACTIVE_THRESHOLD.save(deps.storage, &threshold)?;
} else {
return Err(ContractError::InvalidThreshold {});
}
} else {
return Err(ContractError::InvalidThreshold {});
}
}

DAO.save(deps.storage, &info.sender)?;

match msg.group_contract {
Expand Down Expand Up @@ -108,12 +124,69 @@ pub fn instantiate(

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
_deps: DepsMut,
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::UpdateActiveThreshold { new_threshold } => {
let dao = DAO.load(deps.storage)?;
if info.sender != dao {
return Err(ContractError::Unauthorized {});
}
if let Some(threshold) = new_threshold {
if let ActiveThreshold::AbsoluteCount { count } = threshold {
if count > Uint128::zero() {
ACTIVE_THRESHOLD.save(deps.storage, &threshold)?;
} else {
return Err(ContractError::InvalidThreshold {});
}
} else {
return Err(ContractError::InvalidThreshold {});
}
} else {
ACTIVE_THRESHOLD.remove(deps.storage);
}

Ok(Response::new()
.add_attribute("method", "update_active_threshold")
.add_attribute("status", "success"))
}
}
}

pub fn execute_update_active_threshold(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: ExecuteMsg,
info: MessageInfo,
new_active_threshold: Option<ActiveThreshold>,
) -> Result<Response, ContractError> {
Err(ContractError::NoExecute {})
let dao = DAO.load(deps.storage)?;
if info.sender != dao {
return Err(ContractError::Unauthorized {});
}

if let Some(active_threshold) = new_active_threshold {
match active_threshold {
ActiveThreshold::AbsoluteCount { count } => {
if count.is_zero() {
return Err(ContractError::InvalidThreshold {});
}
ACTIVE_THRESHOLD.save(deps.storage, &active_threshold)?;
}
// Reject percentage-based thresholds
ActiveThreshold::Percentage { .. } => {
return Err(ContractError::InvalidThreshold {});
}
}
} else {
ACTIVE_THRESHOLD.remove(deps.storage);
}

Ok(Response::new()
.add_attribute("method", "update_active_threshold")
.add_attribute("status", "success"))
}

#[cfg_attr(not(feature = "library"), entry_point)]
Expand All @@ -126,6 +199,7 @@ pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
QueryMsg::Info {} => query_info(deps),
QueryMsg::GroupContract {} => to_json_binary(&GROUP_CONTRACT.load(deps.storage)?),
QueryMsg::Dao {} => to_json_binary(&DAO.load(deps.storage)?),
QueryMsg::IsActive {} => query_is_active(deps),
}
}

Expand Down Expand Up @@ -164,10 +238,28 @@ pub fn query_total_power_at_height(deps: Deps, env: Env, height: Option<u64>) ->
}

pub fn query_info(deps: Deps) -> StdResult<Binary> {
let info = cw2::get_contract_version(deps.storage)?;
let info = get_contract_version(deps.storage)?;
to_json_binary(&dao_interface::voting::InfoResponse { info })
}

pub fn query_is_active(deps: Deps) -> StdResult<Binary> {
let active_threshold = ACTIVE_THRESHOLD.load(deps.storage)?;
let group_contract = GROUP_CONTRACT.load(deps.storage)?;
let total_weight: TotalWeightResponse = deps.querier.query_wasm_smart(
group_contract,
&cw4_group::msg::QueryMsg::TotalWeight { at_height: None },
)?;

let is_active = match active_threshold {
ActiveThreshold::AbsoluteCount { count } => {
Uint128::new(total_weight.weight as u128) >= count
}
_ => false, // Should never happen as percentage is not supported
};

to_json_binary(&IsActiveResponse { active: is_active })
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, ContractError> {
let storage_version: ContractVersion = get_contract_version(deps.storage)?;
Expand Down
Loading
Loading