litesvm_token/
mint_to_checked.rs

1use {
2    super::{
3        get_multisig_signers, get_spl_account,
4        spl_token::{instruction::mint_to_checked, state::Mint},
5        TOKEN_ID,
6    },
7    litesvm::{types::FailedTransactionMetadata, LiteSVM},
8    smallvec::{smallvec, SmallVec},
9    solana_keypair::Keypair,
10    solana_pubkey::Pubkey,
11    solana_signer::{signers::Signers, Signer},
12    solana_transaction::Transaction,
13};
14
15/// ### Description
16/// Builder for the [`mint_to_checked`] instruction.
17///
18/// ### Optional fields
19/// - `owner`: `payer` by default.
20/// - `decimals`: `mint` decimals by default.
21/// - `token_program_id`: [`TOKEN_ID`] by default.
22pub struct MintToChecked<'a> {
23    svm: &'a mut LiteSVM,
24    payer: &'a Keypair,
25    mint: &'a Pubkey,
26    destination: &'a Pubkey,
27    token_program_id: Option<&'a Pubkey>,
28    amount: u64,
29    decimals: Option<u8>,
30    signers: SmallVec<[&'a Keypair; 1]>,
31    owner: Option<Pubkey>,
32}
33
34impl<'a> MintToChecked<'a> {
35    /// Creates a new instance of [`mint_to_checked`] instruction.
36    pub fn new(
37        svm: &'a mut LiteSVM,
38        payer: &'a Keypair,
39        mint: &'a Pubkey,
40        destination: &'a Pubkey,
41        amount: u64,
42    ) -> Self {
43        MintToChecked {
44            svm,
45            payer,
46            mint,
47            destination,
48            token_program_id: None,
49            amount,
50            decimals: None,
51            signers: smallvec![payer],
52            owner: None,
53        }
54    }
55
56    /// Sets the decimals of the burn.
57    pub fn decimals(mut self, value: u8) -> Self {
58        self.decimals = Some(value);
59        self
60    }
61
62    pub fn owner(mut self, owner: &'a Keypair) -> Self {
63        self.owner = Some(owner.pubkey());
64        self.signers = smallvec![owner];
65        self
66    }
67
68    pub fn multisig(mut self, multisig: &'a Pubkey, signers: &'a [&'a Keypair]) -> Self {
69        self.owner = Some(*multisig);
70        self.signers = SmallVec::from(signers);
71        self
72    }
73
74    /// Sets the token program id of the mint account.
75    pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
76        self.token_program_id = Some(program_id);
77        self
78    }
79
80    /// Sends the transaction.
81    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        let block_hash = self.svm.latest_blockhash();
101        let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
102        tx.partial_sign(&[self.payer], block_hash);
103        tx.partial_sign(self.signers.as_ref(), block_hash);
104
105        self.svm.send_transaction(tx)?;
106
107        Ok(())
108    }
109}