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::ShortU16Len, 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<_, ShortU16Len>"))]
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<_, ShortU16Len>"))]
34    pub data: Vec<u8>,
35}
36
37impl Sanitize for CompiledInstruction {}
38
39impl CompiledInstruction {
40    #[cfg(feature = "bincode")]
41    pub fn new<T: serde::Serialize>(program_ids_index: u8, data: &T, accounts: Vec<u8>) -> Self {
42        let data = bincode::serialize(data).unwrap();
43        Self {
44            program_id_index: program_ids_index,
45            accounts,
46            data,
47        }
48    }
49
50    pub fn new_from_raw_parts(program_id_index: u8, data: Vec<u8>, accounts: Vec<u8>) -> Self {
51        Self {
52            program_id_index,
53            accounts,
54            data,
55        }
56    }
57
58    pub fn program_id<'a>(&self, program_ids: &'a [Address]) -> &'a Address {
59        &program_ids[self.program_id_index as usize]
60    }
61}