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 is not supported for RuntimeEvent
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
// ...other config items get default
}
#[pallet::event]
pub enum Event<T: Config> {
SomeEvent(u16, u32),
}
}
As shown above, you may also 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
).
§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 frame_support::derive_impl
.
Rust-Analyzer Users: Documentation for this macro can be found at
frame_support::pallet_macros::config
.