pallet_parachain_template/lib.rs
1//! # Template Pallet
2//!
3//! A pallet with minimal functionality to help developers understand the essential components of
4//! writing a FRAME pallet. It is typically used in beginner tutorials or in Polkadot SDK template
5//! as a starting point for creating a new pallet and **not meant to be used in production**.
6//!
7//! ## Overview
8//!
9//! This template pallet contains basic examples of:
10//! - declaring a storage item that stores a single block-number
11//! - declaring and using events
12//! - declaring and using errors
13//! - a dispatchable function that allows a user to set a new value to storage and emits an event
14//! upon success
15//! - another dispatchable function that causes a custom error to be thrown
16//!
17//! Each pallet section is annotated with an attribute using the `#[pallet::...]` procedural macro.
18//! This macro generates the necessary code for a pallet to be aggregated into a FRAME runtime.
19//!
20//! To get started with pallet development, consider using this tutorial:
21//!
22//! <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html>
23//!
24//! And reading the main documentation of the `frame` crate:
25//!
26//! <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/polkadot_sdk/frame_runtime/index.html>
27//!
28//! And looking at the frame [`kitchen-sink`](https://paritytech.github.io/polkadot-sdk/master/pallet_example_kitchensink/index.html)
29//! pallet, a showcase of all pallet macros.
30//!
31//! ### Pallet Sections
32//!
33//! The pallet sections in this template are:
34//!
35//! - A **configuration trait** that defines the types and parameters which the pallet depends on
36//! (denoted by the `#[pallet::config]` attribute). See: [`Config`].
37//! - A **means to store pallet-specific data** (denoted by the `#[pallet::storage]` attribute).
38//! See: [`storage_types`].
39//! - A **declaration of the events** this pallet emits (denoted by the `#[pallet::event]`
40//! attribute). See: [`Event`].
41//! - A **declaration of the errors** that this pallet can throw (denoted by the `#[pallet::error]`
42//! attribute). See: [`Error`].
43//! - A **set of dispatchable functions** that define the pallet's functionality (denoted by the
44//! `#[pallet::call]` attribute). See: [`dispatchables`].
45//!
46//! Run `cargo doc --package pallet-template --open` to view this pallet's documentation.
47
48#![cfg_attr(not(feature = "std"), no_std)]
49
50pub use pallet::*;
51
52#[cfg(test)]
53mod mock;
54
55#[cfg(test)]
56mod tests;
57
58pub mod weights;
59
60#[cfg(feature = "runtime-benchmarks")]
61mod benchmarking;
62
63// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/polkadot_sdk/frame_runtime/index.html>
64// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html>
65//
66// To see a full list of `pallet` macros and their use cases, see:
67// <https://paritytech.github.io/polkadot-sdk/master/pallet_example_kitchensink/index.html>
68// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/index.html>
69#[frame::pallet]
70pub 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 #[allow(deprecated)]
77 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
78
79 /// A type representing the weights required by the dispatchables of this pallet.
80 type WeightInfo: crate::weights::WeightInfo;
81 }
82
83 #[pallet::pallet]
84 pub struct Pallet<T>(_);
85
86 /// A struct to store a single block-number. Has all the right derives to store it in storage.
87 /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_storage_derives/index.html>
88 #[derive(
89 Encode, Decode, MaxEncodedLen, TypeInfo, CloneNoBound, PartialEqNoBound, DefaultNoBound,
90 )]
91 #[scale_info(skip_type_params(T))]
92 pub struct CompositeStruct<T: Config> {
93 /// A block number.
94 pub(crate) block_number: BlockNumberFor<T>,
95 }
96
97 /// The pallet's storage items.
98 /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#storage>
99 /// <https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.storage.html>
100 #[pallet::storage]
101 pub type Something<T: Config> = StorageValue<_, CompositeStruct<T>>;
102
103 /// Pallets use events to inform users when important changes are made.
104 /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
105 #[pallet::event]
106 #[pallet::generate_deposit(pub(super) fn deposit_event)]
107 pub enum Event<T: Config> {
108 /// We usually use passive tense for events.
109 SomethingStored { block_number: BlockNumberFor<T>, who: T::AccountId },
110 }
111
112 /// Errors inform users that something went wrong.
113 /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#event-and-error>
114 #[pallet::error]
115 pub enum Error<T> {
116 /// Error names should be descriptive.
117 NoneValue,
118 /// Errors should have helpful documentation associated with them.
119 StorageOverflow,
120 }
121
122 #[pallet::hooks]
123 impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
124
125 /// Dispatchable functions allows users to interact with the pallet and invoke state changes.
126 /// These functions materialize as "extrinsics", which are often compared to transactions.
127 /// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
128 /// <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#dispatchables>
129 #[pallet::call]
130 impl<T: Config> Pallet<T> {
131 /// An example dispatchable that takes a singles value as a parameter, writes the value to
132 /// storage and emits an event. This function must be dispatched by a signed extrinsic.
133 #[pallet::call_index(0)]
134 #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().writes(1))]
135 pub fn do_something(origin: OriginFor<T>, bn: u32) -> DispatchResultWithPostInfo {
136 // Check that the extrinsic was signed and get the signer.
137 // This function will return an error if the extrinsic is not signed.
138 // <https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/reference_docs/frame_origin/index.html>
139 let who = ensure_signed(origin)?;
140
141 // Convert the u32 into a block number. This is possible because the set of trait bounds
142 // defined in [`frame_system::Config::BlockNumber`].
143 let block_number: BlockNumberFor<T> = bn.into();
144
145 // Update storage.
146 <Something<T>>::put(CompositeStruct { block_number });
147
148 // Emit an event.
149 Self::deposit_event(Event::SomethingStored { block_number, who });
150
151 // Return a successful [`DispatchResultWithPostInfo`] or [`DispatchResult`].
152 Ok(().into())
153 }
154
155 /// An example dispatchable that may throw a custom error.
156 #[pallet::call_index(1)]
157 #[pallet::weight(Weight::from_parts(10_000, 0) + T::DbWeight::get().reads_writes(1,1))]
158 pub fn cause_error(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
159 let _who = ensure_signed(origin)?;
160
161 // Read a value from storage.
162 match <Something<T>>::get() {
163 // Return an error if the value has not been set.
164 None => Err(Error::<T>::NoneValue)?,
165 Some(mut old) => {
166 // Increment the value read from storage; will error in the event of overflow.
167 old.block_number = old
168 .block_number
169 .checked_add(&One::one())
170 // ^^ equivalent is to:
171 // .checked_add(&1u32.into())
172 // both of which build a `One` instance for the type `BlockNumber`.
173 .ok_or(Error::<T>::StorageOverflow)?;
174 // Update the value in storage with the incremented result.
175 <Something<T>>::put(old);
176 // Explore how you can rewrite this using
177 // [`frame_support::storage::StorageValue::mutate`].
178 Ok(().into())
179 },
180 }
181 }
182 }
183}