> ## 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.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.sqd.dev/feedback

```json
{
  "path": "/en/sdk/squid-sdk/solana-indexing/sdk/typegen",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Typegen

> Solana typegen tool for Squid SDK — generates type-safe instruction and event decoders from Anchor IDL files for indexing Solana programs.

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 and generate types:

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

## Usage

The generated utility modules have three intended uses:

1. Constants: Solana instruction discriminators:

   ```ts theme={"system"}
   // 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();
   ```

2. Decoding of Solana instructions

   ```ts theme={"system"}
   // 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 { accounts, data } = decodedSwap
       }
     }
   }
   ```

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']
       })
     }
   })
   ```
