Generate type-safe interfaces
Learn how to leverage Subsquid SDK automated tools to create TypeScript classes for Substrate Events, calls and Storage items.
The intent of this page is to showcase the necessary commands, parameters, and configuration. Head over to the page explaining Typegen for more clarity on the concepts behind it.
The generation of type-safe wrappers for events and calls is currently a two-step process.

Chain exploration

First, you need to explore the chain to find blocks that introduce a new spec version and fetch corresponding metadata. The squid-substrate-metadata-explorer does exactly that. Here's an example to explore Kusama blockchain:
1
npx squid-substrate-metadata-explorer \
2
--chain wss://kusama-rpc.polkadot.io \
3
--archive https://kusama.indexer.gc.subsquid.io/v4/graphql \
4
--out kusamaVersions.json
Copied!
In the above command, the --archive parameter is optional, but it speeds up the process significantly. The exploration of the Kusama network from scratch, without an archive, takes 20-30 minutes.
You can pass the result of any previous exploration to the --out parameter. In that case, exploration will start from the last known block and thus will take much less time.

Types generation

After chain exploration is complete you can use squid-substrate-typegen to generate required wrappers.
1
npx squid-substrate-typegen typegen.json
Copied!
Where typegen.json config file has the following structure:
1
{
2
"outDir": "src/types",
3
"chainVersions": "kusamaVersions.json", // the result of chain exploration
4
"typesBundle": "kusama", // see types bundle section below
5
"events": [ // list of events to generate
6
"balances.Transfer"
7
],
8
"calls": [ // list of calls to generate
9
"timestamp.set"
10
],
11
"storage": [
12
"System.Account" // <-- Qualified storage item name: "${Prefix}.${item}"
13
]
14
}
Copied!
A type-safe definition for each and every version of the event will be generated. Most of the time, one should be able to infer a normalized interface together with some glue code to make it fit the runtime-specific versions.
Note: the Storage prefix is not transformed to camelCase, but kept "as is".
Note: the Storage prefix is equal to the pallet name in most cases, but it this should not be followed as a rule. You always need to verify the Storage prefix.
The typesBundle key in the typegen.json configuration file can either be:
  • a chain name, all lower-case (e.g. kusama, polkadot, statemint, a full list can be found here)
  • a path to a JSON file with the structure specified here