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