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

Commit b6e8438

Browse files
authored
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.
1 parent f02dcaa commit b6e8438

File tree

2 files changed

+95
-72
lines changed

2 files changed

+95
-72
lines changed

frame/vesting/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use frame_system::{RawOrigin, Pallet as System};
2525
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
2626
use sp_runtime::traits::Bounded;
2727

28-
use crate::Module as Vesting;
28+
use crate::Pallet as Vesting;
2929

3030
const SEED: u32 = 0;
3131

frame/vesting/src/lib.rs

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
//! # Vesting Module
18+
//! # Vesting Pallet
1919
//!
2020
//! - [`Config`]
2121
//! - [`Call`]
2222
//!
2323
//! ## Overview
2424
//!
25-
//! A simple module providing a means of placing a linear curve on an account's locked balance. This
26-
//! module ensures that there is a lock in place preventing the balance to drop below the *unvested*
25+
//! A simple pallet providing a means of placing a linear curve on an account's locked balance. This
26+
//! pallet ensures that there is a lock in place preventing the balance to drop below the *unvested*
2727
//! amount for any reason other than transaction fee payment.
2828
//!
2929
//! As the amount vested increases over time, the amount unvested reduces. However, locks remain in
@@ -34,7 +34,7 @@
3434
//!
3535
//! ## Interface
3636
//!
37-
//! This module implements the `VestingSchedule` trait.
37+
//! This pallet implements the `VestingSchedule` trait.
3838
//!
3939
//! ### Dispatchable Functions
4040
//!
@@ -50,37 +50,21 @@ pub mod weights;
5050
use sp_std::prelude::*;
5151
use sp_std::fmt::Debug;
5252
use codec::{Encode, Decode};
53-
use sp_runtime::{DispatchResult, RuntimeDebug, traits::{
53+
use sp_runtime::{RuntimeDebug, traits::{
5454
StaticLookup, Zero, AtLeast32BitUnsigned, MaybeSerializeDeserialize, Convert
5555
}};
56-
use frame_support::{decl_module, decl_event, decl_storage, decl_error, ensure};
56+
use frame_support::{ensure, pallet_prelude::*};
5757
use frame_support::traits::{
5858
Currency, LockableCurrency, VestingSchedule, WithdrawReasons, LockIdentifier,
5959
ExistenceRequirement, Get,
6060
};
61-
use frame_system::{ensure_signed, ensure_root};
61+
use frame_system::{ensure_signed, ensure_root, pallet_prelude::*};
6262
pub use weights::WeightInfo;
63+
pub use pallet::*;
6364

6465
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
6566
type MaxLocksOf<T> = <<T as Config>::Currency as LockableCurrency<<T as frame_system::Config>::AccountId>>::MaxLocks;
6667

67-
pub trait Config: frame_system::Config {
68-
/// The overarching event type.
69-
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
70-
71-
/// The currency trait.
72-
type Currency: LockableCurrency<Self::AccountId>;
73-
74-
/// Convert the block number into a balance.
75-
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>;
76-
77-
/// The minimum amount transferred to call `vested_transfer`.
78-
type MinVestedTransfer: Get<BalanceOf<Self>>;
79-
80-
/// Weight information for extrinsics in this pallet.
81-
type WeightInfo: WeightInfo;
82-
}
83-
8468
const VESTING_ID: LockIdentifier = *b"vesting ";
8569

8670
/// Struct to encode the vesting schedule of an individual account.
@@ -116,23 +100,68 @@ impl<
116100
}
117101
}
118102

119-
decl_storage! {
120-
trait Store for Module<T: Config> as Vesting {
121-
/// Information regarding the vesting of a given account.
122-
pub Vesting get(fn vesting):
123-
map hasher(blake2_128_concat) T::AccountId
124-
=> Option<VestingInfo<BalanceOf<T>, T::BlockNumber>>;
103+
#[frame_support::pallet]
104+
pub mod pallet {
105+
use super::*;
106+
107+
#[pallet::config]
108+
pub trait Config: frame_system::Config {
109+
/// The overarching event type.
110+
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
111+
112+
/// The currency trait.
113+
type Currency: LockableCurrency<Self::AccountId>;
114+
115+
/// Convert the block number into a balance.
116+
type BlockNumberToBalance: Convert<Self::BlockNumber, BalanceOf<Self>>;
117+
118+
/// The minimum amount transferred to call `vested_transfer`.
119+
#[pallet::constant]
120+
type MinVestedTransfer: Get<BalanceOf<Self>>;
121+
122+
/// Weight information for extrinsics in this pallet.
123+
type WeightInfo: WeightInfo;
124+
}
125+
126+
/// Information regarding the vesting of a given account.
127+
#[pallet::storage]
128+
#[pallet::getter(fn vesting)]
129+
pub type Vesting<T: Config> = StorageMap<
130+
_,
131+
Blake2_128Concat,
132+
T::AccountId,
133+
VestingInfo<BalanceOf<T>, T::BlockNumber>,
134+
>;
135+
136+
#[pallet::pallet]
137+
#[pallet::generate_store(pub(super) trait Store)]
138+
pub struct Pallet<T>(_);
139+
140+
#[pallet::genesis_config]
141+
pub struct GenesisConfig<T: Config> {
142+
pub vesting: Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, BalanceOf<T>)>,
125143
}
126-
add_extra_genesis {
127-
config(vesting): Vec<(T::AccountId, T::BlockNumber, T::BlockNumber, BalanceOf<T>)>;
128-
build(|config: &GenesisConfig<T>| {
144+
145+
#[cfg(feature = "std")]
146+
impl<T: Config> Default for GenesisConfig<T> {
147+
fn default() -> Self {
148+
GenesisConfig {
149+
vesting: Default::default(),
150+
}
151+
}
152+
}
153+
154+
#[pallet::genesis_build]
155+
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
156+
fn build(&self) {
129157
use sp_runtime::traits::Saturating;
158+
130159
// Generate initial vesting configuration
131160
// * who - Account which we are generating vesting configuration for
132161
// * begin - Block when the account will start to vest
133162
// * length - Number of blocks from `begin` until fully vested
134163
// * liquid - Number of units which can be spent before vesting begins
135-
for &(ref who, begin, length, liquid) in config.vesting.iter() {
164+
for &(ref who, begin, length, liquid) in self.vesting.iter() {
136165
let balance = T::Currency::free_balance(who);
137166
assert!(!balance.is_zero(), "Currencies must be init'd before vesting");
138167
// Total genesis `balance` minus `liquid` equals funds locked for vesting
@@ -148,47 +177,41 @@ decl_storage! {
148177
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
149178
T::Currency::set_lock(VESTING_ID, who, locked, reasons);
150179
}
151-
})
180+
}
152181
}
153-
}
154182

155-
decl_event!(
156-
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId, Balance = BalanceOf<T> {
183+
#[pallet::event]
184+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
185+
#[pallet::metadata(T::AccountId = "AccountId", BalanceOf<T> = "Balance")]
186+
pub enum Event<T: Config> {
157187
/// The amount vested has been updated. This could indicate more funds are available. The
158188
/// balance given is the amount which is left unvested (and thus locked).
159189
/// \[account, unvested\]
160-
VestingUpdated(AccountId, Balance),
190+
VestingUpdated(T::AccountId, BalanceOf<T>),
161191
/// An \[account\] has become fully vested. No further vesting can happen.
162-
VestingCompleted(AccountId),
192+
VestingCompleted(T::AccountId),
163193
}
164-
);
165194

166-
decl_error! {
167-
/// Error for the vesting module.
168-
pub enum Error for Module<T: Config> {
195+
/// Error for the vesting pallet.
196+
#[pallet::error]
197+
pub enum Error<T> {
169198
/// The account given is not vesting.
170199
NotVesting,
171200
/// An existing vesting schedule already exists for this account that cannot be clobbered.
172201
ExistingVestingSchedule,
173202
/// Amount being transferred is too low to create a vesting schedule.
174203
AmountLow,
175204
}
176-
}
177-
178-
decl_module! {
179-
/// Vesting module declaration.
180-
pub struct Module<T: Config> for enum Call where origin: T::Origin {
181-
type Error = Error<T>;
182-
183-
/// The minimum amount to be transferred to create a new vesting schedule.
184-
const MinVestedTransfer: BalanceOf<T> = T::MinVestedTransfer::get();
185205

186-
fn deposit_event() = default;
206+
#[pallet::hooks]
207+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
187208

209+
#[pallet::call]
210+
impl<T: Config> Pallet<T> {
188211
/// Unlock any vested funds of the sender account.
189212
///
190213
/// The dispatch origin for this call must be _Signed_ and the sender must have funds still
191-
/// locked under this module.
214+
/// locked under this pallet.
192215
///
193216
/// Emits either `VestingCompleted` or `VestingUpdated`.
194217
///
@@ -198,10 +221,10 @@ decl_module! {
198221
/// - Reads: Vesting Storage, Balances Locks, [Sender Account]
199222
/// - Writes: Vesting Storage, Balances Locks, [Sender Account]
200223
/// # </weight>
201-
#[weight = T::WeightInfo::vest_locked(MaxLocksOf::<T>::get())
224+
#[pallet::weight(T::WeightInfo::vest_locked(MaxLocksOf::<T>::get())
202225
.max(T::WeightInfo::vest_unlocked(MaxLocksOf::<T>::get()))
203-
]
204-
fn vest(origin) -> DispatchResult {
226+
)]
227+
pub fn vest(origin: OriginFor<T>) -> DispatchResult {
205228
let who = ensure_signed(origin)?;
206229
Self::update_lock(who)
207230
}
@@ -211,7 +234,7 @@ decl_module! {
211234
/// The dispatch origin for this call must be _Signed_.
212235
///
213236
/// - `target`: The account whose vested funds should be unlocked. Must have funds still
214-
/// locked under this module.
237+
/// locked under this pallet.
215238
///
216239
/// Emits either `VestingCompleted` or `VestingUpdated`.
217240
///
@@ -221,10 +244,10 @@ decl_module! {
221244
/// - Reads: Vesting Storage, Balances Locks, Target Account
222245
/// - Writes: Vesting Storage, Balances Locks, Target Account
223246
/// # </weight>
224-
#[weight = T::WeightInfo::vest_other_locked(MaxLocksOf::<T>::get())
247+
#[pallet::weight(T::WeightInfo::vest_other_locked(MaxLocksOf::<T>::get())
225248
.max(T::WeightInfo::vest_other_unlocked(MaxLocksOf::<T>::get()))
226-
]
227-
fn vest_other(origin, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
249+
)]
250+
pub fn vest_other(origin: OriginFor<T>, target: <T::Lookup as StaticLookup>::Source) -> DispatchResult {
228251
ensure_signed(origin)?;
229252
Self::update_lock(T::Lookup::lookup(target)?)
230253
}
@@ -245,9 +268,9 @@ decl_module! {
245268
/// - Reads: Vesting Storage, Balances Locks, Target Account, [Sender Account]
246269
/// - Writes: Vesting Storage, Balances Locks, Target Account, [Sender Account]
247270
/// # </weight>
248-
#[weight = T::WeightInfo::vested_transfer(MaxLocksOf::<T>::get())]
271+
#[pallet::weight(T::WeightInfo::vested_transfer(MaxLocksOf::<T>::get()))]
249272
pub fn vested_transfer(
250-
origin,
273+
origin: OriginFor<T>,
251274
target: <T::Lookup as StaticLookup>::Source,
252275
schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>,
253276
) -> DispatchResult {
@@ -282,9 +305,9 @@ decl_module! {
282305
/// - Reads: Vesting Storage, Balances Locks, Target Account, Source Account
283306
/// - Writes: Vesting Storage, Balances Locks, Target Account, Source Account
284307
/// # </weight>
285-
#[weight = T::WeightInfo::force_vested_transfer(MaxLocksOf::<T>::get())]
308+
#[pallet::weight(T::WeightInfo::force_vested_transfer(MaxLocksOf::<T>::get()))]
286309
pub fn force_vested_transfer(
287-
origin,
310+
origin: OriginFor<T>,
288311
source: <T::Lookup as StaticLookup>::Source,
289312
target: <T::Lookup as StaticLookup>::Source,
290313
schedule: VestingInfo<BalanceOf<T>, T::BlockNumber>,
@@ -306,8 +329,8 @@ decl_module! {
306329
}
307330
}
308331

309-
impl<T: Config> Module<T> {
310-
/// (Re)set or remove the module's currency lock on `who`'s account in accordance with their
332+
impl<T: Config> Pallet<T> {
333+
/// (Re)set or remove the pallet's currency lock on `who`'s account in accordance with their
311334
/// current unvested amount.
312335
fn update_lock(who: T::AccountId) -> DispatchResult {
313336
let vesting = Self::vesting(&who).ok_or(Error::<T>::NotVesting)?;
@@ -317,17 +340,17 @@ impl<T: Config> Module<T> {
317340
if locked_now.is_zero() {
318341
T::Currency::remove_lock(VESTING_ID, &who);
319342
Vesting::<T>::remove(&who);
320-
Self::deposit_event(RawEvent::VestingCompleted(who));
343+
Self::deposit_event(Event::<T>::VestingCompleted(who));
321344
} else {
322345
let reasons = WithdrawReasons::TRANSFER | WithdrawReasons::RESERVE;
323346
T::Currency::set_lock(VESTING_ID, &who, locked_now, reasons);
324-
Self::deposit_event(RawEvent::VestingUpdated(who, locked_now));
347+
Self::deposit_event(Event::<T>::VestingUpdated(who, locked_now));
325348
}
326349
Ok(())
327350
}
328351
}
329352

330-
impl<T: Config> VestingSchedule<T::AccountId> for Module<T> where
353+
impl<T: Config> VestingSchedule<T::AccountId> for Pallet<T> where
331354
BalanceOf<T>: MaybeSerializeDeserialize + Debug
332355
{
333356
type Moment = T::BlockNumber;

0 commit comments

Comments
 (0)