npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@joystream/hydra-typegen

v5.0.0-alpha.7

Published

"Hydra-cli pluging for generating typed events"

Downloads

445

Readme


description: >- A tool for generating type-safe TypeScript classes for runtime events and extrinsic calls

Hydra Typegen

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

export async function balancesTransfer({
  event
}: EventContext & StoreContext ) {
  const [from, to, value] = new Balances.TransferEvent(event).params
  const { dest, value } = new Balances.TransferCall(event).args
  ...
}

Quickstart

A minimal example for generating classes for the Balances.Transfer and Treasury.Deposit events in Kusama:

hydra-typegen typegen --metadata wss://kusama-rpc.polkadot.io Balances.Transfer

It is also possible to run hydra-typegen against a YAML config file

hydra-typegen typegen typegen.yml --debug

Typegen config

Typegen config file has the following structure:

The config file typegen.yml can look like this:

# Typegen will pull the metadata from Kusama at block with the given hash
metadata:
  source: wss://kusama-rpc.polkadot.io 
  blockHash: '0x45eb7ddd324361adadd4f8cfafadbfb7e0a26393a70a70e5bee6204fc46af62e'
# events and calls for which the typescript types will be generated
events:
  - Balances.Transfer
calls:
  - Balances.transfer
outDir: ./generated

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

import { MyCustomRuntimeClass } from 'my/types/library'

is correctly resolved.

...
customTypes: 
    lib: 'my/types/library',
    typedefs: my-types-json,

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]

display help for hydra-typegen

USAGE
  $ hydra-typegen help [COMMAND]

ARGUMENTS
  COMMAND  command to show help for

OPTIONS
  --all  see all commands in CLI

See code: @oclif/plugin-help

hydra-typegen typegen [CONFIG]

Generate Typescript classes for the Substrate events

USAGE
  $ hydra-typegen typegen [CONFIG]

ARGUMENTS
  CONFIG  Path to YML config file. Overrides the flag options

OPTIONS
  -c, --calls=calls          Comma-separated list of substrate calls in the format <module>.<name>
  -d, --debug                Output debug info
  -e, --events=events        Comma-separated list of substrate events in the formation <module>.<name>

  -h, --blockHash=blockHash  Hash of the block from which the metadata will be fetched. Only applied if metadata is
                             pulled via an RPC call

  -i, --typelib=typelib      A JavaScript module from which the custom types should be imported, e.g.
                             '@joystream/types/augment'

  -m, --metadata=metadata    [default: metadata.json] Chain metadata source. If starts with ws:// or wss:// the metadata
                             is pulled by an RPC call to the provided endpoint. Otherwise a relative path to a json file
                             matching the RPC call response is expected

  -o, --outDir=outDir        [default: generated/types] A relative path the root folder where the generated files will
                             be generated

  -s, --[no-]strict          Strict mode. If on, the generated code throws an error if the input event argument types
                             don't much the metadata definiton

  -t, --typedefs=typedefs    A relative path to a file with JSON definitions for custom types used by the chain

See code: src/commands/typegen/index.ts

A full sample Hydra project can be found here