light-token 0.23.0

SDK for Light Tokens
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
use light_compressed_account::instruction_data::{
    compressed_proof::ValidityProof, traits::LightInstructionData,
};
use light_compressed_token_sdk::compressed_token::mint_action::MintActionMetaConfig;
use light_token_interface::instructions::mint_action::{
    CpiContext, DecompressMintAction, MintActionCompressedInstructionData, MintWithContext,
};
use solana_account_info::AccountInfo;
use solana_cpi::{invoke, invoke_signed};
use solana_instruction::Instruction;
use solana_program_error::ProgramError;
use solana_pubkey::Pubkey;

use crate::{
    constants::{config_pda, rent_sponsor_pda},
    instruction::SystemAccountInfos,
};

/// Decompress a Light Mint to a Mint Solana account.
///
/// Creates an on-chain Mint PDA that becomes the source of truth.
/// The Mint is always compressible.
///
/// # Example
/// ```rust,ignore
/// let instruction = DecompressMint {
///     payer,
///     authority,
///     state_tree,
///     input_queue,
///     output_queue,
///     compressed_mint_with_context,
///     proof,
///     rent_payment: 16,       // epochs (~24 hours rent)
///     write_top_up: 766,      // lamports (~3 hours rent per write)
/// }.instruction()?;
/// ```
#[derive(Debug, Clone)]
pub struct DecompressMint {
    /// Fee payer
    pub payer: Pubkey,
    /// Mint authority (must sign)
    pub authority: Pubkey,
    /// State tree for the Light Mint
    pub state_tree: Pubkey,
    /// Input queue for reading Light Mint
    pub input_queue: Pubkey,
    /// Output queue for updated Light Mint
    pub output_queue: Pubkey,
    /// Light Mint with context (from indexer)
    pub compressed_mint_with_context: MintWithContext,
    /// Validity proof for the Light Mint
    pub proof: ValidityProof,
    /// Rent payment in epochs (must be >= 2)
    pub rent_payment: u8,
    /// Lamports for future write operations
    pub write_top_up: u32,
}

impl DecompressMint {
    pub fn instruction(self) -> Result<Instruction, ProgramError> {
        // Get Mint PDA from Light Mint metadata
        let mint_data = self
            .compressed_mint_with_context
            .mint
            .as_ref()
            .ok_or(ProgramError::InvalidInstructionData)?;
        let mint_pda = Pubkey::from(mint_data.metadata.mint.to_bytes());

        // Build DecompressMintAction
        let action = DecompressMintAction {
            rent_payment: self.rent_payment,
            write_top_up: self.write_top_up,
        };

        // Build instruction data
        let instruction_data = MintActionCompressedInstructionData::new(
            self.compressed_mint_with_context,
            self.proof.0,
        )
        .with_decompress_mint(action);

        // Build account metas with compressible Mint
        let meta_config = MintActionMetaConfig::new(
            self.payer,
            self.authority,
            self.state_tree,
            self.input_queue,
            self.output_queue,
        )
        .with_compressible_mint(mint_pda, config_pda(), rent_sponsor_pda());

        let account_metas = meta_config.to_account_metas();

        let data = instruction_data
            .data()
            .map_err(|e| ProgramError::BorshIoError(e.to_string()))?;

        Ok(Instruction {
            program_id: Pubkey::new_from_array(light_token_interface::LIGHT_TOKEN_PROGRAM_ID),
            accounts: account_metas,
            data,
        })
    }
}

// ============================================================================
// CPI Struct: DecompressMintCpi
// ============================================================================

/// Decompress a Light Mint to a Mint Solana account via CPI.
///
/// Creates an on-chain Mint PDA that becomes the source of truth.
/// The Mint is always compressible.
///
/// # Example
/// ```rust,ignore
/// DecompressMintCpi {
///     authority: authority_account,
///     payer: payer_account,
///     mint: mint_account,
///     compressible_config: config_account,
///     rent_sponsor: rent_sponsor_account,
///     state_tree: state_tree_account,
///     input_queue: input_queue_account,
///     output_queue: output_queue_account,
///     system_accounts,
///     compressed_mint_with_context,
///     proof,
///     rent_payment: 16,
///     write_top_up: 766,
/// }
/// .invoke()?;
/// ```
pub struct DecompressMintCpi<'info> {
    /// Mint authority (must sign)
    pub authority: AccountInfo<'info>,
    /// Fee payer
    pub payer: AccountInfo<'info>,
    /// Mint PDA account (writable)
    pub mint: AccountInfo<'info>,
    /// CompressibleConfig account
    pub compressible_config: AccountInfo<'info>,
    /// Rent sponsor PDA account
    pub rent_sponsor: AccountInfo<'info>,
    /// State tree for the Light Mint
    pub state_tree: AccountInfo<'info>,
    /// Input queue for reading Light Mint
    pub input_queue: AccountInfo<'info>,
    /// Output queue for updated Light Mint
    pub output_queue: AccountInfo<'info>,
    /// System accounts for Light Protocol
    pub system_accounts: SystemAccountInfos<'info>,
    /// Light Mint with context (from indexer)
    pub compressed_mint_with_context: MintWithContext,
    /// Validity proof for the Light Mint
    pub proof: ValidityProof,
    /// Rent payment in epochs (must be >= 2)
    pub rent_payment: u8,
    /// Lamports for future write operations
    pub write_top_up: u32,
}

impl<'info> DecompressMintCpi<'info> {
    pub fn instruction(&self) -> Result<Instruction, ProgramError> {
        DecompressMint::try_from(self)?.instruction()
    }

    pub fn invoke(self) -> Result<(), ProgramError> {
        let instruction = self.instruction()?;

        // Account order must match to_account_metas() from MintActionMetaConfig:
        // 1. light_system_program
        // 2. mint_signer (no sign for decompress)
        // 3. authority (signer)
        // 4. compressible_config
        // 5. mint
        // 6. rent_sponsor
        // 7. fee_payer (signer)
        // 8. cpi_authority_pda
        // 9. registered_program_pda
        // 10. account_compression_authority
        // 11. account_compression_program
        // 12. system_program
        // 13. output_queue
        // 14. tree_pubkey (state_tree)
        // 15. input_queue
        let account_infos = self.build_account_infos();
        invoke(&instruction, &account_infos)
    }

    pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
        let instruction = self.instruction()?;
        let account_infos = self.build_account_infos();
        invoke_signed(&instruction, &account_infos, signer_seeds)
    }

    fn build_account_infos(&self) -> Vec<AccountInfo<'info>> {
        vec![
            self.system_accounts.light_system_program.clone(),
            self.authority.clone(),
            self.compressible_config.clone(),
            self.mint.clone(),
            self.rent_sponsor.clone(),
            self.payer.clone(),
            self.system_accounts.cpi_authority_pda.clone(),
            self.system_accounts.registered_program_pda.clone(),
            self.system_accounts.account_compression_authority.clone(),
            self.system_accounts.account_compression_program.clone(),
            self.system_accounts.system_program.clone(),
            self.output_queue.clone(),
            self.state_tree.clone(),
            self.input_queue.clone(),
        ]
    }
}

impl<'info> TryFrom<&DecompressMintCpi<'info>> for DecompressMint {
    type Error = ProgramError;

    fn try_from(cpi: &DecompressMintCpi<'info>) -> Result<Self, Self::Error> {
        Ok(Self {
            payer: *cpi.payer.key,
            authority: *cpi.authority.key,
            state_tree: *cpi.state_tree.key,
            input_queue: *cpi.input_queue.key,
            output_queue: *cpi.output_queue.key,
            compressed_mint_with_context: cpi.compressed_mint_with_context.clone(),
            proof: cpi.proof,
            rent_payment: cpi.rent_payment,
            write_top_up: cpi.write_top_up,
        })
    }
}

/// Decompress a Light Mint with CPI context support.
///
/// For use in multi-operation ixns where mints are decompressed
/// along with PDAs and token accounts using a single proof.
#[derive(Debug, Clone)]
pub struct DecompressCMintWithCpiContext {
    /// Mint seed pubkey (used to derive Mint PDA)
    pub mint_seed_pubkey: Pubkey,
    /// Fee payer
    pub payer: Pubkey,
    /// Mint authority (must sign)
    pub authority: Pubkey,
    /// State tree for the Light Mint
    pub state_tree: Pubkey,
    /// Input queue for reading Light Mint
    pub input_queue: Pubkey,
    /// Output queue for updated Light Mint
    pub output_queue: Pubkey,
    /// Light Mint with context (from indexer)
    pub compressed_mint_with_context: MintWithContext,
    /// Validity proof for the Light Mint
    pub proof: ValidityProof,
    /// Rent payment in epochs (must be >= 2)
    pub rent_payment: u8,
    /// Lamports for future write operations
    pub write_top_up: u32,
    /// CPI context account
    pub cpi_context_pubkey: Pubkey,
    /// CPI context flags
    pub cpi_context: CpiContext,
    /// Compressible config account (ctoken's config)
    pub compressible_config: Pubkey,
    /// Rent sponsor account (ctoken's rent sponsor)
    pub rent_sponsor: Pubkey,
}

impl DecompressCMintWithCpiContext {
    pub fn instruction(self) -> Result<Instruction, ProgramError> {
        // Derive Mint PDA
        let (mint_pda, _cmint_bump) = crate::instruction::find_mint_address(&self.mint_seed_pubkey);

        // Build DecompressMintAction
        let action = DecompressMintAction {
            rent_payment: self.rent_payment,
            write_top_up: self.write_top_up,
        };

        // Build instruction data with CPI context
        let instruction_data = MintActionCompressedInstructionData::new(
            self.compressed_mint_with_context,
            self.proof.0,
        )
        .with_decompress_mint(action)
        .with_cpi_context(self.cpi_context.clone());

        // Build account metas with compressible Mint and CPI context
        // Use provided config/rent_sponsor instead of hardcoded defaults
        let mut meta_config = MintActionMetaConfig::new(
            self.payer,
            self.authority,
            self.state_tree,
            self.input_queue,
            self.output_queue,
        )
        .with_compressible_mint(mint_pda, self.compressible_config, self.rent_sponsor);

        meta_config.cpi_context = Some(self.cpi_context_pubkey);

        let account_metas = meta_config.to_account_metas();

        let data = instruction_data
            .data()
            .map_err(|e| ProgramError::BorshIoError(e.to_string()))?;

        Ok(Instruction {
            program_id: Pubkey::new_from_array(light_token_interface::LIGHT_TOKEN_PROGRAM_ID),
            accounts: account_metas,
            data,
        })
    }
}

/// CPI struct for decompressing a mint with CPI context.
pub struct DecompressCMintCpiWithContext<'info> {
    /// Mint seed account (used to derive Mint PDA, does not sign)
    pub mint_seed: AccountInfo<'info>,
    /// Mint authority (must sign)
    pub authority: AccountInfo<'info>,
    /// Fee payer
    pub payer: AccountInfo<'info>,
    /// Mint PDA account (writable)
    pub mint: AccountInfo<'info>,
    /// CompressibleConfig account
    pub compressible_config: AccountInfo<'info>,
    /// Rent sponsor PDA account
    pub rent_sponsor: AccountInfo<'info>,
    /// State tree for the Light Mint
    pub state_tree: AccountInfo<'info>,
    /// Input queue for reading Light Mint
    pub input_queue: AccountInfo<'info>,
    /// Output queue for updated Light Mint
    pub output_queue: AccountInfo<'info>,
    /// CPI context account
    pub cpi_context_account: AccountInfo<'info>,
    /// System accounts for Light Protocol
    pub system_accounts: SystemAccountInfos<'info>,
    /// Light token program's CPI authority (GXtd2izAiMJPwMEjfgTRH3d7k9mjn4Jq3JrWFv9gySYy)
    /// This is separate from system_accounts.cpi_authority_pda which is the calling program's authority
    pub light_token_cpi_authority: AccountInfo<'info>,
    /// Light Mint with context (from indexer)
    pub compressed_mint_with_context: MintWithContext,
    /// Validity proof for the Light Mint
    pub proof: ValidityProof,
    /// Rent payment in epochs (must be >= 2)
    pub rent_payment: u8,
    /// Lamports for future write operations
    pub write_top_up: u32,
    /// CPI context flags
    pub cpi_context: CpiContext,
}

impl<'info> DecompressCMintCpiWithContext<'info> {
    pub fn instruction(&self) -> Result<Instruction, ProgramError> {
        DecompressCMintWithCpiContext {
            mint_seed_pubkey: *self.mint_seed.key,
            payer: *self.payer.key,
            authority: *self.authority.key,
            state_tree: *self.state_tree.key,
            input_queue: *self.input_queue.key,
            output_queue: *self.output_queue.key,
            compressed_mint_with_context: self.compressed_mint_with_context.clone(),
            proof: self.proof,
            rent_payment: self.rent_payment,
            write_top_up: self.write_top_up,
            cpi_context_pubkey: *self.cpi_context_account.key,
            cpi_context: self.cpi_context.clone(),
            compressible_config: *self.compressible_config.key,
            rent_sponsor: *self.rent_sponsor.key,
        }
        .instruction()
    }

    pub fn invoke(self) -> Result<(), ProgramError> {
        let instruction = self.instruction()?;
        let account_infos = self.build_account_infos();
        invoke(&instruction, &account_infos)
    }

    pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
        let instruction = self.instruction()?;
        let account_infos = self.build_account_infos();
        invoke_signed(&instruction, &account_infos, signer_seeds)
    }

    fn build_account_infos(&self) -> Vec<AccountInfo<'info>> {
        vec![
            self.system_accounts.light_system_program.clone(),
            self.mint_seed.clone(),
            self.authority.clone(),
            self.compressible_config.clone(),
            self.mint.clone(),
            self.rent_sponsor.clone(),
            self.payer.clone(),
            // Use ctoken's CPI authority for the CPI, not the calling program's authority
            self.light_token_cpi_authority.clone(),
            self.system_accounts.registered_program_pda.clone(),
            self.system_accounts.account_compression_authority.clone(),
            self.system_accounts.account_compression_program.clone(),
            self.system_accounts.system_program.clone(),
            self.cpi_context_account.clone(),
            self.output_queue.clone(),
            self.state_tree.clone(),
            self.input_queue.clone(),
        ]
    }
}

/// Helper to create CPI context for first write (first_set_context = true)
pub fn create_decompress_mint_cpi_context_first(
    address_tree_pubkey: [u8; 32],
    tree_index: u8,
    queue_index: u8,
) -> CpiContext {
    CpiContext {
        first_set_context: true,
        set_context: false,
        in_tree_index: tree_index,
        in_queue_index: queue_index,
        out_queue_index: queue_index,
        token_out_queue_index: 0,
        assigned_account_index: 0,
        read_only_address_trees: [0; 4],
        address_tree_pubkey,
    }
}

/// Helper to create CPI context for subsequent writes (set_context = true)
pub fn create_decompress_mint_cpi_context_set(
    address_tree_pubkey: [u8; 32],
    tree_index: u8,
    queue_index: u8,
) -> CpiContext {
    CpiContext {
        first_set_context: false,
        set_context: true,
        in_tree_index: tree_index,
        in_queue_index: queue_index,
        out_queue_index: queue_index,
        token_out_queue_index: 0,
        assigned_account_index: 0,
        read_only_address_trees: [0; 4],
        address_tree_pubkey,
    }
}

/// Helper to create CPI context for execution (both false - consumes context)
pub fn create_decompress_mint_cpi_context_execute(
    address_tree_pubkey: [u8; 32],
    tree_index: u8,
    queue_index: u8,
) -> CpiContext {
    CpiContext {
        first_set_context: false,
        set_context: false,
        in_tree_index: tree_index,
        in_queue_index: queue_index,
        out_queue_index: queue_index,
        token_out_queue_index: 0,
        assigned_account_index: 0,
        read_only_address_trees: [0; 4],
        address_tree_pubkey,
    }
}