From 43d191f8a70a6b13885e5421f09638c8dc8b1b79 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Mon, 1 Aug 2022 22:36:11 +0800 Subject: [PATCH] Use fungible token traits in alliance pallet --- frame/alliance/src/lib.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/frame/alliance/src/lib.rs b/frame/alliance/src/lib.rs index 111ea5dc6e507..a844f7f0c398e 100644 --- a/frame/alliance/src/lib.rs +++ b/frame/alliance/src/lib.rs @@ -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::{ @@ -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; @@ -129,11 +123,9 @@ pub type ProposalIndex = u32; type UrlOf = BoundedVec>::MaxWebsiteUrlLength>; -type BalanceOf = - <>::Currency as Currency<::AccountId>>::Balance; -type NegativeImbalanceOf = <>::Currency as Currency< +type BalanceOf = <>::Currency as fungible::Inspect< ::AccountId, ->>::NegativeImbalance; +>>::Balance; /// Interface required for identity verification. pub trait IdentityVerifier { @@ -242,10 +234,10 @@ pub mod pallet { type AnnouncementOrigin: EnsureOrigin; /// The currency used for deposits. - type Currency: ReservableCurrency; + type Currency: fungible::BalancedHold; /// What to do with slashed funds. - type Slashed: OnUnbalanced>; + type Slashed: OnUnbalanced>; /// What to do with initial voting members of the Alliance. type InitializeMembers: InitializeMembers; @@ -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) -> 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 @@ -728,7 +722,7 @@ pub mod pallet { Self::has_identity(&who)?; let deposit = T::AllyDeposit::get(); - T::Currency::reserve(&who, deposit).map_err(|_| Error::::InsufficientFunds)?; + T::Currency::hold(&who, deposit).map_err(|_| Error::::InsufficientFunds)?; >::insert(&who, deposit); Self::add_member(&who, MemberRole::Ally)?; @@ -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) -> DispatchResult { + use fungible::MutateHold; + let who = ensure_signed(origin)?; // A member up for kicking cannot retire. ensure!(!Self::is_up_for_kicking(&who), Error::::UpForKicking); @@ -798,8 +794,8 @@ pub mod pallet { Self::remove_member(&who, role)?; let deposit = DepositOf::::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(()) @@ -811,6 +807,8 @@ pub mod pallet { origin: OriginFor, who: ::Source, ) -> DispatchResult { + use fungible::BalancedHold; + T::MembershipManager::ensure_origin(origin)?; let member = T::Lookup::lookup(who)?; @@ -818,7 +816,7 @@ pub mod pallet { Self::remove_member(&member, role)?; let deposit = DepositOf::::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); } >::remove(&member);