Integrate Substrate based 2D barcode scanner Pallet in your Substrate Runtime

Reading Time: 2 minutes

In my previous post Substrate based 2D barcode scanner Pallet in Rust, I explained how to create Substrate pallet easily. A pallet, Also known as Runtime modules, contains the logic, which can modify the features and functionality of your blockchain’s state-transition function.

A typical Substrate project consists of 4 components:

Node
Runtime
Pallets
Execute in Docker

The runtime is Substrate’s state transition function and is divided into separate logical components that are known as runtime modules(Pallets).

In this blog, I would explain how to add a substrate runtime module(pallet) in substrate runtime. I will be using Substrate Node Template as substrate project and will import our substrate-barcode-scanner-pallet in its runtime.

  1. Please complete Create Your First Substrate Chain tutorial to install Node Template.
  2. Import the Pallet: Add the dependency of substrate-barcode-scanner-pallet in runtime/Cargo.toml of Substrate Node Template.
# --snip--
[dependencies.substrate-barcode-scanner-pallet]
default_features = false
git = 'https://github.com/knoldus/substrate-barcode-scanner-pallet'
branch = 'main'

# toward the bottom
[features]
default = ['std']
std = [
    'substrate-barcode-scanner-pallet/std',
    # --snip--
]

3. Configure the Pallet: Now update runtime/src/lib.rs of Substrate Node Template to use our runtime pallet, by adding a trait implementation with our substrate_barcode_scanner_pallet and add it in our construct_runtime! macro.

// add the following code block
impl substrate_barcode_scanner_pallet::Trait for Runtime {
	type Event = Event;
	type ManufactureOrigin = frame_system::EnsureSigned<AccountId>;
}

// --snip--
construct_runtime!(
  pub enum Runtime where
    Block = Block,
    NodeBlock = opaque::Block,
    UncheckedExtrinsic = UncheckedExtrinsic
  {
    // --snip--
    // add the following line
    SubstrateBarcodeScanner: substrate_barcode_scanner_pallet::{Module, Call, Storage, Event<T>},
  }
);

4. Interact with Pallet through UI: Now go back to Substrate Node Template‘s root directory and compile the node in release mode with:

cargo build --release

After the build succeeds, you can start the node:

# Run a temporary node in development mode
./target/release/node-template --dev --tmp

Substrate also provides a Front-End Template to allow you to interact with the Node Template. Please go through front-end setup instructions .

To start the Front-End Template, navigate to its directory and run:

yarn start

You should see substrateBarcodeScanner as new pallet in Pallet Interactor.

After adding new product, a new event ProductInformationStored has been emitted.

Thanks for reading the blog!!

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 Rust Times Newsletter: https://bit.ly/2Vdlld7

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

Leave a Reply