--- a/pallets/collator-selection/src/benchmarking.rs +++ b/pallets/collator-selection/src/benchmarking.rs @@ -40,7 +40,11 @@ use frame_support::{ assert_ok, codec::Decode, - traits::{Currency, EnsureOrigin, Get}, + traits::{ + EnsureOrigin, + fungible::{Inspect, Mutate}, + Get, + }, }; use frame_system::{EventRecord, RawOrigin}; use pallet_authorship::EventHandler; @@ -78,7 +82,7 @@ ) -> T::AccountId { let user = account(string, n, SEED); let balance = balance_unit::() * balance_factor.into(); - let _ = T::Currency::make_free_balance_be(&user, balance); + let _ = T::Currency::set_balance(&user, balance); user } @@ -137,7 +141,7 @@ ); for who in candidates { - T::Currency::make_free_balance_be(&who, >::get() * 2u32.into()); + T::Currency::set_balance(&who, >::get() * 2u32.into()); >::get_license(RawOrigin::Signed(who.clone()).into()).unwrap(); >::onboard(RawOrigin::Signed(who).into()).unwrap(); } @@ -153,14 +157,14 @@ ); for who in candidates { - T::Currency::make_free_balance_be(&who, >::get() * 2u32.into()); + T::Currency::set_balance(&who, >::get() * 2u32.into()); >::get_license(RawOrigin::Signed(who.clone()).into()).unwrap(); } } /// `Currency::minimum_balance` was used originally, but in unique-chain, we have /// zero existential deposit, thus triggering zero bond assertion. -fn balance_unit() -> >::Balance { +fn balance_unit() -> BalanceOf { 200u32.into() } @@ -168,7 +172,9 @@ const INITIAL_INVULNERABLES: u32 = 2; benchmarks! { - where_clause { where T: pallet_authorship::Config + session::Config + configuration::Config } + where_clause { where + T: pallet_authorship::Config + session::Config + configuration::Config + } // todo:collator this and all the following do not work for some reason, going all the way up to 10 in length // Both invulnerables and candidates count together against MaxCollators. @@ -182,7 +188,7 @@ let new_invulnerable: T::AccountId = whitelisted_caller(); let bond: BalanceOf = balance_unit::() * 2u32.into(); - T::Currency::make_free_balance_be(&new_invulnerable, bond.clone()); + T::Currency::set_balance(&new_invulnerable, bond.clone()); >::set_keys( RawOrigin::Signed(new_invulnerable.clone()).into(), @@ -227,7 +233,7 @@ let caller: T::AccountId = whitelisted_caller(); let bond: BalanceOf = balance_unit::() * 2u32.into(); - T::Currency::make_free_balance_be(&caller, bond.clone()); + T::Currency::set_balance(&caller, bond.clone()); >::set_keys( RawOrigin::Signed(caller.clone()).into(), @@ -253,7 +259,7 @@ let caller: T::AccountId = whitelisted_caller(); let bond: BalanceOf = balance_unit::() * 2u32.into(); - T::Currency::make_free_balance_be(&caller, bond.clone()); + T::Currency::set_balance(&caller, bond.clone()); let origin = RawOrigin::Signed(caller.clone()); @@ -329,7 +335,7 @@ // worst case is paying a non-existing candidate account. note_author { >::put(balance_unit::()); - T::Currency::make_free_balance_be( + T::Currency::set_balance( &>::account_id(), balance_unit::() * 4u32.into(), ); @@ -337,11 +343,11 @@ let new_block: T::BlockNumber = 10u32.into(); frame_system::Pallet::::set_block_number(new_block); - assert!(T::Currency::free_balance(&author) == 0u32.into()); + assert!(T::Currency::balance(&author) == 0u32.into()); }: { as EventHandler<_, _>>::note_author(author.clone()) } verify { - assert!(T::Currency::free_balance(&author) > 0u32.into()); + assert!(T::Currency::balance(&author) > 0u32.into()); assert_eq!(frame_system::Pallet::::block_number(), new_block); } --- a/pallets/collator-selection/src/lib.rs +++ b/pallets/collator-selection/src/lib.rs @@ -92,6 +92,7 @@ #[frame_support::pallet] pub mod pallet { + use super::*; pub use crate::weights::WeightInfo; use core::ops::Div; use frame_support::{ @@ -100,8 +101,10 @@ pallet_prelude::*, sp_runtime::traits::{AccountIdConversion, CheckedSub, Saturating, Zero}, traits::{ - Currency, EnsureOrigin, ExistenceRequirement::KeepAlive, ReservableCurrency, + EnsureOrigin, + fungible::{Balanced, BalancedHold, Inspect, InspectHold, Mutate, MutateHold}, ValidatorRegistration, + tokens::{Precision, Preservation}, }, BoundedVec, PalletId, }; @@ -158,6 +161,9 @@ /// The weight information of this pallet. type WeightInfo: WeightInfo; + + #[pallet::constant] + type LicenceBondIdentifier: Get<<::Currency as InspectHold>::Reason>; } #[pallet::pallet] @@ -361,7 +367,7 @@ let deposit = >::get(); - T::Currency::reserve(&who, deposit)?; + T::Currency::hold(&T::LicenceBondIdentifier::get(), &who, deposit)?; LicenseDepositOf::::insert(who.clone(), deposit); Self::deposit_event(Event::LicenseObtained { @@ -523,17 +529,24 @@ let slashed = T::SlashRatio::get() * deposit; let remaining = deposit - slashed; - let (imbalance, _) = T::Currency::slash_reserved(who, slashed); + let (imbalance, _) = + T::Currency::slash(&T::LicenceBondIdentifier::get(), who, slashed); //T::Currency::unreserve(who, remaining); deposit_returned = remaining; - T::Currency::resolve_creating(&T::TreasuryAccountId::get(), imbalance); + T::Currency::resolve(&T::TreasuryAccountId::get(), imbalance) + .map_err(|_| DispatchError::Other("Failed to deposit imbalance"))?; } else { //T::Currency::unreserve(who, deposit); deposit_returned = deposit; } - T::Currency::unreserve(who, deposit_returned); + T::Currency::release( + &T::LicenceBondIdentifier::get(), + who, + deposit_returned, + Precision::Exact, + )?; Ok(()) } else { Err(Error::::NoLicense.into()) @@ -594,12 +607,12 @@ fn note_author(author: T::AccountId) { let pot = Self::account_id(); // assumes an ED will be sent to pot. - let reward = T::Currency::free_balance(&pot) + let reward = T::Currency::balance(&pot) .checked_sub(&T::Currency::minimum_balance()) .unwrap_or_else(Zero::zero) .div(2u32.into()); // `reward` is half of pot account minus ED, this should never fail. - let _success = T::Currency::transfer(&pot, &author, reward, KeepAlive); + let _success = T::Currency::transfer(&pot, &author, reward, Preservation::Preserve); debug_assert!(_success.is_ok()); >::insert(author, frame_system::Pallet::::block_number()); --- a/pallets/collator-selection/src/tests.rs +++ b/pallets/collator-selection/src/tests.rs @@ -417,7 +417,10 @@ fn authorship_event_handler() { new_test_ext().execute_with(|| { // put 100 in the pot + 5 for ED - Balances::make_free_balance_be(&CollatorSelection::account_id(), 105); + as fungible::Mutate>::set_balance( + &CollatorSelection::account_id(), + 105, + ); // 4 is the default author. assert_eq!(Balances::free_balance(4), 100); @@ -441,7 +444,10 @@ // Nothing panics, no reward when no ED in balance Authorship::on_initialize(1); // put some money into the pot at ED - Balances::make_free_balance_be(&CollatorSelection::account_id(), 5); + as fungible::Mutate>::set_balance( + &CollatorSelection::account_id(), + 5, + ); // 4 is the default author. assert_eq!(Balances::free_balance(4), 100); get_license_and_onboard(4); --- a/pallets/common/src/benchmarking.rs +++ b/pallets/common/src/benchmarking.rs @@ -27,12 +27,12 @@ MAX_PROPERTIES_PER_ITEM, }; use frame_support::{ - traits::{Currency, Get}, + traits::{Get, fungible::Balanced, Imbalance, tokens::Precision}, pallet_prelude::ConstU32, BoundedVec, }; use core::convert::TryInto; -use sp_runtime::DispatchError; +use sp_runtime::{DispatchError, traits::Zero}; const SEED: u32 = 1; @@ -85,7 +85,12 @@ ) -> Result, cast: impl FnOnce(CollectionHandle) -> R, ) -> Result { - ::Currency::deposit_creating(&owner.as_sub(), T::CollectionCreationPrice::get()); + let imbalance = ::Currency::deposit( + &owner.as_sub(), + T::CollectionCreationPrice::get(), + Precision::Exact, + )?; + debug_assert!(imbalance.peek().is_zero()); let name = create_u16_data::(); let description = create_u16_data::(); let token_prefix = create_data::(); --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -64,7 +64,11 @@ use frame_support::{ dispatch::{DispatchErrorWithPostInfo, DispatchResultWithPostInfo, Weight, PostDispatchInfo}, ensure, - traits::{Imbalance, Get, Currency, WithdrawReasons, ExistenceRequirement}, + traits::{ + Get, + fungible::{Balanced, Debt, Inspect}, + tokens::{Imbalance, Precision, Preservation}, + }, dispatch::Pays, transactional, fail, }; @@ -85,7 +89,7 @@ pub use pallet::*; use sp_core::H160; -use sp_runtime::{ArithmeticError, DispatchError, DispatchResult}; +use sp_runtime::{ArithmeticError, DispatchError, DispatchResult, traits::Zero}; #[cfg(feature = "runtime-benchmarks")] pub mod benchmarking; @@ -424,7 +428,6 @@ use super::*; use dispatch::CollectionDispatch; use frame_support::{Blake2_128Concat, pallet_prelude::*, storage::Key, traits::StorageVersion}; - use frame_support::traits::Currency; use up_data_structs::{TokenId, mapping::TokenAddressMapping}; use scale_info::TypeInfo; use weights::WeightInfo; @@ -440,12 +443,12 @@ type RuntimeEvent: IsType<::RuntimeEvent> + From>; /// Handler of accounts and payment. - type Currency: Currency; + type Currency: Balanced + Inspect; /// Set price to create a collection. #[pallet::constant] type CollectionCreationPrice: Get< - <::Currency as Currency>::Balance, + <::Currency as Inspect>::Balance, >; /// Dispatcher of operations on collections. @@ -1112,21 +1115,17 @@ // Take a (non-refundable) deposit of collection creation { - let mut imbalance = - <<::Currency as Currency>::PositiveImbalance>::zero(); - imbalance.subsume( - <::Currency as Currency>::deposit_creating( - &T::TreasuryAccountId::get(), - T::CollectionCreationPrice::get(), - ), - ); - ::Currency::settle( - payer.as_sub(), - imbalance, - WithdrawReasons::TRANSFER, - ExistenceRequirement::KeepAlive, - ) - .map_err(|_| Error::::NotSufficientFounds)?; + let mut imbalance = ::Currency>>::zero(); + imbalance.subsume(::Currency::deposit( + &T::TreasuryAccountId::get(), + T::CollectionCreationPrice::get(), + Precision::Exact, + )?); + let credit = + ::Currency::settle(payer.as_sub(), imbalance, Preservation::Preserve) + .map_err(|_| Error::::NotSufficientFounds)?; + + debug_assert!(credit.peek().is_zero()) } >::put(created_count); --- a/pallets/configuration/src/benchmarking.rs +++ b/pallets/configuration/src/benchmarking.rs @@ -19,7 +19,7 @@ use super::*; use frame_benchmarking::benchmarks; use frame_system::{EventRecord, RawOrigin}; -use frame_support::{assert_ok, traits::Currency}; +use frame_support::{assert_ok, traits::fungible::Inspect}; fn assert_last_event(generic_event: ::RuntimeEvent) { let events = frame_system::Pallet::::events(); @@ -68,7 +68,7 @@ } set_collator_selection_license_bond { - let bond_cost: Option> = Some(T::Currency::minimum_balance() * 10u32.into()); + let bond_cost: Option> = Some(T::Balances::minimum_balance() * 10u32.into()); }: { assert_ok!( >::set_collator_selection_license_bond(RawOrigin::Root.into(), bond_cost.clone()) --- a/pallets/configuration/src/lib.rs +++ b/pallets/configuration/src/lib.rs @@ -42,7 +42,7 @@ mod pallet { use super::*; use frame_support::{ - traits::{Get, ReservableCurrency, Currency}, + traits::{fungible, Get, ReservableCurrency, Currency}, pallet_prelude::{StorageValue, ValueQuery, DispatchResult, IsType}, log, }; @@ -50,15 +50,19 @@ pub use crate::weights::WeightInfo; pub type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; + <::Currency as fungible::Inspect<::AccountId>>::Balance; #[pallet::config] pub trait Config: frame_system::Config { /// Overarching event type. type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// The currency mechanism. - type Currency: ReservableCurrency; + type Currency: fungible::Inspect + + fungible::Mutate + + fungible::MutateFreeze + + fungible::InspectHold + + fungible::MutateHold + + fungible::BalancedHold; #[pallet::constant] type DefaultWeightToFeeCoefficient: Get;