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