Skip to main content

solana_message/
compiled_instruction.rs

1#[cfg(feature = "serde")]
2use serde_derive::{Deserialize, Serialize};
3#[cfg(feature = "frozen-abi")]
4use solana_frozen_abi_macro::AbiExample;
5#[cfg(feature = "wincode")]
6use wincode::{containers, len::ShortU16, SchemaRead, SchemaWrite};
7use {solana_address::Address, solana_sanitize::Sanitize};
8
9/// A compact encoding of an instruction.
10///
11/// A `CompiledInstruction` is a component of a multi-instruction [`Message`],
12/// which is the core of a Solana transaction. It is created during the
13/// construction of `Message`. Most users will not interact with it directly.
14///
15/// [`Message`]: crate::Message
16#[cfg_attr(feature = "frozen-abi", derive(AbiExample))]
17#[cfg_attr(
18    feature = "serde",
19    derive(Deserialize, Serialize),
20    serde(rename_all = "camelCase")
21)]
22#[cfg_attr(feature = "wincode", derive(SchemaWrite, SchemaRead))]
23#[derive(Debug, PartialEq, Eq, Clone)]
24pub struct CompiledInstruction {
25    /// Index into the transaction keys array indicating the program account that executes this instruction.
26    pub program_id_index: u8,
27    /// Ordered indices into the transaction keys array indicating which accounts to pass to the program.
28    #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
29    #[cfg_attr(feature = "wincode", wincode(with = "containers::Vec<_, ShortU16>"))]
30    pub accounts: Vec<u8>,
31    /// The program input data.
32    #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
33    #[cfg_attr(feature = "wincode", wincode(with = "containers::Vec<_, ShortU16>"))]
34    pub data: Vec<u8>,
35}
36
37impl Sanitize for CompiledInstruction {}
38
39impl CompiledInstruction {
40    #[cfg(feature = "wincode")]
41    pub fn new<T: wincode::Serialize<Src = T>>(
42        program_ids_index: u8,
43        data: &T,
44        accounts: Vec<u8>,
45    ) -> Self {
46        let data = wincode::serialize(data).unwrap();
47        Self {
48            program_id_index: program_ids_index,
49            accounts,
50            data,
51        }
52    }
53
54    pub fn new_from_raw_parts(program_id_index: u8, data: Vec<u8>, accounts: Vec<u8>) -> Self {
55        Self {
56            program_id_index,
57            accounts,
58            data,
59        }
60    }
61
62    pub fn program_id<'a>(&self, tx_accounts: &'a [Address]) -> &'a Address {
63        &tx_accounts[self.program_id_index as usize]
64    }
65}