15
15
// See the License for the specific language governing permissions and
16
16
// limitations under the License.
17
17
18
- //! # Vesting Module
18
+ //! # Vesting Pallet
19
19
//!
20
20
//! - [`Config`]
21
21
//! - [`Call`]
22
22
//!
23
23
//! ## Overview
24
24
//!
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*
27
27
//! amount for any reason other than transaction fee payment.
28
28
//!
29
29
//! As the amount vested increases over time, the amount unvested reduces. However, locks remain in
34
34
//!
35
35
//! ## Interface
36
36
//!
37
- //! This module implements the `VestingSchedule` trait.
37
+ //! This pallet implements the `VestingSchedule` trait.
38
38
//!
39
39
//! ### Dispatchable Functions
40
40
//!
@@ -50,37 +50,21 @@ pub mod weights;
50
50
use sp_std:: prelude:: * ;
51
51
use sp_std:: fmt:: Debug ;
52
52
use codec:: { Encode , Decode } ;
53
- use sp_runtime:: { DispatchResult , RuntimeDebug , traits:: {
53
+ use sp_runtime:: { RuntimeDebug , traits:: {
54
54
StaticLookup , Zero , AtLeast32BitUnsigned , MaybeSerializeDeserialize , Convert
55
55
} } ;
56
- use frame_support:: { decl_module , decl_event , decl_storage , decl_error , ensure } ;
56
+ use frame_support:: { ensure , pallet_prelude :: * } ;
57
57
use frame_support:: traits:: {
58
58
Currency , LockableCurrency , VestingSchedule , WithdrawReasons , LockIdentifier ,
59
59
ExistenceRequirement , Get ,
60
60
} ;
61
- use frame_system:: { ensure_signed, ensure_root} ;
61
+ use frame_system:: { ensure_signed, ensure_root, pallet_prelude :: * } ;
62
62
pub use weights:: WeightInfo ;
63
+ pub use pallet:: * ;
63
64
64
65
type BalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
65
66
type MaxLocksOf < T > = <<T as Config >:: Currency as LockableCurrency < <T as frame_system:: Config >:: AccountId > >:: MaxLocks ;
66
67
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
-
84
68
const VESTING_ID : LockIdentifier = * b"vesting " ;
85
69
86
70
/// Struct to encode the vesting schedule of an individual account.
@@ -116,23 +100,68 @@ impl<
116
100
}
117
101
}
118
102
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 > ) > ,
125
143
}
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 ) {
129
157
use sp_runtime:: traits:: Saturating ;
158
+
130
159
// Generate initial vesting configuration
131
160
// * who - Account which we are generating vesting configuration for
132
161
// * begin - Block when the account will start to vest
133
162
// * length - Number of blocks from `begin` until fully vested
134
163
// * 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 ( ) {
136
165
let balance = T :: Currency :: free_balance ( who) ;
137
166
assert ! ( !balance. is_zero( ) , "Currencies must be init'd before vesting" ) ;
138
167
// Total genesis `balance` minus `liquid` equals funds locked for vesting
@@ -148,47 +177,41 @@ decl_storage! {
148
177
let reasons = WithdrawReasons :: TRANSFER | WithdrawReasons :: RESERVE ;
149
178
T :: Currency :: set_lock ( VESTING_ID , who, locked, reasons) ;
150
179
}
151
- } )
180
+ }
152
181
}
153
- }
154
182
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 > {
157
187
/// The amount vested has been updated. This could indicate more funds are available. The
158
188
/// balance given is the amount which is left unvested (and thus locked).
159
189
/// \[account, unvested\]
160
- VestingUpdated ( AccountId , Balance ) ,
190
+ VestingUpdated ( T :: AccountId , BalanceOf < T > ) ,
161
191
/// An \[account\] has become fully vested. No further vesting can happen.
162
- VestingCompleted ( AccountId ) ,
192
+ VestingCompleted ( T :: AccountId ) ,
163
193
}
164
- ) ;
165
194
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 > {
169
198
/// The account given is not vesting.
170
199
NotVesting ,
171
200
/// An existing vesting schedule already exists for this account that cannot be clobbered.
172
201
ExistingVestingSchedule ,
173
202
/// Amount being transferred is too low to create a vesting schedule.
174
203
AmountLow ,
175
204
}
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( ) ;
185
205
186
- fn deposit_event( ) = default ;
206
+ #[ pallet:: hooks]
207
+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
187
208
209
+ #[ pallet:: call]
210
+ impl < T : Config > Pallet < T > {
188
211
/// Unlock any vested funds of the sender account.
189
212
///
190
213
/// 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 .
192
215
///
193
216
/// Emits either `VestingCompleted` or `VestingUpdated`.
194
217
///
@@ -198,10 +221,10 @@ decl_module! {
198
221
/// - Reads: Vesting Storage, Balances Locks, [Sender Account]
199
222
/// - Writes: Vesting Storage, Balances Locks, [Sender Account]
200
223
/// # </weight>
201
- #[ weight = T :: WeightInfo :: vest_locked( MaxLocksOf :: <T >:: get( ) )
224
+ #[ pallet :: weight( T :: WeightInfo :: vest_locked( MaxLocksOf :: <T >:: get( ) )
202
225
. max( T :: WeightInfo :: vest_unlocked( MaxLocksOf :: <T >:: get( ) ) )
203
- ]
204
- fn vest( origin) -> DispatchResult {
226
+ ) ]
227
+ pub fn vest ( origin : OriginFor < T > ) -> DispatchResult {
205
228
let who = ensure_signed ( origin) ?;
206
229
Self :: update_lock ( who)
207
230
}
@@ -211,7 +234,7 @@ decl_module! {
211
234
/// The dispatch origin for this call must be _Signed_.
212
235
///
213
236
/// - `target`: The account whose vested funds should be unlocked. Must have funds still
214
- /// locked under this module .
237
+ /// locked under this pallet .
215
238
///
216
239
/// Emits either `VestingCompleted` or `VestingUpdated`.
217
240
///
@@ -221,10 +244,10 @@ decl_module! {
221
244
/// - Reads: Vesting Storage, Balances Locks, Target Account
222
245
/// - Writes: Vesting Storage, Balances Locks, Target Account
223
246
/// # </weight>
224
- #[ weight = T :: WeightInfo :: vest_other_locked( MaxLocksOf :: <T >:: get( ) )
247
+ #[ pallet :: weight( T :: WeightInfo :: vest_other_locked( MaxLocksOf :: <T >:: get( ) )
225
248
. 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 {
228
251
ensure_signed ( origin) ?;
229
252
Self :: update_lock ( T :: Lookup :: lookup ( target) ?)
230
253
}
@@ -245,9 +268,9 @@ decl_module! {
245
268
/// - Reads: Vesting Storage, Balances Locks, Target Account, [Sender Account]
246
269
/// - Writes: Vesting Storage, Balances Locks, Target Account, [Sender Account]
247
270
/// # </weight>
248
- #[ weight = T :: WeightInfo :: vested_transfer( MaxLocksOf :: <T >:: get( ) ) ]
271
+ #[ pallet :: weight( T :: WeightInfo :: vested_transfer( MaxLocksOf :: <T >:: get( ) ) ) ]
249
272
pub fn vested_transfer (
250
- origin,
273
+ origin : OriginFor < T > ,
251
274
target : <T :: Lookup as StaticLookup >:: Source ,
252
275
schedule : VestingInfo < BalanceOf < T > , T :: BlockNumber > ,
253
276
) -> DispatchResult {
@@ -282,9 +305,9 @@ decl_module! {
282
305
/// - Reads: Vesting Storage, Balances Locks, Target Account, Source Account
283
306
/// - Writes: Vesting Storage, Balances Locks, Target Account, Source Account
284
307
/// # </weight>
285
- #[ weight = T :: WeightInfo :: force_vested_transfer( MaxLocksOf :: <T >:: get( ) ) ]
308
+ #[ pallet :: weight( T :: WeightInfo :: force_vested_transfer( MaxLocksOf :: <T >:: get( ) ) ) ]
286
309
pub fn force_vested_transfer (
287
- origin,
310
+ origin : OriginFor < T > ,
288
311
source : <T :: Lookup as StaticLookup >:: Source ,
289
312
target : <T :: Lookup as StaticLookup >:: Source ,
290
313
schedule : VestingInfo < BalanceOf < T > , T :: BlockNumber > ,
@@ -306,8 +329,8 @@ decl_module! {
306
329
}
307
330
}
308
331
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
311
334
/// current unvested amount.
312
335
fn update_lock ( who : T :: AccountId ) -> DispatchResult {
313
336
let vesting = Self :: vesting ( & who) . ok_or ( Error :: < T > :: NotVesting ) ?;
@@ -317,17 +340,17 @@ impl<T: Config> Module<T> {
317
340
if locked_now. is_zero ( ) {
318
341
T :: Currency :: remove_lock ( VESTING_ID , & who) ;
319
342
Vesting :: < T > :: remove ( & who) ;
320
- Self :: deposit_event ( RawEvent :: VestingCompleted ( who) ) ;
343
+ Self :: deposit_event ( Event :: < T > :: VestingCompleted ( who) ) ;
321
344
} else {
322
345
let reasons = WithdrawReasons :: TRANSFER | WithdrawReasons :: RESERVE ;
323
346
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) ) ;
325
348
}
326
349
Ok ( ( ) )
327
350
}
328
351
}
329
352
330
- impl < T : Config > VestingSchedule < T :: AccountId > for Module < T > where
353
+ impl < T : Config > VestingSchedule < T :: AccountId > for Pallet < T > where
331
354
BalanceOf < T > : MaybeSerializeDeserialize + Debug
332
355
{
333
356
type Moment = T :: BlockNumber ;
0 commit comments