light_sdk/interface/finalize.rs
1//! LightFinalize and LightPreInit traits for compression operations.
2//!
3//! These traits are implemented by the `#[derive(LightFinalize)]` macro from light-sdk-macros.
4//! They provide hooks for running compression operations at different points in an instruction:
5//!
6//! - `LightPreInit`: Called at START of instruction - creates mints via CPI context write
7//! - `LightFinalize`: Called at END of instruction - compresses PDAs and executes with proof
8//!
9//! This two-phase design allows mints to be created BEFORE the instruction body runs,
10//! so they can be used during the instruction (e.g., for vault creation, minting tokens).
11
12use solana_account_info::AccountInfo;
13
14/// Trait for pre-initialization operations (mint creation).
15///
16/// This is generated by `#[derive(LightFinalize)]` when `#[light_account(init)]` fields exist.
17/// Called at the START of an instruction to write mint creation to CPI context.
18///
19/// The mints are written to CPI context but NOT executed yet - execution happens
20/// in `light_finalize()` at the end, allowing the shared proof to cover both
21/// mints and PDAs.
22///
23/// # Type Parameters
24/// * `'info` - The account info lifetime
25/// * `P` - The instruction params type (from `#[instruction(params: P)]`)
26pub trait LightPreInit<'info, P> {
27 /// Execute pre-initialization operations (mint creation).
28 ///
29 /// This writes mint creation operations to CPI context. The actual execution
30 /// with proof happens in `light_finalize()`.
31 ///
32 /// # Arguments
33 /// * `remaining_accounts` - The remaining accounts from the context, used for CPI
34 /// * `params` - The instruction parameters containing compression data
35 ///
36 /// # Returns
37 /// `true` if mints were written to CPI context and `light_finalize` should execute
38 /// with CPI context. `false` if no mints exist and normal flow should proceed.
39 fn light_pre_init(
40 &mut self,
41 remaining_accounts: &[AccountInfo<'info>],
42 params: &P,
43 ) -> Result<bool, crate::error::LightSdkError>;
44}
45
46/// Trait for finalizing compression operations on accounts.
47///
48/// # Type Parameters
49/// * `'info` - The account info lifetime
50/// * `P` - The instruction params type (from `#[instruction(params: P)]`)
51///
52pub trait LightFinalize<'info, P> {
53 /// Execute compression finalization.
54 ///
55 /// This method is called at the end of an instruction to batch and execute
56 /// all compression CPIs for accounts marked with `#[compressible(...)]`.
57 ///
58 /// # Arguments
59 /// * `remaining_accounts` - The remaining accounts from the context, used for CPI
60 /// * `params` - The instruction parameters containing compression data
61 /// * `has_pre_init` - Whether `light_pre_init` was called and wrote to CPI context
62 ///
63 /// # Errors
64 /// Returns an error if the compression CPI fails.
65 fn light_finalize(
66 &mut self,
67 remaining_accounts: &[AccountInfo<'info>],
68 params: &P,
69 has_pre_init: bool,
70 ) -> Result<(), crate::error::LightSdkError>;
71}