litesvm_token/
freeze_account.rs

1use {
2    super::{get_multisig_signers, spl_token::instruction::freeze_account, TOKEN_ID},
3    litesvm::{types::FailedTransactionMetadata, LiteSVM},
4    smallvec::{smallvec, SmallVec},
5    solana_keypair::Keypair,
6    solana_pubkey::Pubkey,
7    solana_signer::{signers::Signers, Signer},
8    solana_transaction::Transaction,
9};
10
11/// ### Description
12/// Builder for the [`freeze_account`] instruction.
13///
14/// ### Optional fields
15/// - `account`: associated token account of the owner by default.
16/// - `owner`: `payer` by default.
17/// - `token_program_id`: [`TOKEN_ID`] by default.
18pub struct FreezeAccount<'a> {
19    svm: &'a mut LiteSVM,
20    payer: &'a Keypair,
21    mint: &'a Pubkey,
22    account: Option<&'a Pubkey>,
23    token_program_id: Option<&'a Pubkey>,
24    signers: SmallVec<[&'a Keypair; 1]>,
25    owner: Option<Pubkey>,
26}
27
28impl<'a> FreezeAccount<'a> {
29    /// Creates a new instance of [`freeze_account`] instruction.
30    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair, mint: &'a Pubkey) -> Self {
31        FreezeAccount {
32            svm,
33            payer,
34            account: None,
35            mint,
36            owner: None,
37            signers: smallvec![payer],
38            token_program_id: None,
39        }
40    }
41
42    /// Sets the owner of the account with single owner.
43    pub fn owner(mut self, owner: &'a Keypair) -> Self {
44        self.owner = Some(owner.pubkey());
45        self.signers = smallvec![owner];
46        self
47    }
48
49    /// Sets the owner of the account with multisig owner.
50    pub fn multisig(mut self, multisig: &'a Pubkey, signers: &'a [&'a Keypair]) -> Self {
51        self.owner = Some(*multisig);
52        self.signers = SmallVec::from(signers);
53        self
54    }
55
56    /// Sets the token program id for the instruction.
57    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
58        self.token_program_id = Some(program_id);
59        self
60    }
61
62    /// Sends the transaction.
63    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
64        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
65        let payer_pk = self.payer.pubkey();
66
67        let authority = self.owner.unwrap_or(payer_pk);
68        let signing_keys = self.signers.pubkeys();
69        let signer_keys = get_multisig_signers(&authority, &signing_keys);
70
71        let account = if let Some(account) = self.account {
72            *account
73        } else {
74            spl_associated_token_account_interface::address::get_associated_token_address_with_program_id(
75                &authority,
76                self.mint,
77                token_program_id,
78            )
79        };
80
81        let ix = freeze_account(
82            token_program_id,
83            &account,
84            self.mint,
85            &authority,
86            &signer_keys,
87        )?;
88
89        let block_hash = self.svm.latest_blockhash();
90        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
91        tx.partial_sign(&[self.payer], block_hash);
92        tx.partial_sign(self.signers.as_ref(), block_hash);
93
94        self.svm.send_transaction(tx)?;
95
96        Ok(())
97    }
98}