0

I'm trying to retrieve all values in my StorageMap defined as such in my pallet:

#[pallet::storage]
#[pallet::getter(fn submitted_tasks)]
pub type SubmittedTasks<T: Config> =
    StorageMap<_, Blake2_128Concat, B3Hash, Task<T>, OptionQuery>;

from my runtime api (in runtime/src/lib.rs), currently defined as such and not working:

impl pallet_submitter_runtime_api::SubmitterRuntimeApi<Block> for Runtime {
    fn get_submitted_tasks() -> Vec<Vec<u8>> {
        // This macro generated getter only allows me to query one item.
        SubmitterModule::submitted_tasks();

        // This does not compile due to associated item not found
        SubmitterModule::SubmittedTasks.iter_values().collect();
        
        vec![]
    }
}

Now from my understanding, if i use the macro generated getter, it's only possible to query one value from the StorageMap, however I would like to iterate over all values to collect them. How can I achieve this?

Note: the return type will be the vector of encoded Tasks for simplicity

1 Answer 1

1

This should work. I added a comment above the line in the code to explain what happens there.

impl pallet_submitter_runtime_api::SubmitterRuntimeApi<Block> for Runtime {
    fn get_submitted_tasks() -> Vec<Vec<u8>> {
        // This macro generated getter only allows me to query one item.
        SubmitterModule::submitted_tasks();

        // The return type is Vec<Vec<u8>>, but your StorageMap stores 
        // as value a `Task<T>`. So, you need to first encode this value
        // and then you can collect into a vector. You probably just missed
        // the explicit type to make Rust happy.
        pallet_submitter::SubmittedTasks::<Runtime>::iter_values().map(|v| v.encode()).collect::<Vec<Vec<_>>>();
    }
}
3
  • Yeah, the encoding part is a good tip, however it still complains: "associated item not found in pallet_submitter::Pallet<Runtime>". Referring to the SubmittedTasks, Which makes sense as the StorageMap is technically defined inside the pallet module and not associated with the Pallet struct Apr 8, 2022 at 11:03
  • Ahh yeah that makes sense. I edited my answer. Now it should work.
    – bkchr
    Apr 8, 2022 at 11:10
  • That is the syntax I was looking for, thanks. Now it works Apr 8, 2022 at 11:34

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.