Skip to main content

hpsvm_token/
burn.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::{TOKEN_ID, get_multisig_signers, spl_token::instruction::burn};
9
10/// ### Description
11/// Builder for the [`burn`] instruction.
12///
13/// ### Optional fields
14/// - `authority`: `payer` by default.
15/// - `token_program_id`: [`TOKEN_ID`] by default.
16#[derive(Debug)]
17pub struct Burn<'a> {
18    svm: &'a mut HPSVM,
19    payer: &'a Keypair,
20    mint: &'a Address,
21    account: &'a Address,
22    token_program_id: Option<&'a Address>,
23    amount: u64,
24    signers: SmallVec<[&'a Keypair; 1]>,
25    owner: Option<Address>,
26}
27
28impl<'a> Burn<'a> {
29    /// Creates a new instance of [`burn`] instruction.
30    pub fn new(
31        svm: &'a mut HPSVM,
32        payer: &'a Keypair,
33        mint: &'a Address,
34        account: &'a Address,
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 Address) -> 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 Address, 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 =
79            burn(token_program_id, self.account, self.mint, &authority, &signer_keys, self.amount)?;
80
81        let block_hash = self.svm.latest_blockhash();
82        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
83        tx.partial_sign(&[self.payer], block_hash);
84        tx.partial_sign(self.signers.as_ref(), block_hash);
85
86        self.svm.send_transaction(tx)?;
87
88        Ok(())
89    }
90}