solutions/pallets/template/src/lib.rs

109 lines
3.8 KiB
Rust
Raw Normal View History

2020-03-05 16:53:25 +00:00
#![cfg_attr(not(feature = "std"), no_std)]
2020-07-25 12:35:30 +00:00
/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://docs.substrate.io/reference/frame-pallets/>
pub use pallet::*;
2020-03-05 16:53:25 +00:00
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
pub mod weights;
pub use weights::*;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
2020-03-05 16:53:25 +00:00
#[pallet::pallet]
pub struct Pallet<T>(_);
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
/// Type representing the weight of this pallet
type WeightInfo: WeightInfo;
2020-03-05 16:53:25 +00:00
}
// The pallet's runtime storage items.
// https://docs.substrate.io/main-docs/build/runtime-storage/
#[pallet::storage]
#[pallet::getter(fn something)]
// Learn more about declaring storage items:
// https://docs.substrate.io/main-docs/build/runtime-storage/#declaring-storage-items
pub type Something<T> = StorageValue<_, u32>;
// Pallets use events to inform users when important changes are made.
// https://docs.substrate.io/main-docs/build/events-errors/
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
2020-07-25 12:35:30 +00:00
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
2022-11-17 16:17:29 +00:00
SomethingStored { something: u32, who: T::AccountId },
2020-03-05 16:53:25 +00:00
}
// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
2020-07-25 12:35:30 +00:00
/// Error names should be descriptive.
2020-03-05 16:53:25 +00:00
NoneValue,
2020-07-25 12:35:30 +00:00
/// Errors should have helpful documentation associated with them.
2020-03-05 16:53:25 +00:00
StorageOverflow,
}
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {
2020-07-25 12:35:30 +00:00
/// An example dispatchable that takes a singles value as a parameter, writes the value to
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::do_something())]
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResult {
2020-07-25 12:35:30 +00:00
// Check that the extrinsic was signed and get the signer.
// This function will return an error if the extrinsic is not signed.
// https://docs.substrate.io/main-docs/build/origins/
2020-03-05 16:53:25 +00:00
let who = ensure_signed(origin)?;
2020-07-25 12:35:30 +00:00
// Update storage.
<Something<T>>::put(something);
2020-03-05 16:53:25 +00:00
2020-07-25 12:35:30 +00:00
// Emit an event.
2022-11-17 16:17:29 +00:00
Self::deposit_event(Event::SomethingStored { something, who });
// Return a successful DispatchResultWithPostInfo
Ok(())
2020-03-05 16:53:25 +00:00
}
2020-07-25 12:35:30 +00:00
/// An example dispatchable that may throw a custom error.
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::cause_error())]
pub fn cause_error(origin: OriginFor<T>) -> DispatchResult {
2020-03-05 16:53:25 +00:00
let _who = ensure_signed(origin)?;
2020-07-25 12:35:30 +00:00
// Read a value from storage.
match <Something<T>>::get() {
2020-07-25 12:35:30 +00:00
// Return an error if the value has not been set.
None => return Err(Error::<T>::NoneValue.into()),
2020-03-05 16:53:25 +00:00
Some(old) => {
2020-07-25 12:35:30 +00:00
// Increment the value read from storage; will error in the event of overflow.
2020-03-05 16:53:25 +00:00
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
2020-07-25 12:35:30 +00:00
// Update the value in storage with the incremented result.
<Something<T>>::put(new);
Ok(())
2020-03-05 16:53:25 +00:00
},
}
}
}
}