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};
6
7use super::{TOKEN_ID, get_multisig_signers, spl_token::instruction::freeze_account};
8
9/// ### Description
10/// Builder for the [`freeze_account`] instruction.
11///
12/// ### Optional fields
13/// - `account`: associated token account of the owner by default.
14/// - `owner`: `payer` by default.
15/// - `token_program_id`: [`TOKEN_ID`] by default.
16#[derive(Debug)]
17pub struct FreezeAccount<'a> {
18    svm: &'a mut HPSVM,
19    payer: &'a Keypair,
20    mint: &'a Address,
21    account: Option<&'a Address>,
22    token_program_id: Option<&'a Address>,
23    signers: SmallVec<[&'a Keypair; 1]>,
24    owner: Option<Address>,
25}
26
27impl<'a> FreezeAccount<'a> {
28    /// Creates a new instance of [`freeze_account`] instruction.
29    pub fn new(svm: &'a mut HPSVM, payer: &'a Keypair, mint: &'a Address) -> Self {
30        FreezeAccount {
31            svm,
32            payer,
33            account: None,
34            mint,
35            owner: None,
36            signers: smallvec![payer],
37            token_program_id: None,
38        }
39    }
40
41    /// Sets the owner of the account with single owner.
42    pub fn owner(mut self, owner: &'a Keypair) -> Self {
43        self.owner = Some(owner.pubkey());
44        self.signers = smallvec![owner];
45        self
46    }
47
48    /// Sets the owner of the account with multisig owner.
49    pub fn multisig(mut self, multisig: &'a Address, signers: &'a [&'a Keypair]) -> Self {
50        self.owner = Some(*multisig);
51        self.signers = SmallVec::from(signers);
52        self
53    }
54
55    /// Sets the token program id for the instruction.
56    pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
57        self.token_program_id = Some(program_id);
58        self
59    }
60
61    /// Sends the transaction.
62    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
63        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
64        let payer_pk = self.payer.pubkey();
65
66        let authority = self.owner.unwrap_or(payer_pk);
67        let signing_keys = self.signers.pubkeys();
68        let signer_keys = get_multisig_signers(&authority, &signing_keys);
69
70        let account = if let Some(account) = self.account {
71            *account
72        } else {
73            spl_associated_token_account_interface::address::get_associated_token_address_with_program_id(
74                &authority,
75                self.mint,
76                token_program_id,
77            )
78        };
79
80        let ix = freeze_account(token_program_id, &account, self.mint, &authority, &signer_keys)?;
81
82        super::sign_and_send(self.svm, self.payer, &self.signers, ix)
83    }
84}