> ## Documentation Index
> Fetch the complete documentation index at: https://docs.sqd.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana typegen

> 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

```bash theme={"system"}
npm install @subsquid/solana-typegen
```

<Tip>
  Replace `npx squid-solana-typegen` with `npx @subsquid/solana-typegen` if you don't want to install the package.
</Tip>

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:

1. as a plain JSON file(s):

   ```bash theme={"system"}
   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

   ```bash theme={"system"}
   npx squid-solana-typegen src/abi ./idl/*
   ```

2. load IDL from a Solana node or the SolanaFM registry and generate types:

   ```bash theme={"system"}
   npx squid-solana-typegen src/abi whirLbMiicVdio4qvUfM5KAg6Ct8VwpYzGff3uctyCc#whirlpool
   ```

   Use `--solana-rpc-endpoint <url>` to choose the RPC endpoint for onchain IDL
   lookup.

3. load an IDL from an HTTP(S) URL:

   ```bash theme={"system"}
   npx squid-solana-typegen src/abi https://example.com/whirlpool.json#whirlpool
   ```

Pass `--clean` to delete the output directory before generation.

## Usage

The generated utility modules have three intended uses:

1. Constants: Solana instruction discriminators for [`addInstruction()` data requests](./solana-stream/instructions):

   ```ts theme={"system"}
   // generated by squid-solana-typegen
   import * 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();
   ```

2. Decoding of Solana instructions

   ```ts theme={"system"}
   // generated by squid-solana-typegen
   import * as whirlpool from "./abi/whirlpool";
   import { augmentBlock } from "@subsquid/solana-objects";

   // ...

   // the .d8 getter below requires augmented block items
   let 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()`](./solana-stream/field-selection).

3. Simplifying the account-specific data requests

   ```ts theme={"system"}
   .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`.
