pallet_example_split/lib.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: MIT-0
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy of
7// this software and associated documentation files (the "Software"), to deal in
8// the Software without restriction, including without limitation the rights to
9// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10// of the Software, and to permit persons to whom the Software is furnished to do
11// so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24//! # Split Example Pallet
25//!
26//! **This pallet serves as an example and is not meant to be used in production.**
27//!
28//! A FRAME pallet demonstrating the ability to split sections across multiple files.
29//!
30//! Note that this is purely experimental at this point.
31
32#![cfg_attr(not(feature = "std"), no_std)]
33
34// Re-export pallet items so that they can be accessed from the crate namespace.
35pub use pallet::*;
36
37#[cfg(test)]
38mod mock;
39
40#[cfg(test)]
41mod tests;
42
43#[cfg(feature = "runtime-benchmarks")]
44mod benchmarking;
45mod events;
46
47pub mod weights;
48pub use weights::*;
49
50use frame_support::pallet_macros::*;
51
52/// Imports a [`pallet_section`] defined at [`events::events`].
53/// This brings the events defined in that section into the pallet's namespace.
54#[import_section(events::events)]
55#[frame_support::pallet]
56pub mod pallet {
57 use super::*;
58 use frame_support::pallet_prelude::*;
59 use frame_system::pallet_prelude::*;
60
61 #[pallet::pallet]
62 pub struct Pallet<T>(_);
63
64 /// Configure the pallet by specifying the parameters and types on which it depends.
65 #[pallet::config]
66 pub trait Config: frame_system::Config {
67 /// Type representing the weight of this pallet
68 type WeightInfo: WeightInfo;
69 }
70
71 // The pallet's runtime storage items.
72 #[pallet::storage]
73 pub type Something<T> = StorageValue<_, u32>;
74
75 // Errors inform users that something went wrong.
76 #[pallet::error]
77 pub enum Error<T> {
78 /// Error names should be descriptive.
79 NoneValue,
80 /// Errors should have helpful documentation associated with them.
81 StorageOverflow,
82 }
83
84 // Dispatchable functions allows users to interact with the pallet and invoke state changes.
85 // These functions materialize as "extrinsics", which are often compared to transactions.
86 // Dispatchable functions must be annotated with a weight and must return a DispatchResult.
87 #[pallet::call]
88 impl<T: Config> Pallet<T> {
89 /// An example dispatchable that takes a singles value as a parameter, writes the value to
90 /// storage and emits an event. This function must be dispatched by a signed extrinsic.
91 #[pallet::call_index(0)]
92 #[pallet::weight(T::WeightInfo::do_something())]
93 pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
94 // Check that the extrinsic was signed and get the signer.
95 // This function will return an error if the extrinsic is not signed.
96 let who = ensure_signed(origin)?;
97
98 // Update storage.
99 <Something<T>>::put(something);
100
101 // Emit an event.
102 Self::deposit_event(Event::SomethingStored { something, who });
103 // Return a successful DispatchResultWithPostInfo
104 Ok(())
105 }
106
107 /// An example dispatchable that may throw a custom error.
108 #[pallet::call_index(1)]
109 #[pallet::weight(T::WeightInfo::cause_error())]
110 pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
111 let _who = ensure_signed(origin)?;
112
113 // Read a value from storage.
114 match Something::<T>::get() {
115 // Return an error if the value has not been set.
116 None => return Err(Error::<T>::NoneValue.into()),
117 Some(old) => {
118 // Increment the value read from storage; will error in the event of overflow.
119 let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
120 // Update the value in storage with the incremented result.
121 <Something<T>>::put(new);
122 Ok(())
123 },
124 }
125 }
126 }
127}