Skip to content

Commit

Permalink
chore(pallet-communities): coverage testing for AssetBalance and Nati…
Browse files Browse the repository at this point in the history
…veBalance voting methods
  • Loading branch information
pandres95 committed Feb 3, 2024
1 parent 776a68b commit 8dfb820
Show file tree
Hide file tree
Showing 5 changed files with 529 additions and 140 deletions.
50 changes: 41 additions & 9 deletions pallets/communities/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use crate::{
use frame_support::{
pallet_prelude::*,
traits::{
fungible::MutateHold as FunMutateHold,
fungible::MutateFreeze as FunMutateFreeze,
fungibles::MutateHold as FunsMutateHold,
membership::{GenericRank, Inspect, WithRank},
tokens::Precision,
Polling,
},
};
use sp_runtime::traits::AccountIdConversion;
use sp_runtime::{traits::AccountIdConversion, TokenError};
use sp_std::vec::Vec;

impl<T: Config> Pallet<T> {
Expand Down Expand Up @@ -82,6 +83,10 @@ impl<T: Config> Pallet<T> {
poll_index: PollIndexOf<T>,
vote: VoteOf<T>,
) -> DispatchResult {
if VoteWeight::from(vote.clone()) == 0 {
return Err(TokenError::BelowMinimum.into());
}

T::Polls::try_access_poll(poll_index, |poll_status| {
let (tally, class) = poll_status.ensure_ongoing().ok_or(Error::<T>::NotOngoing)?;
ensure!(community_id == &class, Error::<T>::InvalidTrack);
Expand All @@ -90,28 +95,25 @@ impl<T: Config> Pallet<T> {

let maybe_vote = Self::community_vote_of(who, poll_index);
if let Some(vote) = maybe_vote {
Self::do_unlock_for_vote(who, &poll_index, &vote)?;
tally.remove_vote(vote.clone().into(), vote.into());
}

let say = match vote.clone() {
Vote::AssetBalance(say, asset_id, asset_balance) => {
Vote::AssetBalance(say, asset_id, ..) => {
ensure!(
decision_method == DecisionMethod::CommunityAsset(asset_id.clone()),
Error::<T>::InvalidVoteType
);

T::Assets::hold(asset_id, &HoldReason::VoteCasted(poll_index).into(), who, asset_balance)?;

say
}
Vote::NativeBalance(say, balance) => {
Vote::NativeBalance(say, ..) => {
ensure!(
decision_method == DecisionMethod::NativeToken,
Error::<T>::InvalidVoteType
);

T::Balances::hold(&HoldReason::VoteCasted(poll_index).into(), who, balance)?;

say
}
Vote::Standard(say) => {
Expand All @@ -124,8 +126,8 @@ impl<T: Config> Pallet<T> {
}
};

Self::do_lock_for_vote(who, &poll_index, &vote)?;
tally.add_vote(say, vote.clone().into());
CommunityVotes::<T>::insert(who.clone(), poll_index, vote.clone());

Self::deposit_event(Event::<T>::VoteCasted {
who: who.clone(),
Expand All @@ -136,6 +138,36 @@ impl<T: Config> Pallet<T> {
Ok(())
})
}

fn do_lock_for_vote(who: &AccountIdOf<T>, poll_index: &PollIndexOf<T>, vote: &VoteOf<T>) -> DispatchResult {
let reason = HoldReason::VoteCasted(*poll_index).into();
CommunityVotes::<T>::insert(who.clone(), poll_index, vote.clone());

match vote {
Vote::AssetBalance(_, asset_id, amount) => T::Assets::hold(asset_id.clone(), &reason, who, *amount),
Vote::NativeBalance(_, amount) => {
T::Balances::set_frozen(&reason, who, *amount, frame_support::traits::tokens::Fortitude::Polite)
}
_ => Ok(()),
}
}

pub(crate) fn do_unlock_for_vote(
who: &AccountIdOf<T>,
poll_index: &PollIndexOf<T>,
vote: &VoteOf<T>,
) -> DispatchResult {
let reason = HoldReason::VoteCasted(*poll_index).into();
CommunityVotes::<T>::remove(who, poll_index);

match vote {
Vote::AssetBalance(_, asset_id, amount) => {
T::Assets::release(asset_id.clone(), &reason, who, *amount, Precision::BestEffort).map(|_| ())
}
Vote::NativeBalance(..) => T::Balances::thaw(&reason, who),
_ => Err(Error::<T>::NoLocksInPlace.into()),
}
}
}

impl<T: Config> Tally<T> {
Expand Down
29 changes: 3 additions & 26 deletions pallets/communities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ pub mod pallet {
traits::{
fungible, fungibles,
membership::{self, Inspect, Membership, Mutate, WithRank},
tokens::{fungible::MutateHold as FunMutateHold, fungibles::MutateHold as FunsMutateHold, Precision},
EnsureOrigin, Polling,
},
Blake2_128Concat, Parameter,
Expand Down Expand Up @@ -232,7 +231,7 @@ pub mod pallet {
/// Type represents interactions between fungible tokens (native token)
type Balances: fungible::Inspect<Self::AccountId>
+ fungible::Mutate<Self::AccountId>
+ fungible::hold::Mutate<Self::AccountId, Reason = Self::RuntimeHoldReason>;
+ fungible::freeze::Mutate<Self::AccountId, Id = Self::RuntimeHoldReason>;

/// The overarching hold reason.
type RuntimeHoldReason: From<HoldReason>;
Expand Down Expand Up @@ -541,31 +540,9 @@ pub mod pallet {
pub fn unlock(origin: OriginFor<T>, #[pallet::compact] poll_index: PollIndexOf<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
ensure!(T::Polls::as_ongoing(poll_index).is_none(), Error::<T>::AlreadyOngoing);
let vote = Self::community_vote_of(&who, poll_index).ok_or(Error::<T>::NoLocksInPlace)?;

match Self::community_vote_of(&who, poll_index) {
Some(Vote::AssetBalance(_, asset_id, amount)) => {
T::Assets::release(
asset_id,
&HoldReason::VoteCasted(poll_index).into(),
&who,
amount,
Precision::BestEffort,
)?;
}
Some(Vote::NativeBalance(_, amount)) => {
T::Balances::release(
&HoldReason::VoteCasted(poll_index).into(),
&who,
amount,
Precision::BestEffort,
)?;
}
_ => Err(Error::<T>::NoLocksInPlace)?,
}

CommunityVotes::<T>::remove(&who, poll_index);

Ok(())
Self::do_unlock_for_vote(&who, &poll_index, &vote)
}
}
}
Loading

0 comments on commit 8dfb820

Please sign in to comment.