1#![cfg_attr(not(feature = "std"), no_std)]23/// A FRAME pallet template with necessary imports45/// Feel free to remove or edit this file as needed.6/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs7/// If you remove this file, you can remove those references89/// For more guidance on Substrate FRAME, see the example pallet10/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs1112use frame_support::{decl_module, decl_storage, decl_event, decl_error, dispatch};13use system::ensure_signed;1415#[cfg(test)]16mod mock;1718#[cfg(test)]19mod tests;2021/// The pallet's configuration trait.22pub trait Trait: system::Trait {23 // Add other types and constants required to configure this pallet.2425 /// The overarching event type.26 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;27}2829// This pallet's storage items.30decl_storage! {31 trait Store for Module<T: Trait> as TemplateModule {32 // Just a dummy storage item.33 // Here we are declaring a StorageValue, `Something` as a Option<u32>34 // `get(fn something)` is the default getter which returns either the stored `u32` or `None` if nothing stored35 Something get(fn something): Option<u32>;36 }37}3839// The pallet's events40decl_event!(41 pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {42 /// Just a dummy event.43 /// Event `Something` is declared with a parameter of the type `u32` and `AccountId`44 /// To emit this event, we call the deposit function, from our runtime functions45 SomethingStored(u32, AccountId),46 }47);4849// The pallet's errors50decl_error! {51 pub enum Error for Module<T: Trait> {52 /// Value was None53 NoneValue,54 /// Value reached maximum and cannot be incremented further55 StorageOverflow,56 }57}5859// The pallet's dispatchable functions.60decl_module! {61 /// The module declaration.62 pub struct Module<T: Trait> for enum Call where origin: T::Origin {63 // Initializing errors64 // this includes information about your errors in the node's metadata.65 // it is needed only if you are using errors in your pallet66 type Error = Error<T>;6768 // Initializing events69 // this is needed only if you are using events in your pallet70 fn deposit_event() = default;7172 /// Just a dummy entry point.73 /// function that can be called by the external world as an extrinsics call74 /// takes a parameter of the type `AccountId`, stores it, and emits an event75 pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {76 // Check it was signed and get the signer. See also: ensure_root and ensure_none77 let who = ensure_signed(origin)?;7879 // Code to execute when something calls this.80 // For example: the following line stores the passed in u32 in the storage81 Something::put(something);8283 // Here we are raising the Something event84 Self::deposit_event(RawEvent::SomethingStored(something, who));85 Ok(())86 }8788 /// Another dummy entry point.89 /// takes no parameters, attempts to increment storage value, and possibly throws an error90 pub fn cause_error(origin) -> dispatch::DispatchResult {91 // Check it was signed and get the signer. See also: ensure_root and ensure_none92 let _who = ensure_signed(origin)?;9394 match Something::get() {95 None => Err(Error::<T>::NoneValue)?,96 Some(old) => {97 let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;98 Something::put(new);99 Ok(())100 },101 }102 }103 }104}difftreelog
source
pallets/template/src/lib.rs3.4 KiBsourcehistory