substratepolkadot

the trait `parity_scale_codec::Encode` is not implemented for `std::collections::BTreeMap<u128, T>`


Here is my struct:

#[derive(PartialEq, Eq, PartialOrd, Ord, Default, Clone, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SortitionSumTree<AccountId> {
    pub k: u128,
    pub stack: Vec<u128>,
    pub nodes: Vec<u128>,
    pub ids_to_tree_indexes: BTreeMap<AccountId, u128>,
    pub node_indexes_to_ids: BTreeMap<u128, AccountId>,
}

My storage:

#[pallet::storage]
#[pallet::getter(fn sortition_sum_trees)]
pub type SortitionSumTrees<T> = StorageMap<_, Blake2_128Concat, Vec<u8>, SortitionSumTree<T>>;

But its giving error:

the trait parity_scale_codec::Encode is not implemented for std::collections::BTreeMap<u128, T>


Solution

  • You need to use this:

    #[pallet::getter(fn sortition_sum_trees)]
    pub type SortitionSumTrees<T> = StorageMap<
      _, 
      Blake2_128Concat,
      Vec<u8>,
      SortitionSumTree<T::AccountId>
    >;
    

    Make sure to use T::AccountId in SortitionSumTree<T::AccountId>.