Skip to main content

solanaInstructionDecoder

Create a transformer that decodes Solana program instructions.
solanaInstructionDecoder<T>(config: SolanaInstructionDecoderConfig<T>): Transformer
Returns: A Transformer that transforms SolanaPortalData into decoded instructions. Parameters:
  • range: Slot range { from: number | string | 'latest', to?: number } (required)
  • programId: Program address(es) string | string[] (required)
  • instructions: Map of instruction names to ABI instruction objects (required)
  • profiler: Profiler config { id: string } (optional)
  • onError: Error handler (ctx: BatchCtx, error: any) => unknown | Promise<unknown> (optional)
Example:
const transformer = solanaInstructionDecoder({
  range: { from: 200000000, to: 201000000 },
  programId: orcaWhirlpool.programId,
  instructions: {
    swap: orcaWhirlpool.instructions.swap,
    swapV2: orcaWhirlpool.instructions.swapV2,
  },
});
Use @subsquid/solana-typegen to generate typed ABIs for custom programs. This provides type safety and eliminates the need to manually find instruction discriminators. See the Solana SDK typegen documentation for details on generating and using custom ABIs.
solanaInstructionDecoder returns a Transformer that automatically builds the necessary queries for instruction decoding. You donโ€™t need a query builder when using this transformer - it handles query construction internally. However, you still need a portal source.
Use the AbiDecodeInstruction type helper to extract the decoded instruction data type from your ABI for better TypeScript type safety. See the Types & Methods reference for details.

Decoded Instruction Structure

Each decoded instruction contains:
interface DecodedInstruction<D> {
  instruction: D; // Decoded instruction data
  programId: string;
  blockNumber: number; // Slot number
  timestamp: Date;
  transaction: Transaction;
  innerInstructions: Instruction[];
  rawInstruction: Instruction;
  tokenBalances: TokenBalance[];
}

Instruction Discriminators

Instructions are filtered by discriminators (prefix bytes of instruction data). The discriminator names indicate the number of hex characters returned:
NameHex CharactersBytesCommon Use
d14 chars2 bytesSimple programs
d28 chars4 bytesSome native programs
d414 chars7 bytesCustom programs
d818 chars9 bytesAnchor programs (most common)
The decoder automatically extracts the appropriate discriminator from the ABI instruction definition.

Multiple Programs

Decode instructions from multiple programs:
const decoder = solanaInstructionDecoder({
  range: { from: 200000000 },
  programId: [
    orcaWhirlpool.programId,
    raydiumAmm.programId,
  ],
  instructions: {
    orcaSwap: orcaWhirlpool.instructions.swap,
    raydiumSwap: raydiumAmm.instructions.swapBaseIn,
  },
});

Error Handling

Handle decoding errors:
const decoder = solanaInstructionDecoder({
  range: { from: 200000000 },
  programId: programId,
  instructions: { swap: instructions.swap },
  onError: (ctx, error) => {
    ctx.logger.warn(`Failed to decode instruction: ${error.message}`);
    // Return null to skip this instruction, or throw to stop processing
    return null;
  },
});

Next Steps