I'm trying to understand the #[pallet::extra_constants]
and how it's different from #[pallet::constant]
.
And appropriate scenario for the same.
Substrate and Polkadot Stack Exchange is a question and answer site for developers building blockchains with the Substrate SDK. It only takes a minute to sign up.
Sign up to join this communityhttps://paritytech.github.io/substrate/master/frame_support/attr.pallet.html
// pallet_a.rs
#[pallet::config]
pub trait Config: frame_system::Config {
#[pallet::constant]
type Foo: Get<u32>;
}
#[pallet::extra_constants]
impl<T: Config> Pallet<T> {
pub fn bar() -> u32 {
0
}
}
As you can see, the constant
can only be defined in the Config
trait.
You must define a type first and then mark it as constant.
extra_constants
can be defined without that limitation. But this constants can not be configured in runtime.
// runtime.rs
// You can not configure the `extra_constants` here.
impl pallet_a::Config for Runtime {
type Foo = ConstU32<10>;
}
Config
trait is instantiated by the user of the pallet (who implements the Config
for their runtime) and the extra_constants
are defined by the author of the pallet - potentially totally independently of the main runtime configuration.