mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-18 08:21:03 +00:00
Migrate pallet-template to pallet attribute macro (#7981)
* Converting pallet-template to Framev2 macro's * Add newline * Convert all indents to tabs * Update bin/node-template/pallets/template/src/lib.rs * Update bin/node-template/pallets/template/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
committed by
GitHub
parent
b692954c6e
commit
054b0f542e
@@ -4,8 +4,7 @@
|
|||||||
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
|
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
|
||||||
/// https://substrate.dev/docs/en/knowledgebase/runtime/frame
|
/// https://substrate.dev/docs/en/knowledgebase/runtime/frame
|
||||||
|
|
||||||
use frame_support::{decl_module, decl_storage, decl_event, decl_error, dispatch, traits::Get};
|
pub use pallet::*;
|
||||||
use frame_system::ensure_signed;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod mock;
|
mod mock;
|
||||||
@@ -13,89 +12,91 @@ mod mock;
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
/// Configure the pallet by specifying the parameters and types on which it depends.
|
#[frame_support::pallet]
|
||||||
pub trait Config: frame_system::Config {
|
pub mod pallet {
|
||||||
/// Because this pallet emits events, it depends on the runtime's definition of an event.
|
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
|
||||||
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
|
use frame_system::pallet_prelude::*;
|
||||||
}
|
|
||||||
|
|
||||||
// The pallet's runtime storage items.
|
/// Configure the pallet by specifying the parameters and types on which it depends.
|
||||||
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
|
#[pallet::config]
|
||||||
decl_storage! {
|
pub trait Config: frame_system::Config {
|
||||||
// A unique name is used to ensure that the pallet's storage items are isolated.
|
/// Because this pallet emits events, it depends on the runtime's definition of an event.
|
||||||
// This name may be updated, but each pallet in the runtime must use a unique name.
|
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
|
||||||
// ---------------------------------vvvvvvvvvvvvvv
|
|
||||||
trait Store for Module<T: Config> as TemplateModule {
|
|
||||||
// Learn more about declaring storage items:
|
|
||||||
// https://substrate.dev/docs/en/knowledgebase/runtime/storage#declaring-storage-items
|
|
||||||
Something get(fn something): Option<u32>;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Pallets use events to inform users when important changes are made.
|
#[pallet::pallet]
|
||||||
// https://substrate.dev/docs/en/knowledgebase/runtime/events
|
#[pallet::generate_store(pub(super) trait Store)]
|
||||||
decl_event! {
|
pub struct Pallet<T>(PhantomData<T>);
|
||||||
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId {
|
|
||||||
|
// The pallet's runtime storage items.
|
||||||
|
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
|
||||||
|
#[pallet::storage]
|
||||||
|
#[pallet::getter(fn something)]
|
||||||
|
// Learn more about declaring storage items:
|
||||||
|
// https://substrate.dev/docs/en/knowledgebase/runtime/storage#declaring-storage-items
|
||||||
|
pub type Something<T> = StorageValue<_, u32>;
|
||||||
|
|
||||||
|
// Pallets use events to inform users when important changes are made.
|
||||||
|
// https://substrate.dev/docs/en/knowledgebase/runtime/events
|
||||||
|
#[pallet::event]
|
||||||
|
#[pallet::metadata(T::AccountId = "AccountId")]
|
||||||
|
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||||
|
pub enum Event<T: Config> {
|
||||||
/// Event documentation should end with an array that provides descriptive names for event
|
/// Event documentation should end with an array that provides descriptive names for event
|
||||||
/// parameters. [something, who]
|
/// parameters. [something, who]
|
||||||
SomethingStored(u32, AccountId),
|
SomethingStored(u32, T::AccountId),
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Errors inform users that something went wrong.
|
// Errors inform users that something went wrong.
|
||||||
decl_error! {
|
#[pallet::error]
|
||||||
pub enum Error for Module<T: Config> {
|
pub enum Error<T> {
|
||||||
/// Error names should be descriptive.
|
/// Error names should be descriptive.
|
||||||
NoneValue,
|
NoneValue,
|
||||||
/// Errors should have helpful documentation associated with them.
|
/// Errors should have helpful documentation associated with them.
|
||||||
StorageOverflow,
|
StorageOverflow,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Dispatchable functions allows users to interact with the pallet and invoke state changes.
|
#[pallet::hooks]
|
||||||
// These functions materialize as "extrinsics", which are often compared to transactions.
|
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
|
||||||
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
|
|
||||||
decl_module! {
|
|
||||||
pub struct Module<T: Config> for enum Call where origin: T::Origin {
|
|
||||||
// Errors must be initialized if they are used by the pallet.
|
|
||||||
type Error = Error<T>;
|
|
||||||
|
|
||||||
// Events must be initialized if they are used by the pallet.
|
|
||||||
fn deposit_event() = default;
|
|
||||||
|
|
||||||
|
// 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> {
|
||||||
/// An example dispatchable that takes a singles value as a parameter, writes the value to
|
/// 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.
|
/// storage and emits an event. This function must be dispatched by a signed extrinsic.
|
||||||
#[weight = 10_000 + T::DbWeight::get().writes(1)]
|
#[pallet::weight(10_000 + T::DbWeight::get().writes(1))]
|
||||||
pub fn do_something(origin, something: u32) -> dispatch::DispatchResult {
|
pub fn do_something(origin: OriginFor<T>, something: u32) -> DispatchResultWithPostInfo {
|
||||||
// Check that the extrinsic was signed and get the signer.
|
// Check that the extrinsic was signed and get the signer.
|
||||||
// This function will return an error if the extrinsic is not signed.
|
// This function will return an error if the extrinsic is not signed.
|
||||||
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
|
// https://substrate.dev/docs/en/knowledgebase/runtime/origin
|
||||||
let who = ensure_signed(origin)?;
|
let who = ensure_signed(origin)?;
|
||||||
|
|
||||||
// Update storage.
|
// Update storage.
|
||||||
Something::put(something);
|
<Something<T>>::put(something);
|
||||||
|
|
||||||
// Emit an event.
|
// Emit an event.
|
||||||
Self::deposit_event(RawEvent::SomethingStored(something, who));
|
Self::deposit_event(Event::SomethingStored(something, who));
|
||||||
// Return a successful DispatchResult
|
// Return a successful DispatchResultWithPostInfo
|
||||||
Ok(())
|
Ok(().into())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An example dispatchable that may throw a custom error.
|
/// An example dispatchable that may throw a custom error.
|
||||||
#[weight = 10_000 + T::DbWeight::get().reads_writes(1,1)]
|
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1))]
|
||||||
pub fn cause_error(origin) -> dispatch::DispatchResult {
|
pub fn cause_error(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
|
||||||
let _who = ensure_signed(origin)?;
|
let _who = ensure_signed(origin)?;
|
||||||
|
|
||||||
// Read a value from storage.
|
// Read a value from storage.
|
||||||
match Something::get() {
|
match <Something<T>>::get() {
|
||||||
// Return an error if the value has not been set.
|
// Return an error if the value has not been set.
|
||||||
None => Err(Error::<T>::NoneValue)?,
|
None => Err(Error::<T>::NoneValue)?,
|
||||||
Some(old) => {
|
Some(old) => {
|
||||||
// Increment the value read from storage; will error in the event of overflow.
|
// Increment the value read from storage; will error in the event of overflow.
|
||||||
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
|
let new = old.checked_add(1).ok_or(Error::<T>::StorageOverflow)?;
|
||||||
// Update the value in storage with the incremented result.
|
// Update the value in storage with the incremented result.
|
||||||
Something::put(new);
|
<Something<T>>::put(new);
|
||||||
Ok(())
|
Ok(().into())
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user