hpsvm_token/
create_mint.rs1use hpsvm::{HPSVM, types::FailedTransactionMetadata};
2use solana_address::Address;
3use solana_keypair::Keypair;
4#[cfg(not(feature = "token-2022"))]
5use solana_program_pack::Pack;
6use solana_signer::Signer;
7use solana_system_interface::instruction::create_account;
8use solana_transaction::Transaction;
9#[cfg(feature = "token-2022")]
10use spl_token_2022_interface::extension::ExtensionType;
11
12use super::{
13 TOKEN_ID,
14 spl_token::{instruction::initialize_mint2, state::Mint},
15};
16
17#[derive(Debug)]
26pub struct CreateMint<'a> {
27 svm: &'a mut HPSVM,
28 payer: &'a Keypair,
29 authority: Option<&'a Address>,
30 freeze_authority: Option<&'a Address>,
31 decimals: Option<u8>,
32 token_program_id: Option<&'a Address>,
33}
34
35impl<'a> CreateMint<'a> {
36 pub fn new(svm: &'a mut HPSVM, 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 pub fn authority(mut self, authority: &'a Address) -> Self {
50 self.authority = Some(authority);
51 self
52 }
53
54 pub fn freeze_authority(mut self, freeze_authority: &'a Address) -> Self {
56 self.freeze_authority = Some(freeze_authority);
57 self
58 }
59
60 pub fn decimals(mut self, value: u8) -> Self {
62 self.decimals = Some(value);
63 self
64 }
65
66 pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
68 self.token_program_id = Some(program_id);
69 self
70 }
71
72 pub fn send(self) -> Result<Address, 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}