Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,4 @@ output1.txt
**/~/

expand.rs
output.rs
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 94 additions & 0 deletions sdk-libs/macros/CLAUDE.md
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
```
Comment on lines +32 to +41
Copy link
Contributor

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
-```
+```text
 src/
 ├── lib.rs                 # Macro entry points
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

32-32: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🤖 Prompt for AI Agents
In `@sdk-libs/macros/CLAUDE.md` around lines 32 - 41, The fenced code block
showing the project tree (the block starting with "```" then "src/ ├── lib.rs
...") lacks a language specifier; update the opening fence to include a language
(e.g., change the opening "```" to "```text" or "```bash") so the block is
properly marked as plain text—edit the CLAUDE.md fenced block that contains the
src/ tree and only modify the opening triple-backtick to include the chosen
language specifier.


## 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Consider adding context for requirements.

The requirements section lists LIGHT_CPI_SIGNER and ID but doesn't explain what happens if they're missing. Consider adding a brief note about the compile-time error users will see, which would help with debugging.

🤖 Prompt for AI Agents
In `@sdk-libs/macros/CLAUDE.md` around lines 81 - 85, Update the Requirements
section to briefly explain the consequences of missing constants: state that if
LIGHT_CPI_SIGNER or ID are not defined the macro expansion will fail and Rust
will produce a compile-time error (e.g., unresolved constant or missing
`declare_id!` symbol), and suggest the user define `const LIGHT_CPI_SIGNER:
Pubkey = ...;` and use `declare_id!` for `ID` as the remedy; reference the
symbols LIGHT_CPI_SIGNER and ID so readers know exactly which items will trigger
the compile-time failure.


## Testing

```bash
cargo test -p light-sdk-macros
```

Integration tests are in `sdk-tests/`:
- `csdk-anchor-full-derived-test` - Full macro integration test
72 changes: 72 additions & 0 deletions sdk-libs/macros/docs/CLAUDE.md
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
```
Loading