Skip to main content

light_macros/
lib.rs

1//! # light-macros
2//!
3//! Proc macros for Light Protocol on-chain programs.
4//!
5//! | Macro | Description |
6//! |-------|-------------|
7//! | [`pubkey!`](macro@pubkey) | Convert base58 public key to `Pubkey` at compile time |
8//! | [`pubkey_array!`](macro@pubkey_array) | Convert base58 public key to `[u8; 32]` at compile time |
9//! | [`derive_light_cpi_signer!`](macro@derive_light_cpi_signer) | Derive CPI signer PDA, program ID, and bump seed |
10//! | [`derive_light_cpi_signer_pda!`](macro@derive_light_cpi_signer_pda) | Derive CPI signer PDA address and bump seed |
11//! | [`#[heap_neutral]`](macro@heap_neutral) | Assert a function frees all heap it allocates |
12//! | [`#[derive(Noop)]`](derive@Noop) | No-op derive placeholder |
13
14extern crate proc_macro;
15
16use proc_macro::TokenStream;
17use quote::quote;
18use syn::{parse_macro_input, parse_quote, ItemFn};
19
20mod cpi_signer;
21mod pubkey;
22
23/// Converts a base58 encoded public key into a byte array.
24#[proc_macro]
25pub fn pubkey(input: TokenStream) -> TokenStream {
26    let args = parse_macro_input!(input as pubkey::PubkeyArgs);
27    pubkey::pubkey(args)
28        .unwrap_or_else(|err| err.to_compile_error())
29        .into()
30}
31
32/// Converts a base58 encoded public key into a raw byte array [u8; 32].
33#[proc_macro]
34pub fn pubkey_array(input: TokenStream) -> TokenStream {
35    let args = parse_macro_input!(input as pubkey::PubkeyArgs);
36    pubkey::pubkey_array(args)
37        .unwrap_or_else(|err| err.to_compile_error())
38        .into()
39}
40
41#[proc_macro_attribute]
42pub fn heap_neutral(_: TokenStream, input: TokenStream) -> TokenStream {
43    let mut function = parse_macro_input!(input as ItemFn);
44
45    // Insert memory management code at the beginning of the function
46    let init_code: syn::Stmt = parse_quote! {
47        #[cfg(target_os = "solana")]
48        let pos = light_heap::GLOBAL_ALLOCATOR.get_heap_pos();
49    };
50    let msg = format!("pre: {}", function.sig.ident);
51    let log_pre: syn::Stmt = parse_quote! {
52        #[cfg(all(target_os = "solana", feature = "mem-profiling"))]
53        light_heap::GLOBAL_ALLOCATOR.log_total_heap(#msg);
54    };
55    function.block.stmts.insert(0, init_code);
56    function.block.stmts.insert(1, log_pre);
57
58    // Insert memory management code at the end of the function
59    let msg = format!("post: {}", function.sig.ident);
60    let log_post: syn::Stmt = parse_quote! {
61        #[cfg(all(target_os = "solana", feature = "mem-profiling"))]
62        light_heap::GLOBAL_ALLOCATOR.log_total_heap(#msg);
63    };
64    let cleanup_code: syn::Stmt = parse_quote! {
65        #[cfg(target_os = "solana")]
66        light_heap::GLOBAL_ALLOCATOR.free_heap(pos)?;
67    };
68    let len = function.block.stmts.len();
69    function.block.stmts.insert(len - 1, log_post);
70    function.block.stmts.insert(len - 1, cleanup_code);
71    TokenStream::from(quote! { #function })
72}
73
74/// No-op derive macro that does nothing.
75/// Used as a placeholder for serialization derives when not needed.
76#[proc_macro_derive(Noop)]
77pub fn derive_noop(_input: TokenStream) -> TokenStream {
78    TokenStream::new()
79}
80
81/// Derives a Light Protocol CPI signer PDA at compile time
82///
83/// This macro computes the CPI signer PDA using the "cpi_authority" seed
84/// for the given program ID. Uses `solana_pubkey` with `solana` feature,
85/// otherwise uses `pinocchio` (default).
86///
87/// ## Usage
88///
89/// ```rust
90/// # use light_macros::derive_light_cpi_signer_pda;
91/// // In a Solana program
92/// let (cpi_signer, bump) = derive_light_cpi_signer_pda!("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7");
93/// ```
94///
95/// Returns a tuple `([u8; 32], u8)` containing the PDA address and bump seed.
96#[proc_macro]
97pub fn derive_light_cpi_signer_pda(input: TokenStream) -> TokenStream {
98    cpi_signer::derive_light_cpi_signer_pda(input)
99}
100
101/// Derives a complete Light Protocol CPI configuration at runtime
102///
103/// This macro computes the program ID, CPI signer PDA, and bump seed
104/// for the given program ID. Uses `solana_pubkey` with `solana` feature,
105/// otherwise uses `pinocchio` (default).
106///
107/// ## Usage
108///
109/// ```rust
110/// # use light_macros::derive_light_cpi_signer;
111/// # struct CpiSigner { program_id: [u8; 32], cpi_signer: [u8; 32], bump: u8 }
112/// // In a Solana program
113/// const LIGHT_CPI_SIGNER: CpiSigner = derive_light_cpi_signer!("SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7");
114/// ```
115///
116/// Returns a `CpiSigner` struct (must be in scope) containing:
117/// - `program_id`: Program ID bytes
118/// - `cpi_signer`: CPI signer PDA address
119/// - `bump`: Bump seed
120#[proc_macro]
121pub fn derive_light_cpi_signer(input: TokenStream) -> TokenStream {
122    cpi_signer::derive_light_cpi_signer(input)
123}