referrerpolicy=no-referrer-when-downgrade

pallet_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 Substrate template
5//! nodes 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 `u32` value
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//! Learn more about FRAME macros [here](https://docs.substrate.io/reference/frame-macros/).
21//!
22//! ### Pallet Sections
23//!
24//! The pallet sections in this template are:
25//!
26//! - A **configuration trait** that defines the types and parameters which the pallet depends on
27//!   (denoted by the `#[pallet::config]` attribute). See: [`Config`].
28//! - A **means to store pallet-specific data** (denoted by the `#[pallet::storage]` attribute).
29//!   See: [`storage_types`].
30//! - A **declaration of the events** this pallet emits (denoted by the `#[pallet::event]`
31//!   attribute). See: [`Event`].
32//! - A **declaration of the errors** that this pallet can throw (denoted by the `#[pallet::error]`
33//!   attribute). See: [`Error`].
34//! - A **set of dispatchable functions** that define the pallet's functionality (denoted by the
35//!   `#[pallet::call]` attribute). See: [`dispatchables`].
36//!
37//! Run `cargo doc --package pallet-template --open` to view this pallet's documentation.
38
39// We make sure this pallet uses `no_std` for compiling to Wasm.
40#![cfg_attr(not(feature = "std"), no_std)]
41
42// Re-export pallet items so that they can be accessed from the crate namespace.
43pub use pallet::*;
44
45// FRAME pallets require their own "mock runtimes" to be able to run unit tests. This module
46// contains a mock runtime specific for testing this pallet's functionality.
47#[cfg(test)]
48mod mock;
49
50// This module contains the unit tests for this pallet.
51// Learn about pallet unit testing here: https://docs.substrate.io/test/unit-testing/
52#[cfg(test)]
53mod tests;
54
55// Every callable function or "dispatchable" a pallet exposes must have weight values that correctly
56// estimate a dispatchable's execution time. The benchmarking module is used to calculate weights
57// for each dispatchable and generates this pallet's weight.rs file. Learn more about benchmarking here: https://docs.substrate.io/test/benchmark/
58#[cfg(feature = "runtime-benchmarks")]
59mod benchmarking;
60pub mod weights;
61pub use weights::*;
62
63// All pallet logic is defined in its own module and must be annotated by the `pallet` attribute.
64#[frame_support::pallet]
65pub mod pallet {
66	// Import various useful types required by all FRAME pallets.
67	use super::*;
68	use frame_support::pallet_prelude::*;
69	use frame_system::pallet_prelude::*;
70
71	// The `Pallet` struct serves as a placeholder to implement traits, methods and dispatchables
72	// (`Call`s) in this pallet.
73	#[pallet::pallet]
74	pub struct Pallet<T>(_);
75
76	/// The pallet's configuration trait.
77	///
78	/// All our types and constants a pallet depends on must be declared here.
79	/// These types are defined generically and made concrete when the pallet is declared in the
80	/// `runtime/src/lib.rs` file of your chain.
81	#[pallet::config]
82	pub trait Config: frame_system::Config {
83		/// The overarching runtime event type.
84		#[allow(deprecated)]
85		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
86		/// A type representing the weights required by the dispatchables of this pallet.
87		type WeightInfo: WeightInfo;
88	}
89
90	/// A storage item for this pallet.
91	///
92	/// In this template, we are declaring a storage item called `Something` that stores a single
93	/// `u32` value. Learn more about runtime storage here: <https://docs.substrate.io/build/runtime-storage/>
94	#[pallet::storage]
95	pub type Something<T> = StorageValue<_, u32>;
96
97	/// Events that functions in this pallet can emit.
98	///
99	/// Events are a simple means of indicating to the outside world (such as dApps, chain explorers
100	/// or other users) that some notable update in the runtime has occurred. In a FRAME pallet, the
101	/// documentation for each event field and its parameters is added to a node's metadata so it
102	/// can be used by external interfaces or tools.
103	///
104	///	The `generate_deposit` macro generates a function on `Pallet` called `deposit_event` which
105	/// will convert the event type of your pallet into `RuntimeEvent` (declared in the pallet's
106	/// [`Config`] trait) and deposit it using [`frame_system::Pallet::deposit_event`].
107	#[pallet::event]
108	#[pallet::generate_deposit(pub(super) fn deposit_event)]
109	pub enum Event<T: Config> {
110		/// A user has successfully set a new value.
111		SomethingStored {
112			/// The new value set.
113			something: u32,
114			/// The account who set the new value.
115			who: T::AccountId,
116		},
117	}
118
119	/// Errors that can be returned by this pallet.
120	///
121	/// Errors tell users that something went wrong so it's important that their naming is
122	/// informative. Similar to events, error documentation is added to a node's metadata so it's
123	/// equally important that they have helpful documentation associated with them.
124	///
125	/// This type of runtime error can be up to 4 bytes in size should you want to return additional
126	/// information.
127	#[pallet::error]
128	pub enum Error<T> {
129		/// The value retrieved was `None` as no value was previously set.
130		NoneValue,
131		/// There was an attempt to increment the value in storage over `u32::MAX`.
132		StorageOverflow,
133	}
134
135	/// The pallet's dispatchable functions ([`Call`]s).
136	///
137	/// Dispatchable functions allows users to interact with the pallet and invoke state changes.
138	/// These functions materialize as "extrinsics", which are often compared to transactions.
139	/// They must always return a `DispatchResult` and be annotated with a weight and call index.
140	///
141	/// The [`call_index`] macro is used to explicitly
142	/// define an index for calls in the [`Call`] enum. This is useful for pallets that may
143	/// introduce new dispatchables over time. If the order of a dispatchable changes, its index
144	/// will also change which will break backwards compatibility.
145	///
146	/// The [`weight`] macro is used to assign a weight to each call.
147	#[pallet::call]
148	impl<T: Config> Pallet<T> {
149		/// An example dispatchable that takes a single u32 value as a parameter, writes the value
150		/// to storage and emits an event.
151		///
152		/// It checks that the _origin_ for this call is _Signed_ and returns a dispatch
153		/// error if it isn't. Learn more about origins here: <https://docs.substrate.io/build/origins/>
154		#[pallet::call_index(0)]
155		#[pallet::weight(T::WeightInfo::do_something())]
156		pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
157			// Check that the extrinsic was signed and get the signer.
158			let who = ensure_signed(origin)?;
159
160			// Update storage.
161			Something::<T>::put(something);
162
163			// Emit an event.
164			Self::deposit_event(Event::SomethingStored { something, who });
165
166			// Return a successful `DispatchResult`
167			Ok(())
168		}
169
170		/// An example dispatchable that may throw a custom error.
171		///
172		/// It checks that the caller is a signed origin and reads the current value from the
173		/// `Something` storage item. If a current value exists, it is incremented by 1 and then
174		/// written back to storage.
175		///
176		/// ## Errors
177		///
178		/// The function will return an error under the following conditions:
179		///
180		/// - If no value has been set ([`Error::NoneValue`])
181		/// - If incrementing the value in storage causes an arithmetic overflow
182		///   ([`Error::StorageOverflow`])
183		#[pallet::call_index(1)]
184		#[pallet::weight(T::WeightInfo::cause_error())]
185		pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
186			let _who = ensure_signed(origin)?;
187
188			// Read a value from storage.
189			match Something::<T>::get() {
190				// Return an error if the value has not been set.
191				None => Err(Error::<T>::NoneValue.into()),
192				Some(old) => {
193					// Increment the value read from storage. This will cause an error in the event
194					// of overflow.
195					let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
196					// Update the value in storage with the incremented result.
197					Something::<T>::put(new);
198					Ok(())
199				},
200			}
201		}
202	}
203}