Macro instruction_decoder_collection

Source
instruction_decoder_collection!() { /* proc-macro */ }
Expand description

Generates a collection of instruction decoders and associated enums.

This macro creates a set of enums and implementations to handle decoding of instructions for multiple Solana programs. It generates:

  1. An enum for all instructions
  2. An enum for all instruction types
  3. An enum for all programs
  4. An implementation of InstructionDecoderCollection trait

§Syntax

The macro takes the following arguments:

  1. Name for the all-encompassing instructions enum
  2. Name for the all-encompassing instruction types enum
  3. Name for the programs enum
  4. One or more entries, each consisting of:
    • Program variant name
    • Decoder expression
    • Instruction enum for the program

§Example

instruction_decoder_collection!(
    AllInstructions, AllInstructionTypes, AllPrograms,
    JupSwap => JupiterDecoder => JupiterInstruction,
    MeteoraSwap => MeteoraDecoder => MeteoraInstruction
);

This example will generate:

  • AllInstructions enum with variants JupSwap(JupiterInstruction) and MeteoraSwap(MeteoraInstruction)
  • AllInstructionTypes enum with variants JupSwap(JupiterInstructionType) and MeteoraSwap(MeteoraInstructionType)
  • AllPrograms enum with variants JupSwap and MeteoraSwap
  • An implementation of InstructionDecoderCollection for AllInstructions

§Generated Code

The macro generates the following:

  1. An enum AllInstructions containing variants for each program’s instructions
  2. An enum AllInstructionTypes containing variants for each program’s instruction types
  3. An enum AllPrograms listing all programs
  4. An implementation of InstructionDecoderCollection for AllInstructions, including:
    • parse_instruction method to decode instructions
    • get_type method to retrieve the instruction type

§Note

Ensure that all necessary types (e.g., DecodedInstruction, InstructionDecoderCollection) are in scope where this macro is used.