Skip to main content

light_program

Attribute Macro light_program 

Source
#[light_program]
Expand description

Auto-discovering Light program macro that reads external module files.

This macro automatically discovers #[light_account(init)] fields in Accounts structs by reading external module files. No explicit type list needed!

It also automatically wraps instruction handlers that use Light Accounts structs with light_pre_init/light_finalize logic - no separate attribute needed!

Usage:

#[light_program]
#[program]
pub mod my_program {
    pub mod instruction_accounts;  // Macro reads this file!
    pub mod state;

    use instruction_accounts::*;
    use state::*;

    pub fn create_user(ctx: Context<CreateUser>, params: Params) -> Result<()> {
        // Your business logic
    }
}

The macro:

  1. Scans the crate’s src/ directory for #[derive(Accounts)] structs
  2. Extracts seeds from #[account(seeds = [...])] on #[light_account(init)] fields
  3. Auto-wraps instruction handlers that use those Accounts structs
  4. Generates all necessary types, enums, and instruction handlers

Seeds are declared ONCE in Anchor attributes - no duplication!