light_sdk/cpi/
instruction.rs

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