Skip to main content
Version: Firesquid

Squid Substrate typegen

Installation

The Squid substrate typegen tool is part of Subsquid SDK and is used for generating TypeScript interface classes for Substrate events and calls.

Options for squid-substrate-typegen command

ArgumentDescriptionRequired
-h or --helpdisplay help for command
configJSON file with optionsyes

Usage

npx squid-substrate-typegen typegen.json

The typegen.json config file has the following structure. The specVersions fields is either

{
"outDir": "src/types",
"specVersions": "kusamaVersions.jsonl",
// see types bundle section below
"events": [
// list of events to generate
"Balances.Transfer"
],
"calls": [
// list of calls to generate
"timestamp.set"
],
"storage": [
"System.Account"
// Qualified storage item name: "${Prefix}.${item}"
],
"constants": [
// Qualified name for the constants to be generated: "${Prefix}.${item}"
]
}

To generate all items defined by the runtime, set any of events, calls, storage or constants to true, e.g.

{
"outDir": "src/types",
"specVersions": "kusamaVersions.jsonl",
// generate facade classes for all runtime constants
"constants": true
}

TypeScript class wrappers

The result of the typegen command can be found in the specified outDir (src/types by default). The wrapper classes for event, calls, storage and constants are generated in events.ts, calls.ts, storage.ts and constants.ts respectively.

Examples

Below are sample code snippets showcasing how the classes generated by typegen are used in the mappings.

Events and Calls

import {BalancesTransferEvent} from "./types/events"
import {BalancesForceTransferCall} from "./types/calls"
// ...
processor.run(new TypeormDatabase(), async ctx => {
for (let block of ctx.blocks) {
for (let item of block.items) {
if (item.kind == "event" && item.name == "Balances.Transfer") {
// decode the event `Balances.force_transfer`
let e = new BalancesTransferEvent(ctx, item.event)
if (e.isV1020) {
let [from, to, amount,] = e.asV1020
}
// ... decode all runtime versions similarly with e.isVXXX/e.asVXXX
}
if (item.kind == "call" && item.name == "Balances.force_transfer") {
// decode the call `Balances.force_transfer`
let c = new BalancesForceTransferCall(ctx, item.call)
if (c.isV1020) {
let {source, dest, value} = c.asV1020
}
// ... decode all runtime versions similarly with c.isVXXX/c.asVXXX
}
}
})

Storage

See Storage queries

Constants

import {SystemMaximumBlockLengthConstant} from "./types/constants"
// ...
processor.run(new TypeormDatabase(), async ctx => {
// read System.MaximumBlockLength length
let c = new SystemMaximumBlockLengthConstant(ctx)

ctx.log.info(`System.MaximumBlockLength (runtime version V1): ${c.asV1()}`)
})