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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use light_compressed_token_sdk::utils::is_light_token_owner;
use solana_account_info::AccountInfo;
use solana_instruction::{AccountMeta, Instruction};
use solana_program_error::ProgramError;
use solana_pubkey::Pubkey;

use super::{
    transfer_checked::TransferChecked, transfer_from_spl::TransferFromSpl,
    transfer_to_spl::TransferToSpl,
};
use crate::error::LightTokenError;

/// Internal enum to classify transfer types based on account owners.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TransferType {
    /// light -> light
    LightToLight,
    /// light -> SPL (decompress)
    LightToSpl,
    /// SPL -> light (compress)
    SplToLight,
    /// SPL -> SPL (pass-through to SPL token program)
    SplToSpl,
}

/// Determine transfer type from account owners.
///
/// Returns `Ok(TransferType)` if at least one account is a Light token account.
/// Returns `Err(UseRegularSplTransfer)` if both accounts are non-Light (SPL) accounts.
/// Returns `Err(CannotDetermineAccountType)` if an account owner is unrecognized.
fn determine_transfer_type(
    source_owner: &Pubkey,
    destination_owner: &Pubkey,
) -> Result<TransferType, ProgramError> {
    let source_is_light = is_light_token_owner(source_owner)
        .map_err(|_| ProgramError::Custom(LightTokenError::CannotDetermineAccountType.into()))?;
    let dest_is_light = is_light_token_owner(destination_owner)
        .map_err(|_| ProgramError::Custom(LightTokenError::CannotDetermineAccountType.into()))?;

    match (source_is_light, dest_is_light) {
        (true, true) => Ok(TransferType::LightToLight),
        (true, false) => Ok(TransferType::LightToSpl),
        (false, true) => Ok(TransferType::SplToLight),
        (false, false) => {
            // Both are SPL - verify same token program
            if source_owner == destination_owner {
                Ok(TransferType::SplToSpl)
            } else {
                Err(ProgramError::Custom(
                    LightTokenError::SplTokenProgramMismatch.into(),
                ))
            }
        }
    }
}

/// Required accounts to interface between light and SPL token accounts (Pubkey-based).
///
/// Use this struct when building instructions outside of CPI context.
#[derive(Debug, Clone, Copy)]
pub struct SplInterface {
    pub mint: Pubkey,
    pub spl_token_program: Pubkey,
    pub spl_interface_pda: Pubkey,
    pub spl_interface_pda_bump: u8,
}

impl<'info> From<&SplInterfaceCpi<'info>> for SplInterface {
    fn from(spl: &SplInterfaceCpi<'info>) -> Self {
        Self {
            mint: *spl.mint.key,
            spl_token_program: *spl.spl_token_program.key,
            spl_interface_pda: *spl.spl_interface_pda.key,
            spl_interface_pda_bump: spl.spl_interface_pda_bump,
        }
    }
}

/// Required accounts to interface between light and SPL token accounts (AccountInfo-based).
///
/// Use this struct when building CPIs.
pub struct SplInterfaceCpi<'info> {
    pub mint: AccountInfo<'info>,
    pub spl_token_program: AccountInfo<'info>,
    pub spl_interface_pda: AccountInfo<'info>,
    pub spl_interface_pda_bump: u8,
}

/// # Create a transfer interface instruction that auto-routes based on account types:
/// ```rust
/// # use solana_pubkey::Pubkey;
/// # use light_token::instruction::{TransferInterface, SplInterface, LIGHT_TOKEN_PROGRAM_ID};
/// # let source = Pubkey::new_unique();
/// # let destination = Pubkey::new_unique();
/// # let authority = Pubkey::new_unique();
/// # let payer = Pubkey::new_unique();
/// # let mint = Pubkey::new_unique();
/// // For light -> light transfer (source_owner and destination_owner are LIGHT_TOKEN_PROGRAM_ID)
/// let instruction = TransferInterface {
///     source,
///     destination,
///     amount: 100,
///     decimals: 9,
///     authority,
///     payer,
///     mint,
///     spl_interface: None,
///     source_owner: LIGHT_TOKEN_PROGRAM_ID,
///     destination_owner: LIGHT_TOKEN_PROGRAM_ID,
/// }.instruction()?;
/// # Ok::<(), solana_program_error::ProgramError>(())
/// ```
pub struct TransferInterface {
    pub source: Pubkey,
    pub destination: Pubkey,
    pub amount: u64,
    pub decimals: u8,
    pub authority: Pubkey,
    pub payer: Pubkey,
    pub mint: Pubkey,
    pub spl_interface: Option<SplInterface>,
    /// Owner of the source account (used to determine transfer type)
    pub source_owner: Pubkey,
    /// Owner of the destination account (used to determine transfer type)
    pub destination_owner: Pubkey,
}

impl TransferInterface {
    /// Build instruction based on detected transfer type
    pub fn instruction(self) -> Result<Instruction, ProgramError> {
        match determine_transfer_type(&self.source_owner, &self.destination_owner)? {
            TransferType::LightToLight => TransferChecked {
                source: self.source,
                mint: self.mint,
                destination: self.destination,
                amount: self.amount,
                decimals: self.decimals,
                authority: self.authority,
                fee_payer: self.payer,
            }
            .instruction(),

            TransferType::LightToSpl => {
                let spl = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                TransferToSpl {
                    source: self.source,
                    destination_spl_token_account: self.destination,
                    amount: self.amount,
                    authority: self.authority,
                    mint: spl.mint,
                    payer: self.payer,
                    spl_interface_pda: spl.spl_interface_pda,
                    spl_interface_pda_bump: spl.spl_interface_pda_bump,
                    decimals: self.decimals,
                    spl_token_program: spl.spl_token_program,
                }
                .instruction()
            }

            TransferType::SplToLight => {
                let spl = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                TransferFromSpl {
                    source_spl_token_account: self.source,
                    destination: self.destination,
                    amount: self.amount,
                    authority: self.authority,
                    mint: spl.mint,
                    payer: self.payer,
                    spl_interface_pda: spl.spl_interface_pda,
                    spl_interface_pda_bump: spl.spl_interface_pda_bump,
                    decimals: self.decimals,
                    spl_token_program: spl.spl_token_program,
                }
                .instruction()
            }

            TransferType::SplToSpl => {
                let spl = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;

                // Build SPL transfer_checked instruction manually
                // Discriminator 12 = TransferChecked
                let mut data = vec![12u8];
                data.extend_from_slice(&self.amount.to_le_bytes());
                data.push(self.decimals);

                Ok(Instruction {
                    program_id: self.source_owner, // SPL token program
                    accounts: vec![
                        AccountMeta::new(self.source, false),
                        AccountMeta::new_readonly(spl.mint, false),
                        AccountMeta::new(self.destination, false),
                        AccountMeta::new_readonly(self.authority, true),
                    ],
                    data,
                })
            }
        }
    }
}

impl<'info> From<&TransferInterfaceCpi<'info>> for TransferInterface {
    fn from(cpi: &TransferInterfaceCpi<'info>) -> Self {
        Self {
            source: *cpi.source_account.key,
            destination: *cpi.destination_account.key,
            amount: cpi.amount,
            decimals: cpi.decimals,
            authority: *cpi.authority.key,
            payer: *cpi.payer.key,
            mint: *cpi.mint.key,
            spl_interface: cpi.spl_interface.as_ref().map(SplInterface::from),
            source_owner: *cpi.source_account.owner,
            destination_owner: *cpi.destination_account.owner,
        }
    }
}

/// # Transfer interface via CPI (auto-detects account types):
/// ```rust,no_run
/// # use light_token::instruction::{TransferInterfaceCpi, SplInterfaceCpi};
/// # use solana_account_info::AccountInfo;
/// # let source_account: AccountInfo = todo!();
/// # let destination_account: AccountInfo = todo!();
/// # let authority: AccountInfo = todo!();
/// # let payer: AccountInfo = todo!();
/// # let compressed_token_program_authority: AccountInfo = todo!();
/// # let mint: AccountInfo = todo!();
/// # let system_program: AccountInfo = todo!();
/// TransferInterfaceCpi::new(
///     100,    // amount
///     9,      // decimals
///     source_account,
///     destination_account,
///     authority,
///     payer,
///     compressed_token_program_authority,
///     mint,
///     system_program,
/// )
/// .invoke()?;
/// # Ok::<(), solana_program_error::ProgramError>(())
/// ```
pub struct TransferInterfaceCpi<'info> {
    pub amount: u64,
    pub decimals: u8,
    pub source_account: AccountInfo<'info>,
    pub destination_account: AccountInfo<'info>,
    pub authority: AccountInfo<'info>,
    pub payer: AccountInfo<'info>,
    pub compressed_token_program_authority: AccountInfo<'info>,
    pub mint: AccountInfo<'info>,
    pub spl_interface: Option<SplInterfaceCpi<'info>>,
    /// System program - required for compressible account lamport top-ups
    pub system_program: AccountInfo<'info>,
}

impl<'info> TransferInterfaceCpi<'info> {
    /// # Arguments
    /// * `amount` - Amount to transfer
    /// * `decimals` - Token decimals
    /// * `source_account` - Source token account (can be light or SPL)
    /// * `destination_account` - Destination token account (can be light or SPL)
    /// * `authority` - Authority for the transfer (must be signer)
    /// * `payer` - Payer for the transaction
    /// * `compressed_token_program_authority` - Light Token program authority
    /// * `mint` - Token mint account
    /// * `system_program` - System program (required for compressible account lamport top-ups)
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        amount: u64,
        decimals: u8,
        source_account: AccountInfo<'info>,
        destination_account: AccountInfo<'info>,
        authority: AccountInfo<'info>,
        payer: AccountInfo<'info>,
        compressed_token_program_authority: AccountInfo<'info>,
        mint: AccountInfo<'info>,
        system_program: AccountInfo<'info>,
    ) -> Self {
        Self {
            source_account,
            destination_account,
            authority,
            amount,
            decimals,
            payer,
            compressed_token_program_authority,
            mint,
            spl_interface: None,
            system_program,
        }
    }

    /// # Arguments
    /// * `mint` - Optional mint account (required for SPL<->light transfers)
    /// * `spl_token_program` - Optional SPL token program (required for SPL<->light transfers)
    /// * `spl_interface_pda` - Optional SPL interface PDA (required for SPL<->light transfers)
    /// * `spl_interface_pda_bump` - Optional bump seed for SPL interface PDA
    pub fn with_spl_interface(
        mut self,
        mint: Option<AccountInfo<'info>>,
        spl_token_program: Option<AccountInfo<'info>>,
        spl_interface_pda: Option<AccountInfo<'info>>,
        spl_interface_pda_bump: Option<u8>,
    ) -> Result<Self, ProgramError> {
        let mint =
            mint.ok_or_else(|| ProgramError::Custom(LightTokenError::MissingMintAccount.into()))?;

        let spl_token_program = spl_token_program
            .ok_or_else(|| ProgramError::Custom(LightTokenError::MissingSplTokenProgram.into()))?;

        let spl_interface_pda = spl_interface_pda
            .ok_or_else(|| ProgramError::Custom(LightTokenError::MissingSplInterfacePda.into()))?;

        let spl_interface_pda_bump = spl_interface_pda_bump.ok_or_else(|| {
            ProgramError::Custom(LightTokenError::MissingSplInterfacePdaBump.into())
        })?;

        self.spl_interface = Some(SplInterfaceCpi {
            mint,
            spl_token_program,
            spl_interface_pda,
            spl_interface_pda_bump,
        });
        Ok(self)
    }

    /// Build instruction from CPI context
    pub fn instruction(&self) -> Result<Instruction, ProgramError> {
        TransferInterface::from(self).instruction()
    }

    /// # Errors
    /// * `SplInterfaceRequired` - If transferring to/from SPL without required accounts
    /// * `UseRegularSplTransfer` - If both source and destination are SPL accounts
    pub fn invoke(self) -> Result<(), ProgramError> {
        use solana_cpi::invoke;

        let transfer_type =
            determine_transfer_type(self.source_account.owner, self.destination_account.owner)?;
        let instruction = self.instruction()?;

        match transfer_type {
            TransferType::LightToLight => {
                let account_infos = [
                    self.source_account,
                    self.mint,
                    self.destination_account,
                    self.authority,
                    self.system_program,
                    self.payer,
                ];
                invoke(&instruction, &account_infos)
            }

            TransferType::LightToSpl => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.compressed_token_program_authority,
                    self.payer,
                    config.mint,
                    self.source_account,
                    self.destination_account,
                    self.authority,
                    config.spl_interface_pda,
                    config.spl_token_program,
                ];
                invoke(&instruction, &account_infos)
            }

            TransferType::SplToLight => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.compressed_token_program_authority,
                    self.payer,
                    config.mint,
                    self.destination_account,
                    self.authority,
                    self.source_account,
                    config.spl_interface_pda,
                    config.spl_token_program,
                    self.system_program,
                ];
                invoke(&instruction, &account_infos)
            }

            TransferType::SplToSpl => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.source_account,
                    config.mint,
                    self.destination_account,
                    self.authority,
                ];
                invoke(&instruction, &account_infos)
            }
        }
    }

    /// # Errors
    /// * `SplInterfaceRequired` - If transferring to/from SPL without required accounts
    /// * `UseRegularSplTransfer` - If both source and destination are SPL accounts
    pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
        use solana_cpi::invoke_signed;

        let transfer_type =
            determine_transfer_type(self.source_account.owner, self.destination_account.owner)?;
        let instruction = self.instruction()?;

        match transfer_type {
            TransferType::LightToLight => {
                let account_infos = [
                    self.source_account,
                    self.mint,
                    self.destination_account,
                    self.authority,
                    self.system_program,
                    self.payer,
                ];
                invoke_signed(&instruction, &account_infos, signer_seeds)
            }

            TransferType::LightToSpl => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.compressed_token_program_authority,
                    self.payer,
                    config.mint,
                    self.source_account,
                    self.destination_account,
                    self.authority,
                    config.spl_interface_pda,
                    config.spl_token_program,
                ];
                invoke_signed(&instruction, &account_infos, signer_seeds)
            }

            TransferType::SplToLight => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.compressed_token_program_authority,
                    self.payer,
                    config.mint,
                    self.destination_account,
                    self.authority,
                    self.source_account,
                    config.spl_interface_pda,
                    config.spl_token_program,
                    self.system_program,
                ];
                invoke_signed(&instruction, &account_infos, signer_seeds)
            }

            TransferType::SplToSpl => {
                let config = self.spl_interface.ok_or_else(|| {
                    ProgramError::Custom(LightTokenError::SplInterfaceRequired.into())
                })?;
                let account_infos = [
                    self.source_account,
                    config.mint,
                    self.destination_account,
                    self.authority,
                ];
                invoke_signed(&instruction, &account_infos, signer_seeds)
            }
        }
    }
}