|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! # Split Example Pallet |
| 19 | +//! |
| 20 | +//! **This pallet serves as an example and is not meant to be used in production.** |
| 21 | +//! |
| 22 | +//! A FRAME pallet demonstrating the ability to split sections across multiple files. |
| 23 | +//! |
| 24 | +//! Note that this is purely experimental at this point. |
| 25 | +
|
| 26 | +#![cfg_attr(not(feature = "std"), no_std)] |
| 27 | + |
| 28 | +// Re-export pallet items so that they can be accessed from the crate namespace. |
| 29 | +pub use pallet::*; |
| 30 | + |
| 31 | +#[cfg(test)] |
| 32 | +mod mock; |
| 33 | + |
| 34 | +#[cfg(test)] |
| 35 | +mod tests; |
| 36 | + |
| 37 | +#[cfg(feature = "runtime-benchmarks")] |
| 38 | +mod benchmarking; |
| 39 | +mod events; |
| 40 | + |
| 41 | +pub mod weights; |
| 42 | +pub use weights::*; |
| 43 | + |
| 44 | +use frame_support::pallet_macros::*; |
| 45 | + |
| 46 | +/// Imports a [`pallet_section`] defined at [`events::events`]. |
| 47 | +/// This brings the events defined in that section into the pallet's namespace. |
| 48 | +#[import_section(events::events)] |
| 49 | +#[frame_support::pallet] |
| 50 | +pub mod pallet { |
| 51 | + use super::*; |
| 52 | + use frame_support::pallet_prelude::*; |
| 53 | + use frame_system::pallet_prelude::*; |
| 54 | + |
| 55 | + #[pallet::pallet] |
| 56 | + pub struct Pallet<T>(_); |
| 57 | + |
| 58 | + /// Configure the pallet by specifying the parameters and types on which it depends. |
| 59 | + #[pallet::config] |
| 60 | + pub trait Config: frame_system::Config { |
| 61 | + /// Because this pallet emits events, it depends on the runtime's definition of an event. |
| 62 | + type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; |
| 63 | + /// Type representing the weight of this pallet |
| 64 | + type WeightInfo: WeightInfo; |
| 65 | + } |
| 66 | + |
| 67 | + // The pallet's runtime storage items. |
| 68 | + #[pallet::storage] |
| 69 | + pub type Something<T> = StorageValue<_, u32>; |
| 70 | + |
| 71 | + // Errors inform users that something went wrong. |
| 72 | + #[pallet::error] |
| 73 | + pub enum Error<T> { |
| 74 | + /// Error names should be descriptive. |
| 75 | + NoneValue, |
| 76 | + /// Errors should have helpful documentation associated with them. |
| 77 | + StorageOverflow, |
| 78 | + } |
| 79 | + |
| 80 | + // Dispatchable functions allows users to interact with the pallet and invoke state changes. |
| 81 | + // These functions materialize as "extrinsics", which are often compared to transactions. |
| 82 | + // Dispatchable functions must be annotated with a weight and must return a DispatchResult. |
| 83 | + #[pallet::call] |
| 84 | + impl<T: Config> Pallet<T> { |
| 85 | + /// An example dispatchable that takes a singles value as a parameter, writes the value to |
| 86 | + /// storage and emits an event. This function must be dispatched by a signed extrinsic. |
| 87 | + #[pallet::call_index(0)] |
| 88 | + #[pallet::weight(T::WeightInfo::do_something())] |
| 89 | + pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult { |
| 90 | + // Check that the extrinsic was signed and get the signer. |
| 91 | + // This function will return an error if the extrinsic is not signed. |
| 92 | + let who = ensure_signed(origin)?; |
| 93 | + |
| 94 | + // Update storage. |
| 95 | + <Something<T>>::put(something); |
| 96 | + |
| 97 | + // Emit an event. |
| 98 | + Self::deposit_event(Event::SomethingStored { something, who }); |
| 99 | + // Return a successful DispatchResultWithPostInfo |
| 100 | + Ok(()) |
| 101 | + } |
| 102 | + |
| 103 | + /// An example dispatchable that may throw a custom error. |
| 104 | + #[pallet::call_index(1)] |
| 105 | + #[pallet::weight(T::WeightInfo::cause_error())] |
| 106 | + pub fn cause_error(origin: OriginFor<T>) -> DispatchResult { |
| 107 | + let _who = ensure_signed(origin)?; |
| 108 | + |
| 109 | + // Read a value from storage. |
| 110 | + match <Something<T>>::get() { |
| 111 | + // Return an error if the value has not been set. |
| 112 | + None => return Err(Error::<T>::NoneValue.into()), |
| 113 | + Some(old) => { |
| 114 | + // Increment the value read from storage; will error in the event of overflow. |
| 115 | + let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?; |
| 116 | + // Update the value in storage with the incremented result. |
| 117 | + <Something<T>>::put(new); |
| 118 | + Ok(()) |
| 119 | + }, |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments