Skip to main content

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