Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Use fungible token traits in alliance pallet #11955

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions frame/alliance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ pub mod weights;

use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use sp_runtime::{
traits::{StaticLookup, Zero},
RuntimeDebug,
};
use sp_runtime::{traits::StaticLookup, RuntimeDebug};
use sp_std::{convert::TryInto, prelude::*};

use frame_support::{
Expand All @@ -112,10 +109,7 @@ use frame_support::{
},
ensure,
scale_info::TypeInfo,
traits::{
ChangeMembers, Currency, Get, InitializeMembers, IsSubType, OnUnbalanced,
ReservableCurrency,
},
traits::{tokens::fungible, ChangeMembers, Get, InitializeMembers, IsSubType, OnUnbalanced},
weights::Weight,
};
use pallet_identity::IdentityField;
Expand All @@ -129,11 +123,9 @@ pub type ProposalIndex = u32;

type UrlOf<T, I> = BoundedVec<u8, <T as pallet::Config<I>>::MaxWebsiteUrlLength>;

type BalanceOf<T, I> =
<<T as Config<I>>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type NegativeImbalanceOf<T, I> = <<T as Config<I>>::Currency as Currency<
type BalanceOf<T, I> = <<T as Config<I>>::Currency as fungible::Inspect<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;
>>::Balance;

/// Interface required for identity verification.
pub trait IdentityVerifier<AccountId> {
Expand Down Expand Up @@ -242,10 +234,10 @@ pub mod pallet {
type AnnouncementOrigin: EnsureOrigin<Self::Origin>;

/// The currency used for deposits.
type Currency: ReservableCurrency<Self::AccountId>;
type Currency: fungible::BalancedHold<Self::AccountId>;

/// What to do with slashed funds.
type Slashed: OnUnbalanced<NegativeImbalanceOf<Self, I>>;
type Slashed: OnUnbalanced<fungible::CreditOf<Self::AccountId, Self::Currency>>;

/// What to do with initial voting members of the Alliance.
type InitializeMembers: InitializeMembers<Self::AccountId>;
Expand Down Expand Up @@ -709,6 +701,8 @@ pub mod pallet {
/// Submit oneself for candidacy. A fixed deposit is reserved.
#[pallet::weight(T::WeightInfo::join_alliance())]
pub fn join_alliance(origin: OriginFor<T>) -> DispatchResult {
use fungible::MutateHold;

let who = ensure_signed(origin)?;

// We don't want anyone to join as an Ally before the Alliance has been initialized via
Expand All @@ -728,7 +722,7 @@ pub mod pallet {
Self::has_identity(&who)?;

let deposit = T::AllyDeposit::get();
T::Currency::reserve(&who, deposit).map_err(|_| Error::<T, I>::InsufficientFunds)?;
T::Currency::hold(&who, deposit).map_err(|_| Error::<T, I>::InsufficientFunds)?;
<DepositOf<T, I>>::insert(&who, deposit);

Self::add_member(&who, MemberRole::Ally)?;
Expand Down Expand Up @@ -790,6 +784,8 @@ pub mod pallet {
/// As a member, retire from the alliance and unreserve the deposit.
#[pallet::weight(T::WeightInfo::retire())]
pub fn retire(origin: OriginFor<T>) -> DispatchResult {
use fungible::MutateHold;

let who = ensure_signed(origin)?;
// A member up for kicking cannot retire.
ensure!(!Self::is_up_for_kicking(&who), Error::<T, I>::UpForKicking);
Expand All @@ -798,8 +794,8 @@ pub mod pallet {
Self::remove_member(&who, role)?;
let deposit = DepositOf::<T, I>::take(&who);
if let Some(deposit) = deposit {
let err_amount = T::Currency::unreserve(&who, deposit);
debug_assert!(err_amount.is_zero());
let actual = T::Currency::release(&who, deposit, true)?;
debug_assert!(actual == deposit);
}
Self::deposit_event(Event::MemberRetired { member: who, unreserved: deposit });
Ok(())
Expand All @@ -811,14 +807,16 @@ pub mod pallet {
origin: OriginFor<T>,
who: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
use fungible::BalancedHold;

T::MembershipManager::ensure_origin(origin)?;
let member = T::Lookup::lookup(who)?;

let role = Self::member_role_of(&member).ok_or(Error::<T, I>::NotMember)?;
Self::remove_member(&member, role)?;
let deposit = DepositOf::<T, I>::take(member.clone());
if let Some(deposit) = deposit {
T::Slashed::on_unbalanced(T::Currency::slash_reserved(&member, deposit).0);
T::Slashed::on_unbalanced(T::Currency::slash_held(&member, deposit).0);
}

<UpForKicking<T, I>>::remove(&member);
Expand Down