hpsvm_token/
mint_to_checked.rs1use 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::{
8 TOKEN_ID, get_multisig_signers, get_spl_account,
9 spl_token::{instruction::mint_to_checked, state::Mint},
10};
11
12#[derive(Debug)]
20pub struct MintToChecked<'a> {
21 svm: &'a mut HPSVM,
22 payer: &'a Keypair,
23 mint: &'a Address,
24 destination: &'a Address,
25 token_program_id: Option<&'a Address>,
26 amount: u64,
27 decimals: Option<u8>,
28 signers: SmallVec<[&'a Keypair; 1]>,
29 owner: Option<Address>,
30}
31
32impl<'a> MintToChecked<'a> {
33 pub fn new(
35 svm: &'a mut HPSVM,
36 payer: &'a Keypair,
37 mint: &'a Address,
38 destination: &'a Address,
39 amount: u64,
40 ) -> Self {
41 MintToChecked {
42 svm,
43 payer,
44 mint,
45 destination,
46 token_program_id: None,
47 amount,
48 decimals: None,
49 signers: smallvec![payer],
50 owner: None,
51 }
52 }
53
54 pub fn decimals(mut self, value: u8) -> Self {
56 self.decimals = Some(value);
57 self
58 }
59
60 pub fn owner(mut self, owner: &'a Keypair) -> Self {
62 self.owner = Some(owner.pubkey());
63 self.signers = smallvec![owner];
64 self
65 }
66
67 pub fn multisig(mut self, multisig: &'a Address, signers: &'a [&'a Keypair]) -> Self {
69 self.owner = Some(*multisig);
70 self.signers = SmallVec::from(signers);
71 self
72 }
73
74 pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
76 self.token_program_id = Some(program_id);
77 self
78 }
79
80 pub fn send(self) -> Result<(), FailedTransactionMetadata> {
82 let payer_pk = self.payer.pubkey();
83 let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
84
85 let authority = self.owner.unwrap_or(payer_pk);
86 let signing_keys = self.signers.pubkeys();
87 let signer_keys = get_multisig_signers(&authority, &signing_keys);
88
89 let mint: Mint = get_spl_account(self.svm, self.mint)?;
90 let ix = mint_to_checked(
91 token_program_id,
92 self.mint,
93 self.destination,
94 &authority,
95 &signer_keys,
96 self.amount,
97 self.decimals.unwrap_or(mint.decimals),
98 )?;
99
100 super::sign_and_send(self.svm, self.payer, &self.signers, ix)
101 }
102}