|
2 | 2 |
|
3 | 3 | /// A FRAME pallet for demo benchmark |
4 | 4 |
|
5 | | -use frame_support::{ |
6 | | - decl_module, decl_storage, decl_event, decl_error, dispatch, |
7 | | - traits::{ |
8 | | - Get |
9 | | - }, |
10 | | -}; |
11 | | -use frame_system::{self as system, ensure_signed}; |
| 5 | +pub use pallet::*; |
12 | 6 |
|
13 | 7 | mod benchmarking; |
| 8 | +pub mod weights; |
14 | 9 |
|
15 | | -/// The pallet's configuration trait. |
16 | | -pub trait Trait: system::Trait { |
17 | | - // Add other types and constants required to configure this pallet. |
| 10 | +#[frame_support::pallet] |
| 11 | +pub mod pallet { |
| 12 | + use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*}; |
| 13 | + use frame_system::pallet_prelude::*; |
| 14 | + pub use crate::weights::WeightInfo; |
18 | 15 |
|
19 | | - /// The overarching event type. |
20 | | - type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>; |
21 | | -} |
| 16 | + /// Configure the pallet by specifying the parameters and types on which it depends. |
| 17 | + #[pallet::config] |
| 18 | + pub trait Config: frame_system::Config { |
| 19 | + /// Because this pallet emits events, it depends on the runtime's definition of an event. |
| 20 | + type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>; |
22 | 21 |
|
23 | | -// This pallet's storage items. |
24 | | -decl_storage! { |
25 | | - // It is important to update your storage name so that your pallet's |
26 | | - // storage items are isolated from other pallets. |
27 | | - // ---------------------------------vvvvvvvvvvvvvv |
28 | | - trait Store for Module<T: Trait> as TemplateModule { |
29 | | - // Just a dummy storage item. |
30 | | - // Here we are declaring a StorageValue, `Something` as a Option<u32> |
31 | | - // `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored |
32 | | - Something get(fn something): Option<u32>; |
| 22 | + /// Information on runtime weights. |
| 23 | + type WeightInfo: WeightInfo; |
33 | 24 | } |
34 | | -} |
35 | 25 |
|
36 | | -// The pallet's events |
37 | | -decl_event!( |
38 | | - pub enum Event<T> where AccountId = <T as system::Trait>::AccountId { |
39 | | - /// Just a dummy event. |
40 | | - /// Event `Something` is declared with a parameter of the type `u32` and `AccountId` |
41 | | - /// To emit this event, we call the deposit function, from our runtime functions |
42 | | - SomethingStored(u32, AccountId), |
| 26 | + #[pallet::pallet] |
| 27 | + #[pallet::generate_store(pub(super) trait Store)] |
| 28 | + pub struct Pallet<T>(_); |
| 29 | + |
| 30 | + // The pallet's runtime storage items. |
| 31 | + // https://substrate.dev/docs/en/knowledgebase/runtime/storage |
| 32 | + // Learn more about declaring storage items: |
| 33 | + // https://substrate.dev/docs/en/knowledgebase/runtime/storage#declaring-storage-items |
| 34 | + #[pallet::storage] |
| 35 | + #[pallet::getter(fn something)] |
| 36 | + pub type Something<T> = StorageValue<_, u32>; |
| 37 | + |
| 38 | + // Pallets use events to inform users when important changes are made. |
| 39 | + // https://substrate.dev/docs/en/knowledgebase/runtime/events |
| 40 | + #[pallet::event] |
| 41 | + #[pallet::metadata(T::AccountId = "AccountId")] |
| 42 | + #[pallet::generate_deposit(pub(super) fn deposit_event)] |
| 43 | + pub enum Event<T: Config> { |
| 44 | + /// Event documentation should end with an array that provides descriptive names for event |
| 45 | + /// parameters. [something, who] |
| 46 | + SomethingStored(u32, T::AccountId), |
43 | 47 | } |
44 | | -); |
45 | 48 |
|
46 | | -// The pallet's errors |
47 | | -decl_error! { |
48 | | - pub enum Error for Module<T: Trait> { |
49 | | - /// Value was None |
| 49 | + // Errors inform users that something went wrong. |
| 50 | + #[pallet::error] |
| 51 | + pub enum Error<T> { |
| 52 | + /// Error names should be descriptive. |
50 | 53 | NoneValue, |
51 | | - /// Value reached maximum and cannot be incremented further |
| 54 | + /// Errors should have helpful documentation associated with them. |
52 | 55 | StorageOverflow, |
53 | 56 | } |
54 | | -} |
55 | | - |
56 | | -// The pallet's dispatchable functions. |
57 | | -decl_module! { |
58 | | - /// The module declaration. |
59 | | - pub struct Module<T: Trait> for enum Call where origin: T::Origin { |
60 | | - // Initializing errors |
61 | | - // this includes information about your errors in the node's metadata. |
62 | | - // it is needed only if you are using errors in your pallet |
63 | | - type Error = Error<T>; |
64 | 57 |
|
65 | | - // Initializing events |
66 | | - // this is needed only if you are using events in your pallet |
67 | | - fn deposit_event() = default; |
| 58 | + #[pallet::hooks] |
| 59 | + impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {} |
68 | 60 |
|
69 | | - /// Just a dummy entry point. |
70 | | - /// function that can be called by the external world as an extrinsics call |
71 | | - /// takes a parameter of the type `AccountId`, stores it, and emits an event |
72 | | - /// # <weight> |
73 | | - /// - Base Weight: 44.66 µs |
74 | | - /// - DB Weight: 1 Write |
75 | | - /// # </weight> |
76 | | - #[weight = T::DbWeight::get().writes(1) + 44_660_000] |
77 | | - pub fn do_something(origin, something: u32) -> dispatch::DispatchResult { |
78 | | - // Check it was signed and get the signer. See also: ensure_root and ensure_none |
| 61 | + // Dispatchable functions allows users to interact with the pallet and invoke state changes. |
| 62 | + // These functions materialize as "extrinsics", which are often compared to transactions. |
| 63 | + // Dispatchable functions must be annotated with a weight and must return a DispatchResult. |
| 64 | + #[pallet::call] |
| 65 | + impl<T: Config> Pallet<T> { |
| 66 | + /// An example dispatchable that takes a single value as a parameter, writes the value to |
| 67 | + /// storage and emits an event. This function must be dispatched by a signed extrinsic. |
| 68 | + #[pallet::weight(T::WeightInfo::do_something(*something))] |
| 69 | + pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResultWithPostInfo { |
| 70 | + // Check that the extrinsic was signed and get the signer. |
| 71 | + // This function will return an error if the extrinsic is not signed. |
| 72 | + // https://substrate.dev/docs/en/knowledgebase/runtime/origin |
79 | 73 | let who = ensure_signed(origin)?; |
80 | 74 |
|
81 | | - // Code to execute when something calls this. |
82 | | - // For example: the following line stores the passed in u32 in the storage |
83 | | - Something::put(something); |
| 75 | + // Update storage. |
| 76 | + <Something<T>>::put(something); |
84 | 77 |
|
85 | | - // Here we are raising the Something event |
86 | | - Self::deposit_event(RawEvent::SomethingStored(something, who)); |
87 | | - Ok(()) |
| 78 | + // Emit an event. |
| 79 | + Self::deposit_event(Event::SomethingStored(something, who)); |
| 80 | + // Return a successful DispatchResultWithPostInfo |
| 81 | + Ok(().into()) |
88 | 82 | } |
89 | | - |
90 | 83 | } |
| 84 | + |
91 | 85 | } |
0 commit comments