litesvm_token/
create_ata_idempotent.rs

1use {
2    super::TOKEN_ID,
3    litesvm::{types::FailedTransactionMetadata, LiteSVM},
4    solana_keypair::Keypair,
5    solana_pubkey::Pubkey,
6    solana_signer::Signer,
7    solana_transaction::Transaction,
8    spl_associated_token_account_interface::instruction::create_associated_token_account_idempotent,
9};
10
11/// ### Description
12/// Builder for the [`create_associated_token_account_idempotent`] instruction.
13///
14/// ### Optional fields
15/// - `owner`: `payer` by default.
16/// - `token_program_id`: [`TOKEN_ID`] by default.
17pub struct CreateAssociatedTokenAccountIdempotent<'a> {
18    svm: &'a mut LiteSVM,
19    payer: &'a Keypair,
20    mint: &'a Pubkey,
21    token_program_id: Option<&'a Pubkey>,
22    owner: Option<Pubkey>,
23}
24
25impl<'a> CreateAssociatedTokenAccountIdempotent<'a> {
26    /// Creates a new instance of [`create_associated_token_account_idempotent`] instruction.
27    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair, mint: &'a Pubkey) -> Self {
28        CreateAssociatedTokenAccountIdempotent {
29            svm,
30            payer,
31            owner: None,
32            token_program_id: None,
33            mint,
34        }
35    }
36
37    /// Sets the owner of the account with single owner.
38    pub fn owner(mut self, owner: &'a Pubkey) -> Self {
39        self.owner = Some(*owner);
40        self
41    }
42
43    /// Sets the token program id for the instruction.
44    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
45        self.token_program_id = Some(program_id);
46        self
47    }
48
49    /// Sends the transaction.
50    pub fn send(self) -> Result<Pubkey, FailedTransactionMetadata> {
51        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
52        let payer_pk = self.payer.pubkey();
53
54        let authority = self.owner.unwrap_or(payer_pk);
55
56        let ix = create_associated_token_account_idempotent(
57            &payer_pk,
58            &authority,
59            self.mint,
60            token_program_id,
61        );
62
63        let block_hash = self.svm.latest_blockhash();
64        let tx =
65            Transaction::new_signed_with_payer(&[ix], Some(&payer_pk), &[self.payer], block_hash);
66
67        self.svm.send_transaction(tx)?;
68
69        let ata = spl_associated_token_account_interface::address::get_associated_token_address_with_program_id(
70            &authority,
71            self.mint,
72            token_program_id,
73        );
74
75        Ok(ata)
76    }
77}