2

Is there a way for ink! smart contract developers to use custom structs with HashMap fields inside storage struct?

What I want to do

pub struct SampleStruct {
    sample_vec: ink_prelude::vec::Vec<AccountId>,
    sample_hash_map: ink_prelude::collections::HashMap<AccountId, ()>,
}

#[ink(storage)]
pub struct SampleStorage {
    sample_struct: SampleStruct,
}

To do so, SampleStruct needs to implement scale::Encode, scale::Decode, scale_info::TypeInfo etc with derive macro like below.

#[derive(scale::Encode, scale::Decode)]
#[cfg_attr(
    feature = "std",
    derive(scale_info::TypeInfo)
)]
pub struct SampleStruct {
    sample_vec: ink_prelude::vec::Vec<AccountId>, // <- Vec has no issue
    sample_hash_map: ink_prelude::collections::HashMap<AccountId, ()>,
}

However this derive implementation fails at compilation due to error the trait bound 'HashMap<ink_env::AccountId, ()>: WrapperTypeDecode\' is not satisfied the following other types implement trait 'WrapperTypeDecode': Arc<T> Box<T> Rc<T> required because of the requirements on the impl of 'parity_scale_codec::Decode' for 'HashMap<ink_env::AccountId, ()>'.

ink_storage::Mapping<AccountId, ()> is also not doable.


Dependencies

[dependencies]
ink_primitives = { version = "3.0", default-features = false }
ink_metadata = { version = "3.0", default-features = false, features = ["derive"], optional = true }
ink_env = { version = "3.0", default-features = false }
ink_storage = { version = "3.0", default-features = false }
ink_lang = { version = "3.0", default-features = false }
ink_prelude = { version = "3.0", default-features = false }

scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2", default-features = false, features = ["derive"], optional = true }
1
  • 1
    Yes, this does. Thank you so much for sharing!
    – Shunsuke
    Apr 18, 2022 at 3:57

1 Answer 1

0

(Just upgrading popyakin's comment to an answer so that this question has an answer)

How to have a mapping in a custom structure inside an ink! contract?

Not the answer you're looking for? Browse other questions tagged or ask your own question.