@@ -68,115 +68,118 @@ mod benchmarking;
6868// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/index.html>
6969#[ frame:: pallet]
7070pub mod pallet {
71- use frame:: prelude:: * ;
72-
73- /// Configure the pallet by specifying the parameters and types on which it depends.
74- #[ pallet:: config]
75- pub trait Config : frame_system:: Config {
76- type RuntimeEvent : From < Event < Self > > + IsType < <Self as frame_system:: Config >:: RuntimeEvent > ;
77-
78- /// A type representing the weights required by the dispatchables of this pallet.
79- type WeightInfo : crate :: weights:: WeightInfo ;
80- }
81-
82- #[ pallet:: pallet]
83- pub struct Pallet < T > ( _ ) ;
84-
85- /// A struct to store a single block-number. Has all the right derives to store it in storage.
86- /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_storage_derives/index.html>
87- #[ derive(
88- Encode , Decode , MaxEncodedLen , TypeInfo , CloneNoBound , PartialEqNoBound , DefaultNoBound ,
89- ) ]
90- #[ scale_info( skip_type_params( T ) ) ]
91- pub struct CompositeStruct < T : Config > {
92- /// A block number.
93- pub ( crate ) block_number : BlockNumberFor < T > ,
94- }
95-
96- /// The pallet's storage items.
97- /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#storage>
98- /// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.storage.html>
99- #[ pallet:: storage]
100- pub type Something < T : Config > = StorageValue < _ , CompositeStruct < T > > ;
101-
102- /// Pallets use events to inform users when important changes are made.
103- /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
104- #[ pallet:: event]
105- #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
106- pub enum Event < T : Config > {
107- /// We usually use passive tense for events.
108- SomethingStored { block_number : BlockNumberFor < T > , who : T :: AccountId } ,
109- }
110-
111- /// Errors inform users that something went wrong.
112- /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
113- #[ pallet:: error]
114- pub enum Error < T > {
115- /// Error names should be descriptive.
116- NoneValue ,
117- /// Errors should have helpful documentation associated with them.
118- StorageOverflow ,
119- }
120-
121- #[ pallet:: hooks]
122- impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
123-
124- /// Dispatchable functions allows users to interact with the pallet and invoke state changes.
125- /// These functions materialize as "extrinsics", which are often compared to transactions.
126- /// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
127- /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#dispatchables>
128- #[ pallet:: call]
129- impl < T : Config > Pallet < T > {
130- /// An example dispatchable that takes a singles value as a parameter, writes the value to
131- /// storage and emits an event. This function must be dispatched by a signed extrinsic.
132- #[ pallet:: call_index( 0 ) ]
133- #[ pallet:: weight( Weight :: from_parts( 10_000 , 0 ) + T :: DbWeight :: get( ) . writes( 1 ) ) ]
134- pub fn do_something ( origin : OriginFor < T > , bn : u32 ) -> DispatchResultWithPostInfo {
135- // Check that the extrinsic was signed and get the signer.
136- // This function will return an error if the extrinsic is not signed.
137- // <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html>
138- let who = ensure_signed ( origin) ?;
139-
140- // Convert the u32 into a block number. This is possible because the set of trait bounds
141- // defined in [`frame_system::Config::BlockNumber`].
142- let block_number: BlockNumberFor < T > = bn. into ( ) ;
143-
144- // Update storage.
145- <Something < T > >:: put ( CompositeStruct { block_number } ) ;
146-
147- // Emit an event.
148- Self :: deposit_event ( Event :: SomethingStored { block_number, who } ) ;
149-
150- // Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`].
151- Ok ( ( ) . into ( ) )
152- }
153-
154- /// An example dispatchable that may throw a custom error.
155- #[ pallet:: call_index( 1 ) ]
156- #[ pallet:: weight( Weight :: from_parts( 10_000 , 0 ) + T :: DbWeight :: get( ) . reads_writes( 1 , 1 ) ) ]
157- pub fn cause_error ( origin : OriginFor < T > ) -> DispatchResultWithPostInfo {
158- let _who = ensure_signed ( origin) ?;
159-
160- // Read a value from storage.
161- match <Something < T > >:: get ( ) {
162- // Return an error if the value has not been set.
163- None => Err ( Error :: < T > :: NoneValue ) ?,
164- Some ( mut old) => {
165- // Increment the value read from storage; will error in the event of overflow.
166- old. block_number = old
167- . block_number
168- . checked_add ( & One :: one ( ) )
169- // ^^ equivalent is to:
170- // .checked_add(&1u32.into())
171- // both of which build a `One` instance for the type `BlockNumber`.
172- . ok_or ( Error :: < T > :: StorageOverflow ) ?;
173- // Update the value in storage with the incremented result.
174- <Something < T > >:: put ( old) ;
175- // Explore how you can rewrite this using
176- // [`frame_support::storage::StorageValue::mutate`].
177- Ok ( ( ) . into ( ) )
178- } ,
179- }
180- }
181- }
71+ use frame:: prelude:: * ;
72+
73+ /// Configure the pallet by specifying the parameters and types on which it depends.
74+ #[ pallet:: config]
75+ pub trait Config : frame_system:: Config {
76+ type RuntimeEvent : From < Event < Self > > + IsType < <Self as frame_system:: Config >:: RuntimeEvent > ;
77+
78+ /// A type representing the weights required by the dispatchables of this pallet.
79+ type WeightInfo : crate :: weights:: WeightInfo ;
80+ }
81+
82+ #[ pallet:: pallet]
83+ pub struct Pallet < T > ( _ ) ;
84+
85+ /// A struct to store a single block-number. Has all the right derives to store it in storage.
86+ /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_storage_derives/index.html>
87+ #[ derive(
88+ Encode , Decode , MaxEncodedLen , TypeInfo , CloneNoBound , PartialEqNoBound , DefaultNoBound ,
89+ ) ]
90+ #[ scale_info( skip_type_params( T ) ) ]
91+ pub struct CompositeStruct < T : Config > {
92+ /// A block number.
93+ pub ( crate ) block_number : BlockNumberFor < T > ,
94+ }
95+
96+ /// The pallet's storage items.
97+ /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#storage>
98+ /// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.storage.html>
99+ #[ pallet:: storage]
100+ pub type Something < T : Config > = StorageValue < _ , CompositeStruct < T > > ;
101+
102+ /// Pallets use events to inform users when important changes are made.
103+ /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
104+ #[ pallet:: event]
105+ #[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
106+ pub enum Event < T : Config > {
107+ /// We usually use passive tense for events.
108+ SomethingStored {
109+ block_number : BlockNumberFor < T > ,
110+ who : T :: AccountId ,
111+ } ,
112+ }
113+
114+ /// Errors inform users that something went wrong.
115+ /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
116+ #[ pallet:: error]
117+ pub enum Error < T > {
118+ /// Error names should be descriptive.
119+ NoneValue ,
120+ /// Errors should have helpful documentation associated with them.
121+ StorageOverflow ,
122+ }
123+
124+ #[ pallet:: hooks]
125+ impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > { }
126+
127+ /// Dispatchable functions allows users to interact with the pallet and invoke state changes.
128+ /// These functions materialize as "extrinsics", which are often compared to transactions.
129+ /// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
130+ /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#dispatchables>
131+ #[ pallet:: call]
132+ impl < T : Config > Pallet < T > {
133+ /// An example dispatchable that takes a singles value as a parameter, writes the value to
134+ /// storage and emits an event. This function must be dispatched by a signed extrinsic.
135+ #[ pallet:: call_index( 0 ) ]
136+ #[ pallet:: weight( Weight :: from_parts( 10_000 , 0 ) + T :: DbWeight :: get( ) . writes( 1 ) ) ]
137+ pub fn do_something ( origin : OriginFor < T > , bn : u32 ) -> DispatchResultWithPostInfo {
138+ // Check that the extrinsic was signed and get the signer.
139+ // This function will return an error if the extrinsic is not signed.
140+ // <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html>
141+ let who = ensure_signed ( origin) ?;
142+
143+ // Convert the u32 into a block number. This is possible because the set of trait bounds
144+ // defined in [`frame_system::Config::BlockNumber`].
145+ let block_number: BlockNumberFor < T > = bn. into ( ) ;
146+
147+ // Update storage.
148+ <Something < T > >:: put ( CompositeStruct { block_number } ) ;
149+
150+ // Emit an event.
151+ Self :: deposit_event ( Event :: SomethingStored { block_number, who } ) ;
152+
153+ // Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`].
154+ Ok ( ( ) . into ( ) )
155+ }
156+
157+ /// An example dispatchable that may throw a custom error.
158+ #[ pallet:: call_index( 1 ) ]
159+ #[ pallet:: weight( Weight :: from_parts( 10_000 , 0 ) + T :: DbWeight :: get( ) . reads_writes( 1 , 1 ) ) ]
160+ pub fn cause_error ( origin : OriginFor < T > ) -> DispatchResultWithPostInfo {
161+ let _who = ensure_signed ( origin) ?;
162+
163+ // Read a value from storage.
164+ match <Something < T > >:: get ( ) {
165+ // Return an error if the value has not been set.
166+ None => Err ( Error :: < T > :: NoneValue ) ?,
167+ Some ( mut old) => {
168+ // Increment the value read from storage; will error in the event of overflow.
169+ old. block_number = old
170+ . block_number
171+ . checked_add ( & One :: one ( ) )
172+ // ^^ equivalent is to:
173+ // .checked_add(&1u32.into())
174+ // both of which build a `One` instance for the type `BlockNumber`.
175+ . ok_or ( Error :: < T > :: StorageOverflow ) ?;
176+ // Update the value in storage with the incremented result.
177+ <Something < T > >:: put ( old) ;
178+ // Explore how you can rewrite this using
179+ // [`frame_support::storage::StorageValue::mutate`].
180+ Ok ( ( ) . into ( ) )
181+ }
182+ }
183+ }
184+ }
182185}
0 commit comments