Attribute Macro frame_support::pallet

source ·
#[pallet]
Expand description

The pallet macro has 2 purposes:

§1 - Pallet module declaration

The module to declare a pallet is organized as follow:

#[frame_support::pallet]    // <- the macro
mod pallet {
	#[pallet::pallet]
	pub struct Pallet<T>(_);

	#[pallet::config]
	pub trait Config: frame_system::Config {}

	#[pallet::call]
	impl<T: Config> Pallet<T> {
	}

	/* ... */
}

The documentation for each individual part can be found at frame_support::pallet_macros

§Dev Mode (#[pallet(dev_mode)])

Syntax:

#[frame_support::pallet(dev_mode)]
mod pallet {
	/* ... */
}

Specifying the argument dev_mode will allow you to enable dev mode for a pallet. The aim of dev mode is to loosen some of the restrictions and requirements placed on production pallets for easy tinkering and development. Dev mode pallets should not be used in production. Enabling dev mode has the following effects:

  • Weights no longer need to be specified on every #[pallet::call] declaration. By default, dev mode pallets will assume a weight of zero (0) if a weight is not specified. This is equivalent to specifying #[weight(0)] on all calls that do not specify a weight.
  • Call indices no longer need to be specified on every #[pallet::call] declaration. By default, dev mode pallets will assume a call index based on the order of the call.
  • All storages are marked as unbounded, meaning you do not need to implement MaxEncodedLen on storage types. This is equivalent to specifying #[pallet::unbounded] on all storage type definitions.
  • Storage hashers no longer need to be specified and can be replaced by _. In dev mode, these will be replaced by Blake2_128Concat. In case of explicit key-binding, Hasher can simply be ignored when in dev_mode.

Note that the dev_mode argument can only be supplied to the #[pallet] or #[frame_support::pallet] attribute macro that encloses your pallet module. This argument cannot be specified anywhere else, including but not limited to the #[pallet::pallet] attribute macro.

WARNING:
You should never deploy or use dev mode pallets in production. Doing so can break your
chain. Once you are done tinkering, you should
remove the 'dev_mode' argument from your #[pallet] declaration and fix any compile
errors before attempting to use your pallet in a production scenario.

§2 - Pallet struct placeholder declaration

The pallet struct placeholder #[pallet::pallet] is mandatory and allows you to specify pallet information.

The struct must be defined as follows:

#[frame_support::pallet]
mod pallet {
	#[pallet::pallet]         // <- the macro
	pub struct Pallet<T>(_);  // <- the struct definition

	#[pallet::config]
	pub trait Config: frame_system::Config {}
}

I.e. a regular struct definition named Pallet, with generic T and no where clause.

§Macro expansion:

The macro adds this attribute to the Pallet struct definition:

#[derive(
	frame_support::CloneNoBound,
	frame_support::EqNoBound,
	frame_support::PartialEqNoBound,
	frame_support::RuntimeDebugNoBound,
)]

and replaces the type _ with PhantomData<T>.

It also implements on the pallet:

If the attribute set_storage_max_encoded_len is set then the macro calls StorageInfoTrait for each storage in the implementation of StorageInfoTrait for the pallet. Otherwise it implements StorageInfoTrait for the pallet using the PartialStorageInfoTrait implementation of storages.


Documentation for this macro can be found at frame_support::pallet.