1

I am building a Substrate pallet where I have a storage item defined as a StorageDoubleMap:

#[pallet::storage]
#[pallet::unbounded]
#[pallet::getter(fn labels)]
pub(super) type Labels<T: Config> = StorageDoubleMap<
    _,
    Twox64Concat,
    HashId<T>,
    Twox64Concat,
    AccountOf<T>,
    LabelsDetail<T>,
    OptionQuery,
>;

I need to retrieve data from this storage map by using only the first key (label_id), and then perform some operations like inserting it into another storage item and emitting an event.

I've tried using the iter_prefix method, but I'm having trouble handling the nested Option types it returns. Here's my function:

  pub fn verify_labels(label_id: &HashId<T>) -> DispatchResult {
    let label_info = Labels::<T>::iter_prefix(&label_id).next().ok_or("Label not found")?;
    match label_info {
        Some(value) => {
            VerifiedLabels::<T>::insert(&label_id, &value.owner, &value);
        },
        None => {},
    };
    Self::deposit_event(Event::LabelsVeryfied {
        label_id: label_info.label_id,
        owner: label_info.owner.clone(),
    });
    Ok(())
}

I'm getting a type mismatch error:

mismatched types expected tuple (::AccountId, LabelInfo<::AccountId, ::Hash>) found enum std::option::Option<_>

How can I correctly handle the nested Option types to fix this error?

1 Answer 1

1

You have already unwrapped label_info with .ok_or():

Transforms the Option into a Result<T, E>.

Then followed by ? (docs), which unwraps Ok() or errors when finding Err(). So now you have your tuple which is stated in the error message:

expected tuple (::AccountId, LabelInfo<::AccountId, ::Hash>)

Try:

let label_info = Labels::<T>::iter_prefix(&label_id).next().ok_or("Label not found")?;
VerifiedLabels::<T>::insert(&label_id, &label_info.owner, &label_info);
Self::deposit_event(Event::LabelsVeryfied {
    label_id: label_info.label_id,
    owner: label_info.owner.clone(),
});
Ok(())
2
  • it show me this error the trait bound &(<T as frame_system::Config>::AccountId, LabelInfo<<T as frame_system::Config>::AccountId, <T as frame_system::Config>::Hash>): EncodeLike<LabelInfo<<T as frame_system::Config>::AccountId, <T as frame_system::Config>::Hash>> is not satisfied the following other types implement trait EncodeLike<T>: <() as EncodeLike>
    – Mr Coder
    Oct 5, 2023 at 11:35
  • Does this answer your question: substrate.stackexchange.com/questions/8431/…? Oct 5, 2023 at 13:03

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.