light_token/compressed_token/v2/mint_to_compressed/
instruction.rs

1use light_compressed_account::instruction_data::traits::LightInstructionData;
2use light_token_interface::instructions::mint_action::{CpiContext, MintWithContext, Recipient};
3pub use light_token_types::account_infos::mint_to_compressed::DecompressedMintConfig;
4use light_token_types::CompressedProof;
5use solana_instruction::Instruction;
6use solana_pubkey::Pubkey;
7
8use crate::{
9    compressed_token::mint_action::MintActionMetaConfig,
10    error::{Result, TokenSdkError},
11    spl_interface::SplInterfacePda,
12};
13
14pub const MINT_TO_COMPRESSED_DISCRIMINATOR: u8 = 101;
15
16/// Input parameters for creating a mint_to_compressed instruction
17#[derive(Debug, Clone)]
18pub struct MintToCompressedInputs {
19    pub compressed_mint_inputs: MintWithContext,
20    pub recipients: Vec<Recipient>,
21    pub mint_authority: Pubkey,
22    pub payer: Pubkey,
23    pub state_merkle_tree: Pubkey,
24    pub input_queue: Pubkey,
25    pub output_queue_mint: Pubkey,
26    pub output_queue_tokens: Pubkey,
27    /// Required if the mint is decompressed
28    pub decompressed_mint_config: Option<DecompressedMintConfig<Pubkey>>,
29    pub proof: Option<CompressedProof>,
30    pub token_account_version: u8,
31    pub cpi_context_pubkey: Option<Pubkey>,
32    /// Required if the mint is decompressed
33    pub spl_interface_pda: Option<SplInterfacePda>,
34}
35
36/// Create a mint_to_compressed instruction (wrapper around mint_action)
37pub fn create_mint_to_compressed_instruction(
38    inputs: MintToCompressedInputs,
39    cpi_context: Option<CpiContext>,
40) -> Result<Instruction> {
41    let MintToCompressedInputs {
42        compressed_mint_inputs,
43        recipients,
44        mint_authority,
45        payer,
46        state_merkle_tree,
47        input_queue,
48        output_queue_mint,
49        output_queue_tokens: _,
50        decompressed_mint_config: _,
51        proof,
52        token_account_version,
53        cpi_context_pubkey,
54        spl_interface_pda: _,
55    } = inputs;
56
57    let mint_to_action = light_token_interface::instructions::mint_action::MintToCompressedAction {
58        token_account_version,
59        recipients,
60    };
61
62    let mut instruction_data =
63        light_token_interface::instructions::mint_action::MintActionCompressedInstructionData::new(
64            compressed_mint_inputs.clone(),
65            proof,
66        )
67        .with_mint_to_compressed(mint_to_action);
68
69    if let Some(ctx) = cpi_context {
70        instruction_data = instruction_data.with_cpi_context(ctx);
71    }
72
73    let meta_config = if cpi_context_pubkey.is_some() {
74        MintActionMetaConfig::new_cpi_context(
75            &instruction_data,
76            payer,
77            mint_authority,
78            cpi_context_pubkey.unwrap(),
79        )?
80    } else {
81        MintActionMetaConfig::new(
82            payer,
83            mint_authority,
84            state_merkle_tree,
85            input_queue,
86            output_queue_mint,
87        )
88        .with_mint_compressed_tokens()
89    };
90
91    let account_metas = meta_config.to_account_metas();
92
93    let data = instruction_data
94        .data()
95        .map_err(|_| TokenSdkError::SerializationError)?;
96
97    Ok(Instruction {
98        program_id: solana_pubkey::Pubkey::new_from_array(
99            light_token_interface::LIGHT_TOKEN_PROGRAM_ID,
100        ),
101        accounts: account_metas,
102        data,
103    })
104}