Page cover image

🏗ïļFRAME Pallet

FRAMPallets are individual pieces of runtime logic for us in FRAME runtimes.

substrate_ has a long list of pre-packages pallets, but you can also build your own or modify one built by the substrate_ team.

Pallet Structure

A pallet is composed of a few building blocks. These are:

No Std

The first line of code, always. It tells the rust compiler not to use Rust's standard library except when explicitly told so.

Substrate runtimes are compiled to WASM and a regular native binary so you do not have access to rust's standard library.

This means simple things like printing to the command line using println! will not compile.

Learn more here.

This is where crates are imported, especially the substrate_ framework crates.

All pallets will import frame-support and frame-system

pub use pallet::*;
// other global dependencies...

#[frame_support::pallet]
pub mod pallet {
    use frame_support::pallet_prelude::*;
    use frame_system::pallet_prelude::*;
    // other pallet dependencies...

Declaration of the Pallet Type

A placeholder to implement traits and methods.

    #[pallet::pallet]
    #[pallet::generate_store(pub(super) trait Store)]
    #[pallet::generate_storage_info]
    pub struct Pallet<T>(_);

This is used to access features from other pallets, or constants that impact the pallet's behavior.

This is the part of the code where you say "hey I'm going to create some functions that do with something I'll call coin and I'm going to have the be the coin that's defined in the other pallet....but I'm going to also cast votes, so my votes will be the ones defined in that other voting pallet"

The Config trait allows you to do the above while keeping Generic Types so you can change define and use something called coin or votes from some other Pallet defined at runtime, but swapped with some other pallet with a runtime update later on.

Have some custom data you want on the chain for users or otherwise?

Runtime Storage is where you do that. You declare storage items you'd like to make accessible to runtime.

Events are how you communicate back to people using the block chain like "transaction confirmed!" or "error! not enough cash!"

This is where you'd build some logic that can be executed regularly in some context, for e.g. on_initialize.

Basically, your blockchain's equivalent to handling webhooks.

These are functions that blockchain users can call. This is often called a Dispatchable Call.

decl_module!

This is the macro where dispatchable calls are composed.

A Little More on Each Topic

⮇ïļImports⚙ïļRuntime Configuration TraitðŸ’ūRuntime Storage👍Runtime Events🏑Hook🙋Extrinsics (Dispatchable Calls)

Last updated

Was this helpful?