Skip to content

Commit

Permalink
Remove redundant helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ueco-jb committed Nov 15, 2021
1 parent 3dab901 commit 6078083
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 36 deletions.
19 changes: 12 additions & 7 deletions contracts/cw3-flex-multisig/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ pub fn execute_propose(
let cfg = CONFIG.load(deps.storage)?;

// Only members of the multisig can create a proposal
// Additional check if weight >= 1
let vote_power = cfg
.group_addr
.is_member(&deps.querier, &info.sender, env.block.height)?
.is_member(&deps.querier, &info.sender, None)?
.ok_or(ContractError::Unauthorized {})?;

// max expires also used as default
Expand Down Expand Up @@ -160,8 +159,8 @@ pub fn execute_vote(
// use a snapshot of "start of proposal"
let vote_power = cfg
.group_addr
.is_voting_member(&deps.querier, &info.sender, prop.start_height)
.map_err(|_| ContractError::Unauthorized {})?;
.is_voting_member(&deps.querier, &info.sender, prop.start_height)?
.ok_or(ContractError::Unauthorized {})?;

// cast vote if no vote previously cast
BALLOTS.update(
Expand Down Expand Up @@ -403,7 +402,7 @@ fn list_votes(
fn query_voter(deps: Deps, voter: String) -> StdResult<VoterResponse> {
let cfg = CONFIG.load(deps.storage)?;
let voter_addr = deps.api.addr_validate(&voter)?;
let weight = cfg.group_addr.is_member(&deps.querier, &voter_addr)?;
let weight = cfg.group_addr.is_member(&deps.querier, &voter_addr, None)?;

Ok(VoterResponse { weight })
}
Expand Down Expand Up @@ -916,15 +915,15 @@ mod tests {
// Get the proposal id from the logs
let proposal_id: u64 = res.custom_attrs(1)[2].value.parse().unwrap();

// Owner cannot vote (again)
// Owner with 0 voting power cannot vote
let yes_vote = ExecuteMsg::Vote {
proposal_id,
vote: Vote::Yes,
};
let err = app
.execute_contract(Addr::unchecked(OWNER), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());
assert_eq!(ContractError::Unauthorized {}, err.downcast().unwrap());

// Only voters can vote
let err = app
Expand All @@ -946,6 +945,12 @@ mod tests {
],
);

// VOTER1 cannot vote again
let err = app
.execute_contract(Addr::unchecked(VOTER1), flex_addr.clone(), &yes_vote, &[])
.unwrap_err();
assert_eq!(ContractError::AlreadyVoted {}, err.downcast().unwrap());

// No/Veto votes have no effect on the tally
// Compute the current tally
let tally = get_tally(&app, flex_addr.as_ref(), proposal_id);
Expand Down
40 changes: 11 additions & 29 deletions packages/cw4/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use cosmwasm_std::{
to_binary, Addr, CosmosMsg, Empty, QuerierWrapper, QueryRequest, StdResult, WasmMsg,
WasmQuery,
to_binary, Addr, CosmosMsg, Empty, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery,
};

use crate::msg::Cw4ExecuteMsg;
use crate::query::HooksResponse;
use crate::{
AdminResponse, Cw4QueryMsg, Member, MemberListResponse, MemberResponse, MEMBERS_KEY, TOTAL_KEY,
};
use cw_storage_plus::{Item, Map};
use crate::{AdminResponse, Cw4QueryMsg, Member, MemberListResponse, MemberResponse, TOTAL_KEY};
use cw_storage_plus::Item;

/// Cw4Contract is a wrapper around Addr that provides a lot of helpers
/// for working with cw4 contracts
Expand Down Expand Up @@ -75,45 +72,30 @@ impl Cw4Contract {
Item::new(TOTAL_KEY).query(querier, self.addr())
}

/// Returns member's weight
pub fn member_weight(
&self,
querier: &QuerierWrapper,
addr: &Addr,
height: Option<u64>,
) -> StdResult<Option<u64>> {
match height {
Some(height) => self.member_at_height(querier, addr, height.into()),
None => Map::new(MEMBERS_KEY).query(querier, self.addr(), addr),
}
}

/// Check if this address is a member
/// Check if this address is a member and returns its weight
pub fn is_member<T: Into<String>>(
&self,
querier: &QuerierWrapper,
member: T,
height: impl Into<Option<u64>>,
) -> StdResult<bool> {
match self.member_at_height(querier, member, height.into())? {
Some(_) => Ok(true),
None => Ok(false),
}
) -> StdResult<Option<u64>> {
self.member_at_height(querier, member, height.into())
}

/// Check if this address is a member, and if its weight is >= 1
/// Returns member's weight in positive case
pub fn is_voting_member<T: Into<String>>(
&self,
querier: &QuerierWrapper,
member: T,
height: impl Into<Option<u64>>,
) -> StdResult<bool> {
) -> StdResult<Option<u64>> {
if let Some(weight) = self.member_at_height(querier, member, height.into())? {
if weight >= 1 {
return Ok(true);
return Ok(Some(weight));
}
}
Ok(false)
Ok(None)
}

/// Return the member's weight at the given snapshot - requires a smart query
Expand All @@ -125,7 +107,7 @@ impl Cw4Contract {
) -> StdResult<Option<u64>> {
let query = self.encode_smart_query(Cw4QueryMsg::Member {
addr: member.into(),
at_height: at_height.into(),
at_height,
})?;
let res: MemberResponse = querier.query(&query)?;
Ok(res.weight)
Expand Down

0 comments on commit 6078083

Please sign in to comment.