Generate type-safe Solana instruction and event decoders from Anchor IDL files.
A typegen is a tool for generating utility code for technology-specific operations such as decoding.Solana typegen:
decodes instruction and log message data based on the IDL
exposes useful constants such as program IDs and instruction discriminators
provides functions that simplify selecting data items based on accounts
Install it with
npm install @subsquid/solana-typegen
Replace npx squid-solana-typegen with npx @subsquid/solana-typegen if you don’t want to install the package.
The squid-solana-typegen tool generates TypeScript facades for Solana instructions and logs. It takes JSON IDLs as inputs. The IDLs can be specified in three ways:
as a plain JSON file(s):
npx squid-solana-typegen src/abi whirlpool.json
If you use this option, you can also place your JSON IDLs to the idl folder and run
npx squid-solana-typegen src/abi ./idl/*
load IDL from a Solana node or the SolanaFM registry and generate types:
// generated by squid-solana-typegenimport * as whirlpool from "./abi/whirlpool";const dataSource = new DataSourceBuilder() .setPortal("https://portal.sqd.dev/datasets/solana-mainnet") .addInstruction({ where: { programId: [whirlpool.programId], d8: [whirlpool.instructions.swap.d8] } }) .build();
Decoding of Solana instructions
// generated by squid-solana-typegenimport * as whirlpool from "./abi/whirlpool";import { augmentBlock } from "@subsquid/solana-objects";// ...// the .d8 getter below requires augmented block itemslet blocks = ctx.blocks.map(augmentBlock)for (let block of blocks) { for (let ins of block.instructions) { if (ins.programId === whirlpool.programId && ins.d8 === whirlpool.instructions.swap.d8) { let decodedSwap = whirlpool.instructions.swap.decode(ins) let { accounts, data } = decodedSwap } }}
decode() reads the data and accounts fields of the instruction item — request both with setFields().
Simplifying the account-specific data requests
.addInstruction({ where: { programId: [whirlpool.programId], d8: [whirlpool.instructions.swap.d8], // select instructions for the USDC-SOL pair only ...whirlpool.instructions.swap.accountSelection({ whirlpool: ['7qbRF6YsyGuLUVs6Y1q64bdVrfe4ZcUUz1JRdoVNUJnm'] }) }})
The generated instruction descriptors also work with getInstructionData()
and getInstructionDescriptor() from @subsquid/solana-stream.