Migrate pallet-vesting to pallet attribute macro. (#8440)

* Migrate pallet-vesting to pallet attribute macro.

* Update metadata type alias.

* Replace 'Module' with 'Pallet' in benchmarking.

* Trigger CI.
This commit is contained in:
Shaun Wang
2021-03-25 01:29:30 +13:00
committed by GitHub
parent e309d94f1a
commit f93d7b874e
2 changed files with 95 additions and 72 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ use frame_system::{RawOrigin, Pallet as System};
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite}; use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
use sp_runtime::traits::Bounded; use sp_runtime::traits::Bounded;
use crate::Module as Vesting; use crate::Pallet as Vesting;
const SEED: u32 = 0; const SEED: u32 = 0;
+94 -71
View File
@@ -15,15 +15,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! # Vesting Module //! # Vesting Pallet
//! //!
//! - [`Config`] //! - [`Config`]
//! - [`Call`] //! - [`Call`]
//! //!
//! ## Overview //! ## Overview
//! //!
//! A simple module providing a means of placing a linear curve on an account's locked balance. This //! A simple pallet providing a means of placing a linear curve on an account's locked balance. This
//! module ensures that there is a lock in place preventing the balance to drop below the *unvested* //! pallet ensures that there is a lock in place preventing the balance to drop below the *unvested*
//! amount for any reason other than transaction fee payment. //! amount for any reason other than transaction fee payment.
//! //!
//! As the amount vested increases over time, the amount unvested reduces. However, locks remain in //! As the amount vested increases over time, the amount unvested reduces. However, locks remain in
@@ -34,7 +34,7 @@
//! //!
//! ## Interface //! ## Interface
//! //!
//! This module implements the `VestingSchedule` trait. //! This pallet implements the `VestingSchedule` trait.
//! //!
//! ### Dispatchable Functions //! ### Dispatchable Functions
//! //!
@@ -50,37 +50,21 @@ pub mod weights;
use sp_std::prelude::*; use sp_std::prelude::*;
use sp_std::fmt::Debug; use sp_std::fmt::Debug;
use codec::{Encode, Decode}; use codec::{Encode, Decode};
use sp_runtime::{DispatchResult, RuntimeDebug, traits::{ use sp_runtime::{RuntimeDebug, traits::{
StaticLookup, Zero, AtLeast32BitUnsigned, MaybeSerializeDeserialize, Convert StaticLookup, Zero, AtLeast32BitUnsigned, MaybeSerializeDeserialize, Convert
}}; }};
use frame_support::{decl_module, decl_event, decl_storage, decl_error, ensure}; use frame_support::{ensure, pallet_prelude::*};
use frame_support::traits::{ use frame_support::traits::{
Currency, LockableCurrency, VestingSchedule, WithdrawReasons, LockIdentifier, Currency, LockableCurrency, VestingSchedule, WithdrawReasons, LockIdentifier,
ExistenceRequirement, Get, ExistenceRequirement, Get,
}; };
use frame_system::{ensure_signed, ensure_root}; use frame_system::{ensure_signed, ensure_root, pallet_prelude::*};
pub use weights::WeightInfo; pub use weights::WeightInfo;
pub use pallet::*;
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance; type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
type MaxLocksOf<T> = <<T as Config>::Currency as LockableCurrency<<T as frame_system::Config>::AccountId>>::MaxLocks; type MaxLocksOf<T> = <<T as Config>::Currency as LockableCurrency<<T as frame_system::Config>::AccountId>>::MaxLocks;
pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
/// The currency trait.
type Currency: LockableCurrency<Self::AccountId>;
/// Convert the block number into a balance.
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>;
/// The minimum amount transferred to call `vested_transfer`.
type MinVestedTransfer: Get<BalanceOf<Self>>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
const VESTING_ID: LockIdentifier = *b"vesting "; const VESTING_ID: LockIdentifier = *b"vesting ";
/// Struct to encode the vesting schedule of an individual account. /// Struct to encode the vesting schedule of an individual account.
@@ -116,23 +100,68 @@ impl<
} }
} }
decl_storage! { #[frame_support::pallet]
trait Store for Module<T: Config> as Vesting { pub mod pallet {
/// Information regarding the vesting of a given account. use super::*;
pub Vesting get(fn vesting):
map hasher(blake2_128_concat) T::AccountId #[pallet::config]
=> Option<VestingInfo<BalanceOf<T>, T::BlockNumber>>; pub trait Config: frame_system::Config {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The currency trait.
type Currency: LockableCurrency<Self::AccountId>;
/// Convert the block number into a balance.
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>;
/// The minimum amount transferred to call `vested_transfer`.
#[pallet::constant]
type MinVestedTransfer: Get<BalanceOf<Self>>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
} }
add_extra_genesis {
config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, BalanceOf<T>)>; /// Information regarding the vesting of a given account.
build(|config: &GenesisConfig<T>| { #[pallet::storage]
#[pallet::getter(fn vesting)]
pub type Vesting<T: Config> = StorageMap<
_,
Blake2_128Concat,
T::AccountId,
VestingInfo<BalanceOf<T>, T::BlockNumber>,
>;
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);
#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub vesting: Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, BalanceOf<T>)>,
}
#[cfg(feature = "std")]
impl<T: Config> Default for GenesisConfig<T> {
fn default() -> Self {
GenesisConfig {
vesting: Default::default(),
}
}
}
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
use sp_runtime::traits::Saturating; use sp_runtime::traits::Saturating;
// Generate initial vesting configuration // Generate initial vesting configuration
// * who - Account which we are generating vesting configuration for // * who - Account which we are generating vesting configuration for
// * begin - Block when the account will start to vest // * begin - Block when the account will start to vest
// * length - Number of blocks from `begin` until fully vested // * length - Number of blocks from `begin` until fully vested
// * liquid - Number of units which can be spent before vesting begins // * liquid - Number of units which can be spent before vesting begins
for &(ref who, begin, length, liquid) in config.vesting.iter() { for &(ref who, begin, length, liquid) in self.vesting.iter() {
let balance = T::Currency::free_balance(who); let balance = T::Currency::free_balance(who);
assert!(!balance.is_zero(), "Currencies must be init'd before vesting"); assert!(!balance.is_zero(), "Currencies must be init'd before vesting");
// Total genesis `balance` minus `liquid` equals funds locked for vesting // Total genesis `balance` minus `liquid` equals funds locked for vesting
@@ -148,24 +177,24 @@ decl_storage! {
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE; let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
T::Currency::set_lock(VESTING_ID, who, locked, reasons); T::Currency::set_lock(VESTING_ID, who, locked, reasons);
} }
}) }
} }
}
decl_event!( #[pallet::event]
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId, Balance = BalanceOf<T> { #[pallet::generate_deposit(pub(super) fn deposit_event)]
#[pallet::metadata(T::AccountId = "AccountId", BalanceOf<T> = "Balance")]
pub enum Event<T: Config> {
/// The amount vested has been updated. This could indicate more funds are available. The /// The amount vested has been updated. This could indicate more funds are available. The
/// balance given is the amount which is left unvested (and thus locked). /// balance given is the amount which is left unvested (and thus locked).
/// \[account, unvested\] /// \[account, unvested\]
VestingUpdated(AccountId, Balance), VestingUpdated(T::AccountId, BalanceOf<T>),
/// An \[account\] has become fully vested. No further vesting can happen. /// An \[account\] has become fully vested. No further vesting can happen.
VestingCompleted(AccountId), VestingCompleted(T::AccountId),
} }
);
decl_error! { /// Error for the vesting pallet.
/// Error for the vesting module. #[pallet::error]
pub enum Error for Module<T: Config> { pub enum Error<T> {
/// The account given is not vesting. /// The account given is not vesting.
NotVesting, NotVesting,
/// An existing vesting schedule already exists for this account that cannot be clobbered. /// An existing vesting schedule already exists for this account that cannot be clobbered.
@@ -173,22 +202,16 @@ decl_error! {
/// Amount being transferred is too low to create a vesting schedule. /// Amount being transferred is too low to create a vesting schedule.
AmountLow, AmountLow,
} }
}
decl_module! { #[pallet::hooks]
/// Vesting module declaration. impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
pub struct Module<T: Config> for enum Call where origin: T::Origin {
type Error = Error<T>;
/// The minimum amount to be transferred to create a new vesting schedule.
const MinVestedTransfer: BalanceOf<T> = T::MinVestedTransfer::get();
fn deposit_event() = default;
#[pallet::call]
impl<T: Config> Pallet<T> {
/// Unlock any vested funds of the sender account. /// Unlock any vested funds of the sender account.
/// ///
/// The dispatch origin for this call must be _Signed_ and the sender must have funds still /// The dispatch origin for this call must be _Signed_ and the sender must have funds still
/// locked under this module. /// locked under this pallet.
/// ///
/// Emits either `VestingCompleted` or `VestingUpdated`. /// Emits either `VestingCompleted` or `VestingUpdated`.
/// ///
@@ -198,10 +221,10 @@ decl_module! {
/// - Reads: Vesting Storage, Balances Locks, [Sender Account] /// - Reads: Vesting Storage, Balances Locks, [Sender Account]
/// - Writes: Vesting Storage, Balances Locks, [Sender Account] /// - Writes: Vesting Storage, Balances Locks, [Sender Account]
/// # </weight> /// # </weight>
#[weight = T::WeightInfo::vest_locked(MaxLocksOf::<T>::get()) #[pallet::weight(T::WeightInfo::vest_locked(MaxLocksOf::<T>::get())
.max(T::WeightInfo::vest_unlocked(MaxLocksOf::<T>::get())) .max(T::WeightInfo::vest_unlocked(MaxLocksOf::<T>::get()))
] )]
fn vest(origin) -> DispatchResult { pub fn vest(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?; let who = ensure_signed(origin)?;
Self::update_lock(who) Self::update_lock(who)
} }
@@ -211,7 +234,7 @@ decl_module! {
/// The dispatch origin for this call must be _Signed_. /// The dispatch origin for this call must be _Signed_.
/// ///
/// - `target`: The account whose vested funds should be unlocked. Must have funds still /// - `target`: The account whose vested funds should be unlocked. Must have funds still
/// locked under this module. /// locked under this pallet.
/// ///
/// Emits either `VestingCompleted` or `VestingUpdated`. /// Emits either `VestingCompleted` or `VestingUpdated`.
/// ///
@@ -221,10 +244,10 @@ decl_module! {
/// - Reads: Vesting Storage, Balances Locks, Target Account /// - Reads: Vesting Storage, Balances Locks, Target Account
/// - Writes: Vesting Storage, Balances Locks, Target Account /// - Writes: Vesting Storage, Balances Locks, Target Account
/// # </weight> /// # </weight>
#[weight = T::WeightInfo::vest_other_locked(MaxLocksOf::<T>::get()) #[pallet::weight(T::WeightInfo::vest_other_locked(MaxLocksOf::<T>::get())
.max(T::WeightInfo::vest_other_unlocked(MaxLocksOf::<T>::get())) .max(T::WeightInfo::vest_other_unlocked(MaxLocksOf::<T>::get()))
] )]
fn vest_other(origin, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult { pub fn vest_other(origin: OriginFor<T>, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
ensure_signed(origin)?; ensure_signed(origin)?;
Self::update_lock(T::Lookup::lookup(target)?) Self::update_lock(T::Lookup::lookup(target)?)
} }
@@ -245,9 +268,9 @@ decl_module! {
/// - Reads: Vesting Storage, Balances Locks, Target Account, [Sender Account] /// - Reads: Vesting Storage, Balances Locks, Target Account, [Sender Account]
/// - Writes: Vesting Storage, Balances Locks, Target Account, [Sender Account] /// - Writes: Vesting Storage, Balances Locks, Target Account, [Sender Account]
/// # </weight> /// # </weight>
#[weight = T::WeightInfo::vested_transfer(MaxLocksOf::<T>::get())] #[pallet::weight(T::WeightInfo::vested_transfer(MaxLocksOf::<T>::get()))]
pub fn vested_transfer( pub fn vested_transfer(
origin, origin: OriginFor<T>,
target: <T::Lookup as StaticLookup>::Source, target: <T::Lookup as StaticLookup>::Source,
schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>, schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>,
) -> DispatchResult { ) -> DispatchResult {
@@ -282,9 +305,9 @@ decl_module! {
/// - Reads: Vesting Storage, Balances Locks, Target Account, Source Account /// - Reads: Vesting Storage, Balances Locks, Target Account, Source Account
/// - Writes: Vesting Storage, Balances Locks, Target Account, Source Account /// - Writes: Vesting Storage, Balances Locks, Target Account, Source Account
/// # </weight> /// # </weight>
#[weight = T::WeightInfo::force_vested_transfer(MaxLocksOf::<T>::get())] #[pallet::weight(T::WeightInfo::force_vested_transfer(MaxLocksOf::<T>::get()))]
pub fn force_vested_transfer( pub fn force_vested_transfer(
origin, origin: OriginFor<T>,
source: <T::Lookup as StaticLookup>::Source, source: <T::Lookup as StaticLookup>::Source,
target: <T::Lookup as StaticLookup>::Source, target: <T::Lookup as StaticLookup>::Source,
schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>, schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>,
@@ -306,8 +329,8 @@ decl_module! {
} }
} }
impl<T: Config> Module<T> { impl<T: Config> Pallet<T> {
/// (Re)set or remove the module's currency lock on `who`'s account in accordance with their /// (Re)set or remove the pallet's currency lock on `who`'s account in accordance with their
/// current unvested amount. /// current unvested amount.
fn update_lock(who: T::AccountId) -> DispatchResult { fn update_lock(who: T::AccountId) -> DispatchResult {
let vesting = Self::vesting(&who).ok_or(Error::<T>::NotVesting)?; let vesting = Self::vesting(&who).ok_or(Error::<T>::NotVesting)?;
@@ -317,17 +340,17 @@ impl<T: Config> Module<T> {
if locked_now.is_zero() { if locked_now.is_zero() {
T::Currency::remove_lock(VESTING_ID, &who); T::Currency::remove_lock(VESTING_ID, &who);
Vesting::<T>::remove(&who); Vesting::<T>::remove(&who);
Self::deposit_event(RawEvent::VestingCompleted(who)); Self::deposit_event(Event::<T>::VestingCompleted(who));
} else { } else {
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE; let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
T::Currency::set_lock(VESTING_ID, &who, locked_now, reasons); T::Currency::set_lock(VESTING_ID, &who, locked_now, reasons);
Self::deposit_event(RawEvent::VestingUpdated(who, locked_now)); Self::deposit_event(Event::<T>::VestingUpdated(who, locked_now));
} }
Ok(()) Ok(())
} }
} }
impl<T: Config> VestingSchedule<T::AccountId> for Module<T> where impl<T: Config> VestingSchedule<T::AccountId> for Pallet<T> where
BalanceOf<T>: MaybeSerializeDeserialize + Debug BalanceOf<T>: MaybeSerializeDeserialize + Debug
{ {
type Moment = T::BlockNumber; type Moment = T::BlockNumber;