【问题标题】:Compile error in Substrate smart contract 'the trait bound ink_storage::Vec<...>: WrapperTypeEncode is not satisfied'Substrate 智能合约中的编译错误 \'the trait bound ink_storage::Vec<...>: WrapperTypeEncode is not满足\'
【发布时间】:2022-06-08 07:16:47
【问题描述】:

我是 Substrate 和区块链开发的新手。目前,我正在使用 Substrate with ink! 编写智能合约!我已经定义了一个自定义类型(代码中的struct Person)并在合约的存储中创建了这种类型的向量。

然后我创建合约函数来处理该向量数据。但是代码无法编译。 get_person_list() 函数存在错误。第一个是\'the trait bound ink_storage::Vec&lt;Person&gt;: WrapperTypeEncode is not satisfied\'

但是,如果我去掉这个函数,代码就可以编译成功了。我该如何解决这个编译错误?谢谢。

#![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()
        }
    }
}

    标签: substrate


    【解决方案1】:

    我认为如果您使用use ink_prelude::vec::Vec; 而不是use ink_storage::collections::Vec;,它会起作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-04
      • 1970-01-01
      • 1970-01-01
      • 2017-05-17
      • 2020-03-05
      • 1970-01-01
      • 2016-09-10
      • 2022-08-04
      相关资源
      最近更新 更多