litesvm_token/
burn_checked.rs

1use {
2    super::{
3        get_multisig_signers, get_spl_account,
4        spl_token::{instruction::burn_checked, state::Mint},
5        TOKEN_ID,
6    },
7    litesvm::{types::FailedTransactionMetadata, LiteSVM},
8    smallvec::{smallvec, SmallVec},
9    solana_keypair::Keypair,
10    solana_pubkey::Pubkey,
11    solana_signer::{signers::Signers, Signer},
12    solana_transaction::Transaction,
13};
14
15/// ### Description
16/// Builder for the [`burn_checked`] instruction.
17///
18/// ### Optional fields
19/// - `authority`: `payer` by default.
20/// - `decimals`: `mint` decimals by default.
21/// - `token_program_id`: [`TOKEN_ID`] by default.
22pub struct BurnChecked<'a> {
23    svm: &'a mut LiteSVM,
24    payer: &'a Keypair,
25    mint: &'a Pubkey,
26    account: &'a Pubkey,
27    token_program_id: Option<&'a Pubkey>,
28    amount: u64,
29    decimals: Option<u8>,
30    signers: SmallVec<[&'a Keypair; 1]>,
31    owner: Option<Pubkey>,
32}
33
34impl<'a> BurnChecked<'a> {
35    /// Creates a new instance of [`burn_checked`] instruction.
36    pub fn new(
37        svm: &'a mut LiteSVM,
38        payer: &'a Keypair,
39        mint: &'a Pubkey,
40        account: &'a Pubkey,
41        amount: u64,
42    ) -> Self {
43        BurnChecked {
44            svm,
45            payer,
46            mint,
47            account,
48            token_program_id: None,
49            amount,
50            decimals: None,
51            owner: None,
52            signers: smallvec![payer],
53        }
54    }
55
56    /// Sets the token program id of the burn.
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 decimals of the burn.
63    pub fn decimals(mut self, value: u8) -> Self {
64        self.decimals = Some(value);
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 Pubkey, 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 mint: Mint = get_spl_account(self.svm, self.mint)?;
92        let ix = burn_checked(
93            token_program_id,
94            self.account,
95            self.mint,
96            &authority,
97            &signer_keys,
98            self.amount,
99            self.decimals.unwrap_or(mint.decimals),
100        )?;
101
102        let block_hash = self.svm.latest_blockhash();
103        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
104        tx.partial_sign(&[self.payer], block_hash);
105        tx.partial_sign(self.signers.as_ref(), block_hash);
106
107        self.svm.send_transaction(tx)?;
108
109        Ok(())
110    }
111}