Skip to content

Commit

Permalink
Start planning proposal Status refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jake Hartnell committed Aug 30, 2023
1 parent 3347840 commit 55512f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
21 changes: 16 additions & 5 deletions packages/dao-pre-propose-base/src/execute.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use cosmwasm_schema::schemars::JsonSchema;
use cosmwasm_std::{
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg, WasmMsg,
to_binary, Addr, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult, SubMsg,
Timestamp, WasmMsg,
};

use cw2::set_contract_version;

use cw_denom::UncheckedDenom;
use dao_interface::voting::{Query as CwCoreQuery, VotingPowerAtHeightResponse};
use dao_voting::{
deposit::{DepositRefundPolicy, UncheckedDepositInfo},
status::Status,
};
use serde::Serialize;
use std::mem::discriminant;

use crate::{
error::PreProposeError,
Expand Down Expand Up @@ -280,12 +281,22 @@ where
return Err(PreProposeError::NotModule {});
}

// We match based on the enum variant while ignoring its
// exact value.
let status_variant = discriminant::<Status>(&new_status);
let executed_variant = discriminant::<Status>(&Status::Closed {
at_time: Timestamp::from_seconds(0),
});
let closed_variant = discriminant::<Status>(&Status::Closed {
at_time: Timestamp::from_seconds(0),
});

// If we receive a proposal completed hook from a proposal
// module, and it is not in one of these states, something
// bizare has happened. In that event, this message errors
// which ought to cause the proposal module to remove this
// module and open proposal submission to anyone.
if new_status != Status::Closed && new_status != Status::Executed {
if status_variant != closed_variant && status_variant != executed_variant {
return Err(PreProposeError::NotClosedOrExecuted { status: new_status });
}

Expand All @@ -294,9 +305,9 @@ where
let messages = if let Some(ref deposit_info) = deposit_info {
// Refund can be issued if proposal if it is going to
// closed or executed.
let should_refund_to_proposer = (new_status == Status::Closed
let should_refund_to_proposer = (status_variant == closed_variant
&& deposit_info.refund_policy == DepositRefundPolicy::Always)
|| (new_status == Status::Executed
|| (status_variant == executed_variant
&& deposit_info.refund_policy != DepositRefundPolicy::Never);

if should_refund_to_proposer {
Expand Down
1 change: 1 addition & 0 deletions packages/dao-proposal-hooks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum ProposalHookMsg {
},
ProposalStatusChanged {
id: u64,
// TODO make actual dao_voting::status::Status?
old_status: String,
new_status: String,
},
Expand Down
23 changes: 11 additions & 12 deletions packages/dao-voting/src/status.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,36 @@
use cosmwasm_schema::cw_serde;
use cosmwasm_std::Timestamp;

// TODO add more timestamps for consistency?
// TODO review these if we are going to go through the pain of changing them
#[cw_serde]
#[derive(Copy)]
pub enum Status {
/// The proposal is open for voting.
Open,
/// The proposal has been rejected.
Rejected,
Rejected { at_time: Timestamp },
/// The proposal has been passed but has not been executed.
Passed { at_time: Timestamp },
/// The proposal has been passed and executed.
Executed,
Executed { tx_hash: String },
/// The proposal has failed or expired and has been closed. A
/// proposal deposit refund has been issued if applicable.
Closed,
Closed { at_time: Timestamp },
/// The proposal's execution failed.
ExecutionFailed,
ExecutionFailed { err: String },
/// The proposal has been vetoed.
Vetoed,
Vetoed { rational: Option<String> },
}

impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Open => write!(f, "open"),
Status::Rejected => write!(f, "rejected"),
Status::Rejected { at_time } => write!(f, "rejected {:?}", at_time),
Status::Passed { at_time } => write!(f, "passed {:?}", at_time),
Status::Executed => write!(f, "executed"),
Status::Closed => write!(f, "closed"),
Status::ExecutionFailed => write!(f, "execution_failed"),
Status::Vetoed => write!(f, "vetoed"),
Status::Executed { tx_hash } => write!(f, "executed {:?}", tx_hash),
Status::Closed { at_time } => write!(f, "closed {:?}", at_time),
Status::ExecutionFailed { err } => write!(f, "execution_failed {:?}", err),
Status::Vetoed { rational } => write!(f, "vetoed {:?}", rational),
}
}
}

0 comments on commit 55512f8

Please sign in to comment.