Attribute Macro frame_support::pallet_macros::config

source ·
#[config]
Expand description

The mandatory attribute allowing definition of configurable types for the pallet.

Item must be defined as:

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

	#[pallet::config]
	pub trait Config: frame_system::Config // + $optionally_some_other_supertraits
	// $optional_where_clause
	{
		// config items here
	}
}

I.e. a regular trait definition named Config, with the supertrait frame_system::pallet::Config, and optionally other supertraits and a where clause. (Specifying other supertraits here is known as tight coupling)

The associated type RuntimeEvent is reserved. If defined, it must have the bounds From<Event> and IsType<<Self as frame_system::Config>::RuntimeEvent>.

#[pallet::event] must be present if RuntimeEvent exists as a config item in your #[pallet::config].

§Optional: with_default

An optional with_default argument may also be specified. Doing so will automatically generate a DefaultConfig trait inside your pallet which is suitable for use with #[derive_impl(..) to derive a default testing config:

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

	#[pallet::config(with_default)] // <- with_default is optional
	pub trait Config: frame_system::Config {
		/// The overarching event type.
		#[pallet::no_default_bounds] // Default with bounds is not supported for RuntimeEvent
		type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;

		/// A more complex type.
		#[pallet::no_default] // Example of type where no default should be provided
		type MoreComplexType: SomeMoreComplexBound;

		/// A simple type.
		// Default with bounds is supported for simple types
		type SimpleType: From<u32>;
	}

	#[pallet::event]
	pub enum Event<T: Config> {
		SomeEvent(u16, u32),
	}
}

As shown above:

  • you may attach the #[pallet::no_default] attribute to specify that a particular trait item cannot be used as a default when a test Config is derived using the #[derive_impl(..)] attribute macro. This will cause that particular trait item to simply not appear in default testing configs based on this config (the trait item will not be included in DefaultConfig).
  • you may attach the #[pallet::no_default_bounds] attribute to specify that a particular trait item can be used as a default when a test Config is derived using the #[derive_impl(..)] attribute macro. But its bounds cannot be enforced at this point and should be discarded when generating the default config trait.
  • you may not specify any attribute to generate a trait item in the default config trait.

In case origin of error is not clear it is recommended to disable all default with #[pallet::no_default] and enable them one by one.

§DefaultConfig Caveats

The auto-generated DefaultConfig trait:

  • is always a subset of your pallet’s Config trait.
  • can only contain items that don’t rely on externalities, such as frame_system::Config.

Trait items that do rely on externalities should be marked with #[pallet::no_default]

Consequently:

  • Any items that rely on externalities must be marked with #[pallet::no_default] or your trait will fail to compile when used with derive_impl.
  • Items marked with #[pallet::no_default] are entirely excluded from the DefaultConfig trait, and therefore any impl of DefaultConfig doesn’t need to implement such items.

For more information, see:


Documentation for this macro can be found at frame_support::pallet_macros::config.