Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • intin SCALE integers can be encoded as fixed-width integer and compact integer. Compact integer itself has 4 modes. So generally we have 5 ways to serde integer (1 fixed-width + 4 modes of compact integers) 

    Code Block
    languagebash
    {
    	"type": "int",
    	"mode": "CompactTwoByte" //possible values FixedWidth, CompactSingleByte, CompactTwoByte, CompactFourByte, CompactBigInteger,
    }


  • bool: no tricks here 


    Code Block
    languagebash
    {
    	"type": "bool"
    }


2. Built-in containers 

  • [Option / Result / Vec / typle : basically we are interested in only inner values of the container


    Code Block
    [
    {
        "type": "Option",
        "item_type": {
            "type" : "bool"
        }
    
    },
    {
        "type": "Result",
        "item_type": {
            "type": "int",
            "mode": "CompactFourByte"
        }
    },
    {
        "type": "Vec",
        "item_type": {
            "type": "bool"
        }
    },
    {
        "type": "tuple",
        "items": [
            {
                "type": "int",
                "mode": "CompactFourByte"
            },
            {
                "type": "Option",
                "item_type": {
                    "type" : "bool"
                }
            
            }
        ]
    }
    ]


...

  • Structures: in structures declare definitions inline for built-in types and declare a reference for custom types 


    Code Block
    languagebash
    {
        "type": "struct",
        "name": "Foo",
        "codec": "SCALE", //possible values SCALE or JSON
        "properties": [
            {
                "name": "enabled", //optional name
                "definition": {
                    "type": "bool"
                }
            },
            {
                "name": "id", //optional name
                "definition": {
                    "type": "struct", //reference to other struct
                    "struct_name": "Id"
                }
            },
            {
                "name": "counter", //optional name
                "definition": {
                    "type": "int",
                    "mode": "FixedWidth"
                }
            }
        ]
    }


  • Enums: similar to Structures but also every variant of enum has a discriminator that used in Scale. By default based on 0, but can be overriden by attributes


    Code Block
    {
        "type": "enum",
        "name": "Bar",
        "codec": "SCALE", //possible values SCALE or JSON
        "variants": [
            {
                "name": "FirstVariant",
                "discriminator": 1,
                "properties": {
                    ...
                }
            },
            {
                "name": "SecondVariant",
                "discriminator": 2,
                "properties": {
                    ...
                }
            }
        ]
    }



Additional Information

  1. Scale codec description in Substrate docs [https://substrate.dev/docs/en/knowledgebase/advanced/codec]
  2. Scale codec Github page [https://github.com/paritytech/parity-scale-codec]