Typegen
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 with
npm install @subsquid/solana-typegen
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 runnpx squid-solana-typegen src/abi ./idl/*
-
load IDL from a Solana node and generate types:
npx squid-solana-typegen src/abi whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc#whirlpool
Usage
The generated utility modules have three intended uses:
-
Constants: Solana instruction discriminators:
// generated by evm-typegen
import * as whirlpool from "./abi/whirlpool";
const dataSource = new DataSourceBuilder()
.setGateway("https://v2.archive.subsquid.io/network/solana-mainnet")
.addInstruction({
where: {
programId: [whirlpool.programId],
d8: [whirlpool.instructions.swap.d8]
}
})
.build(); -
Decoding of Solana instructions
// generated by evm-typegen
import * as whirlpool from "./abi/whirlpool";
// ...
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 decodedAccounts = whirlpool.instructions.swap.decodeAccounts(ins.accounts)
let decodedData = whirlpool.instructions.swap.decodeData(ins.data)
}
}
} -
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']
})
}
})