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