【问题标题】:How can I save string value on Substrate如何在 Substrate 上保存字符串值
【发布时间】:2020-08-02 23:18:30
【问题描述】:
  1. 我想在 Substrate 上保存“字符串”值
  2. 一开始,我使用“Vec”,但波卡 JS 无法识别它
  3. 我使用“字节”,所以我得到以下错误
  4. 我该如何解决这个问题?

请帮帮我。

  • 使用“字节”作为存储字符串的方式是否正确?
  • 如果正确,如何解决以下错误?
  • 如果不是,使用什么方法正确?
  • 如果您有示例代码,请告诉我
#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[derive(Clone, Eq, PartialEq, Default, Encode, Decode, Hash)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, Debug))]
pub struct TestData<BlockNumber,Bytes> {
    pub name: Bytes,                                                                                                                                                                    
    pub address: Bytes,
}
pub type TestDataOf<T> = TestData<primitives::Bytes>;
--snip--
// This pallet's storage items.
decl_storage! {
    // It is important to update your storage name so that your pallet's
    // storage items are isolated from other pallets.
    // ---------------------------------vvvvvvvvvvvvvv
    trait Store for Module<T: Trait> as TemplateModule {
        pub TestDatas: map hasher(blake2_128_concat) T::AccountId => Option<TestDataOf<T>>;
    }
}
--snip--
decl_module! {
    /// The module declaration.
    pub struct Module<T: Trait> for enum Call where origin: T::Origin {
        // Initializing errors
        // this includes information about your errors in the node's metadata.
        // it is needed only if you are using errors in your pallet
        type Error = Error<T>;

        // Initializing events
        // this is needed only if you are using events in your pallet
        fn deposit_event() = default;


        /// regist public data
        #[weight = 10_000]
        pub fn register_test_data(origin, name:Bytes, address:Bytes) -> dispatch::DispatchResult {
            let registerer = ensure_signed(origin)?;
            let test_data = TestDataOf::<T> {
                name,
                address,
            };
            <TestDatas<T>>::insert(&registerer, test_data);
            Ok(())
        }
    }
}
--snip--

错误是...

the trait `_::_parity_scale_codec::Encode` is not implemented for `TestData<substrate_primitives::Bytes>`
the trait `_::_parity_scale_codec::Decode` is not implemented for `TestData<substrate_primitives::Bytes>`
the trait `_::_parity_scale_codec::WrapperTypeEncode` is not implemented for `substrate_primitives::Bytes`

【问题讨论】:

标签: substrate


【解决方案1】:

您应该使用Vec&lt;u8&gt; 在运行时存储中像字符串一样存储任意字节。

当访问 Polkadot JS 中的字符串 Vec&lt;u8&gt; 时,您应该使用类型 Text,它会自动处理解析并将此类型转换为常规 UTF-8 文本。

例子:

生锈:

pub struct TestData {
    pub name: Vec<u8>,                                                                                                                                                                    
    pub address: Vec<u8>,
}

Polkadot JS 类型定义:

TestData: {
    name: 'Text',
    address: 'Text'
}

如果这有帮助或者您还有其他问题,请告诉我。

【讨论】:

  • 感谢您的回答。我想通过 Polkadot/Substrate Portal(polkadot.js.org/apps/#/extrinsics) 上的“Extrinsics”调用我的函数。如何定义 Polkadot JS 类型定义?
  • @Shin go to Settings > Developer 你会看到一个 JSON 输入,你可以在其中定义你的 JS 类型。请注意,有关于如何使用它的有用信息指南。
  • 再次感谢您的回答。我可以按照你告诉我的方式调用我的函数。非常感谢!这个问题解决了。
  • 在引入scale_info::TypeInfo后,不需要配置JS类型,如果字段不包含ASCII,则不能解释为字符串,只能是十六进制字符串。如何解决?
  • @gfan,是的,您在最新版本中是正确的,我们不需要显式定义任何类型,对于字符串,在您的托盘中使用 Vec 后,您可以直接在 polkadot.js 门户中传递字符串(文本)格式,当您从存储中获取时,您将得到相同的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多