在 Polkadot Apps 开发人员选项卡中,您如何编码 Rust 元组?

In Polkadot Apps developer tab how do you encode a Rust tuple?

有几个示例包含在运行时模块中声明的自定义 Rust 类型,可以应用 Polkadot 应用程序接口 here。但是我不确定如何编码元组。对于任何其他计划在其代码和运行时中使用元组的人来说也是如此 API 用于 polkadot-js。

这是它在 Substrate Runtime 模块中的声明方式:

// tuple (struct)
pub type Code = u16;
pub type Type = u16;
pub struct Tuple(Code, Type);

// Complex struct using tuple
pub struct Record<T::AccountId,Tuple> {
    pub address: T::AccountId,
    pub tuple_values: Tuple,
}

// Storage
Record get(record):
        map T::AccountId => Option<Record<T::AccountId,Tuple>>;

我假设我会在 Polkadot 应用程序设置的 Developer 选项卡中添加类似这样的内容,但我不知道这是否是正确的语法。

{
    "Code": "u16",
    "Type": "u16",
    "Tuple": ["Code", "Type"],
    "Record": {
        "address": "AccountId",
        "tuple_values": "Tuple"
    }
}

元组、元组结构和命名结构都以相同的方式编码。来自 https://docs.substrate.io/v3/advanced/scale-codec#tuples :

Tuples A fixed-size series of values, each with a possibly different but predetermined and fixed type. This is simply the concatenation of each encoded value.

For structures, the values are named, but that is irrelevant for the encoding (names are ignored - only order matters).

所以在你的情况下,这样的事情会起作用:

{
    "Code": "u16",
    "Type": "u16",
    "Tuple": {
        "bogus_name_1": "Code",
        "bogus_name_2": "Type"
    },
    "Record": {
        "address": "AccountId",
        "tuple_values": "Tuple"
    }
}