Skip to main content

hpsvm_token/
freeze_account.rs

1use hpsvm::{HPSVM, types::FailedTransactionMetadata};
2use smallvec::{SmallVec, smallvec};
3use solana_address::Address;
4use solana_keypair::Keypair;
5use solana_signer::{Signer, signers::Signers};
6use solana_transaction::Transaction;
7
8use super::{TOKEN_ID, get_multisig_signers, spl_token::instruction::freeze_account};
9
10/// ### Description
11/// Builder for the [`freeze_account`] instruction.
12///
13/// ### Optional fields
14/// - `account`: associated token account of the owner by default.
15/// - `owner`: `payer` by default.
16/// - `token_program_id`: [`TOKEN_ID`] by default.
17#[derive(Debug)]
18pub struct FreezeAccount<'a> {
19    svm: &'a mut HPSVM,
20    payer: &'a Keypair,
21    mint: &'a Address,
22    account: Option<&'a Address>,
23    token_program_id: Option<&'a Address>,
24    signers: SmallVec<[&'a Keypair; 1]>,
25    owner: Option<Address>,
26}
27
28impl<'a> FreezeAccount<'a> {
29    /// Creates a new instance of [`freeze_account`] instruction.
30    pub fn new(svm: &'a mut HPSVM, payer: &'a Keypair, mint: &'a Address) -> 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 Address, 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 Address) -> 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(token_program_id, &account, self.mint, &authority, &signer_keys)?;
82
83        let block_hash = self.svm.latest_blockhash();
84        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
85        tx.partial_sign(&[self.payer], block_hash);
86        tx.partial_sign(self.signers.as_ref(), block_hash);
87
88        self.svm.send_transaction(tx)?;
89
90        Ok(())
91    }
92}