@@ -29,86 +29,51 @@ use sp_std::prelude::*;
29
29
use codec:: Codec ;
30
30
use sp_runtime:: MultiAddress ;
31
31
use sp_runtime:: traits:: {
32
- StaticLookup , Member , LookupError , Zero , Saturating , AtLeast32Bit
32
+ StaticLookup , LookupError , Zero , Saturating , AtLeast32Bit
33
33
} ;
34
- use frame_support:: { Parameter , decl_module, decl_error, decl_event, decl_storage, ensure} ;
35
- use frame_support:: dispatch:: DispatchResult ;
36
- use frame_support:: traits:: { Currency , ReservableCurrency , Get , BalanceStatus :: Reserved } ;
37
- use frame_system:: { ensure_signed, ensure_root} ;
34
+ use frame_support:: traits:: { Currency , ReservableCurrency , BalanceStatus :: Reserved } ;
38
35
pub use weights:: WeightInfo ;
39
36
40
37
type BalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
41
38
42
- /// The module's config trait.
43
- pub trait Config : frame_system:: Config {
44
- /// Type used for storing an account's index; implies the maximum number of accounts the system
45
- /// can hold.
46
- type AccountIndex : Parameter + Member + Codec + Default + AtLeast32Bit + Copy ;
39
+ pub use pallet:: * ;
47
40
48
- /// The currency trait.
49
- type Currency : ReservableCurrency < Self :: AccountId > ;
41
+ #[ frame_support:: pallet]
42
+ pub mod pallet {
43
+ use frame_support:: pallet_prelude:: * ;
44
+ use frame_system:: pallet_prelude:: * ;
45
+ use super :: * ;
50
46
51
- /// The deposit needed for reserving an index.
52
- type Deposit : Get < BalanceOf < Self > > ;
47
+ /// The module's config trait.
48
+ #[ pallet:: config]
49
+ pub trait Config : frame_system:: Config {
50
+ /// Type used for storing an account's index; implies the maximum number of accounts the system
51
+ /// can hold.
52
+ type AccountIndex : Parameter + Member + MaybeSerializeDeserialize + Codec + Default + AtLeast32Bit + Copy ;
53
53
54
- /// The overarching event type .
55
- type Event : From < Event < Self > > + Into < < Self as frame_system :: Config > :: Event > ;
54
+ /// The currency trait .
55
+ type Currency : ReservableCurrency < Self :: AccountId > ;
56
56
57
- /// Weight information for extrinsics in this pallet.
58
- type WeightInfo : WeightInfo ;
59
- }
60
-
61
- decl_storage ! {
62
- trait Store for Module <T : Config > as Indices {
63
- /// The lookup from index to account.
64
- pub Accounts build( |config: & GenesisConfig <T >|
65
- config. indices. iter( )
66
- . cloned( )
67
- . map( |( a, b) | ( a, ( b, Zero :: zero( ) , false ) ) )
68
- . collect:: <Vec <_>>( )
69
- ) : map hasher( blake2_128_concat) T :: AccountIndex => Option <( T :: AccountId , BalanceOf <T >, bool ) >;
70
- }
71
- add_extra_genesis {
72
- config( indices) : Vec <( T :: AccountIndex , T :: AccountId ) >;
73
- }
74
- }
57
+ /// The deposit needed for reserving an index.
58
+ #[ pallet:: constant]
59
+ type Deposit : Get < BalanceOf < Self > > ;
75
60
76
- decl_event ! (
77
- pub enum Event <T > where
78
- <T as frame_system:: Config >:: AccountId ,
79
- <T as Config >:: AccountIndex
80
- {
81
- /// A account index was assigned. \[index, who\]
82
- IndexAssigned ( AccountId , AccountIndex ) ,
83
- /// A account index has been freed up (unassigned). \[index\]
84
- IndexFreed ( AccountIndex ) ,
85
- /// A account index has been frozen to its current account ID. \[index, who\]
86
- IndexFrozen ( AccountIndex , AccountId ) ,
87
- }
88
- ) ;
61
+ /// The overarching event type.
62
+ type Event : From < Event < Self > > + IsType < <Self as frame_system:: Config >:: Event > ;
89
63
90
- decl_error ! {
91
- pub enum Error for Module <T : Config > {
92
- /// The index was not already assigned.
93
- NotAssigned ,
94
- /// The index is assigned to another account.
95
- NotOwner ,
96
- /// The index was not available.
97
- InUse ,
98
- /// The source and destination accounts are identical.
99
- NotTransfer ,
100
- /// The index is permanent and may not be freed/changed.
101
- Permanent ,
64
+ /// Weight information for extrinsics in this pallet.
65
+ type WeightInfo : WeightInfo ;
102
66
}
103
- }
104
67
105
- decl_module ! {
106
- pub struct Module <T : Config > for enum Call where origin: T :: Origin , system = frame_system {
107
- /// The deposit needed for reserving an index.
108
- const Deposit : BalanceOf <T > = T :: Deposit :: get( ) ;
68
+ #[ pallet:: pallet]
69
+ #[ pallet:: generate_store( pub ( super ) trait Store ) ]
70
+ pub struct Pallet < T > ( PhantomData < T > ) ;
109
71
110
- fn deposit_event( ) = default ;
72
+ #[ pallet:: hooks]
73
+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
111
74
75
+ #[ pallet:: call]
76
+ impl < T : Config > Pallet < T > {
112
77
/// Assign an previously unassigned index.
113
78
///
114
79
/// Payment: `Deposit` is reserved from the sender account.
@@ -127,16 +92,17 @@ decl_module! {
127
92
/// -------------------
128
93
/// - DB Weight: 1 Read/Write (Accounts)
129
94
/// # </weight>
130
- #[ weight = T :: WeightInfo :: claim( ) ]
131
- fn claim( origin, index: T :: AccountIndex ) {
95
+ #[ pallet :: weight( T :: WeightInfo :: claim( ) ) ]
96
+ pub ( crate ) fn claim ( origin : OriginFor < T > , index : T :: AccountIndex ) -> DispatchResult {
132
97
let who = ensure_signed ( origin) ?;
133
98
134
99
Accounts :: < T > :: try_mutate ( index, |maybe_value| {
135
100
ensure ! ( maybe_value. is_none( ) , Error :: <T >:: InUse ) ;
136
101
* maybe_value = Some ( ( who. clone ( ) , T :: Deposit :: get ( ) , false ) ) ;
137
102
T :: Currency :: reserve ( & who, T :: Deposit :: get ( ) )
138
103
} ) ?;
139
- Self :: deposit_event( RawEvent :: IndexAssigned ( who, index) ) ;
104
+ Self :: deposit_event ( Event :: IndexAssigned ( who, index) ) ;
105
+ Ok ( ( ) )
140
106
}
141
107
142
108
/// Assign an index already owned by the sender to another account. The balance reservation
@@ -159,8 +125,12 @@ decl_module! {
159
125
/// - Reads: Indices Accounts, System Account (recipient)
160
126
/// - Writes: Indices Accounts, System Account (recipient)
161
127
/// # </weight>
162
- #[ weight = T :: WeightInfo :: transfer( ) ]
163
- fn transfer( origin, new: T :: AccountId , index: T :: AccountIndex ) {
128
+ #[ pallet:: weight( T :: WeightInfo :: transfer( ) ) ]
129
+ pub ( crate ) fn transfer (
130
+ origin : OriginFor < T > ,
131
+ new : T :: AccountId ,
132
+ index : T :: AccountIndex ,
133
+ ) -> DispatchResult {
164
134
let who = ensure_signed ( origin) ?;
165
135
ensure ! ( who != new, Error :: <T >:: NotTransfer ) ;
166
136
@@ -172,7 +142,8 @@ decl_module! {
172
142
* maybe_value = Some ( ( new. clone ( ) , amount. saturating_sub ( lost) , false ) ) ;
173
143
Ok ( ( ) )
174
144
} ) ?;
175
- Self :: deposit_event( RawEvent :: IndexAssigned ( new, index) ) ;
145
+ Self :: deposit_event ( Event :: IndexAssigned ( new, index) ) ;
146
+ Ok ( ( ) )
176
147
}
177
148
178
149
/// Free up an index owned by the sender.
@@ -193,8 +164,8 @@ decl_module! {
193
164
/// -------------------
194
165
/// - DB Weight: 1 Read/Write (Accounts)
195
166
/// # </weight>
196
- #[ weight = T :: WeightInfo :: free( ) ]
197
- fn free( origin, index: T :: AccountIndex ) {
167
+ #[ pallet :: weight( T :: WeightInfo :: free( ) ) ]
168
+ pub ( crate ) fn free ( origin : OriginFor < T > , index : T :: AccountIndex ) -> DispatchResult {
198
169
let who = ensure_signed ( origin) ?;
199
170
200
171
Accounts :: < T > :: try_mutate ( index, |maybe_value| -> DispatchResult {
@@ -204,7 +175,8 @@ decl_module! {
204
175
T :: Currency :: unreserve ( & who, amount) ;
205
176
Ok ( ( ) )
206
177
} ) ?;
207
- Self :: deposit_event( RawEvent :: IndexFreed ( index) ) ;
178
+ Self :: deposit_event ( Event :: IndexFreed ( index) ) ;
179
+ Ok ( ( ) )
208
180
}
209
181
210
182
/// Force an index to an account. This doesn't require a deposit. If the index is already
@@ -228,8 +200,13 @@ decl_module! {
228
200
/// - Reads: Indices Accounts, System Account (original owner)
229
201
/// - Writes: Indices Accounts, System Account (original owner)
230
202
/// # </weight>
231
- #[ weight = T :: WeightInfo :: force_transfer( ) ]
232
- fn force_transfer( origin, new: T :: AccountId , index: T :: AccountIndex , freeze: bool ) {
203
+ #[ pallet:: weight( T :: WeightInfo :: force_transfer( ) ) ]
204
+ pub ( crate ) fn force_transfer (
205
+ origin : OriginFor < T > ,
206
+ new : T :: AccountId ,
207
+ index : T :: AccountIndex ,
208
+ freeze : bool ,
209
+ ) -> DispatchResult {
233
210
ensure_root ( origin) ?;
234
211
235
212
Accounts :: < T > :: mutate ( index, |maybe_value| {
@@ -238,7 +215,8 @@ decl_module! {
238
215
}
239
216
* maybe_value = Some ( ( new. clone ( ) , Zero :: zero ( ) , freeze) ) ;
240
217
} ) ;
241
- Self :: deposit_event( RawEvent :: IndexAssigned ( new, index) ) ;
218
+ Self :: deposit_event ( Event :: IndexAssigned ( new, index) ) ;
219
+ Ok ( ( ) )
242
220
}
243
221
244
222
/// Freeze an index so it will always point to the sender account. This consumes the deposit.
@@ -258,8 +236,8 @@ decl_module! {
258
236
/// -------------------
259
237
/// - DB Weight: 1 Read/Write (Accounts)
260
238
/// # </weight>
261
- #[ weight = T :: WeightInfo :: freeze( ) ]
262
- fn freeze( origin, index: T :: AccountIndex ) {
239
+ #[ pallet :: weight( T :: WeightInfo :: freeze( ) ) ]
240
+ pub ( crate ) fn freeze ( origin : OriginFor < T > , index : T :: AccountIndex ) -> DispatchResult {
263
241
let who = ensure_signed ( origin) ?;
264
242
265
243
Accounts :: < T > :: try_mutate ( index, |maybe_value| -> DispatchResult {
@@ -270,12 +248,74 @@ decl_module! {
270
248
* maybe_value = Some ( ( account, Zero :: zero ( ) , true ) ) ;
271
249
Ok ( ( ) )
272
250
} ) ?;
273
- Self :: deposit_event( RawEvent :: IndexFrozen ( index, who) ) ;
251
+ Self :: deposit_event ( Event :: IndexFrozen ( index, who) ) ;
252
+ Ok ( ( ) )
253
+ }
254
+ }
255
+
256
+ #[ pallet:: event]
257
+ #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
258
+ #[ pallet:: metadata( T :: AccountId = "AccountId" , T :: AccountIndex = "AccountIndex" ) ]
259
+ pub enum Event < T : Config > {
260
+ /// A account index was assigned. \[index, who\]
261
+ IndexAssigned ( T :: AccountId , T :: AccountIndex ) ,
262
+ /// A account index has been freed up (unassigned). \[index\]
263
+ IndexFreed ( T :: AccountIndex ) ,
264
+ /// A account index has been frozen to its current account ID. \[index, who\]
265
+ IndexFrozen ( T :: AccountIndex , T :: AccountId ) ,
266
+ }
267
+
268
+ /// Old name generated by `decl_event`.
269
+ #[ deprecated( note="use `Event` instead" ) ]
270
+ pub type RawEvent < T > = Event < T > ;
271
+
272
+ #[ pallet:: error]
273
+ pub enum Error < T > {
274
+ /// The index was not already assigned.
275
+ NotAssigned ,
276
+ /// The index is assigned to another account.
277
+ NotOwner ,
278
+ /// The index was not available.
279
+ InUse ,
280
+ /// The source and destination accounts are identical.
281
+ NotTransfer ,
282
+ /// The index is permanent and may not be freed/changed.
283
+ Permanent ,
284
+ }
285
+
286
+ /// The lookup from index to account.
287
+ #[ pallet:: storage]
288
+ pub type Accounts < T : Config > = StorageMap <
289
+ _ , Blake2_128Concat ,
290
+ T :: AccountIndex ,
291
+ ( T :: AccountId , BalanceOf < T > , bool )
292
+ > ;
293
+
294
+ #[ pallet:: genesis_config]
295
+ pub struct GenesisConfig < T : Config > {
296
+ pub indices : Vec < ( T :: AccountIndex , T :: AccountId ) > ,
297
+ }
298
+
299
+ #[ cfg( feature = "std" ) ]
300
+ impl < T : Config > Default for GenesisConfig < T > {
301
+ fn default ( ) -> Self {
302
+ Self {
303
+ indices : Default :: default ( ) ,
304
+ }
305
+ }
306
+ }
307
+
308
+ #[ pallet:: genesis_build]
309
+ impl < T : Config > GenesisBuild < T > for GenesisConfig < T > {
310
+ fn build ( & self ) {
311
+ for ( a, b) in & self . indices {
312
+ <Accounts < T > >:: insert ( a, ( b, <BalanceOf < T > >:: zero ( ) , false ) )
313
+ }
274
314
}
275
315
}
276
316
}
277
317
278
- impl < T : Config > Module < T > {
318
+ impl < T : Config > Pallet < T > {
279
319
// PUBLIC IMMUTABLES
280
320
281
321
/// Lookup an T::AccountIndex to get an Id, if there's one there.
@@ -295,7 +335,7 @@ impl<T: Config> Module<T> {
295
335
}
296
336
}
297
337
298
- impl < T : Config > StaticLookup for Module < T > {
338
+ impl < T : Config > StaticLookup for Pallet < T > {
299
339
type Source = MultiAddress < T :: AccountId , T :: AccountIndex > ;
300
340
type Target = T :: AccountId ;
301
341
0 commit comments