I'm developing a contract that should have some type of a map from AccountId to Balance and I should be able to iterate over it.
Using ink_storage::Mapping I can't iterate over the collection. But using ink_prelude::collections::HashMap gives me the iterator methods I need but it consumes more gas.

So what is the best way to iterate over such a collection?

1 Answer 1


As far as I understand, the ink prelude types are not optimised for contract usage and should be avoided if possible. ink_prelude::collections::HashMap increases the contract size compared with ink_storage::Mapping.

You can implement your own indexes for Mapping although there are plans to create an iterator for it in future.

  • Yeah, I've already come up with the same idea of storing indexes in Vec, thnx
    Commented Jun 8, 2022 at 19:31

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.