1515// See the License for the specific language governing permissions and
1616// limitations under the License.
1717
18- //! # Utility Module
19- //! A stateless module with helpers for dispatch management which does no re-authentication.
18+ //! # Utility Pallet
19+ //! A stateless pallet with helpers for dispatch management which does no re-authentication.
2020//!
2121//! - [`Config`]
2222//! - [`Call`]
2323//!
2424//! ## Overview
2525//!
26- //! This module contains two basic pieces of functionality:
26+ //! This pallet contains two basic pieces of functionality:
2727//! - Batch dispatch: A stateless operation, allowing any origin to execute multiple calls in a
2828//! single dispatch. This can be useful to amalgamate proposals, combining `set_code` with
2929//! corresponding `set_storage`s, for efficient multiple payouts with just a single signature
3434//! need multiple distinct accounts (e.g. as controllers for many staking accounts), but where
3535//! it's perfectly fine to have each of them controlled by the same underlying keypair.
3636//! Derivative accounts are, for the purposes of proxy filtering considered exactly the same as
37- //! the oigin and are thus hampered with the origin's filters.
37+ //! the origin and are thus hampered with the origin's filters.
3838//!
39- //! Since proxy filters are respected in all dispatches of this module , it should never need to be
39+ //! Since proxy filters are respected in all dispatches of this pallet , it should never need to be
4040//! filtered by any proxy.
4141//!
4242//! ## Interface
@@ -60,58 +60,58 @@ use sp_std::prelude::*;
6060use codec:: { Encode , Decode } ;
6161use sp_core:: TypeId ;
6262use sp_io:: hashing:: blake2_256;
63- use frame_support:: { decl_module, decl_event, decl_storage, Parameter , transactional} ;
6463use frame_support:: {
65- traits:: { OriginTrait , UnfilteredDispatchable , Get } ,
66- weights:: { Weight , GetDispatchInfo , DispatchClass , extract_actual_weight} ,
67- dispatch:: { PostDispatchInfo , DispatchResultWithPostInfo } ,
64+ transactional,
65+ traits:: { OriginTrait , UnfilteredDispatchable } ,
66+ weights:: { GetDispatchInfo , extract_actual_weight} ,
67+ dispatch:: PostDispatchInfo ,
6868} ;
69- use frame_system:: { ensure_signed, ensure_root} ;
70- use sp_runtime:: { DispatchError , traits:: Dispatchable } ;
69+ use sp_runtime:: traits:: Dispatchable ;
7170pub use weights:: WeightInfo ;
7271
73- /// Configuration trait.
74- pub trait Config : frame_system:: Config {
75- /// The overarching event type.
76- type Event : From < Event > + Into < <Self as frame_system:: Config >:: Event > ;
72+ pub use pallet:: * ;
7773
78- /// The overarching call type.
79- type Call : Parameter + Dispatchable < Origin =Self :: Origin , PostInfo =PostDispatchInfo >
80- + GetDispatchInfo + From < frame_system:: Call < Self > >
81- + UnfilteredDispatchable < Origin =Self :: Origin > ;
74+ #[ frame_support:: pallet]
75+ pub mod pallet {
76+ use frame_support:: pallet_prelude:: * ;
77+ use frame_system:: pallet_prelude:: * ;
78+ use super :: * ;
8279
83- /// Weight information for extrinsics in this pallet.
84- type WeightInfo : WeightInfo ;
85- }
80+ # [ pallet:: pallet ]
81+ # [ pallet :: generate_store ( pub ( super ) trait Store ) ]
82+ pub struct Pallet < T > ( _ ) ;
8683
87- decl_storage ! {
88- trait Store for Module <T : Config > as Utility { }
89- }
9084
91- decl_event ! {
92- /// Events type.
85+ /// Configuration trait.
86+ #[ pallet:: config]
87+ pub trait Config : frame_system:: Config {
88+ /// The overarching event type.
89+ type Event : From < Event > + IsType < <Self as frame_system:: Config >:: Event > ;
90+
91+ /// The overarching call type.
92+ type Call : Parameter + Dispatchable < Origin =Self :: Origin , PostInfo =PostDispatchInfo >
93+ + GetDispatchInfo + From < frame_system:: Call < Self > >
94+ + UnfilteredDispatchable < Origin =Self :: Origin > ;
95+
96+ /// Weight information for extrinsics in this pallet.
97+ type WeightInfo : WeightInfo ;
98+ }
99+
100+ #[ pallet:: event]
101+ #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
93102 pub enum Event {
94103 /// Batch of dispatches did not complete fully. Index of first failing dispatch given, as
95104 /// well as the error. \[index, error\]
96105 BatchInterrupted ( u32 , DispatchError ) ,
97106 /// Batch of dispatches completed fully with no error.
98107 BatchCompleted ,
99108 }
100- }
101-
102- /// A module identifier. These are per module and should be stored in a registry somewhere.
103- #[ derive( Clone , Copy , Eq , PartialEq , Encode , Decode ) ]
104- struct IndexedUtilityModuleId ( u16 ) ;
105109
106- impl TypeId for IndexedUtilityModuleId {
107- const TYPE_ID : [ u8 ; 4 ] = * b"suba" ;
108- }
109-
110- decl_module ! {
111- pub struct Module <T : Config > for enum Call where origin: T :: Origin {
112- /// Deposit one of this module's events by using the default implementation.
113- fn deposit_event( ) = default ;
110+ #[ pallet:: hooks]
111+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
114112
113+ #[ pallet:: call]
114+ impl < T : Config > Pallet < T > {
115115 /// Send a batch of dispatch calls.
116116 ///
117117 /// May be called from any origin.
@@ -130,7 +130,7 @@ decl_module! {
130130 /// `BatchInterrupted` event is deposited, along with the number of successful calls made
131131 /// and the error of the failed call. If all were successful, then the `BatchCompleted`
132132 /// event is deposited.
133- #[ weight = {
133+ #[ pallet :: weight( {
134134 let dispatch_infos = calls. iter( ) . map( |call| call. get_dispatch_info( ) ) . collect:: <Vec <_>>( ) ;
135135 let dispatch_weight = dispatch_infos. iter( )
136136 . map( |di| di. weight)
@@ -147,8 +147,11 @@ decl_module! {
147147 }
148148 } ;
149149 ( dispatch_weight, dispatch_class)
150- } ]
151- fn batch( origin, calls: Vec <<T as Config >:: Call >) -> DispatchResultWithPostInfo {
150+ } ) ]
151+ pub fn batch (
152+ origin : OriginFor < T > ,
153+ calls : Vec < <T as Config >:: Call > ,
154+ ) -> DispatchResultWithPostInfo {
152155 let is_root = ensure_root ( origin. clone ( ) ) . is_ok ( ) ;
153156 let calls_len = calls. len ( ) ;
154157 // Track the actual weight of each of the batch calls.
@@ -189,7 +192,7 @@ decl_module! {
189192 /// NOTE: Prior to version *12, this was called `as_limited_sub`.
190193 ///
191194 /// The dispatch origin for this call must be _Signed_.
192- #[ weight = {
195+ #[ pallet :: weight( {
193196 let dispatch_info = call. get_dispatch_info( ) ;
194197 (
195198 T :: WeightInfo :: as_derivative( )
@@ -198,8 +201,12 @@ decl_module! {
198201 . saturating_add( T :: DbWeight :: get( ) . reads_writes( 1 , 1 ) ) ,
199202 dispatch_info. class,
200203 )
201- } ]
202- fn as_derivative( origin, index: u16 , call: Box <<T as Config >:: Call >) -> DispatchResultWithPostInfo {
204+ } ) ]
205+ pub fn as_derivative (
206+ origin : OriginFor < T > ,
207+ index : u16 ,
208+ call : Box < <T as Config >:: Call > ,
209+ ) -> DispatchResultWithPostInfo {
203210 let mut origin = origin;
204211 let who = ensure_signed ( origin. clone ( ) ) ?;
205212 let pseudonym = Self :: derivative_account_id ( who, index) ;
@@ -229,7 +236,7 @@ decl_module! {
229236 /// # <weight>
230237 /// - Complexity: O(C) where C is the number of calls to be batched.
231238 /// # </weight>
232- #[ weight = {
239+ #[ pallet :: weight( {
233240 let dispatch_infos = calls. iter( ) . map( |call| call. get_dispatch_info( ) ) . collect:: <Vec <_>>( ) ;
234241 let dispatch_weight = dispatch_infos. iter( )
235242 . map( |di| di. weight)
@@ -246,9 +253,12 @@ decl_module! {
246253 }
247254 } ;
248255 ( dispatch_weight, dispatch_class)
249- } ]
256+ } ) ]
250257 #[ transactional]
251- fn batch_all( origin, calls: Vec <<T as Config >:: Call >) -> DispatchResultWithPostInfo {
258+ pub fn batch_all (
259+ origin : OriginFor < T > ,
260+ calls : Vec < <T as Config >:: Call > ,
261+ ) -> DispatchResultWithPostInfo {
252262 let is_root = ensure_root ( origin. clone ( ) ) . is_ok ( ) ;
253263 let calls_len = calls. len ( ) ;
254264 // Track the actual weight of each of the batch calls.
@@ -276,9 +286,18 @@ decl_module! {
276286 Ok ( Some ( base_weight + weight) . into ( ) )
277287 }
278288 }
289+
290+ }
291+
292+ /// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
293+ #[ derive( Clone , Copy , Eq , PartialEq , Encode , Decode ) ]
294+ struct IndexedUtilityPalletId ( u16 ) ;
295+
296+ impl TypeId for IndexedUtilityPalletId {
297+ const TYPE_ID : [ u8 ; 4 ] = * b"suba" ;
279298}
280299
281- impl < T : Config > Module < T > {
300+ impl < T : Config > Pallet < T > {
282301 /// Derive a derivative account ID from the owner account and the sub-account index.
283302 pub fn derivative_account_id ( who : T :: AccountId , index : u16 ) -> T :: AccountId {
284303 let entropy = ( b"modlpy/utilisuba" , who, index) . using_encoded ( blake2_256) ;
0 commit comments