Skip to content

Commit

Permalink
store executed result, panic on execution error
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed Jul 19, 2024
1 parent a4c30de commit fa49279
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions x/programs/rust/examples/multisig/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use borsh::{BorshDeserialize, BorshSerialize};
use wasmlanche_sdk::{
public, state_keys, Address, Context, DeferDeserialize, ExternalCallError, Program,
};
use wasmlanche_sdk::{public, state_keys, Address, Context, DeferDeserialize, Program};

pub const MIN_VOTES: u64 = 2;

Expand All @@ -11,7 +9,7 @@ pub struct Proposal {
method: String,
args: Vec<u8>,
value: u64,
executed: bool,
executed_result: Option<DeferDeserialize>,
voters_len: u64,
}

Expand All @@ -28,7 +26,6 @@ pub enum ProposalError {
NonexistentProposal,
QuorumNotReached,
NotVoter,
ExecutionFailed(ExternalCallError),
}

#[state_keys]
Expand Down Expand Up @@ -82,7 +79,7 @@ pub fn propose(
method,
args,
value,
executed: false,
executed_result: None,
voters_len,
},
)
Expand Down Expand Up @@ -119,7 +116,7 @@ pub fn vote(
let proposal =
internal::get_proposal(&context, proposal_id).ok_or(ProposalError::NonexistentProposal)?;

if proposal.executed {
if proposal.executed_result.is_some() {
return Err(ProposalError::AlreadyExecuted);
}

Expand Down Expand Up @@ -171,11 +168,11 @@ pub fn execute(
context: Context<StateKeys>,
proposal_id: u64,
max_units: u64,
) -> Result<DeferDeserialize, ProposalError> {
) -> Result<(), ProposalError> {
let mut proposal =
internal::get_proposal(&context, proposal_id).ok_or(ProposalError::NonexistentProposal)?;

if proposal.executed {
if proposal.executed_result.is_some() {
return Err(ProposalError::AlreadyExecuted);
}

Expand All @@ -192,17 +189,19 @@ pub fn execute(
return Err(ProposalError::QuorumNotReached);
}

proposal.executed = true;

proposal
let result = proposal
.to
.call_function::<DeferDeserialize>(
&proposal.method,
&proposal.args,
max_units,
proposal.value,
)
.map_err(ProposalError::ExecutionFailed)
.expect("the external execution failed");

proposal.executed_result = Some(result);

Ok(())
}

/// Return the id of the next proposal.
Expand Down

0 comments on commit fa49279

Please sign in to comment.