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