3

I would like to map T::AccountId to a custom struct I have Amendments <AccountId:

    #[pallet::storage]
    #[pallet::getter(fn declaration_map)]
    pub(super) type Declarations<T: Config> = StorageMap
    <
    _,
    Blake2_128Concat,
    T::AccountId,
    Amendment<T::AccountId>,
    ValueQuery
    >;

However when I try to use T::AccountId in this context I get this error:

error[E0277]: the trait bound `<T as frame_system::Config>::AccountId: std::default::Default` is not satisfied
   --> pallets/template/src/lib.rs:243:12
    |
243 |     #[pallet::storage]
    |               ^^^^^^^ the trait `std::default::Default` is not implemented for `<T as frame_system::Config>::AccountId`

It makes sense that T::AccountId doesn't have a default since it wouldn't make sense for it to have one. However I understand that a map in this context probably can't take a AccountId without having a default value for it. Is there a way for me to get around this so I can still map the AccountIds?

1 Answer 1

4

As you specify to it to use a ValueQuery, a default has to exist.
The solution is to use an OptionQuery instead which then returns None:

#[pallet::storage]
#[pallet::getter(fn declaration_map)]
pub(super) type Declarations<T: Config> = StorageMap
    <
        _,
        Blake2_128Concat,
        T::AccountId,
        Amendment<T::AccountId>,
        OptionQuery // <- HERE
    >;

A ResultQuery should probably also work.

2
  • 2
    You can also use ValueQuery, and then define what default value you want with OnEmpty.
    – Shawn Tabrizi
    Commented Jun 16, 2022 at 17:40
  • This worked perfectly thank you! Commented Jun 16, 2022 at 17: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.