1515// See the License for the specific language governing permissions and
1616// limitations under the License.
1717
18- //! # Nicks Module
18+ //! # Nicks Pallet
1919//!
2020//! - [`Config`]
2121//! - [`Call`]
2222//!
2323//! ## Overview
2424//!
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
2626//! 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
2828//! have not been designed to be economically secure. Do not use this pallet as-is in production.
2929//!
3030//! ## Interface
@@ -45,88 +45,85 @@ use sp_std::prelude::*;
4545use sp_runtime:: {
4646 traits:: { StaticLookup , Zero }
4747} ;
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:: * ;
5350
5451type BalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: Balance ;
5552type NegativeImbalanceOf < T > = <<T as Config >:: Currency as Currency < <T as frame_system:: Config >:: AccountId > >:: NegativeImbalance ;
5653
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 :: * ;
6059
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 > ;
6364
64- /// Reservation fee .
65- type ReservationFee : Get < BalanceOf < Self > > ;
65+ /// The currency trait .
66+ type Currency : ReservableCurrency < Self :: AccountId > ;
6667
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 > > ;
6971
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 > > ;
7274
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 > ;
7577
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 > ;
7981
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 > ;
8485 }
85- }
8686
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 > {
8991 /// A name was set. \[who\]
90- NameSet ( AccountId ) ,
92+ NameSet ( T :: AccountId ) ,
9193 /// A name was forcibly set. \[target\]
92- NameForced ( AccountId ) ,
94+ NameForced ( T :: AccountId ) ,
9395 /// A name was changed. \[who\]
94- NameChanged ( AccountId ) ,
96+ NameChanged ( T :: AccountId ) ,
9597 /// A name was cleared, and the given balance returned. \[who, deposit\]
96- NameCleared ( AccountId , Balance ) ,
98+ NameCleared ( T :: AccountId , BalanceOf < T > ) ,
9799 /// A name was removed and the given balance slashed. \[target, deposit\]
98- NameKilled ( AccountId , Balance ) ,
100+ NameKilled ( T :: AccountId , BalanceOf < T > ) ,
99101 }
100- ) ;
101102
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 > {
105106 /// A name is too short.
106107 TooShort ,
107108 /// A name is too long.
108109 TooLong ,
109110 /// An account isn't named.
110111 Unnamed ,
111112 }
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 ;
120113
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 > ) > ;
123117
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 > ( _ ) ;
126121
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 > { }
129124
125+ #[ pallet:: call]
126+ impl < T : Config > Pallet < T > {
130127 /// Set an account's name. The name should be a UTF-8-encoded string by convention, though
131128 /// we don't check it.
132129 ///
@@ -143,24 +140,25 @@ decl_module! {
143140 /// - One storage read/write.
144141 /// - One event.
145142 /// # </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 {
148145 let sender = ensure_signed ( origin) ?;
149146
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 ) ;
152149
153150 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 ( ) ) ) ;
155152 deposit
156153 } else {
157154 let deposit = T :: ReservationFee :: get ( ) ;
158155 T :: Currency :: reserve ( & sender, deposit. clone ( ) ) ?;
159- Self :: deposit_event( RawEvent :: NameSet ( sender. clone( ) ) ) ;
156+ Self :: deposit_event ( Event :: < T > :: NameSet ( sender. clone ( ) ) ) ;
160157 deposit
161158 } ;
162159
163160 <NameOf < T > >:: insert ( & sender, ( name, deposit) ) ;
161+ Ok ( ( ) . into ( ) )
164162 }
165163
166164 /// Clear an account's name and return the deposit. Fails if the account was not named.
@@ -173,16 +171,17 @@ decl_module! {
173171 /// - One storage read/write.
174172 /// - One event.
175173 /// # </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 {
178176 let sender = ensure_signed ( origin) ?;
179177
180178 let deposit = <NameOf < T > >:: take ( & sender) . ok_or ( Error :: < T > :: Unnamed ) ?. 1 ;
181179
182180 let err_amount = T :: Currency :: unreserve ( & sender, deposit. clone ( ) ) ;
183181 debug_assert ! ( err_amount. is_zero( ) ) ;
184182
185- Self :: deposit_event( RawEvent :: NameCleared ( sender, deposit) ) ;
183+ Self :: deposit_event ( Event :: < T > :: NameCleared ( sender, deposit) ) ;
184+ Ok ( ( ) . into ( ) )
186185 }
187186
188187 /// Remove an account's name and take charge of the deposit.
@@ -198,8 +197,11 @@ decl_module! {
198197 /// - One storage read/write.
199198 /// - One event.
200199 /// # </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 {
203205 T :: ForceOrigin :: ensure_origin ( origin) ?;
204206
205207 // Figure out who we're meant to be clearing.
@@ -209,7 +211,8 @@ decl_module! {
209211 // Slash their deposit from them.
210212 T :: Slashed :: on_unbalanced ( T :: Currency :: slash_reserved ( & target, deposit. clone ( ) ) . 0 ) ;
211213
212- Self :: deposit_event( RawEvent :: NameKilled ( target, deposit) ) ;
214+ Self :: deposit_event ( Event :: < T > :: NameKilled ( target, deposit) ) ;
215+ Ok ( ( ) . into ( ) )
213216 }
214217
215218 /// Set a third-party account's name with no deposit.
@@ -224,15 +227,20 @@ decl_module! {
224227 /// - One storage read/write.
225228 /// - One event.
226229 /// # </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 {
229236 T :: ForceOrigin :: ensure_origin ( origin) ?;
230237
231238 let target = T :: Lookup :: lookup ( target) ?;
232239 let deposit = <NameOf < T > >:: get ( & target) . map ( |x| x. 1 ) . unwrap_or_else ( Zero :: zero) ;
233240 <NameOf < T > >:: insert ( & target, ( name, deposit) ) ;
234241
235- Self :: deposit_event( RawEvent :: NameForced ( target) ) ;
242+ Self :: deposit_event ( Event :: < T > :: NameForced ( target) ) ;
243+ Ok ( ( ) . into ( ) )
236244 }
237245 }
238246}
@@ -308,8 +316,8 @@ mod tests {
308316 }
309317 parameter_types ! {
310318 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 ;
313321 }
314322 ord_parameter_types ! {
315323 pub const One : u64 = 1 ;
0 commit comments