bonfida_test_utils/
program_test_ext.rs

1use solana_program::{program_pack::Pack, pubkey::Pubkey};
2use solana_program_test::ProgramTest;
3use solana_sdk::account::Account;
4use spl_token::state::Mint;
5
6pub trait ProgramTestExt {
7    fn add_mint(
8        &mut self,
9        key: Option<Pubkey>,
10        decimals: u8,
11        mint_authority: &Pubkey,
12    ) -> (Pubkey, Mint);
13
14    fn add_account_with_lamports(&mut self, key: Pubkey, lamports: u64);
15}
16
17impl ProgramTestExt for ProgramTest {
18    fn add_mint(
19        &mut self,
20        key: Option<Pubkey>,
21        decimals: u8,
22        mint_authority: &Pubkey,
23    ) -> (Pubkey, Mint) {
24        let address = key.unwrap_or_else(Pubkey::new_unique);
25        let mint_info = Mint {
26            mint_authority: Some(*mint_authority).into(),
27            supply: u32::MAX.into(),
28            decimals,
29            is_initialized: true,
30            freeze_authority: None.into(),
31        };
32        let mut data = [0; Mint::LEN];
33        mint_info.pack_into_slice(&mut data);
34        self.add_account(
35            address,
36            Account {
37                lamports: u32::MAX.into(),
38                data: data.into(),
39                owner: spl_token::ID,
40                executable: false,
41                ..Account::default()
42            },
43        );
44        (address, mint_info)
45    }
46
47    fn add_account_with_lamports(&mut self, key: Pubkey, lamports: u64) {
48        self.add_account(
49            key,
50            Account {
51                lamports,
52                ..Default::default()
53            },
54        );
55    }
56}