How Substrate Frame v2 different from Frame v1

Reading Time: 3 minutes

Blockchain is a specific type of database. It differs from a typical database in the way it stores information. Blockchains store data in blocks that are then chained together. Blockchain has changed how we perceive problems. It has brought tons of benefits.

  • Blockchain technology solves key issues like trust in a network.
  • Blockchain technology utilizes advanced security compared to other platforms or record-keeping systems.
  • By using blockchain, organizations can bring down a lot of costs associated with 3rd party vendors.

The substrate is a modular framework that enables you to create purpose-built blockchains by composing custom or pre-built components. We can use substrate in one of three ways:

  • Substrate Node
  • Substrate FRAME
  • Substrate Core

Why Frame v2 ?

There are different kinds of macros supported by Rust. All the kinds of macros basically do the same thing, take some code and write some more code in a programmable way. The existing FRAME macros use declarative macros, where basically you define your own custom syntax. The FRAME v2 uses attribute like macros. Attribute-like macros are similar to a custom derive macros, but instead of generating code for the Derived attribute, they allow you to create new attributes.

Pallets

Pallet Skeleton in Frame v1

use support::{decl_module, decl_event, decl_storage}

pub trait Config: frame_system::Config {
     type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
}

decl_event!(
    pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId {
        SomethingStored(u32, AccountId),
	}
);

decl_storage! { 
    trait Store for Module<T: Config> as TemplateModule {
         Something get(fn something): Option<u32>;
       }
  }

decl_module! {
    pub struct Module<T: Config> for enum Call where origin: T::Origin {
        fn deposit_event() = default;
    }
}

A FRAME v1 pallet is commonly composed of 5 sections:

  • Imports and Dependencies use for Imports and Dependencies.
  • Runtime Configuration Trait use to write all types and constants for runtime.
  • Runtime Events use to defines the pallet’s events.
  • Runtime Storage use to declare storage items.
  • The Pallet Declaration use to defines the  function, common to all pallets that define behavior that emits an event.

Pallet Skeleton in Frame v2

#[frame_support::pallet]
pub mod pallet {
	use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
	use frame_system::pallet_prelude::*;

#[pallet::config]
	pub trait Config: frame_system::Config {
		type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
	}

#[pallet::storage]
	#[pallet::getter(fn something)]
	pub type Something<T> = StorageValue<_, u32>;


#[pallet::event]
	#[pallet::metadata(T::AccountId = "AccountId")]
	#[pallet::generate_deposit(pub(super) fn deposit_event)]
	pub enum Event<T: Config> {
		SomethingStored(u32, T::AccountId),
	}


#[pallet::error]
	pub enum Error<T> {
		/// Error names should be descriptive.
		NoneValue,
		/// Errors should have helpful documentation associated with them.
		StorageOverflow,
	}

A FRAME v2 pallet is commonly composed of 7 sections:

  • Imports and Dependencies use for Imports and Dependencies.
  • Declaration of the Pallet type use as a placeholder to implement traits and methods.
  • Runtime Configuration Trait use to write all types and constants for runtime.
  • Runtime Storage use to declare storage items.
  • Runtime Events use to defines the pallet’s events.
  • Hooks use to write logic of the pallet.
  • Extrinsics use to write functions that are callable from outside the runtime.

In conclusion,

The FRAME v2 macros have been design to be much more modular and thus errors are reported to the user in a much more friendly way, allowing users to more easily figure out problems. Both the old and new macros expand to the same final Rust code, thus they are completely compatible with one another.

I hope you got an idea what is the difference between Frame Version 1 and Version 2

Thanks for reading !!

f 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