mpl_testing_utils/utils/
mod.rs

1mod edition_marker;
2mod external_price;
3mod master_edition_v2;
4mod metadata;
5mod vault;
6
7pub use edition_marker::EditionMarker;
8pub use external_price::ExternalPrice;
9pub use master_edition_v2::MasterEditionV2;
10pub use metadata::Metadata;
11use solana_program_test::*;
12use solana_sdk::{
13    account::Account, program_pack::Pack, pubkey::Pubkey, signature::Signer,
14    signer::keypair::Keypair, system_instruction, transaction::Transaction,
15};
16use spl_token::state::Mint;
17pub use vault::Vault;
18
19pub fn program_test() -> ProgramTest {
20    ProgramTest::new("mpl_token_metadata", mpl_token_metadata::id(), None)
21}
22
23pub async fn get_account(context: &mut ProgramTestContext, pubkey: &Pubkey) -> Account {
24    context
25        .banks_client
26        .get_account(*pubkey)
27        .await
28        .expect("account not found")
29        .expect("account empty")
30}
31
32pub async fn get_mint(context: &mut ProgramTestContext, pubkey: &Pubkey) -> Mint {
33    let account = get_account(context, pubkey).await;
34    Mint::unpack(&account.data).unwrap()
35}
36
37pub async fn mint_tokens(
38    context: &mut ProgramTestContext,
39    mint: &Pubkey,
40    account: &Pubkey,
41    amount: u64,
42    owner: &Pubkey,
43    additional_signer: Option<&Keypair>,
44) -> Result<(), BanksClientError> {
45    let mut signing_keypairs = vec![&context.payer];
46    if let Some(signer) = additional_signer {
47        signing_keypairs.push(signer);
48    }
49
50    let tx = Transaction::new_signed_with_payer(
51        &[
52            spl_token::instruction::mint_to(&spl_token::id(), mint, account, owner, &[], amount)
53                .unwrap(),
54        ],
55        Some(&context.payer.pubkey()),
56        &signing_keypairs,
57        context.last_blockhash,
58    );
59
60    context.banks_client.process_transaction(tx).await
61}
62
63pub async fn create_token_account(
64    context: &mut ProgramTestContext,
65    account: &Keypair,
66    mint: &Pubkey,
67    manager: &Pubkey,
68) -> Result<(), BanksClientError> {
69    let rent = context.banks_client.get_rent().await.unwrap();
70
71    let tx = Transaction::new_signed_with_payer(
72        &[
73            system_instruction::create_account(
74                &context.payer.pubkey(),
75                &account.pubkey(),
76                rent.minimum_balance(spl_token::state::Account::LEN),
77                spl_token::state::Account::LEN as u64,
78                &spl_token::id(),
79            ),
80            spl_token::instruction::initialize_account(
81                &spl_token::id(),
82                &account.pubkey(),
83                mint,
84                manager,
85            )
86            .unwrap(),
87        ],
88        Some(&context.payer.pubkey()),
89        &[&context.payer, account],
90        context.last_blockhash,
91    );
92
93    context.banks_client.process_transaction(tx).await
94}
95
96pub async fn create_mint(
97    context: &mut ProgramTestContext,
98    mint: &Keypair,
99    manager: &Pubkey,
100    freeze_authority: Option<&Pubkey>,
101) -> Result<(), BanksClientError> {
102    let rent = context.banks_client.get_rent().await.unwrap();
103
104    let tx = Transaction::new_signed_with_payer(
105        &[
106            system_instruction::create_account(
107                &context.payer.pubkey(),
108                &mint.pubkey(),
109                rent.minimum_balance(spl_token::state::Mint::LEN),
110                spl_token::state::Mint::LEN as u64,
111                &spl_token::id(),
112            ),
113            spl_token::instruction::initialize_mint(
114                &spl_token::id(),
115                &mint.pubkey(),
116                manager,
117                freeze_authority,
118                0,
119            )
120            .unwrap(),
121        ],
122        Some(&context.payer.pubkey()),
123        &[&context.payer, mint],
124        context.last_blockhash,
125    );
126
127    context.banks_client.process_transaction(tx).await
128}