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::{frozen_abi, AbiExample, StableAbi, StableAbiSample};
5use {alloc::vec::Vec, 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(
20    feature = "frozen-abi",
21    derive(AbiExample, StableAbi, StableAbiSample),
22    frozen_abi(
23        abi_digest = "ANAoDM13eiKa3WRnfiwYi8jcgaEgC32xHWyA8xVAkUGV",
24        abi_serializer = ["bincode", "wincode"],
25        test_roundtrip = "eq_and_wire"
26    )
27)]
28#[cfg_attr(
29    feature = "serde",
30    derive(Deserialize, Serialize),
31    serde(rename_all = "camelCase")
32)]
33#[cfg_attr(feature = "wincode", derive(SchemaWrite, SchemaRead))]
34#[derive(Debug, PartialEq, Eq, Clone)]
35pub struct CompiledInstruction {
36    /// Index into the transaction keys array indicating the program account that executes this instruction.
37    pub program_id_index: u8,
38    /// Ordered indices into the transaction keys array indicating which accounts to pass to the program.
39    #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
40    #[cfg_attr(feature = "wincode", wincode(with = "containers::Vec<_, ShortU16>"))]
41    pub accounts: Vec<u8>,
42    /// The program input data.
43    #[cfg_attr(feature = "serde", serde(with = "solana_short_vec"))]
44    #[cfg_attr(feature = "wincode", wincode(with = "containers::Vec<_, ShortU16>"))]
45    pub data: Vec<u8>,
46}
47
48impl Sanitize for CompiledInstruction {}
49
50impl CompiledInstruction {
51    #[cfg(feature = "wincode")]
52    pub fn new<T: wincode::Serialize<Src = T>>(
53        program_ids_index: u8,
54        data: &T,
55        accounts: Vec<u8>,
56    ) -> Self {
57        let data = wincode::serialize(data).unwrap();
58        Self {
59            program_id_index: program_ids_index,
60            accounts,
61            data,
62        }
63    }
64
65    pub fn new_from_raw_parts(program_id_index: u8, data: Vec<u8>, accounts: Vec<u8>) -> Self {
66        Self {
67            program_id_index,
68            accounts,
69            data,
70        }
71    }
72
73    pub fn program_id<'a>(&self, tx_accounts: &'a [Address]) -> &'a Address {
74        &tx_accounts[self.program_id_index as usize]
75    }
76}