Skip to main content

hpsvm_token/
set_authority.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::{
9    TOKEN_ID, get_multisig_signers,
10    spl_token::instruction::{AuthorityType, set_authority},
11};
12
13/// ### Description
14/// Builder for the [`set_authority`] instruction.
15///
16/// ### Optional fields
17/// - `owner`: `payer` by default.
18/// - `token_program_id`: [`TOKEN_ID`] by default.
19#[derive(Debug)]
20pub struct SetAuthority<'a> {
21    svm: &'a mut HPSVM,
22    payer: &'a Keypair,
23    authority_type: AuthorityType,
24    account: &'a Address,
25    new_authority: Option<&'a Address>,
26    signers: SmallVec<[&'a Keypair; 1]>,
27    owner: Option<Address>,
28    token_program_id: Option<&'a Address>,
29}
30
31impl<'a> SetAuthority<'a> {
32    /// Creates a new instance of [`set_authority`] instruction.
33    pub fn new(
34        svm: &'a mut HPSVM,
35        payer: &'a Keypair,
36        account: &'a Address,
37        authority_type: AuthorityType,
38    ) -> Self {
39        SetAuthority {
40            svm,
41            payer,
42            owner: None,
43            authority_type,
44            account,
45            new_authority: None,
46            token_program_id: None,
47            signers: smallvec![payer],
48        }
49    }
50
51    /// Sets the owner of the account with single owner.
52    pub fn owner(mut self, owner: &'a Keypair) -> Self {
53        self.owner = Some(owner.pubkey());
54        self.signers = smallvec![owner];
55        self
56    }
57
58    /// Sets the owner of the account with multisig owner.
59    pub fn multisig(mut self, multisig: &'a Address, signers: &'a [&'a Keypair]) -> Self {
60        self.owner = Some(*multisig);
61        self.signers = SmallVec::from(signers);
62        self
63    }
64
65    /// Sets the token program id for the instruction.
66    pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
67        self.token_program_id = Some(program_id);
68        self
69    }
70
71    /// Sets the new authority.
72    pub fn new_authority(mut self, new_authority: &'a Address) -> Self {
73        self.new_authority = Some(new_authority);
74        self
75    }
76
77    /// Sends the transaction.
78    pub fn send(self) -> Result<(), FailedTransactionMetadata> {
79        let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
80        let payer_pk = self.payer.pubkey();
81
82        let authority = self.owner.unwrap_or(payer_pk);
83        let signing_keys = self.signers.pubkeys();
84        let signer_keys = get_multisig_signers(&authority, &signing_keys);
85
86        let ix = set_authority(
87            token_program_id,
88            self.account,
89            self.new_authority,
90            self.authority_type,
91            &authority,
92            &signer_keys,
93        )?;
94
95        let block_hash = self.svm.latest_blockhash();
96        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
97        tx.partial_sign(&[self.payer], block_hash);
98        tx.partial_sign(self.signers.as_ref(), block_hash);
99
100        self.svm.send_transaction(tx)?;
101
102        Ok(())
103    }
104}