0

I have a StorageNMap

    #[pallet::storage]
    #[pallet::getter(fn resources)]
    /// Stores resource info
    pub type Resources<T: Config> = StorageNMap<
        _,
        (
            NMapKey<Blake2_128Concat, CollectionId>,
            NMapKey<Blake2_128Concat, NftId>,
            NMapKey<Blake2_128Concat, ResourceId>,
        ),
        ResourceOf<T, T::PartsLimit>,
        OptionQuery,
    >;

I have a function that iterates through (CollectionId, NftId) with iter_prefix_values where I need to find a matching condition. But once I match, I need the ResourceId itself (not the contents of the Resource). The Id isn't stored in the ResourceOf/Resource struct, though I could add it (though I'd rather not if I can get this key otherwise).

Here's the basic iteration matching

let resources_matching_base_iter =
    pallet_rmrk_core::Resources::<T>::iter_prefix_values((item_collection_id, item_nft_id));

for resource in resources_matching_base_iter {
    if <logic> {
        <value_i_need> = resource.id;
    }
}

This only works if I put the .id in the struct, which I could avoid if I could figure out how to get the keys itself from the iter_prefix_values. Any ideas how, or if this would be possible?

1 Answer 1

1

StorageNMap also exposes a bunch of APIs which provider an iterator over the key and value under the trait IterableStorageNMap:

https://crates.parity.io/frame_support/storage/trait.IterableStorageNMap.html#tymethod.iter_prefix

In general, you probably should not be doing any kind of Storage Map iteration on chain. It is EXTREMELY expensive, and probably will end up being an attack on your chain, where the map contains many items, and a user triggers you to iterate over that large map.

Instead, if you would do any kind of iteration like this, you probably want to store things as a BoundedVec. You can learn more about those tradeoffs here:

https://youtu.be/9S8rmW8LD5o?t=1266

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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