solana_block_decoder/instruction/
inner_instruction.rs

1
2use {
3    crate::{
4        instruction::CompiledInstruction,
5        errors::{
6            conversion_error::ConversionError,
7        }
8    },
9    serde_derive::{Deserialize, Serialize},
10    solana_transaction_status::{
11        UiCompiledInstruction,
12        UiInnerInstructions,
13        UiInstruction,
14    },
15    std::{
16        convert::TryFrom,
17    },
18};
19
20#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
21pub struct InnerInstructions {
22    /// Transaction instruction index
23    pub index: u8,
24    /// List of inner instructions
25    pub instructions: Vec<InnerInstruction>,
26}
27
28impl TryFrom<UiInnerInstructions> for InnerInstructions {
29    type Error = ConversionError;
30
31    fn try_from(ui_inner_instructions: UiInnerInstructions) -> Result<Self, Self::Error> {
32        let instructions_result: Result<Vec<_>, _> = ui_inner_instructions
33            .instructions
34            .into_iter()
35            .map(|ix| match ix {
36                UiInstruction::Compiled(ui_compiled) => Ok(InnerInstruction::from(ui_compiled)),
37                _ => Err(ConversionError::UnsupportedInstructionFormat),
38            })
39            .collect();
40
41        match instructions_result {
42            Ok(instructions) => Ok(Self {
43                index: ui_inner_instructions.index,
44                instructions,
45            }),
46            Err(e) => Err(e),
47        }
48    }
49}
50
51#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
52pub struct InnerInstruction {
53    /// Compiled instruction
54    pub instruction: CompiledInstruction,
55    /// Invocation stack height of the instruction,
56    pub stack_height: Option<u32>,
57}
58
59impl From<UiCompiledInstruction> for InnerInstruction {
60    fn from(ui_compiled_instruction: UiCompiledInstruction) -> Self {
61        Self {
62            instruction: CompiledInstruction::from(ui_compiled_instruction.clone()), // Clone is needed if CompiledInstruction::from consumes its argument
63            stack_height: ui_compiled_instruction.stack_height,
64        }
65    }
66}
67
68impl From<InnerInstructions> for solana_transaction_status::InnerInstructions {
69    fn from(instr: InnerInstructions) -> Self {
70        Self {
71            index: instr.index,
72            instructions: instr.instructions.into_iter().map(Into::into).collect(),
73        }
74    }
75}
76
77impl From<InnerInstruction> for solana_transaction_status::InnerInstruction {
78    fn from(instr: InnerInstruction) -> Self {
79        Self {
80            instruction: instr.instruction.into(),
81            stack_height: instr.stack_height,
82        }
83    }
84}
85
86