Create runtime API of Substrate based 2D barcode scanner Pallet for Querying

Table of contents
Reading Time: 2 minutes

In my previous post Integrate Substrate based 2D barcode scanner Pallet in your Substrate Runtime, I explained how to integrate Substrate pallet in your runtime. 

Substrate runtime is its State Transition Function, which contains business logic of a Blockchain that determines how the state changes when a block is processed and defines its behaviour of the Blockchain.

Substrate runtime can be accessed to outer node though an API, which is called substrate runtime API. A Substrate runtime API is an interface between node and the runtime.

Using Runtime API, node can query to runtime to fetch the information or provide the information. In this blog, I would explain how to create runtime API of your substrate runtime.

In my previous blog, we created a method in our pallet to verify barcode.

impl<T: Trait> Module<T> {
    pub fn is_valid_barcode(barcode: T::Hash) -> bool {
        ProductInformation::<T>::contains_key(&barcode)
    }
}

We will be creating runtime API to verify barcode.

As a first step, we will be defining interface here https://github.com/knoldus/substrate-barcode-scanner-pallet/blob/main/runtime-api/src/lib.rs using decl_runtime_apis! macro

sp_api::decl_runtime_apis! {
    /// The API to verify barcode
    pub trait VerifyBarcodeApi<Hash> where
		Hash: codec::Codec, {
		/// Verify barcode
        fn is_valid_barcode(barcode: Hash) -> bool;
    }
}

Next step is to implement this API interface impl_runtime_apis! macro in your runtime.

Before that, we need to import this runtime interface in our runtime. We will use Substrate Node Template as substrate project since we already imported our substrate-barcode-scanner-pallet in its runtime. Add dependency in runtime/Cargo.toml of Substrate Node Template.

[dependencies.substrate-barcode-scanner-pallet-runtime-api]
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-runtime-api/std',
    # --snip--
]

In runtime/lib.rs, add below implementation details in impl_runtime_apis! macro.

impl_runtime_apis! {
  // --snip--

 	impl substrate_barcode_scanner_pallet_runtime_api::VerifyBarcodeApi<Block, Hash> for Runtime {
		fn is_valid_barcode(barcode: Hash) -> bool {
			SubstrateBarcodeScanner::is_valid_barcode(barcode)
		}
	}
}

Now go back to Substrate Node Template‘s root directory and compile the node in release mode with:

cargo build --release

We have successfully added our runtime API in our runtime. Next step is to connect this API with RPC so that it can be called by end user.

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.