Skip to content

Commit

Permalink
fix: temporary nature of proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
gcranju committed Dec 19, 2024
1 parent 5f760f4 commit 21ebffc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 10 additions & 3 deletions contracts/soroban/contracts/multisig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ impl ProposalContract {
}

pub fn create_proposal(env: Env, proposal_data: String, wallet: Address) -> Result<(), ContractError> {

let proposal_id = states::get_count(&env);
let proposal = Proposal {
proposal_id,
proposal_data,
approved: false,
signatures: Vec::new(&env),
wallet
};
states::increase_count(&env);
states::set_proposal(&env, states::get_count(&env), proposal);
states::set_proposal(&env, proposal_id, proposal);
Ok(())
}


pub fn add_approval_signature(env: Env, proposal_id: u32, sender: Address, signature: String) -> Result<(), ContractError> {
sender.require_auth();
let key = states::get_proposal(&env, proposal_id);
if states::is_proposal_expired(&env, proposal_id) {
return Err(ContractError::ProposalExpired);
}
let mut proposal: Proposal = env.storage().temporary().get(&key).unwrap();
if !is_signer(&env, &proposal.wallet, sender.clone()) {
return Err(ContractError::NotAValidSigner);
Expand All @@ -64,11 +68,14 @@ impl ProposalContract {
Ok(())
}

pub fn get_proposals(env: Env) -> Vec<Proposal> {
pub fn get_active_proposals(env: Env) -> Vec<Proposal> {
let count = states::get_count(&env);
let mut proposals = Vec::new(&env);
for i in 0..count {
let key = StorageKey::Proposals(i);
if !env.storage().temporary().has(&key) {
continue;
}
let proposal: Proposal = env.storage().temporary().get(&key).unwrap();
proposals.push_back(proposal);
}
Expand Down
7 changes: 7 additions & 0 deletions contracts/soroban/contracts/multisig/src/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub struct MultisigWallet {
#[contracttype]
#[derive(Clone, Debug)]
pub struct Proposal {
pub proposal_id: u32,
pub proposal_data: String,
pub approved: bool,
pub signatures: Vec<Signature>,
Expand All @@ -39,6 +40,7 @@ pub enum ContractError {
AlreadyVoted = 1,
NotAValidSigner = 2,
AlreadyInitialized = 3,
ProposalExpired = 4
}

const DAY_IN_LEDGERS: u32 = 17280; // assumes 5s a ledger
Expand Down Expand Up @@ -91,6 +93,11 @@ pub fn get_threshold(env: &Env, wallet: Address) -> u32 {
multisig.threshold
}

pub fn is_proposal_expired(env: &Env, proposal_id: u32) -> bool {
let key = StorageKey::Proposals(proposal_id);
!env.storage().temporary().has(&key)
}

pub fn is_initialized(env: &Env) -> bool {
env.storage().instance().has(&StorageKey::Count)
}
Expand Down

0 comments on commit 21ebffc

Please sign in to comment.