Pallets in Substrate and using them in runtime.

Reading Time: 3 minutes

Pallets are an interesting part of blockchain development using the substrate. If you heard about pallets but did not know what these exactly are and how to use them, then you are at the right place. Let’s explore pallets and learn how to include them in runtime.

Pallets

Pallets are special kinds of RUST modules that build the runtime of our substrate-based blockchain. So, pallets basically are building blocks of our runtime. Let’s quickly understand the runtime of substrate-based chains.

Runtime Composition

Runtime Overview

The runtime of a blockchain is the business logic that defines its behaviour. The runtime is basically the state transition function where developers define the storage items that represent the blockchain’s state. It also contains the functions that allow our blockchain to change to different states.

Structure of a Pallet

use support::{decl_module, decl_event, decl_storage, ...}

The pallet supports the use of any Rust library which compiles with the `no_std` flag.

pub trait Config: frame_system::Config { ... }

All of the runtime types and consts go in here. If the pallet is dependent on specific other pallets, then their configuration traits should be added to the inherited traits list.

decl_event! { ... }

This macro contains events that are emitted when the chain wants to tell external entities about changes or conditions in the runtime to external entities.

decl_storage! { ... }

decl_storage macro allows you to store data in your blockchain that is persisted between blocks and can be accessed from within your runtime.

decl_module! { ... }

This macro defines the callable functions that this pallet exposes and manages actions this pallet takes throughout block execution.

Add a pallet to your runtime

Now we will try to add a pallet named test-pallet to our substrate-based chain to show how we can add any pallet to our runtime.

In runtime/Cargo.toml

[dependencies] 
#--snip--
test-pallet = { path = "../pallets/test-pallet", default-features = false, version = '3.0.0' }

[features]
default = ["std"] 
std = [ 
'test-pallet/std',
 #--snip-- 
]

In runtime/src/lib.rs

impl test_pallet::Config for Runtime {
    type Event = Event;
}

To add a pallet to runtime we need to implement the Config trait for that specific pallet in runtime. The parameter_types macro contains some constants. Our implementation of Config trait uses these constants. Now that we have implemented our pallet in runtime we need to at last add the pallet in construct_runtime macro.

construct_runtime!(
    pub enum Runtime where
        Block = Block,
        NodeBlock = opaque::Block,
        UncheckedExtrinsic = UncheckedExtrinsic
    {
        /* --snip-- */

        /*** Add This Line ***/
        TestPallet: test_pallet::{Module, Call, Storage, Event<T>},
    }
);

TestPallet is the name which we have given to our pallet. We can use any name of our choice as long as it is relevant.

Now we have successfully added our pallet to our runtime. Similarly, we can add any other pallet we make in the runtime.

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe to Rust Times Newsletter: https://bit.ly/2Vdlld7.


Knoldus-blog-footer-image

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading