|
| 1 | +use std::str::FromStr; |
| 2 | + |
| 3 | +use solana_program_test::ProgramTestContext; |
| 4 | +use solana_sdk::{ |
| 5 | + account::Account, |
| 6 | + program_pack::Pack, |
| 7 | + pubkey::Pubkey, |
| 8 | + signer::{keypair::Keypair, Signer}, |
| 9 | + system_instruction, |
| 10 | + transaction::Transaction, |
| 11 | + transport, |
| 12 | +}; |
| 13 | +use spl_token::state::Mint; |
| 14 | + |
| 15 | +use crate::core::{master_edition_v2::MasterEditionV2, metadata}; |
| 16 | + |
| 17 | +/// Perform native lamports transfer. |
| 18 | +pub async fn _transfer_lamports( |
| 19 | + client: &mut ProgramTestContext, |
| 20 | + wallet: &Keypair, |
| 21 | + to: &Pubkey, |
| 22 | + amount: u64, |
| 23 | +) -> transport::Result<()> { |
| 24 | + let tx = Transaction::new_signed_with_payer( |
| 25 | + &[system_instruction::transfer(&wallet.pubkey(), to, amount)], |
| 26 | + Some(&wallet.pubkey()), |
| 27 | + &[wallet], |
| 28 | + client.last_blockhash, |
| 29 | + ); |
| 30 | + |
| 31 | + client.banks_client.process_transaction(tx).await?; |
| 32 | + |
| 33 | + Ok(()) |
| 34 | +} |
| 35 | + |
| 36 | +pub async fn get_token_account( |
| 37 | + client: &mut ProgramTestContext, |
| 38 | + token_account: &Pubkey, |
| 39 | +) -> transport::Result<spl_token::state::Account> { |
| 40 | + let account = client.banks_client.get_account(*token_account).await?; |
| 41 | + Ok(spl_token::state::Account::unpack(&account.unwrap().data).unwrap()) |
| 42 | +} |
| 43 | + |
| 44 | +pub async fn get_balance(context: &mut ProgramTestContext, pubkey: &Pubkey) -> u64 { |
| 45 | + context.banks_client.get_balance(*pubkey).await.unwrap() |
| 46 | +} |
| 47 | + |
| 48 | +pub async fn get_token_balance(context: &mut ProgramTestContext, token_account: &Pubkey) -> u64 { |
| 49 | + get_token_account(context, token_account) |
| 50 | + .await |
| 51 | + .unwrap() |
| 52 | + .amount |
| 53 | +} |
| 54 | + |
| 55 | +pub async fn airdrop( |
| 56 | + context: &mut ProgramTestContext, |
| 57 | + receiver: &Pubkey, |
| 58 | + amount: u64, |
| 59 | +) -> transport::Result<()> { |
| 60 | + let tx = Transaction::new_signed_with_payer( |
| 61 | + &[system_instruction::transfer( |
| 62 | + &context.payer.pubkey(), |
| 63 | + receiver, |
| 64 | + amount, |
| 65 | + )], |
| 66 | + Some(&context.payer.pubkey()), |
| 67 | + &[&context.payer], |
| 68 | + context.last_blockhash, |
| 69 | + ); |
| 70 | + |
| 71 | + context.banks_client.process_transaction(tx).await.unwrap(); |
| 72 | + Ok(()) |
| 73 | +} |
| 74 | + |
| 75 | +pub fn clone_keypair(keypair: &Keypair) -> Keypair { |
| 76 | + Keypair::from_bytes(&keypair.to_bytes()).unwrap() |
| 77 | +} |
| 78 | + |
| 79 | +pub fn clone_pubkey(pubkey: &Pubkey) -> Pubkey { |
| 80 | + Pubkey::from_str(&pubkey.to_string()).unwrap() |
| 81 | +} |
| 82 | + |
| 83 | +pub async fn get_account(context: &mut ProgramTestContext, pubkey: &Pubkey) -> Account { |
| 84 | + context |
| 85 | + .banks_client |
| 86 | + .get_account(*pubkey) |
| 87 | + .await |
| 88 | + .expect("account not found") |
| 89 | + .expect("account empty") |
| 90 | +} |
| 91 | + |
| 92 | +pub async fn assert_account_empty(context: &mut ProgramTestContext, pubkey: &Pubkey) { |
| 93 | + let account = context |
| 94 | + .banks_client |
| 95 | + .get_account(*pubkey) |
| 96 | + .await |
| 97 | + .expect("Could not get account!"); |
| 98 | + assert_eq!(account, None); |
| 99 | +} |
| 100 | + |
| 101 | +pub async fn _get_mint(context: &mut ProgramTestContext, pubkey: &Pubkey) -> Mint { |
| 102 | + let account = get_account(context, pubkey).await; |
| 103 | + Mint::unpack(&account.data).unwrap() |
| 104 | +} |
| 105 | + |
| 106 | +pub async fn _create_token_account( |
| 107 | + context: &mut ProgramTestContext, |
| 108 | + account: &Keypair, |
| 109 | + mint: &Pubkey, |
| 110 | + manager: &Pubkey, |
| 111 | +) -> transport::Result<()> { |
| 112 | + let rent = context.banks_client.get_rent().await.unwrap(); |
| 113 | + |
| 114 | + let tx = Transaction::new_signed_with_payer( |
| 115 | + &[ |
| 116 | + system_instruction::create_account( |
| 117 | + &context.payer.pubkey(), |
| 118 | + &account.pubkey(), |
| 119 | + rent.minimum_balance(spl_token::state::Account::LEN), |
| 120 | + spl_token::state::Account::LEN as u64, |
| 121 | + &spl_token::id(), |
| 122 | + ), |
| 123 | + spl_token::instruction::initialize_account( |
| 124 | + &spl_token::id(), |
| 125 | + &account.pubkey(), |
| 126 | + mint, |
| 127 | + manager, |
| 128 | + ) |
| 129 | + .unwrap(), |
| 130 | + ], |
| 131 | + Some(&context.payer.pubkey()), |
| 132 | + &[&context.payer, account], |
| 133 | + context.last_blockhash, |
| 134 | + ); |
| 135 | + |
| 136 | + context.banks_client.process_transaction(tx).await |
| 137 | +} |
| 138 | + |
| 139 | +pub async fn create_associated_token_account( |
| 140 | + context: &mut ProgramTestContext, |
| 141 | + wallet: &Pubkey, |
| 142 | + token_mint: &Pubkey, |
| 143 | +) -> transport::Result<Pubkey> { |
| 144 | + let recent_blockhash = context.last_blockhash; |
| 145 | + |
| 146 | + let tx = Transaction::new_signed_with_payer( |
| 147 | + &[ |
| 148 | + spl_associated_token_account::create_associated_token_account( |
| 149 | + &context.payer.pubkey(), |
| 150 | + wallet, |
| 151 | + token_mint, |
| 152 | + ), |
| 153 | + ], |
| 154 | + Some(&context.payer.pubkey()), |
| 155 | + &[&context.payer], |
| 156 | + recent_blockhash, |
| 157 | + ); |
| 158 | + context.banks_client.process_transaction(tx).await.unwrap(); |
| 159 | + |
| 160 | + Ok(spl_associated_token_account::get_associated_token_address( |
| 161 | + wallet, token_mint, |
| 162 | + )) |
| 163 | +} |
| 164 | + |
| 165 | +pub async fn create_mint( |
| 166 | + context: &mut ProgramTestContext, |
| 167 | + authority: &Pubkey, |
| 168 | + freeze_authority: Option<&Pubkey>, |
| 169 | + decimals: u8, |
| 170 | + mint: Option<Keypair>, |
| 171 | +) -> transport::Result<Keypair> { |
| 172 | + let mint = mint.unwrap_or_else(Keypair::new); |
| 173 | + let rent = context.banks_client.get_rent().await.unwrap(); |
| 174 | + |
| 175 | + let tx = Transaction::new_signed_with_payer( |
| 176 | + &[ |
| 177 | + system_instruction::create_account( |
| 178 | + &context.payer.pubkey(), |
| 179 | + &mint.pubkey(), |
| 180 | + rent.minimum_balance(Mint::LEN), |
| 181 | + Mint::LEN as u64, |
| 182 | + &spl_token::id(), |
| 183 | + ), |
| 184 | + spl_token::instruction::initialize_mint( |
| 185 | + &spl_token::id(), |
| 186 | + &mint.pubkey(), |
| 187 | + authority, |
| 188 | + freeze_authority, |
| 189 | + decimals, |
| 190 | + ) |
| 191 | + .unwrap(), |
| 192 | + ], |
| 193 | + Some(&context.payer.pubkey()), |
| 194 | + &[&context.payer, &mint], |
| 195 | + context.last_blockhash, |
| 196 | + ); |
| 197 | + |
| 198 | + context.banks_client.process_transaction(tx).await.unwrap(); |
| 199 | + Ok(mint) |
| 200 | +} |
| 201 | + |
| 202 | +pub async fn mint_to_wallets( |
| 203 | + context: &mut ProgramTestContext, |
| 204 | + mint_pubkey: &Pubkey, |
| 205 | + authority: &Keypair, |
| 206 | + allocations: Vec<(Pubkey, u64)>, |
| 207 | +) -> transport::Result<Vec<Pubkey>> { |
| 208 | + let mut atas = Vec::with_capacity(allocations.len()); |
| 209 | + |
| 210 | + #[allow(clippy::needless_range_loop)] |
| 211 | + for i in 0..allocations.len() { |
| 212 | + let ata = create_associated_token_account(context, &allocations[i].0, mint_pubkey).await?; |
| 213 | + // println!("Minting to wallet {}", i); |
| 214 | + // println!( |
| 215 | + // "Token account ATA: {:#?}", |
| 216 | + // get_token_account(context, &ata).await.unwrap() |
| 217 | + // ); |
| 218 | + mint_tokens( |
| 219 | + context, |
| 220 | + authority, |
| 221 | + mint_pubkey, |
| 222 | + &ata, |
| 223 | + allocations[i].1, |
| 224 | + None, |
| 225 | + ) |
| 226 | + .await?; |
| 227 | + atas.push(ata); |
| 228 | + } |
| 229 | + Ok(atas) |
| 230 | +} |
| 231 | + |
| 232 | +pub async fn mint_tokens( |
| 233 | + context: &mut ProgramTestContext, |
| 234 | + authority: &Keypair, |
| 235 | + mint: &Pubkey, |
| 236 | + account: &Pubkey, |
| 237 | + amount: u64, |
| 238 | + additional_signer: Option<&Keypair>, |
| 239 | +) -> transport::Result<()> { |
| 240 | + let mut signing_keypairs = vec![authority, &context.payer]; |
| 241 | + if let Some(signer) = additional_signer { |
| 242 | + signing_keypairs.push(signer); |
| 243 | + } |
| 244 | + |
| 245 | + let ix = spl_token::instruction::mint_to( |
| 246 | + &spl_token::id(), |
| 247 | + mint, |
| 248 | + account, |
| 249 | + &authority.pubkey(), |
| 250 | + &[], |
| 251 | + amount, |
| 252 | + ) |
| 253 | + .unwrap(); |
| 254 | + |
| 255 | + let tx = Transaction::new_signed_with_payer( |
| 256 | + &[ix], |
| 257 | + Some(&context.payer.pubkey()), |
| 258 | + &signing_keypairs, |
| 259 | + context.last_blockhash, |
| 260 | + ); |
| 261 | + context.banks_client.process_transaction(tx).await |
| 262 | +} |
| 263 | + |
| 264 | +pub async fn _transfer( |
| 265 | + context: &mut ProgramTestContext, |
| 266 | + mint: &Pubkey, |
| 267 | + from: &Keypair, |
| 268 | + to: &Keypair, |
| 269 | +) -> transport::Result<()> { |
| 270 | + create_associated_token_account(context, &to.pubkey(), mint).await?; |
| 271 | + let tx = Transaction::new_signed_with_payer( |
| 272 | + &[spl_token::instruction::transfer( |
| 273 | + &spl_token::id(), |
| 274 | + &from.pubkey(), |
| 275 | + &to.pubkey(), |
| 276 | + &from.pubkey(), |
| 277 | + &[&from.pubkey()], |
| 278 | + 0, |
| 279 | + ) |
| 280 | + .unwrap()], |
| 281 | + Some(&from.pubkey()), |
| 282 | + &[from], |
| 283 | + context.last_blockhash, |
| 284 | + ); |
| 285 | + |
| 286 | + context.banks_client.process_transaction(tx).await |
| 287 | +} |
| 288 | + |
| 289 | +pub async fn prepare_nft(context: &mut ProgramTestContext, minter: &Keypair) -> MasterEditionV2 { |
| 290 | + let nft_info = metadata::Metadata::new(minter); |
| 291 | + create_mint( |
| 292 | + context, |
| 293 | + &minter.pubkey(), |
| 294 | + Some(&minter.pubkey()), |
| 295 | + 0, |
| 296 | + Some(clone_keypair(&nft_info.mint)), |
| 297 | + ) |
| 298 | + .await |
| 299 | + .unwrap(); |
| 300 | + mint_to_wallets( |
| 301 | + context, |
| 302 | + &nft_info.mint.pubkey(), |
| 303 | + minter, |
| 304 | + vec![(nft_info.token, 1)], |
| 305 | + ) |
| 306 | + .await |
| 307 | + .unwrap(); |
| 308 | + MasterEditionV2::new(&nft_info) |
| 309 | +} |
0 commit comments