Skip to main content

hpsvm_token/
create_native_mint_2022.rs

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