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 testConfig
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 inDefaultConfig
). - you may attach the
#[pallet::no_default_bounds]
attribute to specify that a particular trait item can be used as a default when a testConfig
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 withderive_impl
. - Items marked with
#[pallet::no_default]
are entirely excluded from theDefaultConfig
trait, and therefore any impl ofDefaultConfig
doesn’t need to implement such items.
For more information, see:
§Optional: without_automatic_metadata
By default, the associated types of the Config
trait that require the TypeInfo
or
Parameter
bounds are included in the metadata of the pallet.
The optional without_automatic_metadata
argument can be used to exclude these
associated types from the metadata collection.
Furthermore, the without_automatic_metadata
argument can be used in combination with
the #[pallet::include_metadata]
attribute to selectively
include only certain associated types in the metadata collection.
#[frame_support::pallet]
mod pallet {
#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::config(with_default, without_automatic_metadata)] // <- with_default and without_automatic_metadata are 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 simple type.
// Type that would have been included in metadata, but is now excluded.
type SimpleType: From<u32> + TypeInfo;
// The `pallet::include_metadata` is used to selectively include this type in metadata.
#[pallet::include_metadata]
type SelectivelyInclude: From<u32> + TypeInfo;
}
#[pallet::event]
pub enum Event<T: Config> {
SomeEvent(u16, u32),
}
}
Documentation for this macro can be found at frame_support::pallet_macros::config
.