Search code examples
rustsubstrate

Substrate Mutate throws an error when updating a member


I Have a substrate pallet implementation as follows

#[pallet::storage]
#[pallet::getter(fn get_payload)]
pub(super) type Payload<T: Config> = StorageMap<
    _,
    Blake2_128Concat,
    Vec<u8>,
    Messages<T>
>;

#[derive(Clone, Decode, Encode, Eq, PartialEq, Debug)]
pub struct Messages<T: Config> {

    pub meta: Option<Vec<u8>>,

    pub header: Option<Vec<u8>>,

}

#[pallet::weight(0)]
pub fn update(
    origin: OriginFor<T>,
    key: Vec<u8>,
    header: Vec<u8>,

) -> DispatchResultWithPostInfo {
    let origin_account = ensure_signed(origin)?;
    let mut payload = Payload::<T>::get(key.clone());
    match payload {
        Some(mut val) => {
            println!("{:?}",val.header);
            <Payload<T>>::mutate(val, val.header=header)
        },
        None => println!("Not found")
    }

and in the update function, when I tried to mutate it throws the following error

the trait `EncodeLike<Vec<u8>>` is not implemented for `Messages<T>`

The struct which corresponds to the Messages derived from Encode and Decode. Documentation is unclear on how to fix this. How to fix this issue?


Solution

  • The error message is correct, Messages<T> doesn't encode like a Vec<u8> and somewhere you try to use Mesages<T> where it only takes something that encodes like a Vec<u8>.

    In <Payload<T>>::mutate call: the first argument must be something that encode like Vec<u8> and you give a variable of type Messages<T, it is an error. instead you probably wants to write <Payload<T>>::mutate(key.clone, ..)