2

I'm trying to understand the #[pallet::extra_constants] and how it's different from #[pallet::constant].

And appropriate scenario for the same.

1 Answer 1

2

https://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>;
}
1
  • 1
    Worth noting the key difference: the 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.
    – rob
    Sep 10, 2022 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.