Search code examples
substratepolkadotpolkadot-js

Use custom types (Substrate FRAME pallet + Polkadot/Substrate frontend)


Scenario: You are developing a FRAME pallet for a custom Substrate node that uses custom types within Storage or Events. To interact with your custom Substrate node, you use the Polkadot/Substrate frontend

Problem:

  1. When you query the storage that contains your custom type, the frontend returns "unknown" as the value
  2. When you submit an extrinsic to be included in a block that should lead to the emission of an event using the custom type, the Polkadot/Substrate frontend seems to get stuck while stating that the extrinsic is "ready" for inclusion in a block. This is not the case though, if you inspect the block the extrinsic should be included in, you can see the following error message:

Unable to retrieve the specified block details. createType(Vec):: Struct: failed on 'data':: Cannot construct unknown type YOUR_CUSTOM_TYPE


Solution

  • Solution: You have to specify your custom types in the Polkadot/Substrate frontend. To do so, select "Settings" -> "Developer" in the top menu bar:

    Settings

    Below you can see a text field titled "Additional types as a JSON file". In this text field you have to enter your custom types for the frontend to be able to interpret those correctly. In the top right corner, right below the version numbers, a big white question mark in a gray circle should be visible. By clicking on this question mark, an help dialog slides in.

    Example configuration: Let's take the pallet I learned this lesson with as an example. In the highlighted lines, you see the custom enum "States":

    pub enum States {
        Propose,
        VotePropose,
        Concern,
        VoteConcern,
        VoteCouncil,
    }
    

    Using the help dialog (the white question mark), it turns out that the correct configuration JSON snippet looks like this:

    "States": {
        "_enum": [
            "Propose",
            "VotePropose",
            "Concern",
            "VoteConcern",
            "VoteCouncil"
        ]
    }