litesvm_token/
burn.rs

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