-
Notifications
You must be signed in to change notification settings - Fork 90
refactor: sdk macros 2 #2175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: sdk macros 2 #2175
Changes from all commits
8fd073f
8b0f961
947da83
07ef51b
25a5507
6493ea8
3e6fd1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,3 +95,4 @@ output1.txt | |
| **/~/ | ||
|
|
||
| expand.rs | ||
| output.rs | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # light-sdk-macros | ||
|
|
||
| Procedural macros for Light Protocol's rent-free compression system. | ||
|
|
||
| ## Crate Overview | ||
|
|
||
| This crate provides macros that enable rent-free compressed accounts on Solana with minimal boilerplate. | ||
|
|
||
| **Package**: `light-sdk-macros` | ||
| **Location**: `sdk-libs/macros/` | ||
|
|
||
| ## Main Macros | ||
|
|
||
| | Macro | Type | Purpose | | ||
| |-------|------|---------| | ||
| | `#[derive(RentFree)]` | Derive | Generates `LightPreInit`/`LightFinalize` for Accounts structs | | ||
| | `#[rentfree_program]` | Attribute | Program-level auto-discovery and instruction generation | | ||
| | `#[derive(LightCompressible)]` | Derive | Combined traits for compressible account data | | ||
| | `#[derive(Compressible)]` | Derive | Compression traits (HasCompressionInfo, CompressAs, Size) | | ||
| | `#[derive(CompressiblePack)]` | Derive | Pack/Unpack with Pubkey-to-index compression | | ||
|
|
||
| ## Documentation | ||
|
|
||
| Detailed macro documentation is in the `docs/` directory: | ||
|
|
||
| - **`docs/CLAUDE.md`** - Documentation structure guide | ||
| - **`docs/rentfree.md`** - `#[derive(RentFree)]` and trait derives | ||
| - **`docs/rentfree_program/`** - `#[rentfree_program]` attribute macro (architecture.md + codegen.md) | ||
|
|
||
| ## Source Structure | ||
|
|
||
| ``` | ||
| src/ | ||
| ├── lib.rs # Macro entry points | ||
| ├── rentfree/ # RentFree macro system | ||
| │ ├── accounts/ # #[derive(RentFree)] for Accounts structs | ||
| │ ├── program/ # #[rentfree_program] attribute macro | ||
| │ ├── traits/ # Trait derive macros | ||
| │ └── shared_utils.rs # Common utilities | ||
| └── hasher/ # LightHasherSha derive macro | ||
| ``` | ||
|
|
||
| ## Usage Example | ||
|
|
||
| ```rust | ||
| use light_sdk_macros::{rentfree_program, RentFree, LightCompressible}; | ||
|
|
||
| // State account with compression support | ||
| #[derive(Default, Debug, InitSpace, LightCompressible)] | ||
| #[account] | ||
| pub struct UserRecord { | ||
| pub owner: Pubkey, | ||
| pub score: u64, | ||
| pub compression_info: Option<CompressionInfo>, | ||
| } | ||
|
|
||
| // Accounts struct with rent-free field | ||
| #[derive(Accounts, RentFree)] | ||
| #[instruction(params: CreateParams)] | ||
| pub struct Create<'info> { | ||
| #[account(mut)] | ||
| pub fee_payer: Signer<'info>, | ||
|
|
||
| #[account(init, payer = fee_payer, space = 8 + UserRecord::INIT_SPACE, seeds = [b"user", params.owner.as_ref()], bump)] | ||
| #[rentfree] | ||
| pub user_record: Account<'info, UserRecord>, | ||
| } | ||
|
|
||
| // Program with auto-wrapped instructions | ||
| #[rentfree_program] | ||
| #[program] | ||
| pub mod my_program { | ||
| pub fn create(ctx: Context<Create>, params: CreateParams) -> Result<()> { | ||
| // Business logic - compression handled automatically | ||
| ctx.accounts.user_record.owner = params.owner; | ||
| Ok(()) | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Requirements | ||
|
|
||
| Programs using these macros must define: | ||
| - `LIGHT_CPI_SIGNER: Pubkey` - CPI signer constant | ||
| - `ID` - Program ID (from `declare_id!`) | ||
|
Comment on lines
+81
to
+85
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Consider adding context for requirements. The requirements section lists 🤖 Prompt for AI Agents |
||
|
|
||
| ## Testing | ||
|
|
||
| ```bash | ||
| cargo test -p light-sdk-macros | ||
| ``` | ||
|
|
||
| Integration tests are in `sdk-tests/`: | ||
| - `csdk-anchor-full-derived-test` - Full macro integration test | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # Documentation Structure | ||
|
|
||
| ## Overview | ||
|
|
||
| Documentation for the rentfree macro system in `light-sdk-macros`. These macros enable rent-free compressed accounts on Solana with minimal boilerplate. | ||
|
|
||
| ## Structure | ||
|
|
||
| | File | Description | | ||
| |------|-------------| | ||
| | **`CLAUDE.md`** | This file - documentation structure guide | | ||
| | **`../CLAUDE.md`** | Main entry point for sdk-libs/macros | | ||
| | **`rentfree.md`** | `#[derive(RentFree)]` macro and trait derives | | ||
| | **`rentfree_program/`** | `#[rentfree_program]` attribute macro | | ||
| | **`rentfree_program/architecture.md`** | Architecture overview, usage, generated items | | ||
| | **`rentfree_program/codegen.md`** | Technical implementation details (code generation) | | ||
| | **`traits/`** | Trait derive macros for compressible data structs | | ||
|
|
||
| ### Traits Documentation | ||
|
|
||
| | File | Macro | Description | | ||
| |------|-------|-------------| | ||
| | **`traits/has_compression_info.md`** | `#[derive(HasCompressionInfo)]` | Accessor methods for compression_info field | | ||
| | **`traits/compress_as.md`** | `#[derive(CompressAs)]` | Creates compressed representation for hashing | | ||
| | **`traits/compressible.md`** | `#[derive(Compressible)]` | Combined: HasCompressionInfo + CompressAs + Size | | ||
| | **`traits/compressible_pack.md`** | `#[derive(CompressiblePack)]` | Pack/Unpack with Pubkey-to-index compression | | ||
| | **`traits/light_compressible.md`** | `#[derive(LightCompressible)]` | All traits for rent-free accounts | | ||
|
|
||
| ## Navigation Tips | ||
|
|
||
| ### Starting Points | ||
|
|
||
| - **Data struct traits**: Start with `traits/light_compressible.md` for the all-in-one derive macro for compressible data structs | ||
| - **Building account structs**: Use `rentfree.md` for the accounts-level derive macro that marks fields for compression | ||
| - **Program-level integration**: Use `rentfree_program/architecture.md` for program-level auto-discovery and instruction generation | ||
| - **Implementation details**: Use `rentfree_program/codegen.md` for technical code generation details | ||
|
|
||
| ### Macro Hierarchy | ||
|
|
||
| ``` | ||
| #[rentfree_program] <- Program-level (rentfree_program/) | ||
| | | ||
| +-- Discovers #[derive(RentFree)] structs | ||
| | | ||
| +-- Generates: | ||
| - RentFreeAccountVariant enum | ||
| - Seeds structs | ||
| - Compress/Decompress instructions | ||
| - Config instructions | ||
|
|
||
| #[derive(RentFree)] <- Account-level (rentfree.md) | ||
| | | ||
| +-- Generates LightPreInit + LightFinalize impls | ||
| | | ||
| +-- Uses trait derives (traits/): | ||
| - HasCompressionInfo <- traits/has_compression_info.md | ||
| - CompressAs <- traits/compress_as.md | ||
| - Compressible <- traits/compressible.md | ||
| - CompressiblePack <- traits/compressible_pack.md | ||
| - LightCompressible <- traits/light_compressible.md (combines all) | ||
| ``` | ||
|
|
||
| ## Related Source Code | ||
|
|
||
| ``` | ||
| sdk-libs/macros/src/rentfree/ | ||
| ├── accounts/ # #[derive(RentFree)] implementation | ||
| ├── program/ # #[rentfree_program] implementation | ||
| ├── traits/ # Trait derive macros | ||
| ├── shared_utils.rs # Common utilities | ||
| └── mod.rs # Module exports | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Add language specifier to source structure block.
Per the static analysis hint, the fenced code block should have a language specified.
Suggested fix
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)
32-32: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🤖 Prompt for AI Agents