hpsvm_token/
create_account.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_account3, state::Account},
15};
16
17#[derive(Debug)]
24pub struct CreateAccount<'a> {
25 svm: &'a mut HPSVM,
26 payer: &'a Keypair,
27 mint: &'a Address,
28 owner: Option<&'a Address>,
29 account_kp: Option<Keypair>,
30 token_program_id: Option<&'a Address>,
31 #[cfg(feature = "token-2022")]
32 extensions: Vec<ExtensionType>,
33}
34
35impl<'a> CreateAccount<'a> {
36 pub const fn new(svm: &'a mut HPSVM, payer: &'a Keypair, mint: &'a Address) -> Self {
38 CreateAccount {
39 svm,
40 payer,
41 mint,
42 owner: None,
43 account_kp: None,
44 token_program_id: None,
45 #[cfg(feature = "token-2022")]
46 extensions: vec![],
47 }
48 }
49
50 pub const fn owner(mut self, owner: &'a Address) -> Self {
52 self.owner = Some(owner);
53 self
54 }
55
56 pub fn account_kp(mut self, account_kp: Keypair) -> Self {
58 self.account_kp = Some(account_kp);
59 self
60 }
61
62 pub const fn token_program_id(mut self, program_id: &'a Address) -> Self {
64 self.token_program_id = Some(program_id);
65 self
66 }
67
68 pub fn send(self) -> Result<Address, FailedTransactionMetadata> {
70 #[cfg(feature = "token-2022")]
71 let account_len = ExtensionType::try_calculate_account_len::<Account>(&self.extensions)?;
72 #[cfg(not(feature = "token-2022"))]
73 let account_len = Account::LEN;
74
75 let lamports = self.svm.minimum_balance_for_rent_exemption(account_len);
76
77 let account_kp = self.account_kp.unwrap_or(Keypair::new());
78 let account_pk = account_kp.pubkey();
79 let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
80 let payer_pk = self.payer.pubkey();
81
82 let ix1 =
83 create_account(&payer_pk, &account_pk, lamports, account_len as u64, token_program_id);
84
85 let ix2 = initialize_account3(
86 token_program_id,
87 &account_pk,
88 self.mint,
89 self.owner.unwrap_or(&payer_pk),
90 )?;
91
92 let block_hash = self.svm.latest_blockhash();
93 let tx = Transaction::new_signed_with_payer(
94 &[ix1, ix2],
95 Some(&payer_pk),
96 &[self.payer, &account_kp],
97 block_hash,
98 );
99 self.svm.send_transaction(tx)?;
100
101 Ok(account_pk)
102 }
103}