Pallet Struct
The Pallet
struct is the anchor on which we implement all logic and traits for our Pallet.
#![allow(unused)] fn main() { #[pallet::pallet] pub struct Pallet<T>(core::marker::PhantomData<T>); }
Function Implementations
For example, when you look more at the template, you will see code like:
#![allow(unused)] fn main() { impl<T: Config> Pallet<T> { // -- snip -- } }
This is just regular Rust and how you would implement functions on a struct.
Trait Implementations
Imagine we had a trait Hooks
we wanted to implement, we would also use the Pallet
struct to to that:
#![allow(unused)] fn main() { impl<T: Config> Hooks for Pallet<T> { fn on_finalize() { // -- snip -- } } }
And then we could access that those functions like:
#![allow(unused)] fn main() { pallet_kitties::Pallet::<T>::on_finalize(); }
In fact, many traits are automatically implemented on top of Pallet
and are accessible thanks to the #[pallet::pallet]
attribute macro.
FRAME Traits
You can see all the different traits implemented on Pallet
by looking at the autogenerated Rust docs.
One simple example is the trait PalletInfoAccess
.
With this trait, you can do things like call pallet_kitties::Pallet::<T>::module_name()
which will return to you the name of the rust module, in this case pallet_kitties
. Information like this is used mostly between other macros, which is why we hide it all from you behind the macros.
In this tutorial, you will not interact with any of these automatically generated traits, but knowing that they exist can allow you to investigate further after learning the basics.
#![allow(unused)] #![cfg_attr(not(feature = "std"), no_std)] fn main() { mod impls; mod tests; use frame::prelude::*; pub use pallet::*; #[frame::pallet(dev_mode)] pub mod pallet { use super::*; /* 🚧 TODO 🚧: Learn about the Pallet struct. */ #[pallet::pallet] pub struct Pallet<T>(core::marker::PhantomData<T>); #[pallet::config] pub trait Config: frame_system::Config { type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>; } #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] pub enum Event<T: Config> { Created { owner: T::AccountId }, } #[pallet::error] pub enum Error<T> {} #[pallet::call] impl<T: Config> Pallet<T> { pub fn create_kitty(origin: OriginFor<T>) -> DispatchResult { let who = ensure_signed(origin)?; Self::mint(who)?; Ok(()) } } } }
diff --git a/src/lib.rs b/src/lib.rs
index dae4e99..c77966d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -6,11 +6,11 @@ mod tests;
use frame::prelude::*;
pub use pallet::*;
-/* 🚧 TODO 🚧: Learn about macros used in the `polkadot-sdk`, making pallet development easier. */
#[frame::pallet(dev_mode)]
pub mod pallet {
use super::*;
+ /* 🚧 TODO 🚧: Learn about the Pallet struct. */
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);