|
| 1 | +--- |
| 2 | +title: "Anchor Constraints LightAccount" |
| 3 | +description: "Syntax reference for #[light_account] constraints with sponsored rent-exemption." |
| 4 | +--- |
| 5 | + |
| 6 | +- [`#[light_account(init)]`](#light_accountinit) creates a PDA |
| 7 | +- [`#[light_account(init, token::...)]`](#light_accountinit-token) creates a token account via CPI |
| 8 | +- [`#[light_account(init, associated_token::...)]`](#light_accountinit-associated_token) creates an ATA via CPI |
| 9 | +- [`#[light_account(init, mint::...)]`](#light_accountinit-mint) creates a mint via CPI |
| 10 | +- [Required accounts](#required-accounts) per account type |
| 11 | + |
| 12 | +Place `#[light_account]` below standard Anchor `#[account]` on the same field. |
| 13 | +Anchor handles seeds, space, and payer; LightAccount handles rent-free creation. |
| 14 | + |
| 15 | +--- |
| 16 | + |
| 17 | +### `#[light_account(init)]` |
| 18 | + |
| 19 | +Creates a rent-free PDA. No additional parameters — uses Anchor `#[account]` seeds and space. |
| 20 | + |
| 21 | +**attribute** |
| 22 | + |
| 23 | +``` |
| 24 | +#[light_account(init)] |
| 25 | +``` |
| 26 | + |
| 27 | +```rust |
| 28 | +#[account( // ← Anchor: seeds, space, payer |
| 29 | + init, |
| 30 | + payer = fee_payer, |
| 31 | + space = 8 + <Counter as anchor_lang::Space>::INIT_SPACE, |
| 32 | + seeds = [COUNTER_SEED, owner.key().as_ref()], |
| 33 | + bump, |
| 34 | +)] |
| 35 | +#[light_account(init)] // ← Light: register for rent-free creation |
| 36 | +pub counter: Account<'info, Counter>, |
| 37 | +``` |
| 38 | + |
| 39 | +Examples: [Github](https://github.com/Lightprotocol/examples-light-token/tree/main/programs/anchor/basic-macros/counter) |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +### `#[light_account(init, token::...)]` |
| 44 | + |
| 45 | +Creates a rent-free token account via CPI to the Light Token program. The account type is `UncheckedAccount` because Light Token program initializes it via CPI. |
| 46 | + |
| 47 | +**attribute** |
| 48 | + |
| 49 | +``` |
| 50 | +#[light_account(init, |
| 51 | + token::authority = <seeds>, |
| 52 | + token::mint = <account>, |
| 53 | + token::owner = <account>, |
| 54 | + token::bump = <expr> |
| 55 | +)] |
| 56 | +``` |
| 57 | + |
| 58 | +```rust |
| 59 | +#[account( |
| 60 | + mut, |
| 61 | + seeds = [VAULT_SEED, mint.key().as_ref()], |
| 62 | + bump, |
| 63 | +)] |
| 64 | +#[light_account(init, |
| 65 | + token::authority = [VAULT_SEED, self.mint.key()], |
| 66 | + token::mint = mint, |
| 67 | + token::owner = vault_authority, |
| 68 | + token::bump = params.vault_bump |
| 69 | +)] |
| 70 | +pub vault: UncheckedAccount<'info>, |
| 71 | +``` |
| 72 | + |
| 73 | +Examples: [Github](https://github.com/Lightprotocol/examples-light-token/tree/main/programs/anchor/basic-macros/create-token-account) |
| 74 | + |
| 75 | +--- |
| 76 | + |
| 77 | +### `#[light_account(init, associated_token::...)]` |
| 78 | + |
| 79 | +Creates a rent-free associated token account via CPI. |
| 80 | + |
| 81 | +**attribute** |
| 82 | + |
| 83 | +``` |
| 84 | +#[light_account(init, |
| 85 | + associated_token::authority = <account>, |
| 86 | + associated_token::mint = <account>, |
| 87 | + associated_token::bump = <expr> |
| 88 | +)] |
| 89 | +``` |
| 90 | + |
| 91 | +```rust |
| 92 | +#[account(mut)] |
| 93 | +#[light_account(init, |
| 94 | + associated_token::authority = associated_token_account_owner, |
| 95 | + associated_token::mint = associated_token_account_mint, |
| 96 | + associated_token::bump = params.associated_token_account_bump |
| 97 | +)] |
| 98 | +pub associated_token_account: UncheckedAccount<'info>, |
| 99 | +``` |
| 100 | + |
| 101 | +Examples: [Github](https://github.com/Lightprotocol/examples-light-token/tree/main/programs/anchor/basic-macros/create-associated-token-account) |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +### `#[light_account(init, mint::...)]` |
| 106 | + |
| 107 | +Creates a rent-free mint via CPI. |
| 108 | + |
| 109 | +**attribute (required)** |
| 110 | + |
| 111 | +``` |
| 112 | +#[light_account(init, mint, |
| 113 | + mint::signer = <account>, |
| 114 | + mint::authority = <account>, |
| 115 | + mint::decimals = <expr>, |
| 116 | + mint::seeds = <seeds>, |
| 117 | +)] |
| 118 | +``` |
| 119 | + |
| 120 | +**attribute (optional)** |
| 121 | + |
| 122 | +``` |
| 123 | +mint::bump = <expr> |
| 124 | +mint::freeze_authority = <ident> |
| 125 | +mint::authority_seeds = <seeds> |
| 126 | +mint::name = <expr> // requires symbol + uri |
| 127 | +mint::symbol = <expr> // requires name + uri |
| 128 | +mint::uri = <expr> // requires name + symbol |
| 129 | +mint::update_authority = <ident> // requires name, symbol, uri |
| 130 | +mint::additional_metadata = <expr> // requires name, symbol, uri |
| 131 | +``` |
| 132 | + |
| 133 | +```rust |
| 134 | +#[account(mut)] |
| 135 | +#[light_account(init, mint, |
| 136 | + mint::signer = mint_signer, |
| 137 | + mint::authority = fee_payer, |
| 138 | + mint::decimals = 9, |
| 139 | + mint::seeds = &[MINT_SIGNER_SEED, self.authority.to_account_info().key.as_ref()], |
| 140 | +)] |
| 141 | +pub mint: UncheckedAccount<'info>, |
| 142 | +``` |
| 143 | + |
| 144 | +<Accordion title="Mint with metadata"> |
| 145 | + |
| 146 | +```rust |
| 147 | +#[account(mut)] |
| 148 | +#[light_account(init, mint, |
| 149 | + mint::signer = mint_signer, |
| 150 | + mint::authority = fee_payer, |
| 151 | + mint::decimals = 9, |
| 152 | + mint::seeds = &[MINT_SIGNER_SEED, self.authority.to_account_info().key.as_ref()], |
| 153 | + mint::bump = params.mint_signer_bump, |
| 154 | + mint::name = params.name.clone(), |
| 155 | + mint::symbol = params.symbol.clone(), |
| 156 | + mint::uri = params.uri.clone(), |
| 157 | + mint::update_authority = authority, |
| 158 | + mint::additional_metadata = params.additional_metadata.clone() |
| 159 | +)] |
| 160 | +pub mint: UncheckedAccount<'info>, |
| 161 | +``` |
| 162 | + |
| 163 | +</Accordion> |
| 164 | + |
| 165 | +Examples: [Github](https://github.com/Lightprotocol/examples-light-token/tree/main/programs/anchor/basic-macros/create-mint) |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## Required accounts |
| 170 | + |
| 171 | +Add these to your `init` struct alongside the fields above. The `LightAccounts` derive macro appends additional protocol accounts to the instruction at runtime — you don't add those manually. |
| 172 | + |
| 173 | +<Tabs> |
| 174 | +<Tab title="PDA"> |
| 175 | + |
| 176 | +| Account | Description | |
| 177 | +|:-|:-| |
| 178 | +| `compression_config` | Per-program PDA. Stores rent sponsor address and compression settings. Initialize once via `InitializeRentFreeConfig`. | |
| 179 | +| `pda_rent_sponsor` | Per-program PDA. Receives rent-exemption lamports when accounts compress. Must be mutable. | |
| 180 | +| `system_program` | Solana System Program. Transfers lamports for rent-exemption. | |
| 181 | + |
| 182 | +</Tab> |
| 183 | +<Tab title="Token / ATA / Mint"> |
| 184 | + |
| 185 | +| Account | Description | |
| 186 | +|:-|:-| |
| 187 | +| `light_token_compressible_config` | Protocol PDA. Stores rent-config for Light Token accounts. | |
| 188 | +| `light_token_rent_sponsor` | Protocol PDA. Holds the lamport pool that sponsors rent-exemption. Reclaims lamports when accounts compress. | |
| 189 | +| `light_token_program` | Light Token program. CPI target for token, ATA, and mint creation. | |
| 190 | +| `light_token_cpi_authority` | Signer PDA. Authorizes CPI calls from your program to the Light Token program. | |
| 191 | +| `system_program` | Solana System Program. Transfers lamports for rent-exemption. | |
| 192 | + |
| 193 | +</Tab> |
| 194 | +</Tabs> |
0 commit comments