Skip to main content

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