litesvm_token/
create_account.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_account3, state::Account},
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_account3`] instruction.
20///
21/// ### Optional fields
22/// - `owner`: `payer` by default.
23/// - `account_kp`: [`Keypair::new()`] by default.
24pub struct CreateAccount<'a> {
25    svm: &'a mut LiteSVM,
26    payer: &'a Keypair,
27    mint: &'a Pubkey,
28    owner: Option<&'a Pubkey>,
29    account_kp: Option<Keypair>,
30    token_program_id: Option<&'a Pubkey>,
31    #[cfg(feature = "token-2022")]
32    extensions: Vec<ExtensionType>,
33}
34
35impl<'a> CreateAccount<'a> {
36    /// Creates a new instance of the [`initialize_account3`] instruction.
37    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair, mint: &'a Pubkey) -> 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    /// Sets the owner of the spl account.
51    pub fn owner(mut self, owner: &'a Pubkey) -> Self {
52        self.owner = Some(owner);
53        self
54    }
55
56    /// Sets the [`Keypair`] of the spl account.
57    pub fn account_kp(mut self, account_kp: Keypair) -> Self {
58        self.account_kp = Some(account_kp);
59        self
60    }
61
62    /// Sets the token program id of the spl account.
63    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
64        self.token_program_id = Some(program_id);
65        self
66    }
67
68    /// Sends the transaction.
69    pub fn send(self) -> Result<Pubkey, 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 = create_account(
83            &payer_pk,
84            &account_pk,
85            lamports,
86            account_len as u64,
87            token_program_id,
88        );
89
90        let ix2 = initialize_account3(
91            token_program_id,
92            &account_pk,
93            self.mint,
94            self.owner.unwrap_or(&payer_pk),
95        )?;
96
97        let block_hash = self.svm.latest_blockhash();
98        let tx = Transaction::new_signed_with_payer(
99            &[ix1, ix2],
100            Some(&payer_pk),
101            &[self.payer, &account_kp],
102            block_hash,
103        );
104        self.svm.send_transaction(tx)?;
105
106        Ok(account_pk)
107    }
108}