light_instruction_decoder/
core.rs

1//! Core types for instruction decoding.
2
3use solana_instruction::AccountMeta;
4use solana_pubkey::Pubkey;
5
6/// A decoded instruction field for display.
7#[derive(Debug, Clone)]
8pub struct DecodedField {
9    /// Field name
10    pub name: String,
11    /// Field value as string
12    pub value: String,
13    /// Optional nested fields (for complex types)
14    pub children: Vec<DecodedField>,
15}
16
17impl DecodedField {
18    /// Create a simple field with name and value.
19    pub fn new(name: impl Into<String>, value: impl Into<String>) -> Self {
20        Self {
21            name: name.into(),
22            value: value.into(),
23            children: Vec::new(),
24        }
25    }
26
27    /// Create a field with nested children.
28    pub fn with_children(name: impl Into<String>, children: Vec<DecodedField>) -> Self {
29        Self {
30            name: name.into(),
31            value: String::new(),
32            children,
33        }
34    }
35}
36
37/// Result of decoding an instruction.
38#[derive(Debug, Clone)]
39pub struct DecodedInstruction {
40    /// Human-readable instruction name (e.g., "Transfer", "MintTo")
41    pub name: String,
42    /// Decoded fields to display
43    pub fields: Vec<DecodedField>,
44    /// Account names in order (index corresponds to account position)
45    pub account_names: Vec<String>,
46}
47
48impl DecodedInstruction {
49    /// Create a decoded instruction with fields and account names.
50    pub fn with_fields_and_accounts(
51        name: impl Into<String>,
52        fields: Vec<DecodedField>,
53        account_names: Vec<String>,
54    ) -> Self {
55        Self {
56            name: name.into(),
57            fields,
58            account_names,
59        }
60    }
61}
62
63/// Trait for instruction decoders - each program implements this.
64pub trait InstructionDecoder: Send + Sync {
65    /// Program ID this decoder handles.
66    fn program_id(&self) -> Pubkey;
67
68    /// Human-readable program name (e.g., "Compressed Token Program").
69    fn program_name(&self) -> &'static str;
70
71    /// Decode instruction data into a structured representation.
72    /// Returns None if decoding fails or instruction is unknown.
73    fn decode(&self, data: &[u8], accounts: &[AccountMeta]) -> Option<DecodedInstruction>;
74}