Pallet
In Substrate, a pallet is a runtime component providing specific functionality to a blockchain, such as managing balances, governance, or even consensus. In essence, it is a self-contained Rust module comprising a set of states and logic exposed for state transition. To maximize reusability, pallets are also designed to be configurable.
To achieve all these, writing a pallet involves a ton of boilerplate code.
The Rust procedural macro is a powerful codegen tool that fits perfectly for this use case.
This is why every pallet module is preceded by the procedural macro #[pallet] that takes in the whole module and generates the boilerplate code for us.
On top of that, the pallet builder requires two additional attributes:
#[pallet::pallet]to identify the pallet struct which contains the pallet’s configuration and metadata#[pallet::config]to identify the pallet’s configuration trait which defines the types and constants used by the pallet
Below is the bare minimal skeleton for a pallet.
use polkadot_sdk::polkadot_sdk_frame as frame;
#[frame::pallet]
pub mod pallet {
// `frame_system::Config` as supertrait is required by the generated code.
#[pallet::config]
pub trait Config: polkadot_sdk::frame_system::Config {}
// The generic parameter `<T>` is required by the generated code.
// It represents the runtime configuration which implements the `Config` trait.
#[pallet::pallet]
pub struct Pallet<T>(_);
}
// Re-exporting is required to properly import the pallet.
pub use pallet::*;frame_system acts as the base layer for other pallets to interact with the Substrate framework components, by providing
- the essential types and traits like
frame_system::Config - some fundamental utils for other pallets
frame_support offers pallet development support, namely the macros like #[pallet] and construct_runtime! {}, with preludes to be used alongside them.