Skip to content

Commit

Permalink
voting on proposals
Browse files Browse the repository at this point in the history
  • Loading branch information
SkymanOne committed Jul 31, 2022
1 parent 193bdd0 commit 188eb29
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
62 changes: 54 additions & 8 deletions pallets/slashing-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod types;

#[frame_support::pallet]
pub mod pallet {
use crate::types::{Proposal, Data};
use crate::types::{Proposal, Data, Vote};
use frame_support::dispatch::{DispatchError, DispatchResult};
use frame_support::ensure;
use frame_support::pallet_prelude::CountedStorageMap;
Expand Down Expand Up @@ -58,21 +58,16 @@ pub mod pallet {
account: T::AccountId,
cashout: BalanceOf<T>,
},
/// A motion (given hash) has been proposed (by given account) with a threshold (given
/// `MemberCount`).
/// A motion (given hash) has been proposed (by given account)
Proposed {
account: T::AccountId,
proposal_hash: T::Hash,
threshold: MemberCount,
},
/// A motion (given hash) has been voted on by given account, leaving
/// a tally (yes votes and no votes given respectively as `MemberCount`).
Voted {
account: T::AccountId,
proposal_hash: T::Hash,
voted: bool,
yes: MemberCount,
no: MemberCount,
},
/// A motion was approved by the required threshold.
Approved {
Expand Down Expand Up @@ -199,14 +194,56 @@ pub mod pallet {

let proposal = Proposal {
title: *proposal_text,
proposer: signer,
proposer: signer.clone(),
ayes: Vec::new(),
nays: Vec::new(),
end
};

<ProposalData<T>>::insert(proposal_hash, proposal);

Self::deposit_event(Event::<T>::Proposed {
account: signer,
proposal_hash
});

Ok(())
}

#[pallet::weight(1_000)]
pub fn vote(origin: OriginFor<T>, proposal: T::Hash, vote: Vote) -> DispatchResult {
let signer = ensure_signed(origin)?;

//check if signer is a member already | tested
ensure!(Self::is_member(&signer), Error::<T>::NotMember);

let result = Self::already_voted_and_exist(&signer, &proposal);
ensure!(result.is_some(), Error::<T>::ProposalMissing);

let voted = result.unwrap();
ensure!(!voted, Error::<T>::DuplicateVote);

let proposal_data = <ProposalData<T>>::get(&proposal);
ensure!(proposal_data.is_some(), Error::<T>::ProposalMissing);

let mut proposal_data = proposal_data.unwrap();

match vote {
Vote::Yes => {
proposal_data.ayes.push(signer.clone());
}
Vote::No => {
proposal_data.nays.push(signer.clone());
}
};

<ProposalData<T>>::set(proposal, Some(proposal_data));

Self::deposit_event(Event::<T>::Voted {
account: signer,
proposal_hash: proposal
});

Ok(())
}
}
Expand All @@ -221,4 +258,13 @@ impl<T: Config> Pallet<T> {
let proposals = <Proposals<T>>::get();
(proposals.contains(proposal), proposals)
}

pub fn already_voted_and_exist(who: &T::AccountId, proposal_hash: &T::Hash) -> Option<bool> {
let result = <ProposalData<T>>::get(proposal_hash);
if let Some(proposal) = result {
Some(proposal.ayes.contains(who) || proposal.nays.contains(who))
} else {
None
}
}
}
6 changes: 6 additions & 0 deletions pallets/slashing-voting/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ pub struct Proposal<AccountId, BlockNumber> {
pub end: BlockNumber,
}

#[derive(Clone, Eq, PartialEq, RuntimeDebug, Encode, Decode, TypeInfo)]
pub enum Vote {
Yes,
No
}

0 comments on commit 188eb29

Please sign in to comment.