hpsvm_token/
freeze_account.rs1use 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#[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 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 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 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 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 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}