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}"
]
}

TypeScript class wrappers

The result of the typegen command can be found under the src/types folder. In the case of the squid template repository, since it only cares about the Balances.Transfer Event, the typegen command generated an events.ts file that contains a class, named BalancesTransferEvent and here it is:

export class BalancesTransferEvent {

// constructors

/**
* Transfer succeeded (from, to, value, fees).
*/
get isV1020(): boolean {
return this.ctx._chain.getEventHash(
'balances.Transfer') === 'e1ceec345fa4674275d2608b64d810ecec8e9c26719985db4998568cfcafa72b'
}

/**
* Transfer succeeded (from, to, value, fees).
*/
get asV1020(): [Uint8Array, Uint8Array, bigint, bigint] {
assert(this.isV1020)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

/**
* Transfer succeeded (from, to, value).
*/
get isV1050(): boolean {
return this.ctx._chain.getEventHash(
'balances.Transfer') === '2082574713e816229f596f97b58d3debbdea4b002607df469a619e037cc11120'
}

/**
* Transfer succeeded (from, to, value).
*/
get asV1050(): [Uint8Array, Uint8Array, bigint] {
assert(this.isV1050)
return this.ctx._chain.decodeEvent(this.ctx.event)
}

}

This manages different runtime versions, including the starting hash for each and instructions for how to process ( decode) the event itself.

Now the BalanceTransferEvent,can be used to handle events such as this:

``

Where, upon processing an event, its metadata version is checked, and the metadata is extracted accordingly, making
things a lot easier.