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

Final CW4 cleanup #723

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion contracts/voting/dao-voting-cw4/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
Uint128, WasmMsg,
};
use cw2::set_contract_version;
use cw4::{MemberResponse, TotalWeightResponse};
use cw4::{MemberListResponse, MemberResponse, TotalWeightResponse};
use cw_utils::parse_reply_instantiate_data;

use crate::error::ContractError;
Expand Down Expand Up @@ -81,6 +81,20 @@ pub fn instantiate(
}
GroupContract::Existing { address } => {
let group_contract = deps.api.addr_validate(&address)?;

// Validate valid group contract that has at least one member.
let res: MemberListResponse = deps.querier.query_wasm_smart(
group_contract.clone(),
&cw4_group::msg::QueryMsg::ListMembers {
start_after: None,
limit: Some(1),
},
)?;

if res.members.is_empty() {
return Err(ContractError::NoMembers {});
}

GROUP_CONTRACT.save(deps.storage, &group_contract)?;

Ok(Response::new()
Expand Down
4 changes: 2 additions & 2 deletions contracts/voting/dao-voting-cw4/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_std::StdError;
use thiserror::Error;

#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),
Expand All @@ -21,7 +21,7 @@ pub enum ContractError {
#[error("Contract only supports queries")]
NoExecute {},

#[error("Cannot instantiate a group contract with no initial members")]
#[error("Cannot instantiate or use a group contract with no initial members")]
NoMembers {},

#[error("Got a submessage reply with unknown id: {id}")]
Expand Down
18 changes: 2 additions & 16 deletions contracts/voting/dao-voting-cw4/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
use cosmwasm_std::{Addr, Uint128};
use cw_storage_plus::{Item, SnapshotItem, SnapshotMap, Strategy};

pub const USER_WEIGHTS: SnapshotMap<&Addr, Uint128> = SnapshotMap::new(
"user_weights",
"user_weights__checkpoints",
"user_weights__changelog",
Strategy::EveryBlock,
);

pub const TOTAL_WEIGHT: SnapshotItem<Uint128> = SnapshotItem::new(
"total_weight",
"total_weight__checkpoints",
"total_weight__changelog",
Strategy::EveryBlock,
);
use cosmwasm_std::Addr;
use cw_storage_plus::Item;

pub const GROUP_CONTRACT: Item<Addr> = Item::new("group_contract");
pub const DAO: Item<Addr> = Item::new("dao_address");
34 changes: 34 additions & 0 deletions contracts/voting/dao-voting-cw4/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use dao_interface::voting::{
use crate::{
contract::{migrate, CONTRACT_NAME, CONTRACT_VERSION},
msg::{GroupContract, InstantiateMsg, MigrateMsg, QueryMsg},
ContractError,
};

const DAO_ADDR: &str = "dao";
Expand Down Expand Up @@ -150,6 +151,39 @@ pub fn test_instantiate_existing_contract() {
let voting_id = app.store_code(voting_contract());
let cw4_id = app.store_code(cw4_contract());

// Fail with no members.
let cw4_addr = app
.instantiate_contract(
cw4_id,
Addr::unchecked(DAO_ADDR),
&cw4_group::msg::InstantiateMsg {
admin: Some(DAO_ADDR.to_string()),
members: vec![],
},
&[],
"cw4 group",
None,
)
.unwrap();

let err: ContractError = app
.instantiate_contract(
voting_id,
Addr::unchecked(DAO_ADDR),
&InstantiateMsg {
group_contract: GroupContract::Existing {
address: cw4_addr.to_string(),
},
},
&[],
"voting module",
None,
)
.unwrap_err()
.downcast()
.unwrap();
assert_eq!(err, ContractError::NoMembers {});

let cw4_addr = app
.instantiate_contract(
cw4_id,
Expand Down