Compile error in Substrate smart contract 'the trait bound ink_storage::Vec<...>: WrapperTypeEncode is not satisfied'

Viewed 166

I'm a newbie on Substrate and blockchain development. Currently, I'm writing a smart contract using Substrate with ink!. I've defined a custom type (struct Person in the code) and created the vector of this type in the contract's storage.

I then create contract's functions for working with that vector data. But the code fails to compile. There are errors with get_person_list() function. The first one is 'the trait bound ink_storage::Vec<Person>: WrapperTypeEncode is not satisfied'.

However, if I remove this function, the code can be compiled successfully. How can I solve this compile error? thanks.

#![cfg_attr(not(feature = "std"), no_std)]

use ink_lang as ink;

#[ink::contract]
mod test {
    use ink_prelude::string::String;
    use ink_storage::collections::Vec;
    use ink_storage::traits::{
        SpreadLayout,
        PackedLayout,
    };
    use scale::{Encode, Decode};

    #[derive(Debug, Clone, Encode, Decode, SpreadLayout, PackedLayout)]
    #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
    pub struct Person {
        name: String,
        age: u32,
    }

    #[ink(storage)]
    pub struct Test {
        person_list: Vec<Person>,
    }

    impl Test {
        #[ink(constructor)]
        pub fn new() -> Self {
            Self { person_list: Vec::new() }
        }

        #[ink(message)]
        pub fn add_person(&mut self, name: String, age: u32) {
            self.person_list.push(Person { name, age });
        }

        #[ink(message)]
        pub fn get_person(&self, index: u32) -> Option<Person> {
            self.person_list.get(index).cloned()
        }

        #[ink(message)]
        pub fn person_list_count(&self) -> u32 {
            self.person_list.len() as u32
        }

        #[ink(message)]
        pub fn get_person_list(&self) -> Vec<Person> {
            self.person_list.clone()
        }
    }
}
1 Answers

I think it will work if you use use ink_prelude::vec::Vec; instead of use ink_storage::collections::Vec;.

Related