litesvm_token/
approve.rs

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