Move LockableCurrency trait to fungibles::Lockable and deprecate LockableCurrency (#12798)

* WIP move LockableCurrency to fungibles

* rename Lockable and LockIdentifier to funginbles::*

* fix imports further

* change Lockable from fungible to fungibles

* reintroduce LockableCurrency but marked as deprecated

* fix imports

* fix imports

* cargo fmt

* add allow deprecated warnings

* remove unused benchmark import

* fix some of the docs

* fix failing doctest check

* reexport LockIdentifier and LockableCurrency from support/traits

* reexport LockIdentifier and LockableCurrency from support/traits

* allow using deprecated re-export

* replace LockableCurrency and LockIdentifier with a module alias

* Update frame/support/src/traits/tokens/fungibles/lockable.rs

* Update frame/staking/src/pallet/mod.rs

Co-authored-by: Squirrel <gilescope@gmail.com>

* Update frame/support/src/traits.rs

Co-authored-by: Squirrel <gilescope@gmail.com>

* REVERT removing fungibles::Lockable import

Co-authored-by: parity-processbot <>
Co-authored-by: Squirrel <gilescope@gmail.com>
This commit is contained in:
Anthony Alaribe
2022-12-08 14:47:13 +02:00
committed by GitHub
parent 39cb3b06cd
commit 9a014d1ecd
20 changed files with 135 additions and 105 deletions
+4 -4
View File
@@ -32,9 +32,9 @@ use frame_support::{
pallet_prelude::Get, pallet_prelude::Get,
parameter_types, parameter_types,
traits::{ traits::{
fungible::ItemOf, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16, ConstU32, fungible::ItemOf, fungibles, AsEnsureOriginWithArg, ConstBool, ConstU128, ConstU16,
Currency, EitherOfDiverse, EqualPrivilegeOnly, Everything, Imbalance, InstanceFilter, ConstU32, Currency, EitherOfDiverse, EqualPrivilegeOnly, Everything, Imbalance,
KeyOwnerProofSystem, LockIdentifier, Nothing, OnUnbalanced, U128CurrencyToVote, InstanceFilter, KeyOwnerProofSystem, Nothing, OnUnbalanced, U128CurrencyToVote,
WithdrawReasons, WithdrawReasons,
}, },
weights::{ weights::{
@@ -1003,7 +1003,7 @@ parameter_types! {
pub const DesiredRunnersUp: u32 = 7; pub const DesiredRunnersUp: u32 = 7;
pub const MaxVoters: u32 = 10 * 1000; pub const MaxVoters: u32 = 10 * 1000;
pub const MaxCandidates: u32 = 1000; pub const MaxCandidates: u32 = 1000;
pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect"; pub const ElectionsPhragmenPalletId: fungibles::LockIdentifier = *b"phrelect";
} }
// Make sure that there are no more than `MaxMembers` members elected via elections-phragmen. // Make sure that there are no more than `MaxMembers` members elected via elections-phragmen.
+4 -4
View File
@@ -57,7 +57,7 @@ that you need, then you can avoid coupling with the Balances module.
fungible assets system. fungible assets system.
- [`ReservableCurrency`](https://docs.rs/frame-support/latest/frame_support/traits/trait.ReservableCurrency.html): - [`ReservableCurrency`](https://docs.rs/frame-support/latest/frame_support/traits/trait.ReservableCurrency.html):
Functions for dealing with assets that can be reserved from an account. Functions for dealing with assets that can be reserved from an account.
- [`LockableCurrency`](https://docs.rs/frame-support/latest/frame_support/traits/trait.LockableCurrency.html): Functions for - [`Lockable`](https://docs.rs/frame-support/latest/frame_support/traits/fungibles/trait.Lockable.html): Functions for
dealing with accounts that allow liquidity restrictions. dealing with accounts that allow liquidity restrictions.
- [`Imbalance`](https://docs.rs/frame-support/latest/frame_support/traits/trait.Imbalance.html): Functions for handling - [`Imbalance`](https://docs.rs/frame-support/latest/frame_support/traits/trait.Imbalance.html): Functions for handling
imbalances between total issuance in the system and account balances. Must be used when a function imbalances between total issuance in the system and account balances. Must be used when a function
@@ -88,13 +88,13 @@ pub type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<<T as fra
``` ```
The Staking module uses the `LockableCurrency` trait to lock a stash account's funds: The Staking module uses the `fungibles::Lockable` trait to lock a stash account's funds:
```rust ```rust
use frame_support::traits::{WithdrawReasons, LockableCurrency}; use frame_support::traits::{WithdrawReasons, fungibles};
use sp_runtime::traits::Bounded; use sp_runtime::traits::Bounded;
pub trait Config: frame_system::Config { pub trait Config: frame_system::Config {
type Currency: LockableCurrency<Self::AccountId, Moment=Self::BlockNumber>; type Currency: fungibles::Lockable<Self::AccountId, Moment=Self::BlockNumber>;
} }
fn update_ledger<T: Config>( fn update_ledger<T: Config>(
+14 -12
View File
@@ -79,7 +79,7 @@
//! - [`ReservableCurrency`](frame_support::traits::ReservableCurrency): //! - [`ReservableCurrency`](frame_support::traits::ReservableCurrency):
//! - [`NamedReservableCurrency`](frame_support::traits::NamedReservableCurrency): //! - [`NamedReservableCurrency`](frame_support::traits::NamedReservableCurrency):
//! Functions for dealing with assets that can be reserved from an account. //! Functions for dealing with assets that can be reserved from an account.
//! - [`LockableCurrency`](frame_support::traits::LockableCurrency): Functions for //! - [`Lockable`](frame_support::traits::fungibles::Lockable): Functions for
//! dealing with accounts that allow liquidity restrictions. //! dealing with accounts that allow liquidity restrictions.
//! - [`Imbalance`](frame_support::traits::Imbalance): Functions for handling //! - [`Imbalance`](frame_support::traits::Imbalance): Functions for handling
//! imbalances between total issuance in the system and account balances. Must be used when a //! imbalances between total issuance in the system and account balances. Must be used when a
@@ -113,13 +113,13 @@
//! # fn main() {} //! # fn main() {}
//! ``` //! ```
//! //!
//! The Staking pallet uses the `LockableCurrency` trait to lock a stash account's funds: //! The Staking pallet uses the `fungibles::Lockable` trait to lock a stash account's funds:
//! //!
//! ``` //! ```
//! use frame_support::traits::{WithdrawReasons, LockableCurrency}; //! use frame_support::traits::{WithdrawReasons, fungibles, fungibles::Lockable};
//! use sp_runtime::traits::Bounded; //! use sp_runtime::traits::Bounded;
//! pub trait Config: frame_system::Config { //! pub trait Config: frame_system::Config {
//! type Currency: LockableCurrency<Self::AccountId, Moment=Self::BlockNumber>; //! type Currency: fungibles::Lockable<Self::AccountId, Moment=Self::BlockNumber>;
//! } //! }
//! # struct StakingLedger<T: Config> { //! # struct StakingLedger<T: Config> {
//! # stash: <T as frame_system::Config>::AccountId, //! # stash: <T as frame_system::Config>::AccountId,
@@ -171,11 +171,13 @@ use frame_support::{
ensure, ensure,
pallet_prelude::DispatchResult, pallet_prelude::DispatchResult,
traits::{ traits::{
tokens::{fungible, BalanceStatus as Status, DepositConsequence, WithdrawConsequence}, tokens::{
fungible, fungibles, BalanceStatus as Status, DepositConsequence, WithdrawConsequence,
},
Currency, DefensiveSaturating, ExistenceRequirement, Currency, DefensiveSaturating, ExistenceRequirement,
ExistenceRequirement::{AllowDeath, KeepAlive}, ExistenceRequirement::{AllowDeath, KeepAlive},
Get, Imbalance, LockIdentifier, LockableCurrency, NamedReservableCurrency, OnUnbalanced, Get, Imbalance, NamedReservableCurrency, OnUnbalanced, ReservableCurrency, SignedImbalance,
ReservableCurrency, SignedImbalance, StoredMap, TryDrop, WithdrawReasons, StoredMap, TryDrop, WithdrawReasons,
}, },
WeakBoundedVec, WeakBoundedVec,
}; };
@@ -662,7 +664,7 @@ impl BitOr for Reasons {
#[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)] #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
pub struct BalanceLock<Balance> { pub struct BalanceLock<Balance> {
/// An identifier for this lock. Only one lock may be in existence for each identifier. /// An identifier for this lock. Only one lock may be in existence for each identifier.
pub id: LockIdentifier, pub id: fungibles::LockIdentifier,
/// The amount which the free balance may not drop below when this lock is in effect. /// The amount which the free balance may not drop below when this lock is in effect.
pub amount: Balance, pub amount: Balance,
/// If true, then the lock remains in effect even for payment of transaction fees. /// If true, then the lock remains in effect even for payment of transaction fees.
@@ -2131,7 +2133,7 @@ where
} }
} }
impl<T: Config<I>, I: 'static> LockableCurrency<T::AccountId> for Pallet<T, I> impl<T: Config<I>, I: 'static> fungibles::Lockable<T::AccountId> for Pallet<T, I>
where where
T::Balance: MaybeSerializeDeserialize + Debug, T::Balance: MaybeSerializeDeserialize + Debug,
{ {
@@ -2142,7 +2144,7 @@ where
// Set a lock on the balance of `who`. // Set a lock on the balance of `who`.
// Is a no-op if lock amount is zero or `reasons` `is_none()`. // Is a no-op if lock amount is zero or `reasons` `is_none()`.
fn set_lock( fn set_lock(
id: LockIdentifier, id: fungibles::LockIdentifier,
who: &T::AccountId, who: &T::AccountId,
amount: T::Balance, amount: T::Balance,
reasons: WithdrawReasons, reasons: WithdrawReasons,
@@ -2164,7 +2166,7 @@ where
// Extend a lock on the balance of `who`. // Extend a lock on the balance of `who`.
// Is a no-op if lock amount is zero or `reasons` `is_none()`. // Is a no-op if lock amount is zero or `reasons` `is_none()`.
fn extend_lock( fn extend_lock(
id: LockIdentifier, id: fungibles::LockIdentifier,
who: &T::AccountId, who: &T::AccountId,
amount: T::Balance, amount: T::Balance,
reasons: WithdrawReasons, reasons: WithdrawReasons,
@@ -2193,7 +2195,7 @@ where
Self::update_locks(who, &locks[..]); Self::update_locks(who, &locks[..]);
} }
fn remove_lock(id: LockIdentifier, who: &T::AccountId) { fn remove_lock(id: fungibles::LockIdentifier, who: &T::AccountId) {
let mut locks = Self::locks(who); let mut locks = Self::locks(who);
locks.retain(|l| l.id != id); locks.retain(|l| l.id != id);
Self::update_locks(who, &locks[..]); Self::update_locks(who, &locks[..]);
+3 -3
View File
@@ -28,15 +28,15 @@ macro_rules! decl_tests {
use frame_support::{ use frame_support::{
assert_noop, assert_storage_noop, assert_ok, assert_err, assert_noop, assert_storage_noop, assert_ok, assert_err,
traits::{ traits::{
LockableCurrency, LockIdentifier, WithdrawReasons, fungibles, fungibles::Lockable, WithdrawReasons,
Currency, ReservableCurrency, ExistenceRequirement::AllowDeath Currency, ReservableCurrency, ExistenceRequirement::AllowDeath
} }
}; };
use pallet_transaction_payment::{ChargeTransactionPayment, Multiplier}; use pallet_transaction_payment::{ChargeTransactionPayment, Multiplier};
use frame_system::RawOrigin; use frame_system::RawOrigin;
const ID_1: LockIdentifier = *b"1 "; const ID_1: fungibles::LockIdentifier = *b"1 ";
const ID_2: LockIdentifier = *b"2 "; const ID_2: fungibles::LockIdentifier = *b"2 ";
pub const CALL: &<$test as frame_system::Config>::RuntimeCall = pub const CALL: &<$test as frame_system::Config>::RuntimeCall =
&RuntimeCall::Balances(pallet_balances::Call::transfer { dest: 0, value: 0 }); &RuntimeCall::Balances(pallet_balances::Call::transfer { dest: 0, value: 0 });
+1 -1
View File
@@ -37,7 +37,7 @@ use frame_support::{
parameter_types, parameter_types,
storage::child, storage::child,
traits::{ traits::{
BalanceStatus, ConstU32, ConstU64, Contains, Currency, Get, LockableCurrency, OnIdle, fungibles::Lockable, BalanceStatus, ConstU32, ConstU64, Contains, Currency, Get, OnIdle,
OnInitialize, ReservableCurrency, WithdrawReasons, OnInitialize, ReservableCurrency, WithdrawReasons,
}, },
weights::{constants::WEIGHT_PER_SECOND, Weight}, weights::{constants::WEIGHT_PER_SECOND, Weight},
@@ -23,7 +23,7 @@ use assert_matches::assert_matches;
use frame_benchmarking::{account, benchmarks_instance_pallet, whitelist_account}; use frame_benchmarking::{account, benchmarks_instance_pallet, whitelist_account};
use frame_support::{ use frame_support::{
dispatch::RawOrigin, dispatch::RawOrigin,
traits::{fungible, Currency, Get}, traits::{Currency, Get},
}; };
use sp_runtime::traits::Bounded; use sp_runtime::traits::Bounded;
use sp_std::collections::btree_map::BTreeMap; use sp_std::collections::btree_map::BTreeMap;
+3 -3
View File
@@ -31,7 +31,7 @@ use frame_support::{
dispatch::{DispatchError, DispatchResult}, dispatch::{DispatchError, DispatchResult},
ensure, ensure,
traits::{ traits::{
fungible, Currency, Get, LockIdentifier, LockableCurrency, PollStatus, Polling, fungible, fungibles, fungibles::Lockable, Currency, Get, PollStatus, Polling,
ReservableCurrency, WithdrawReasons, ReservableCurrency, WithdrawReasons,
}, },
}; };
@@ -60,7 +60,7 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")] #[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking; pub mod benchmarking;
const CONVICTION_VOTING_ID: LockIdentifier = *b"pyconvot"; const CONVICTION_VOTING_ID: fungibles::LockIdentifier = *b"pyconvot";
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source; type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
type BalanceOf<T, I = ()> = type BalanceOf<T, I = ()> =
@@ -104,7 +104,7 @@ pub mod pallet {
type WeightInfo: WeightInfo; type WeightInfo: WeightInfo;
/// Currency type with which voting happens. /// Currency type with which voting happens.
type Currency: ReservableCurrency<Self::AccountId> type Currency: ReservableCurrency<Self::AccountId>
+ LockableCurrency<Self::AccountId, Moment = Self::BlockNumber> + fungibles::Lockable<Self::AccountId, Moment = Self::BlockNumber>
+ fungible::Inspect<Self::AccountId>; + fungible::Inspect<Self::AccountId>;
/// The implementation of the logic which conducts polls. /// The implementation of the logic which conducts polls.
+6 -4
View File
@@ -157,9 +157,11 @@ use frame_support::{
ensure, ensure,
traits::{ traits::{
defensive_prelude::*, defensive_prelude::*,
fungibles,
fungibles::Lockable,
schedule::{v3::Named as ScheduleNamed, DispatchTime}, schedule::{v3::Named as ScheduleNamed, DispatchTime},
Bounded, Currency, Get, LockIdentifier, LockableCurrency, OnUnbalanced, QueryPreimage, Bounded, Currency, Get, OnUnbalanced, QueryPreimage, ReservableCurrency, StorePreimage,
ReservableCurrency, StorePreimage, WithdrawReasons, WithdrawReasons,
}, },
weights::Weight, weights::Weight,
}; };
@@ -189,7 +191,7 @@ pub mod benchmarking;
pub mod migrations; pub mod migrations;
const DEMOCRACY_ID: LockIdentifier = *b"democrac"; const DEMOCRACY_ID: fungibles::LockIdentifier = *b"democrac";
/// A proposal index. /// A proposal index.
pub type PropIndex = u32; pub type PropIndex = u32;
@@ -234,7 +236,7 @@ pub mod pallet {
/// Currency type for this pallet. /// Currency type for this pallet.
type Currency: ReservableCurrency<Self::AccountId> type Currency: ReservableCurrency<Self::AccountId>
+ LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>; + fungibles::Lockable<Self::AccountId, Moment = Self::BlockNumber>;
/// The period between a proposal being approved and enacted. /// The period between a proposal being approved and enacted.
/// ///
@@ -101,8 +101,8 @@
use codec::{Decode, Encode}; use codec::{Decode, Encode};
use frame_support::{ use frame_support::{
traits::{ traits::{
defensive_prelude::*, ChangeMembers, Contains, ContainsLengthBound, Currency, defensive_prelude::*, fungibles, fungibles::Lockable, ChangeMembers, Contains,
CurrencyToVote, Get, InitializeMembers, LockIdentifier, LockableCurrency, OnUnbalanced, ContainsLengthBound, Currency, CurrencyToVote, Get, InitializeMembers, OnUnbalanced,
ReservableCurrency, SortedMembers, WithdrawReasons, ReservableCurrency, SortedMembers, WithdrawReasons,
}, },
weights::Weight, weights::Weight,
@@ -199,10 +199,10 @@ pub mod pallet {
/// Identifier for the elections-phragmen pallet's lock /// Identifier for the elections-phragmen pallet's lock
#[pallet::constant] #[pallet::constant]
type PalletId: Get<LockIdentifier>; type PalletId: Get<fungibles::LockIdentifier>;
/// The currency that people are electing with. /// The currency that people are electing with.
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber> type Currency: fungibles::Lockable<Self::AccountId, Moment = Self::BlockNumber>
+ ReservableCurrency<Self::AccountId>; + ReservableCurrency<Self::AccountId>;
/// What to do when the members change. /// What to do when the members change.
@@ -1274,7 +1274,7 @@ mod tests {
} }
parameter_types! { parameter_types! {
pub const ElectionsPhragmenPalletId: LockIdentifier = *b"phrelect"; pub const ElectionsPhragmenPalletId: fungibles::LockIdentifier = *b"phrelect";
pub const PhragmenMaxVoters: u32 = 1000; pub const PhragmenMaxVoters: u32 = 1000;
pub const PhragmenMaxCandidates: u32 = 100; pub const PhragmenMaxCandidates: u32 = 100;
} }
+3 -6
View File
@@ -620,10 +620,7 @@ mod tests {
use frame_support::{ use frame_support::{
assert_err, parameter_types, assert_err, parameter_types,
traits::{ traits::{fungibles, ConstU32, ConstU64, ConstU8, Currency, WithdrawReasons},
ConstU32, ConstU64, ConstU8, Currency, LockIdentifier, LockableCurrency,
WithdrawReasons,
},
weights::{ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFee}, weights::{ConstantMultiplier, IdentityFee, RuntimeDbWeight, Weight, WeightToFee},
}; };
use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo}; use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
@@ -1185,11 +1182,11 @@ mod tests {
#[test] #[test]
fn can_pay_for_tx_fee_on_full_lock() { fn can_pay_for_tx_fee_on_full_lock() {
let id: LockIdentifier = *b"0 "; let id: fungibles::LockIdentifier = *b"0 ";
let execute_with_lock = |lock: WithdrawReasons| { let execute_with_lock = |lock: WithdrawReasons| {
let mut t = new_test_ext(1); let mut t = new_test_ext(1);
t.execute_with(|| { t.execute_with(|| {
<pallet_balances::Pallet<Runtime> as LockableCurrency<Balance>>::set_lock( <pallet_balances::Pallet<Runtime> as fungibles::Lockable<Balance>>::set_lock(
id, &1, 110, lock, id, &1, 110, lock,
); );
let xt = TestXt::new( let xt = TestXt::new(
+3 -4
View File
@@ -1,5 +1,3 @@
// This file is part of Substrate.
// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd. // Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
@@ -68,11 +66,12 @@ use codec::{Codec, Encode};
use frame_support::{ use frame_support::{
ensure, ensure,
traits::{ traits::{
fungibles,
schedule::{ schedule::{
v3::{Anon as ScheduleAnon, Named as ScheduleNamed}, v3::{Anon as ScheduleAnon, Named as ScheduleNamed},
DispatchTime, DispatchTime,
}, },
Currency, LockIdentifier, OnUnbalanced, OriginTrait, PollStatus, Polling, QueryPreimage, Currency, OnUnbalanced, OriginTrait, PollStatus, Polling, QueryPreimage,
ReservableCurrency, StorePreimage, VoteTally, ReservableCurrency, StorePreimage, VoteTally,
}, },
BoundedVec, BoundedVec,
@@ -133,7 +132,7 @@ macro_rules! impl_tracksinfo_get {
}; };
} }
const ASSEMBLY_ID: LockIdentifier = *b"assembly"; const ASSEMBLY_ID: fungibles::LockIdentifier = *b"assembly";
#[frame_support::pallet] #[frame_support::pallet]
pub mod pallet { pub mod pallet {
+3 -2
View File
@@ -25,8 +25,9 @@ use frame_support::{
dispatch::WithPostDispatchInfo, dispatch::WithPostDispatchInfo,
pallet_prelude::*, pallet_prelude::*,
traits::{ traits::{
Currency, CurrencyToVote, Defensive, DefensiveResult, EstimateNextNewSession, Get, fungibles::Lockable, Currency, CurrencyToVote, Defensive, DefensiveResult,
Imbalance, LockableCurrency, OnUnbalanced, TryCollect, UnixTime, WithdrawReasons, EstimateNextNewSession, Get, Imbalance, OnUnbalanced, TryCollect, UnixTime,
WithdrawReasons,
}, },
weights::Weight, weights::Weight,
}; };
+4 -4
View File
@@ -24,8 +24,8 @@ use frame_support::{
dispatch::Codec, dispatch::Codec,
pallet_prelude::*, pallet_prelude::*,
traits::{ traits::{
Currency, CurrencyToVote, Defensive, DefensiveResult, DefensiveSaturating, EnsureOrigin, fungibles, fungibles::Lockable, Currency, CurrencyToVote, Defensive, DefensiveResult,
EstimateNextNewSession, Get, LockIdentifier, LockableCurrency, OnUnbalanced, TryCollect, DefensiveSaturating, EnsureOrigin, EstimateNextNewSession, Get, OnUnbalanced, TryCollect,
UnixTime, UnixTime,
}, },
weights::Weight, weights::Weight,
@@ -50,7 +50,7 @@ use crate::{
ValidatorPrefs, ValidatorPrefs,
}; };
const STAKING_ID: LockIdentifier = *b"staking "; const STAKING_ID: fungibles::LockIdentifier = *b"staking ";
#[frame_support::pallet] #[frame_support::pallet]
pub mod pallet { pub mod pallet {
@@ -78,7 +78,7 @@ pub mod pallet {
#[pallet::config] #[pallet::config]
pub trait Config: frame_system::Config { pub trait Config: frame_system::Config {
/// The staking balance. /// The staking balance.
type Currency: LockableCurrency< type Currency: fungibles::Lockable<
Self::AccountId, Self::AccountId,
Moment = Self::BlockNumber, Moment = Self::BlockNumber,
Balance = Self::CurrencyBalance, Balance = Self::CurrencyBalance,
+1
View File
@@ -20,6 +20,7 @@
//! NOTE: If you're looking for `parameter_types`, it has moved in to the top-level module. //! NOTE: If you're looking for `parameter_types`, it has moved in to the top-level module.
pub mod tokens; pub mod tokens;
#[allow(deprecated)]
pub use tokens::{ pub use tokens::{
currency::{ currency::{
ActiveIssuanceOf, Currency, LockIdentifier, LockableCurrency, NamedReservableCurrency, ActiveIssuanceOf, Currency, LockIdentifier, LockableCurrency, NamedReservableCurrency,
@@ -32,7 +32,10 @@ use sp_std::fmt::Debug;
mod reservable; mod reservable;
pub use reservable::{NamedReservableCurrency, ReservableCurrency}; pub use reservable::{NamedReservableCurrency, ReservableCurrency};
mod lockable; mod lockable;
pub use lockable::{LockIdentifier, LockableCurrency, VestingSchedule};
#[deprecated(note = "Deprecated in favour of using fungibles::Lockable trait directly")]
pub use super::fungibles::{LockIdentifier, Lockable as LockableCurrency};
pub use lockable::VestingSchedule;
/// Abstraction over a fungible assets system. /// Abstraction over a fungible assets system.
pub trait Currency<AccountId> { pub trait Currency<AccountId> {
@@ -17,52 +17,8 @@
//! The lockable currency trait and some associated types. //! The lockable currency trait and some associated types.
use super::{super::misc::WithdrawReasons, Currency}; use super::Currency;
use crate::{dispatch::DispatchResult, traits::misc::Get}; use crate::dispatch::DispatchResult;
/// An identifier for a lock. Used for disambiguating different locks so that
/// they can be individually replaced or removed.
pub type LockIdentifier = [u8; 8];
/// A currency whose accounts can have liquidity restrictions.
pub trait LockableCurrency<AccountId>: Currency<AccountId> {
/// The quantity used to denote time; usually just a `BlockNumber`.
type Moment;
/// The maximum number of locks a user should have on their account.
type MaxLocks: Get<u32>;
/// Create a new balance lock on account `who`.
///
/// If the new lock is valid (i.e. not already expired), it will push the struct to
/// the `Locks` vec in storage. Note that you can lock more funds than a user has.
///
/// If the lock `id` already exists, this will update it.
fn set_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
);
/// Changes a balance lock (selected by `id`) so that it becomes less liquid in all
/// parameters or creates a new one if it does not exist.
///
/// Calling `extend_lock` on an existing lock `id` differs from `set_lock` in that it
/// applies the most severe constraints of the two, while `set_lock` replaces the lock
/// with the new parameters. As in, `extend_lock` will set:
/// - maximum `amount`
/// - bitwise mask of all `reasons`
fn extend_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
);
/// Remove an existing lock.
fn remove_lock(id: LockIdentifier, who: &AccountId);
}
/// A vesting schedule over a currency. This allows a particular currency to have vesting limits /// A vesting schedule over a currency. This allows a particular currency to have vesting limits
/// applied to it. /// applied to it.
@@ -29,6 +29,7 @@ use sp_runtime::traits::Saturating;
mod balanced; mod balanced;
mod imbalance; mod imbalance;
pub use balanced::{Balanced, Unbalanced}; pub use balanced::{Balanced, Unbalanced};
pub use imbalance::{CreditOf, DebtOf, HandleImbalanceDrop, Imbalance}; pub use imbalance::{CreditOf, DebtOf, HandleImbalanceDrop, Imbalance};
@@ -33,7 +33,9 @@ pub mod metadata;
pub use balanced::{Balanced, Unbalanced}; pub use balanced::{Balanced, Unbalanced};
mod imbalance; mod imbalance;
pub use imbalance::{CreditOf, DebtOf, HandleImbalanceDrop, Imbalance}; pub use imbalance::{CreditOf, DebtOf, HandleImbalanceDrop, Imbalance};
mod lockable;
pub mod roles; pub mod roles;
pub use lockable::{LockIdentifier, Lockable};
/// Trait for providing balance-inspection access to a set of named fungible assets. /// Trait for providing balance-inspection access to a set of named fungible assets.
pub trait Inspect<AccountId> { pub trait Inspect<AccountId> {
@@ -0,0 +1,65 @@
// This file is part of Substrate.
// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! The Lockable trait and some associated types.
use super::{super::misc::WithdrawReasons, currency::Currency};
use crate::traits::misc::Get;
/// An identifier for a lock. Used for disambiguating different locks so that
/// they can be individually replaced or removed.
pub type LockIdentifier = [u8; 8];
/// A currency whose accounts can have liquidity restrictions.
pub trait Lockable<AccountId>: Currency<AccountId> {
/// The quantity used to denote time; usually just a `BlockNumber`.
type Moment;
/// The maximum number of locks a user should have on their account.
type MaxLocks: Get<u32>;
/// Create a new balance lock on account `who`.
///
/// If the new lock is valid (i.e. not already expired), it will push the struct to
/// the `Locks` vec in storage. Note that you can lock more funds than a user has.
///
/// If the lock `id` already exists, this will update it.
fn set_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
);
/// Changes a balance lock (selected by `id`) so that it becomes less liquid in all
/// parameters or creates a new one if it does not exist.
///
/// Calling `extend_lock` on an existing lock `id` differs from `set_lock` in that it
/// applies the most severe constraints of the two, while `set_lock` replaces the lock
/// with the new parameters. As in, `extend_lock` will set:
/// - maximum `amount`
/// - bitwise mask of all `reasons`
fn extend_lock(
id: LockIdentifier,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
);
/// Remove an existing lock.
fn remove_lock(id: LockIdentifier, who: &AccountId);
}
+6 -5
View File
@@ -62,7 +62,7 @@ use frame_support::{
ensure, ensure,
storage::bounded_vec::BoundedVec, storage::bounded_vec::BoundedVec,
traits::{ traits::{
Currency, ExistenceRequirement, Get, LockIdentifier, LockableCurrency, VestingSchedule, fungibles, fungibles::Lockable, Currency, ExistenceRequirement, Get, VestingSchedule,
WithdrawReasons, WithdrawReasons,
}, },
weights::Weight, weights::Weight,
@@ -83,11 +83,12 @@ pub use weights::WeightInfo;
type BalanceOf<T> = type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type MaxLocksOf<T> = type MaxLocksOf<T> = <<T as Config>::Currency as fungibles::Lockable<
<<T as Config>::Currency as LockableCurrency<<T as frame_system::Config>::AccountId>>::MaxLocks; <T as frame_system::Config>::AccountId,
>>::MaxLocks;
type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source; type AccountIdLookupOf<T> = <<T as frame_system::Config>::Lookup as StaticLookup>::Source;
const VESTING_ID: LockIdentifier = *b"vesting "; const VESTING_ID: fungibles::LockIdentifier = *b"vesting ";
// A value placed in storage that represents the current version of the Vesting storage. // A value placed in storage that represents the current version of the Vesting storage.
// This value is used by `on_runtime_upgrade` to determine whether we run storage migration logic. // This value is used by `on_runtime_upgrade` to determine whether we run storage migration logic.
@@ -159,7 +160,7 @@ pub mod pallet {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// The currency trait. /// The currency trait.
type Currency: LockableCurrency<Self::AccountId>; type Currency: fungibles::Lockable<Self::AccountId>;
/// Convert the block number into a balance. /// Convert the block number into a balance.
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>; type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>;