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