substratepolkadot-js

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


There are several examples for including custom Rust types declared in a runtime module that can be applied Polkadot Apps interface here. However I am not sure how to encode tuples. This would also be the case for anyone else planning on using Tuples in their code and runtime API for use in polkadot-js.

Here's how it is declared in a Substrate Runtime module:

// 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>>;

I am presuming I would add something like this to the Developer tab in the Polkadot Apps settings, but I don't know if this is the correct syntax to use.

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

Solution

  • Tuples, Tuple structs, and Named structs are all encoded the same way. From 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).

    So in your case something like this will work:

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