psu
psu

Reputation: 111

Substrate storage declaration

I'm working on a pallet and after switching to the newest node template it stopped building. Here is declaration

    #[derive(Encode, Decode, Clone)]
    pub struct VendorData<T :Config>
    {
        pub vendor : VendorId,
        pub owner_account_id : T::AccountId,
    }
    impl<T:Config> Default for VendorData<T> {
        fn default() -> Self {
            Self {
                vendor : Default::default(),
                owner_account_id : Default::default(),
            }
        }
    }

    #[pallet::storage]
    pub(super) type Vendors<T: Config> = StorageMap<_, Blake2_128Concat, VendorId, VendorData<T>, ValueQuery>;

The cargo build error:

error[E0277]: the trait bound `VendorData<T>: TypeInfo` is not satisfied
...
#[pallet::storage]
          ^^^^^^^ the trait `TypeInfo` is not implemented for `VendorData<T>`

I've tried declaring struct in a different ways, for example by adding TypeInfo:

#[derive(Encode, Decode, Default, Eq, PartialEq, TypeInfo)]

but every time another errors arise, like this:

error: cannot find macro `format` in this scope
    --> /home/psu/.cargo/git/checkouts/substrate-7e08433d4c370a21/352c46a/primitives/consensus/aura/src/lib.rs:50:3
     |
  50 |         app_crypto!(ed25519, AURA);
     |         ^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: consider importing this macro:
             scale_info::prelude::format
     = note: this error originates in the macro `$crate::app_crypto_public_common_if_std` (in Nightly builds, run with -Z macro-backtrace for more info)

There is another storage item which does not cause errors

    pub type Cid = Vec<u8>;
    #[pallet::storage]
    pub(super) type VendorModels<T: Config> = StorageMap<_, Blake2_128Concat, VendorId, Vec<Cid>, ValueQuery>;

So it seems it's only about storing VendorData struct. Please assist, after hours of experiments and googling I'm lost

UPDATE: after adding TypeInfo to all storage structs declaration the pallet is compiled but a lot of errors like below arised:

error: cannot find macro `format` in this scope
error: cannot find macro `vec` in this scope

Upvotes: 1

Views: 442

Answers (1)

apopiak
apopiak

Reputation: 454

You will need to either make sure that all your generic types implement TypeInfo or skip the types explicitly. Based on this example code in Substrate you could write:

#[derive(Encode, Decode, Clone, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct VendorData<T :Config>
{
    pub vendor : VendorId,
    pub owner_account_id : T::AccountId,
}

Or alternatively the following should work:

#[derive(Encode, Decode, Clone, TypeInfo)]
pub struct VendorData<AccountId>
{
    pub vendor : VendorId,
    pub owner_account_id : AccountId,
}

Upvotes: 2

Related Questions