Search code examples
blockchainsubstrate

How do I define a custom type with the oo7 Substrate library?


I am using the Substrate Bonds library (oo7) to generate custom UI for my custom Substrate Runtime Module.

To support my custom module in the Substrate UI, I need to define a custom type. How do I do that?


Solution

  • The oo7 Substrate library exposes the addCodecTransform() function which enables you to define custom types which you can then use within the UI.

    For example, given this structure defined in your module:

    #[derive(Encode, Decode, Default, Clone, PartialEq)]
    #[cfg_attr(feature = "std", derive(Debug))]
    pub struct Kitty<Hash, Balance> {
        id: Hash,
        dna: Hash,
        price: Balance,
        gen: u64,
    }
    

    You could make the following JavaScript call:

    addCodecTransform('Kitty<Hash,Balance>', { 
        id: 'Hash',
        dna: 'Hash',
        price: 'Balance',
        gen: 'u64'
    });
    

    If you add this do your applications constructor() function, you can make sure that it is called before your dependent React functions require it.