Search code examples
substratepolkadot

How to set the default value for StorageValue?


I am trying set default value for StorageValue, but its giving error:

#[pallet::type_value]
pub fn DefaultRegistrationFees<T: Config>() -> u128 { 100u128 }

#[pallet::storage]
#[pallet::getter(fn profile_registration_fees)]
pub type RegistrationFee<T> = StorageValue<_, u128, OptionQuery, DefaultRegistrationFees<T>>;

Error:
lib.rs(81, 12): the trait frame_support::storage::types::QueryKindTrait<u128, pallet::DefaultRegistrationFees<T>> is not implemented for frame_support::pallet_prelude::OptionQuery

https://substrate.dev/docs/en/knowledgebase/runtime/storage#default-values


Solution

  • I guess you want to write:

    pub type RegistrationFee<T> = StorageValue<_, u128, ValueQuery, DefaultRegistrationFees<T>>;
    

    So to use a ValueQuery instead of OptionQuery.

    the QueryKind generic of the storage determine how the storage should be handled when there is no value in storage. With OptionQuery, when no value is in storage the method get will return None. With ValueQuery, when no value is in storage the method get will return the value configured with the generic OnEmpty. So when configuring a specific default, you want to use ValueQuery.