Hydra Typegen
A tool for generating type-safe TypeScript classes for runtime events and extrinsic calls
Motivation
Hydra Typegen is a code generation tool for creating Typescript types for substrate events and extrinsics. Its primary use-case is to provide type-safe interfaces for Hydra mappings. For example, once a typescript class for the Balances.Transfer
event is generated, a mapping can look like
Quickstart
A minimal example for generating classes for the Balances.Transfer
and Treasury.Deposit
events in Kusama:
It is also possible to run hydra-typegen
against a YAML config file
Typegen config
Typegen config file has the following structure:
Field
Type
Description
metadata.source
string
Where typegen will source node metadata. Can either be a ws endpoint or a path to a static json file with the same content as the result of state_getMetadata
RPC call
metadata.blockHash
string
(optional) If metadata.source
is a WS endpoint, hash of the block from which metadata will be sourced, in hex format
events
[string]
A list of events for which TS classes will be generated, in the format <section>.<name>
calls
[string]
A list of extrinsics (calls) for which TS classes will be generated, in the format <section>.<method>
outDir
string
Root directory for the generated classes
strict
boolean
Default: false
If true, the event/extrinsic constructor throws an error if the raw data does not match the format in metadata (e.g. due to a runtime upgrade).
customTypes.lib
string
(optional) Import path for custom types that will be used in the generated sources
customTypes.typedefs
string
(optional) Path to a JSON file with custom type definitions, as expected by polkadot.js createApi
method
The config file typegen.yml
can look like this:
Custom types
Hydra Typegen supports custom substrate types via the --typedefs
flag. The provided .json
file should include type definitions for the arguments and parameters of the events and extrinsics to be generated. The type definitions file is copied to the generated sources.
In the config file, place the definition into the customTypes
section. It assumes that all the custom runtime types are already available for import from a library, so that e.g. the generated import statement
is correctly resolved.
Note, that when used in the mappings, the library with custom types (here my/types/library
) must be added as a dependency for the mappings module in mappings/package.json
Commands
hydra-typegen help [COMMAND]
hydra-typegen help [COMMAND]
display help for hydra-typegen
See code: @oclif/plugin-help
hydra-typegen typegen [CONFIG]
hydra-typegen typegen [CONFIG]
Generate Typescript classes for the Substrate events
See code: src/commands/typegen/index.ts
A full sample Hydra project can be found here
Last updated