如何在 Substrate 上保存字符串值
How can I save string value on Substrate
- 我想在 Substrate 上保存“字符串”值
- 一开始我用的是“Vec”,但是Polkadot JS无法识别
- 我使用“字节”,所以出现以下错误
- 我该如何解决这个问题?
请帮帮我。
- 使用“字节”作为存储字符串的方式是否正确?
- 如果正确,我该如何修正下面的错误?
- 如果不是,正确的用法是什么?
- 如果您有示例代码,请告诉我
#[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(®isterer, 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`
您应该使用 Vec<u8>
来像字符串一样在运行时存储中存储任意字节。
当访问 Polkadot JS 中的 Vec<u8>
字符串时,您应该使用 Text
类型,它会自动处理解析并将此类型转换为常规 UTF-8 文本。
示例:
生锈:
pub struct TestData {
pub name: Vec<u8>,
pub address: Vec<u8>,
}
Polkadot JS 类型定义:
TestData: {
name: 'Text',
address: 'Text'
}
如果这对您有帮助或者您还有其他问题,请告诉我。
- 我想在 Substrate 上保存“字符串”值
- 一开始我用的是“Vec”,但是Polkadot JS无法识别
- 我使用“字节”,所以出现以下错误
- 我该如何解决这个问题?
请帮帮我。
- 使用“字节”作为存储字符串的方式是否正确?
- 如果正确,我该如何修正下面的错误?
- 如果不是,正确的用法是什么?
- 如果您有示例代码,请告诉我
#[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(®isterer, 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`
您应该使用 Vec<u8>
来像字符串一样在运行时存储中存储任意字节。
当访问 Polkadot JS 中的 Vec<u8>
字符串时,您应该使用 Text
类型,它会自动处理解析并将此类型转换为常规 UTF-8 文本。
示例:
生锈:
pub struct TestData {
pub name: Vec<u8>,
pub address: Vec<u8>,
}
Polkadot JS 类型定义:
TestData: {
name: 'Text',
address: 'Text'
}
如果这对您有帮助或者您还有其他问题,请告诉我。