litesvm_token/
create_mint.rs

1#[cfg(not(feature = "token-2022"))]
2use solana_program_pack::Pack;
3#[cfg(feature = "token-2022")]
4use spl_token_2022_interface::extension::ExtensionType;
5use {
6    super::{
7        spl_token::{instruction::initialize_mint2, state::Mint},
8        TOKEN_ID,
9    },
10    litesvm::{types::FailedTransactionMetadata, LiteSVM},
11    solana_keypair::Keypair,
12    solana_pubkey::Pubkey,
13    solana_signer::Signer,
14    solana_system_interface::instruction::create_account,
15    solana_transaction::Transaction,
16};
17
18/// ### Description
19/// Builder for the [`initialize_mint2`] instruction.
20///
21/// ### Optional fields
22/// - `authority`: `payer` by default.
23/// - `freeze_authority`: None by default.
24/// - `decimals`: 8 by default.
25/// - `token_program_id`: [`TOKEN_ID`] by default.
26pub struct CreateMint<'a> {
27    svm: &'a mut LiteSVM,
28    payer: &'a Keypair,
29    authority: Option<&'a Pubkey>,
30    freeze_authority: Option<&'a Pubkey>,
31    decimals: Option<u8>,
32    token_program_id: Option<&'a Pubkey>,
33}
34
35impl<'a> CreateMint<'a> {
36    /// Creates a new instance of the [`initialize_mint2`] instruction.
37    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair) -> Self {
38        CreateMint {
39            svm,
40            payer,
41            authority: None,
42            freeze_authority: None,
43            decimals: None,
44            token_program_id: None,
45        }
46    }
47
48    /// Sets the authority of the mint.
49    pub fn authority(mut self, authority: &'a Pubkey) -> Self {
50        self.authority = Some(authority);
51        self
52    }
53
54    /// Sets the freeze authority of the mint.
55    pub fn freeze_authority(mut self, freeze_authority: &'a Pubkey) -> Self {
56        self.freeze_authority = Some(freeze_authority);
57        self
58    }
59
60    /// Sets the decimals of the mint.
61    pub fn decimals(mut self, value: u8) -> Self {
62        self.decimals = Some(value);
63        self
64    }
65
66    /// Sets the token program id of the mint account.
67    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
68        self.token_program_id = Some(program_id);
69        self
70    }
71
72    /// Sends the transaction.
73    pub fn send(self) -> Result<Pubkey, FailedTransactionMetadata> {
74        #[cfg(feature = "token-2022")]
75        let mint_size = ExtensionType::try_calculate_account_len::<Mint>(&[])?;
76        #[cfg(not(feature = "token-2022"))]
77        let mint_size = Mint::LEN;
78        let mint_kp = Keypair::new();
79        let mint_pk = mint_kp.pubkey();
80        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
81        let payer_pk = self.payer.pubkey();
82
83        let ix1 = create_account(
84            &payer_pk,
85            &mint_pk,
86            self.svm.minimum_balance_for_rent_exemption(mint_size),
87            mint_size as u64,
88            token_program_id,
89        );
90        let ix2 = initialize_mint2(
91            token_program_id,
92            &mint_pk,
93            self.authority.unwrap_or(&payer_pk),
94            self.freeze_authority,
95            self.decimals.unwrap_or(8),
96        )?;
97
98        let block_hash = self.svm.latest_blockhash();
99        let tx = Transaction::new_signed_with_payer(
100            &[ix1, ix2],
101            Some(&payer_pk),
102            &[self.payer, &mint_kp],
103            block_hash,
104        );
105        self.svm.send_transaction(tx)?;
106
107        Ok(mint_pk)
108    }
109}