litesvm_token/
thaw_account.rs

1use {
2    super::{get_multisig_signers, spl_token::instruction::thaw_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 [`thaw_account`] instruction.
13///
14/// ### Optional fields
15/// - `account`: associated token account of the `payer` by default.
16/// - `authority`: `payer` by default.
17/// - `token_program_id`: [`TOKEN_ID`] by default.
18pub struct ThawAccount<'a> {
19    svm: &'a mut LiteSVM,
20    payer: &'a Keypair,
21    mint: &'a Pubkey,
22    signers: SmallVec<[&'a Keypair; 1]>,
23    account: Option<&'a Pubkey>,
24    owner: Option<Pubkey>,
25    token_program_id: Option<&'a Pubkey>,
26}
27
28impl<'a> ThawAccount<'a> {
29    /// Creates a new instance of [`thaw_account`] instruction.
30    pub fn new(svm: &'a mut LiteSVM, payer: &'a Keypair, mint: &'a Pubkey) -> Self {
31        ThawAccount {
32            svm,
33            payer,
34            mint,
35            account: None,
36            owner: None,
37            token_program_id: None,
38            signers: smallvec![payer],
39        }
40    }
41
42    /// Sets the token program id for the instruction.
43    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
44        self.token_program_id = Some(program_id);
45        self
46    }
47
48    /// Sets the owner of the account with single owner.
49    pub fn owner(mut self, owner: &'a Keypair) -> Self {
50        self.owner = Some(owner.pubkey());
51        self.signers = smallvec![owner];
52        self
53    }
54
55    /// Sets the owner of the account with multisig owner.
56    pub fn multisig(mut self, multisig: &'a Pubkey, signers: &'a [&'a Keypair]) -> Self {
57        self.owner = Some(*multisig);
58        self.signers = SmallVec::from(signers);
59        self
60    }
61
62    /// Sends the transaction.
63    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
64        let payer_pk = self.payer.pubkey();
65        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
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 = thaw_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}