Skip to content

Commit

Permalink
added pre-propose query to check if an address can propose
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jul 6, 2024
1 parent cf9b913 commit 0729960
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 2 deletions.
20 changes: 20 additions & 0 deletions contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,17 @@ fn get_deposit_info(app: &App, module: Addr, id: u64) -> DepositInfoResponse {
.unwrap()
}

fn query_can_propose(app: &App, module: Addr, address: impl Into<String>) -> bool {
app.wrap()
.query_wasm_smart(
module,
&QueryMsg::CanPropose {
address: address.into(),
},
)
.unwrap()
}

fn update_config(
app: &mut App,
module: Addr,
Expand Down Expand Up @@ -1362,6 +1373,7 @@ fn test_anyone_denylist() {
let rando = "rando";

// Proposal succeeds when anyone can propose.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);

Check failure on line 1376 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_pre_proposal(&mut app, pre_propose.clone(), rando, &[]);

update_config(
Expand All @@ -1375,6 +1387,7 @@ fn test_anyone_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);

Check failure on line 1390 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand All @@ -1398,6 +1411,7 @@ fn test_anyone_denylist() {
);

// Proposing succeeds if not on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);

Check failure on line 1414 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_pre_proposal(&mut app, pre_propose, "ekez", &[]);
}

Expand All @@ -1423,11 +1437,13 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds for member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);

Check failure on line 1440 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_pre_proposal(&mut app, pre_propose.clone(), "ekez", &[]);

let rando = "rando";

// Proposing fails for non-member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);

Check failure on line 1446 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand Down Expand Up @@ -1463,6 +1479,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);

Check failure on line 1482 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_pre_proposal(&mut app, pre_propose.clone(), rando, &[]);

update_config(
Expand All @@ -1478,6 +1495,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);

Check failure on line 1498 in contracts/pre-propose/dao-pre-propose-approval-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand Down Expand Up @@ -1513,6 +1531,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if members not allowed.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand All @@ -1536,6 +1555,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);
make_pre_proposal(&mut app, pre_propose.clone(), rando, &[]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ pub fn execute_reset_approver(
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::CanPropose { address } => {
let approval_contract = PRE_PROPOSE_APPROVAL_CONTRACT.load(deps.storage)?;
let can_propose = address == approval_contract;
to_json_binary(&can_propose)
}
QueryMsg::QueryExtension { msg } => match msg {
QueryExt::PreProposeApprovalContract {} => {
to_json_binary(&PRE_PROPOSE_APPROVAL_CONTRACT.load(deps.storage)?)
Expand Down
35 changes: 35 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 @@ -457,6 +457,17 @@ fn get_latest_proposal_id(app: &App, module: Addr) -> u64 {
props.proposals[props.proposals.len() - 1].id
}

fn query_can_propose(app: &App, module: Addr, address: impl Into<String>) -> bool {
app.wrap()
.query_wasm_smart(
module,
&QueryMsg::CanPropose {
address: address.into(),
},
)
.unwrap()
}

fn update_config(
app: &mut App,
module: Addr,
Expand Down Expand Up @@ -1534,6 +1545,30 @@ fn test_approver_unsupported_update_submission_policy() {
assert_eq!(err, PreProposeError::Unsupported {});
}

#[test]
fn test_approver_can_propose() {
let mut app = App::default();

// Need to instantiate this so contract addresses match with cw20 test cases
let _ = instantiate_cw20_base_default(&mut app);

let DefaultTestSetup {
pre_propose,
pre_propose_approver,
..
} = setup_default_test(&mut app, None, true);

// Only the pre-propose-approval-single contract can propose.
assert_eq!(

Check failure on line 1562 in contracts/pre-propose/dao-pre-propose-approver/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
query_can_propose(&app, pre_propose_approver.clone(), pre_propose),
true
);
assert_eq!(

Check failure on line 1566 in contracts/pre-propose/dao-pre-propose-approver/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
query_can_propose(&app, pre_propose_approver, "someone_else"),
false
);
}

#[test]
fn test_withdraw() {
let mut app = App::default();
Expand Down
20 changes: 20 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 @@ -362,6 +362,17 @@ fn get_deposit_info(app: &App, module: Addr, id: u64) -> DepositInfoResponse {
.unwrap()
}

fn query_can_propose(app: &App, module: Addr, address: impl Into<String>) -> bool {
app.wrap()
.query_wasm_smart(
module,
&QueryMsg::CanPropose {
address: address.into(),
},
)
.unwrap()
}

fn update_config(
app: &mut App,
module: Addr,
Expand Down Expand Up @@ -1035,6 +1046,7 @@ fn test_anyone_denylist() {
let rando = "rando";

// Proposal succeeds when anyone can propose.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -1054,6 +1066,7 @@ fn test_anyone_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand Down Expand Up @@ -1083,6 +1096,7 @@ fn test_anyone_denylist() {
);

// Proposing succeeds if not on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);
make_proposal(&mut app, pre_propose, proposal_single.clone(), "ekez", &[]);
}

Expand All @@ -1108,6 +1122,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds for member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -1119,6 +1134,7 @@ fn test_specific_allowlist_denylist() {
let rando = "rando";

// Proposing fails for non-member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand Down Expand Up @@ -1160,6 +1176,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -1181,6 +1198,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand Down Expand Up @@ -1222,6 +1240,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if members not allowed.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand Down Expand Up @@ -1251,6 +1270,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);
make_proposal(
&mut app,
pre_propose.clone(),
Expand Down
20 changes: 20 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 @@ -343,6 +343,17 @@ fn get_deposit_info(app: &App, module: Addr, id: u64) -> DepositInfoResponse {
.unwrap()
}

fn query_can_propose(app: &App, module: Addr, address: impl Into<String>) -> bool {
app.wrap()
.query_wasm_smart(
module,
&QueryMsg::CanPropose {
address: address.into(),
},
)
.unwrap()
}

fn update_config(
app: &mut App,
module: Addr,
Expand Down Expand Up @@ -971,6 +982,7 @@ fn test_anyone_denylist() {
let rando = "rando";

// Proposal succeeds when anyone can propose.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);

Check failure on line 985 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -990,6 +1002,7 @@ fn test_anyone_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);

Check failure on line 1005 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand All @@ -1013,6 +1026,7 @@ fn test_anyone_denylist() {
);

// Proposing succeeds if not on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);

Check failure on line 1029 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_proposal(&mut app, pre_propose, proposal_single.clone(), "ekez", &[]);
}

Expand All @@ -1038,6 +1052,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds for member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), true);

Check failure on line 1055 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -1049,6 +1064,7 @@ fn test_specific_allowlist_denylist() {
let rando = "rando";

// Proposing fails for non-member.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), false);

Check failure on line 1067 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked(rando),
Expand Down Expand Up @@ -1084,6 +1100,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);

Check failure on line 1103 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_proposal(
&mut app,
pre_propose.clone(),
Expand All @@ -1105,6 +1122,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if on denylist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);

Check failure on line 1125 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand Down Expand Up @@ -1140,6 +1158,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposing fails if members not allowed.
assert_eq!(query_can_propose(&app, pre_propose.clone(), "ekez"), false);

Check failure on line 1161 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
let err: PreProposeError = app
.execute_contract(
Addr::unchecked("ekez"),
Expand All @@ -1163,6 +1182,7 @@ fn test_specific_allowlist_denylist() {
);

// Proposal succeeds if on allowlist.
assert_eq!(query_can_propose(&app, pre_propose.clone(), rando), true);

Check failure on line 1185 in contracts/pre-propose/dao-pre-propose-single/src/tests.rs

View workflow job for this annotation

GitHub Actions / Lints

used `assert_eq!` with a literal bool
make_proposal(
&mut app,
pre_propose.clone(),
Expand Down
20 changes: 18 additions & 2 deletions packages/dao-pre-propose-base/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cosmwasm_schema::schemars::JsonSchema;
use cosmwasm_std::{
to_json_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg,
WasmMsg,
to_json_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
SubMsg, WasmMsg,
};

use cw2::set_contract_version;
Expand Down Expand Up @@ -575,6 +575,22 @@ where
proposer,
})
}
QueryMsg::CanPropose { address } => {
let addr = deps.api.addr_validate(&address)?;
match self.check_can_submit(deps, addr) {
Ok(_) => to_json_binary(&true),
Err(err) => match err {
PreProposeError::SubmissionPolicy(
PreProposeSubmissionPolicyError::Unauthorized {},
) => to_json_binary(&false),
PreProposeError::Std(err) => Err(err),
_ => Err(StdError::generic_err(format!(
"unexpected error: {:?}",
err
))),
},
}
}
QueryMsg::ProposalSubmittedHooks {} => {
to_json_binary(&self.proposal_submitted_hooks.query_hooks(deps)?)
}
Expand Down
3 changes: 3 additions & 0 deletions packages/dao-pre-propose-base/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ where
/// PROPOSAL_ID.
#[returns(DepositInfoResponse)]
DepositInfo { proposal_id: u64 },
/// Returns whether or not the address can submit proposals.
#[returns(bool)]
CanPropose { address: String },
/// Returns list of proposal submitted hooks.
#[returns(cw_hooks::HooksResponse)]
ProposalSubmittedHooks {},
Expand Down

0 comments on commit 0729960

Please sign in to comment.