light_sdk/cpi/
instruction.rs

1use light_compressed_account::instruction_data::compressed_proof::ValidityProof;
2
3#[cfg(feature = "poseidon")]
4use crate::DataHasher;
5use crate::{
6    account::LightAccount, AnchorDeserialize, AnchorSerialize, LightDiscriminator, ProgramError,
7};
8
9/// Trait for Light CPI instruction types
10pub trait LightCpiInstruction: Sized {
11    /// Creates a new CPI instruction builder with a validity proof.
12    ///
13    /// # Arguments
14    /// * `cpi_signer` - The CPI signer containing program ID and bump seed
15    /// * `proof` - Validity proof for compressed account operations
16    fn new_cpi(cpi_signer: crate::cpi::CpiSigner, proof: ValidityProof) -> Self;
17
18    /// Adds a compressed account to the instruction (using SHA256 hashing).
19    ///
20    /// The account can be an input (for updating/closing), output (for creating/updating),
21    /// or both. The method automatically handles the conversion based on the account state.
22    ///
23    /// # Arguments
24    /// * `account` - The light account to add to the instruction
25    ///
26    /// # Type Parameters
27    /// * `A` - The compressed account data type
28    #[must_use = "with_light_account returns a new value"]
29    fn with_light_account<A>(self, account: LightAccount<A>) -> Result<Self, ProgramError>
30    where
31        A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + Default;
32
33    /// Adds a compressed account to the instruction (using Poseidon hashing).
34    ///
35    /// Similar to [`with_light_account`](Self::with_light_account), but uses Poseidon hashing
36    /// instead of SHA256. Use this when your compressed account data implements [`DataHasher`].
37    ///
38    /// # Arguments
39    /// * `account` - The light account to add to the instruction
40    ///
41    /// # Type Parameters
42    /// * `A` - The compressed account data type that implements DataHasher
43    #[cfg(feature = "poseidon")]
44    #[must_use = "with_light_account_poseidon returns a new value"]
45    fn with_light_account_poseidon<A>(
46        self,
47        account: crate::account::poseidon::LightAccount<A>,
48    ) -> Result<Self, ProgramError>
49    where
50        A: AnchorSerialize + AnchorDeserialize + LightDiscriminator + DataHasher + Default;
51
52    /// Returns the instruction mode (0 for v1, 1 for v2).
53    fn get_mode(&self) -> u8;
54
55    /// Returns the CPI signer bump seed.
56    fn get_bump(&self) -> u8;
57
58    /// Writes instruction to CPI context as the first operation in a batch.
59    ///
60    /// # Availability
61    /// Only available with the `cpi-context` feature enabled.
62    #[cfg(feature = "cpi-context")]
63    #[must_use = "write_to_cpi_context_first returns a new value"]
64    fn write_to_cpi_context_first(self) -> Self;
65
66    /// Writes instruction to CPI context as a subsequent operation in a batch.
67    ///
68    /// # Availability
69    /// Only available with the `cpi-context` feature enabled.
70    #[cfg(feature = "cpi-context")]
71    #[must_use = "write_to_cpi_context_set returns a new value"]
72    fn write_to_cpi_context_set(self) -> Self;
73
74    /// Executes all operations accumulated in CPI context.
75    ///
76    /// # Availability
77    /// Only available with the `cpi-context` feature enabled.
78    #[cfg(feature = "cpi-context")]
79    #[must_use = "execute_with_cpi_context returns a new value"]
80    fn execute_with_cpi_context(self) -> Self;
81
82    /// Returns whether this instruction uses CPI context.
83    ///
84    /// # Availability
85    /// Only available with the `cpi-context` feature enabled.
86    #[cfg(feature = "cpi-context")]
87    fn get_with_cpi_context(&self) -> bool;
88
89    /// Returns the CPI context configuration.
90    ///
91    /// # Availability
92    /// Only available with the `cpi-context` feature enabled.
93    #[cfg(feature = "cpi-context")]
94    fn get_cpi_context(
95        &self,
96    ) -> &light_compressed_account::instruction_data::cpi_context::CompressedCpiContext;
97
98    /// Returns whether this instruction has any read-only accounts.
99    ///
100    /// # Availability
101    /// Only available with the `cpi-context` feature enabled.
102    #[cfg(feature = "cpi-context")]
103    fn has_read_only_accounts(&self) -> bool;
104}