15
15
// See the License for the specific language governing permissions and
16
16
// limitations under the License.
17
17
18
- //! # Nicks Module
18
+ //! # Nicks Pallet
19
19
//!
20
20
//! - [`Config`]
21
21
//! - [`Call`]
22
22
//!
23
23
//! ## Overview
24
24
//!
25
- //! Nicks is an example module for keeping track of account names on-chain. It makes no effort to
25
+ //! Nicks is an example pallet for keeping track of account names on-chain. It makes no effort to
26
26
//! create a name hierarchy, be a DNS replacement or provide reverse lookups. Furthermore, the
27
- //! weights attached to this module 's dispatchable functions are for demonstration purposes only and
27
+ //! weights attached to this pallet 's dispatchable functions are for demonstration purposes only and
28
28
//! have not been designed to be economically secure. Do not use this pallet as-is in production.
29
29
//!
30
30
//! ## Interface
@@ -45,88 +45,85 @@ use sp_std::prelude::*;
45
45
use sp_runtime:: {
46
46
traits:: { StaticLookup , Zero }
47
47
} ;
48
- use frame_support:: {
49
- decl_module, decl_event, decl_storage, ensure, decl_error,
50
- traits:: { Currency , EnsureOrigin , ReservableCurrency , OnUnbalanced , Get } ,
51
- } ;
52
- use frame_system:: ensure_signed;
48
+ use frame_support:: traits:: { Currency , ReservableCurrency , OnUnbalanced } ;
49
+ pub use pallet:: * ;
53
50
54
51
type BalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
55
52
type NegativeImbalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: NegativeImbalance ;
56
53
57
- pub trait Config : frame_system:: Config {
58
- /// The overarching event type.
59
- type Event : From < Event < Self > > + Into < <Self as frame_system:: Config >:: Event > ;
54
+ #[ frame_support:: pallet]
55
+ pub mod pallet {
56
+ use frame_system:: { ensure_signed, pallet_prelude:: * } ;
57
+ use frame_support:: { ensure, pallet_prelude:: * , traits:: { EnsureOrigin , Get } } ;
58
+ use super :: * ;
60
59
61
- /// The currency trait.
62
- type Currency : ReservableCurrency < Self :: AccountId > ;
60
+ #[ pallet:: config]
61
+ pub trait Config : frame_system:: Config {
62
+ /// The overarching event type.
63
+ type Event : From < Event < Self > > + IsType < <Self as frame_system:: Config >:: Event > ;
63
64
64
- /// Reservation fee .
65
- type ReservationFee : Get < BalanceOf < Self > > ;
65
+ /// The currency trait .
66
+ type Currency : ReservableCurrency < Self :: AccountId > ;
66
67
67
- /// What to do with slashed funds.
68
- type Slashed : OnUnbalanced < NegativeImbalanceOf < Self > > ;
68
+ /// Reservation fee.
69
+ #[ pallet:: constant]
70
+ type ReservationFee : Get < BalanceOf < Self > > ;
69
71
70
- /// The origin which may forcibly set or remove a name. Root can always do this .
71
- type ForceOrigin : EnsureOrigin < Self :: Origin > ;
72
+ /// What to do with slashed funds .
73
+ type Slashed : OnUnbalanced < NegativeImbalanceOf < Self > > ;
72
74
73
- /// The minimum length a name may be .
74
- type MinLength : Get < usize > ;
75
+ /// The origin which may forcibly set or remove a name. Root can always do this .
76
+ type ForceOrigin : EnsureOrigin < Self :: Origin > ;
75
77
76
- /// The maximum length a name may be.
77
- type MaxLength : Get < usize > ;
78
- }
78
+ /// The minimum length a name may be.
79
+ # [ pallet :: constant ]
80
+ type MinLength : Get < u32 > ;
79
81
80
- decl_storage ! {
81
- trait Store for Module <T : Config > as Nicks {
82
- /// The lookup table for names.
83
- NameOf : map hasher( twox_64_concat) T :: AccountId => Option <( Vec <u8 >, BalanceOf <T >) >;
82
+ /// The maximum length a name may be.
83
+ #[ pallet:: constant]
84
+ type MaxLength : Get < u32 > ;
84
85
}
85
- }
86
86
87
- decl_event ! (
88
- pub enum Event <T > where AccountId = <T as frame_system:: Config >:: AccountId , Balance = BalanceOf <T > {
87
+ #[ pallet:: event]
88
+ #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
89
+ #[ pallet:: metadata( T :: AccountId = "AccountId" , BalanceOf <T > = "Balance" ) ]
90
+ pub enum Event < T : Config > {
89
91
/// A name was set. \[who\]
90
- NameSet ( AccountId ) ,
92
+ NameSet ( T :: AccountId ) ,
91
93
/// A name was forcibly set. \[target\]
92
- NameForced ( AccountId ) ,
94
+ NameForced ( T :: AccountId ) ,
93
95
/// A name was changed. \[who\]
94
- NameChanged ( AccountId ) ,
96
+ NameChanged ( T :: AccountId ) ,
95
97
/// A name was cleared, and the given balance returned. \[who, deposit\]
96
- NameCleared ( AccountId , Balance ) ,
98
+ NameCleared ( T :: AccountId , BalanceOf < T > ) ,
97
99
/// A name was removed and the given balance slashed. \[target, deposit\]
98
- NameKilled ( AccountId , Balance ) ,
100
+ NameKilled ( T :: AccountId , BalanceOf < T > ) ,
99
101
}
100
- ) ;
101
102
102
- decl_error ! {
103
- /// Error for the nicks module.
104
- pub enum Error for Module < T : Config > {
103
+ /// Error for the nicks pallet.
104
+ # [ pallet :: error ]
105
+ pub enum Error < T > {
105
106
/// A name is too short.
106
107
TooShort ,
107
108
/// A name is too long.
108
109
TooLong ,
109
110
/// An account isn't named.
110
111
Unnamed ,
111
112
}
112
- }
113
-
114
- decl_module ! {
115
- /// Nicks module declaration.
116
- pub struct Module <T : Config > for enum Call where origin: T :: Origin {
117
- type Error = Error <T >;
118
-
119
- fn deposit_event( ) = default ;
120
113
121
- /// Reservation fee.
122
- const ReservationFee : BalanceOf <T > = T :: ReservationFee :: get( ) ;
114
+ /// The lookup table for names.
115
+ #[ pallet:: storage]
116
+ pub ( super ) type NameOf < T : Config > = StorageMap < _ , Twox64Concat , T :: AccountId , ( Vec < u8 > , BalanceOf < T > ) > ;
123
117
124
- /// The minimum length a name may be.
125
- const MinLength : u32 = T :: MinLength :: get( ) as u32 ;
118
+ #[ pallet:: pallet]
119
+ #[ pallet:: generate_store( pub ( super ) trait Store ) ]
120
+ pub struct Pallet < T > ( _ ) ;
126
121
127
- /// The maximum length a name may be.
128
- const MaxLength : u32 = T :: MaxLength :: get ( ) as u32 ;
122
+ # [ pallet :: hooks ]
123
+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
129
124
125
+ #[ pallet:: call]
126
+ impl < T : Config > Pallet < T > {
130
127
/// Set an account's name. The name should be a UTF-8-encoded string by convention, though
131
128
/// we don't check it.
132
129
///
@@ -143,24 +140,25 @@ decl_module! {
143
140
/// - One storage read/write.
144
141
/// - One event.
145
142
/// # </weight>
146
- #[ weight = 50_000_000 ]
147
- fn set_name( origin, name: Vec <u8 >) {
143
+ #[ pallet :: weight( 50_000_000 ) ]
144
+ pub ( super ) fn set_name ( origin : OriginFor < T > , name : Vec < u8 > ) -> DispatchResultWithPostInfo {
148
145
let sender = ensure_signed ( origin) ?;
149
146
150
- ensure!( name. len( ) >= T :: MinLength :: get( ) , Error :: <T >:: TooShort ) ;
151
- ensure!( name. len( ) <= T :: MaxLength :: get( ) , Error :: <T >:: TooLong ) ;
147
+ ensure ! ( name. len( ) >= T :: MinLength :: get( ) as usize , Error :: <T >:: TooShort ) ;
148
+ ensure ! ( name. len( ) <= T :: MaxLength :: get( ) as usize , Error :: <T >:: TooLong ) ;
152
149
153
150
let deposit = if let Some ( ( _, deposit) ) = <NameOf < T > >:: get ( & sender) {
154
- Self :: deposit_event( RawEvent :: NameChanged ( sender. clone( ) ) ) ;
151
+ Self :: deposit_event ( Event :: < T > :: NameChanged ( sender. clone ( ) ) ) ;
155
152
deposit
156
153
} else {
157
154
let deposit = T :: ReservationFee :: get ( ) ;
158
155
T :: Currency :: reserve ( & sender, deposit. clone ( ) ) ?;
159
- Self :: deposit_event( RawEvent :: NameSet ( sender. clone( ) ) ) ;
156
+ Self :: deposit_event ( Event :: < T > :: NameSet ( sender. clone ( ) ) ) ;
160
157
deposit
161
158
} ;
162
159
163
160
<NameOf < T > >:: insert ( & sender, ( name, deposit) ) ;
161
+ Ok ( ( ) . into ( ) )
164
162
}
165
163
166
164
/// Clear an account's name and return the deposit. Fails if the account was not named.
@@ -173,16 +171,17 @@ decl_module! {
173
171
/// - One storage read/write.
174
172
/// - One event.
175
173
/// # </weight>
176
- #[ weight = 70_000_000 ]
177
- fn clear_name( origin) {
174
+ #[ pallet :: weight( 70_000_000 ) ]
175
+ pub ( super ) fn clear_name ( origin : OriginFor < T > ) -> DispatchResultWithPostInfo {
178
176
let sender = ensure_signed ( origin) ?;
179
177
180
178
let deposit = <NameOf < T > >:: take ( & sender) . ok_or ( Error :: < T > :: Unnamed ) ?. 1 ;
181
179
182
180
let err_amount = T :: Currency :: unreserve ( & sender, deposit. clone ( ) ) ;
183
181
debug_assert ! ( err_amount. is_zero( ) ) ;
184
182
185
- Self :: deposit_event( RawEvent :: NameCleared ( sender, deposit) ) ;
183
+ Self :: deposit_event ( Event :: < T > :: NameCleared ( sender, deposit) ) ;
184
+ Ok ( ( ) . into ( ) )
186
185
}
187
186
188
187
/// Remove an account's name and take charge of the deposit.
@@ -198,8 +197,11 @@ decl_module! {
198
197
/// - One storage read/write.
199
198
/// - One event.
200
199
/// # </weight>
201
- #[ weight = 70_000_000 ]
202
- fn kill_name( origin, target: <T :: Lookup as StaticLookup >:: Source ) {
200
+ #[ pallet:: weight( 70_000_000 ) ]
201
+ pub ( super ) fn kill_name (
202
+ origin : OriginFor < T > ,
203
+ target : <T :: Lookup as StaticLookup >:: Source
204
+ ) -> DispatchResultWithPostInfo {
203
205
T :: ForceOrigin :: ensure_origin ( origin) ?;
204
206
205
207
// Figure out who we're meant to be clearing.
@@ -209,7 +211,8 @@ decl_module! {
209
211
// Slash their deposit from them.
210
212
T :: Slashed :: on_unbalanced ( T :: Currency :: slash_reserved ( & target, deposit. clone ( ) ) . 0 ) ;
211
213
212
- Self :: deposit_event( RawEvent :: NameKilled ( target, deposit) ) ;
214
+ Self :: deposit_event ( Event :: < T > :: NameKilled ( target, deposit) ) ;
215
+ Ok ( ( ) . into ( ) )
213
216
}
214
217
215
218
/// Set a third-party account's name with no deposit.
@@ -224,15 +227,20 @@ decl_module! {
224
227
/// - One storage read/write.
225
228
/// - One event.
226
229
/// # </weight>
227
- #[ weight = 70_000_000 ]
228
- fn force_name( origin, target: <T :: Lookup as StaticLookup >:: Source , name: Vec <u8 >) {
230
+ #[ pallet:: weight( 70_000_000 ) ]
231
+ pub ( super ) fn force_name (
232
+ origin : OriginFor < T > ,
233
+ target : <T :: Lookup as StaticLookup >:: Source ,
234
+ name : Vec < u8 >
235
+ ) -> DispatchResultWithPostInfo {
229
236
T :: ForceOrigin :: ensure_origin ( origin) ?;
230
237
231
238
let target = T :: Lookup :: lookup ( target) ?;
232
239
let deposit = <NameOf < T > >:: get ( & target) . map ( |x| x. 1 ) . unwrap_or_else ( Zero :: zero) ;
233
240
<NameOf < T > >:: insert ( & target, ( name, deposit) ) ;
234
241
235
- Self :: deposit_event( RawEvent :: NameForced ( target) ) ;
242
+ Self :: deposit_event ( Event :: < T > :: NameForced ( target) ) ;
243
+ Ok ( ( ) . into ( ) )
236
244
}
237
245
}
238
246
}
@@ -308,8 +316,8 @@ mod tests {
308
316
}
309
317
parameter_types ! {
310
318
pub const ReservationFee : u64 = 2 ;
311
- pub const MinLength : usize = 3 ;
312
- pub const MaxLength : usize = 16 ;
319
+ pub const MinLength : u32 = 3 ;
320
+ pub const MaxLength : u32 = 16 ;
313
321
}
314
322
ord_parameter_types ! {
315
323
pub const One : u64 = 1 ;
0 commit comments