Skip to main content

hpsvm_token/
mint_to.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::mint_to};
8
9/// ### Description
10/// Builder for the [`mint_to`] instruction.
11///
12/// ### Optional fields
13/// - `owner`: payer by default.
14/// - `token_program_id`: [`TOKEN_ID`] by default.
15#[derive(Debug)]
16pub struct MintTo<'a> {
17    svm: &'a mut HPSVM,
18    payer: &'a Keypair,
19    mint: &'a Address,
20    destination: &'a Address,
21    token_program_id: Option<&'a Address>,
22    owner: Option<Address>,
23    signers: SmallVec<[&'a Keypair; 1]>,
24    amount: u64,
25}
26
27impl<'a> MintTo<'a> {
28    /// Creates a new instance of [`mint_to`] instruction.
29    pub fn new(
30        svm: &'a mut HPSVM,
31        payer: &'a Keypair,
32        mint: &'a Address,
33        destination: &'a Address,
34        amount: u64,
35    ) -> Self {
36        MintTo {
37            svm,
38            payer,
39            mint,
40            destination,
41            token_program_id: None,
42            signers: smallvec![payer],
43            owner: None,
44            amount,
45        }
46    }
47
48    /// Sets the token program id of the mint account.
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    /// Set the owner for the mint operation
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    /// Set multisig authorization for the mint operation
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 = mint_to(
78            token_program_id,
79            self.mint,
80            self.destination,
81            &authority,
82            &signer_keys,
83            self.amount,
84        )?;
85
86        super::sign_and_send(self.svm, self.payer, &self.signers, ix)
87    }
88}