Skip to main content

litesvm_token/
create_native_mint_2022.rs

1use {
2    litesvm::{types::FailedTransactionMetadata, LiteSVM},
3    solana_address::Address,
4    solana_keypair::Keypair,
5    solana_signer::Signer,
6    solana_transaction::Transaction,
7    spl_token_2022_interface::instruction::create_native_mint,
8};
9
10/// ### Description
11/// Builder for the [`create_native_mint`] instruction.
12pub struct CreateNativeMint<'a> {
13    svm: &'a mut LiteSVM,
14    payer: &'a Keypair,
15    token_program_id: Option<&'a Address>,
16}
17
18impl<'a> CreateNativeMint<'a> {
19    /// Creates a new instance of [`create_native_mint`] instruction.
20    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair) -> Self {
21        CreateNativeMint {
22            svm,
23            payer,
24            token_program_id: None,
25        }
26    }
27
28    /// Sets the token program id for the instruction.
29    pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
30        self.token_program_id = Some(program_id);
31        self
32    }
33
34    /// Sends the transaction.
35    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
36        let token_program_id = self
37            .token_program_id
38            .unwrap_or(&spl_token_2022_interface::ID);
39        let payer_pk = self.payer.pubkey();
40
41        let ix = create_native_mint(token_program_id, &payer_pk)?;
42
43        let block_hash = self.svm.latest_blockhash();
44        let tx =
45            Transaction::new_signed_with_payer(&[ix], Some(&payer_pk), &[self.payer], block_hash);
46        self.svm.send_transaction(tx)?;
47
48        Ok(())
49    }
50}