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