0

Storage:

#[pallet::storage]
#[pallet::getter(fn voting)]
pub type Voting<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, Voter<T::AccountId>, ValueQuery>;

Type:

#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct Voter<AccountId> {
    pub votes: Vec<AccountId>,
    pub score: u128,
}

Without ValueQuery it does not give error, but with ValueQuery it gives error, how to fix the error. Seems I need to implement default, not sure how to do it.

the trait `std::default::Default` is not implemented for `types::Voter<<T as frame_system::Config>::AccountId>`

1 Answer 1

1

If you want to use ValueQuery then yes, you need to implement the Default trait for your structure if you want to return anything in case the key is missing. I think this should work fine (not sure 100%):

impl<AccountId> Default for Voter<AccountId> {
  fn default() -> Self {
    Self { votes: Default::default(), score: Default::default() }
  }
}

You can always set the Default::default() values for a different one in case you want to return something different, the votes can be initialized with some accounts and the score can be any number.

If you omit ValueQuery, when interacting with you structure you will get Option<Voter<AccountId>>, which means, if you try to get some value from your StorageMap you will get either Some(Voter) or None. Using ValueQuery will always return a value, so you don't have to deal with unwrapping the get calls.

Hope it helps.

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.