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
2 changes: 1 addition & 1 deletion sdk-libs/compressible-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ pub mod compressible_instruction {
use super::*;

/// Returns program account metas for decompress_accounts_idempotent with CToken support.
/// Includes ctoken_rent_sponsor, light_token_program, ctoken_cpi_authority, ctoken_config.
/// Includes ctoken_rent_sponsor, light_token_program, light_token_cpi_authority, ctoken_config.
pub fn accounts(
fee_payer: Pubkey,
config: Pubkey,
Expand Down
14 changes: 7 additions & 7 deletions sdk-libs/macros/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This crate provides macros that enable rent-free compressed accounts on Solana w

| Macro | Type | Purpose |
|-------|------|---------|
| `#[derive(RentFree)]` | Derive | Generates `LightPreInit`/`LightFinalize` for Accounts structs |
| `#[derive(LightAccounts)]` | 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) |
Expand All @@ -24,17 +24,17 @@ This crate provides macros that enable rent-free compressed accounts on Solana w
Detailed macro documentation is in the `docs/` directory:

- **`docs/CLAUDE.md`** - Documentation structure guide
- **`docs/rentfree.md`** - `#[derive(RentFree)]` and trait derives
- **`docs/rentfree.md`** - `#[derive(LightAccounts)]` 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
├── rentfree/ # LightAccounts macro system
│ ├── account/ # Trait derive macros for account data structs
│ ├── accounts/ # #[derive(RentFree)] for Accounts structs
│ ├── accounts/ # #[derive(LightAccounts)] for Accounts structs
│ ├── program/ # #[rentfree_program] attribute macro
│ └── shared_utils.rs # Common utilities
└── hasher/ # LightHasherSha derive macro
Expand All @@ -43,7 +43,7 @@ src/
## Usage Example

```rust
use light_sdk_macros::{rentfree_program, RentFree, LightCompressible};
use light_sdk_macros::{rentfree_program, LightAccounts, LightCompressible};

// State account with compression support
#[derive(Default, Debug, InitSpace, LightCompressible)]
Expand All @@ -55,14 +55,14 @@ pub struct UserRecord {
}

// Accounts struct with rent-free field
#[derive(Accounts, RentFree)]
#[derive(Accounts, LightAccounts)]
#[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]
#[light_account(init)]
pub user_record: Account<'info, UserRecord>,
}

Expand Down
14 changes: 7 additions & 7 deletions sdk-libs/macros/docs/CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Documentation for the rentfree macro system in `light-sdk-macros`. These macros
|------|-------------|
| **`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.md`** | `#[derive(LightAccounts)]` 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) |
Expand All @@ -19,13 +19,13 @@ Documentation for the rentfree macro system in `light-sdk-macros`. These macros

### Accounts Field Attributes

Field-level attributes applied inside `#[derive(RentFree)]` Accounts structs:
Field-level attributes applied inside `#[derive(LightAccounts)]` Accounts structs:

| File | Attribute | Description |
|------|-----------|-------------|
| **`accounts/light_mint.md`** | `#[light_mint(...)]` | Creates compressed mint with automatic decompression |
| **`accounts/light_mint.md`** | `#[light_account(init, mint,...)]` | Creates compressed mint with automatic decompression |

See also: `#[rentfree]` attribute documented in `rentfree.md`
See also: `#[light_account(init)]` attribute documented in `rentfree.md`

### Account Trait Documentation

Expand All @@ -51,15 +51,15 @@ See also: `#[rentfree]` attribute documented in `rentfree.md`
```
#[rentfree_program] <- Program-level (rentfree_program/)
|
+-- Discovers #[derive(RentFree)] structs
+-- Discovers #[derive(LightAccounts)] structs
|
+-- Generates:
- RentFreeAccountVariant enum
- Seeds structs
- Compress/Decompress instructions
- Config instructions

#[derive(RentFree)] <- Account-level (rentfree.md)
#[derive(LightAccounts)] <- Account-level (rentfree.md)
|
+-- Generates LightPreInit + LightFinalize impls
|
Expand All @@ -76,7 +76,7 @@ See also: `#[rentfree]` attribute documented in `rentfree.md`
```
sdk-libs/macros/src/rentfree/
├── account/ # Trait derive macros for account data structs
├── accounts/ # #[derive(RentFree)] implementation
├── accounts/ # #[derive(LightAccounts)] implementation
├── program/ # #[rentfree_program] implementation
├── shared_utils.rs # Common utilities
└── mod.rs # Module exports
Expand Down
14 changes: 7 additions & 7 deletions sdk-libs/macros/docs/account/light_compressible.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

The `#[derive(LightCompressible)]` macro is a convenience derive that combines all traits required for a fully compressible account. It is the recommended way to prepare account structs for Light Protocol's rent-free compression system.

**When to use**: Apply this derive to any account struct that will be used with `#[rentfree]` in an Accounts struct. This is the standard approach for most use cases.
**When to use**: Apply this derive to any account struct that will be used with `#[light_account(init)]` in an Accounts struct. This is the standard approach for most use cases.

**Source**: `sdk-libs/macros/src/rentfree/traits/light_compressible.rs` (lines 56-79)

Expand Down Expand Up @@ -83,11 +83,11 @@ INPUT GENERATED
| Data Struct | --> | Accounts Struct | --> | Runtime |
+-------------------+ +-------------------+ +-------------------+
| #[derive( | | #[derive(Accounts,| | light_pre_init() |
| LightCompressible)] | RentFree)] | | Uses: |
| LightCompressible)] | LightAccounts)] | | Uses: |
| | | #[instruction] | | - DataHasher |
| Provides: | | pub struct Create | | - LightDiscrim. |
| - Hashing | | { | | - HasCompression|
| - Discriminator | | #[rentfree] | | Info |
| - Discriminator | | #[light_account(init)] | | Info |
| - Compression | | pub user_record | | - CompressAs |
| - Pack/Unpack | | } | | - Size |
+-------------------+ +-------------------+ | - Pack |
Expand Down Expand Up @@ -256,9 +256,9 @@ If no `Pubkey` fields exist, identity implementations are generated instead.

---

## 10. Usage with RentFree
## 10. Usage with LightAccounts

`LightCompressible` prepares the data struct for use with `#[derive(RentFree)]` on Accounts structs:
`LightCompressible` prepares the data struct for use with `#[derive(LightAccounts)]` on Accounts structs:

```rust
// Data struct - apply LightCompressible
Expand All @@ -271,14 +271,14 @@ pub struct UserRecord {
}

// Accounts struct - apply RentFree
#[derive(Accounts, RentFree)]
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateParams)]
pub struct Create<'info> {
#[account(mut)]
pub fee_payer: Signer<'info>,

#[account(init, payer = fee_payer, space = 8 + UserRecord::INIT_SPACE, ...)]
#[rentfree]
#[light_account(init)]
pub user_record: Account<'info, UserRecord>,
}
```
Expand Down
52 changes: 26 additions & 26 deletions sdk-libs/macros/docs/accounts/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 1. Overview

The `#[derive(RentFree)]` macro and associated trait derives enable rent-free compressed accounts on Solana with minimal boilerplate. These macros generate code for:
The `#[derive(LightAccounts)]` macro and associated trait derives enable rent-free compressed accounts on Solana with minimal boilerplate. These macros generate code for:

- Pre-instruction compression setup (`LightPreInit` trait)
- Post-instruction cleanup (`LightFinalize` trait)
Expand All @@ -16,7 +16,7 @@ sdk-libs/macros/src/rentfree/
|-- mod.rs # Module exports
|-- shared_utils.rs # Common utilities (constant detection, identifier extraction)
|
|-- accounts/ # #[derive(RentFree)] for Accounts structs
|-- accounts/ # #[derive(LightAccounts)] for Accounts structs
| |-- mod.rs # Module entry point
| |-- derive.rs # Orchestration layer
| |-- builder.rs # Code generation builder
Expand All @@ -36,7 +36,7 @@ sdk-libs/macros/src/rentfree/

---

## 2. `#[derive(RentFree)]` Derive Macro
## 2. `#[derive(LightAccounts)]` Derive Macro

### 2.1 Purpose

Expand All @@ -46,12 +46,12 @@ Generates `LightPreInit` and `LightFinalize` trait implementations for Anchor Ac

### 2.2 Supported Attributes

#### `#[rentfree]` - Mark PDA Fields for Compression
#### `#[light_account(init)]` - Mark PDA Fields for Compression

Applied to `Account<'info, T>` or `Box<Account<'info, T>>` fields.

```rust
#[derive(Accounts, RentFree)]
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateParams)]
pub struct CreateAccounts<'info> {
#[account(
Expand All @@ -61,7 +61,7 @@ pub struct CreateAccounts<'info> {
seeds = [b"user", params.owner.as_ref()],
bump
)]
#[rentfree] // Uses default address_tree_info and output_tree from params
#[light_account(init)] // Uses default address_tree_info and output_tree from params
pub user_record: Account<'info, UserRecord>,
}
```
Expand All @@ -78,12 +78,12 @@ pub struct CreateAccounts<'info> {
pub user_record: Account<'info, UserRecord>,
```

#### `#[light_mint(...)]` - Mark Mint Fields
#### `#[light_account(init, mint,...)]` - Mark Mint Fields

Creates a compressed mint with automatic decompression.

```rust
#[light_mint(
#[light_account(init, mint,
mint_signer = mint_signer, // AccountInfo that seeds the mint PDA (required)
authority = authority, // Mint authority (required)
decimals = 9, // Token decimals (required)
Expand All @@ -98,10 +98,10 @@ pub cmint: Account<'info, CMint>,

#### `#[instruction(...)]` - Specify Instruction Parameters (Required)

Must be present on the struct when using `#[rentfree]` or `#[light_mint]`.
Must be present on the struct when using `#[light_account(init)]` or `#[light_account(init)]`.

```rust
#[derive(Accounts, RentFree)]
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateParams)]
pub struct CreateAccounts<'info> { ... }
Comment thread
ananas-block marked this conversation as resolved.
```
Expand All @@ -114,10 +114,10 @@ Infrastructure fields are auto-detected by naming convention. No attribute requi
|------------|----------------|
| Fee Payer | `fee_payer`, `payer`, `creator` |
| Compression Config | `compression_config` |
| CToken Config | `ctoken_compressible_config`, `ctoken_config`, `light_token_config_account` |
| CToken Config | `light_token_compressible_config`, `ctoken_config`, `light_token_config_account` |
| CToken Rent Sponsor | `ctoken_rent_sponsor`, `light_token_rent_sponsor` |
| CToken Program | `ctoken_program`, `light_token_program` |
| CToken CPI Authority | `ctoken_cpi_authority`, `light_token_program_cpi_authority`, `compress_token_program_cpi_authority` |
| CToken CPI Authority | `light_token_cpi_authority`, `light_token_program_cpi_authority`, `compress_token_program_cpi_authority` |

**Source**: `sdk-libs/macros/src/rentfree/accounts/parse.rs` (lines 30-53)

Expand All @@ -127,14 +127,14 @@ Infrastructure fields are auto-detected by naming convention. No attribute requi
1. Parse
|-- parse_rentfree_struct() extracts:
| - Struct name and generics
| - #[rentfree] fields -> RentFreeField
| - #[light_mint] fields -> LightMintField
| - #[light_account(init)] fields -> RentFreeField
| - #[light_account(init)] fields -> LightMintField
| - #[instruction] args
| - Infrastructure fields by naming convention
|
2. Validate
|-- Total fields <= 255 (u8 index limit)
|-- #[instruction] required when #[rentfree] or #[light_mint] present
|-- #[instruction] required when #[light_account(init)] or #[light_account(init)] present
|
3. Generate pre_init Body
|-- PDAs + Mints: generate_pre_init_pdas_and_mints()
Expand All @@ -156,7 +156,7 @@ Infrastructure fields are auto-detected by naming convention. No attribute requi
**Input**:

```rust
#[derive(Accounts, RentFree)]
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateParams)]
pub struct CreateAccounts<'info> {
#[account(mut)]
Expand All @@ -171,7 +171,7 @@ pub struct CreateAccounts<'info> {
seeds = [b"user", params.owner.as_ref()],
bump
)]
#[rentfree]
#[light_account(init)]
pub user_record: Account<'info, UserRecord>,
}
```
Expand Down Expand Up @@ -254,7 +254,7 @@ The following diagram shows how the derive macros compose together to enable ren
====================

+--------------------+
| #[derive(RentFree)]| <-- Applied to Anchor Accounts struct
| #[derive(LightAccounts)]| <-- Applied to Anchor Accounts struct
+--------------------+
|
| generates
Expand Down Expand Up @@ -327,11 +327,11 @@ The following diagram shows how the derive macros compose together to enable ren
| } |
| |
| // Accounts struct - apply RentFree |
| #[derive(Accounts, RentFree)] |
| #[derive(Accounts, LightAccounts)] |
| #[instruction(params: CreateParams)] |
| pub struct Create<'info> { |
| #[account(init, ...)] |
| #[rentfree] <-- Marks for compression |
| #[light_account(init)] <-- Marks for compression |
| pub user_record: Account<'info, UserRecord>, |
| } |
| |
Expand Down Expand Up @@ -533,14 +533,14 @@ sdk-libs/macros/src/rentfree/
| |-- builder.rs RentFreeBuilder for code generation
| |-- parse.rs Attribute parsing with darling
| | - ParsedRentFreeStruct
| | - RentFreeField (#[rentfree] data)
| | - RentFreeField (#[light_account(init)] data)
| | - InfraFields (auto-detected infrastructure)
| | - InfraFieldClassifier (naming convention matching)
| |-- pda.rs PDA compression block generation
| | - PdaBlockBuilder
| | - generate_pda_compress_blocks()
| +-- light_mint.rs Mint action CPI generation
| - LightMintField (#[light_mint] data)
| - LightMintField (#[light_account(init)] data)
| - InfraRefs - resolved infrastructure field references
| - LightMintBuilder - builder pattern for mint CPI generation
| - CpiContextParts - encapsulates CPI context branching logic
Expand Down Expand Up @@ -571,13 +571,13 @@ sdk-libs/macros/src/rentfree/
## 5. Limitations

### Field Limits
- **Maximum 255 fields**: Total `#[rentfree]` + `#[light_mint]` fields must be <= 255 (u8 index limit)
- **Single mint field**: Currently only the first `#[light_mint]` field is processed
- **Maximum 255 fields**: Total `#[light_account(init)]` + `#[light_account(init)]` fields must be <= 255 (u8 index limit)
- **Single mint field**: Currently only the first `#[light_account(init)]` field is processed

### Type Restrictions
- `#[rentfree]` only applies to `Account<'info, T>` or `Box<Account<'info, T>>` fields
- `#[light_account(init)]` only applies to `Account<'info, T>` or `Box<Account<'info, T>>` fields
- Nested `Box<Box<Account<...>>>` is not supported
Comment thread
ananas-block marked this conversation as resolved.
- `#[rentfree]` and `#[light_mint]` are mutually exclusive on the same field
- `#[light_account(init)]` and `#[light_account(init)]` are mutually exclusive on the same field

### No-op Fallback
When no `#[instruction]` attribute is present, the macro generates no-op implementations for backwards compatibility with non-compressible Accounts structs.
Expand Down
Loading