|
| 1 | +use borsh::BorshDeserialize; |
| 2 | +use litesvm::LiteSVM; |
| 3 | +use program_derived_addresses_native_program::state::{IncrementPageVisits, PageVisits}; |
| 4 | +use solana_instruction::{AccountMeta, Instruction}; |
| 5 | +use solana_keypair::{Keypair, Signer}; |
| 6 | +use solana_native_token::LAMPORTS_PER_SOL; |
| 7 | +use solana_pubkey::Pubkey; |
| 8 | +use solana_rent::Rent; |
| 9 | +use solana_system_interface::instruction::create_account; |
| 10 | +use solana_transaction::Transaction; |
| 11 | + |
| 12 | +#[test] |
| 13 | +fn test_pda() { |
| 14 | + let mut svm = LiteSVM::new(); |
| 15 | + |
| 16 | + let program_id = Pubkey::new_unique(); |
| 17 | + let program_bytes = |
| 18 | + include_bytes!("../../../../../target/deploy/program_derived_addresses_native_program.so"); |
| 19 | + svm.add_program(program_id, program_bytes).unwrap(); |
| 20 | + |
| 21 | + let payer = Keypair::new(); |
| 22 | + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); |
| 23 | + |
| 24 | + let test_user = Keypair::new(); |
| 25 | + |
| 26 | + let rent = Rent::default(); |
| 27 | + |
| 28 | + let create_ix = create_account( |
| 29 | + &payer.pubkey(), |
| 30 | + &test_user.pubkey(), |
| 31 | + solana_rent::Rent::minimum_balance(&rent, 0), |
| 32 | + 0, |
| 33 | + &solana_system_interface::program::ID, |
| 34 | + ); |
| 35 | + |
| 36 | + let tx = Transaction::new_signed_with_payer( |
| 37 | + &[create_ix], |
| 38 | + Some(&payer.pubkey()), |
| 39 | + &[&payer, &test_user], |
| 40 | + svm.latest_blockhash(), |
| 41 | + ); |
| 42 | + |
| 43 | + let _ = svm.send_transaction(tx).is_ok(); |
| 44 | + |
| 45 | + let (pda, bump) = |
| 46 | + Pubkey::find_program_address(&[b"page_visits", test_user.pubkey().as_ref()], &program_id); |
| 47 | + |
| 48 | + let data = borsh::to_vec(&PageVisits { |
| 49 | + page_visits: 0, |
| 50 | + bump, |
| 51 | + }) |
| 52 | + .unwrap(); |
| 53 | + |
| 54 | + let ix = Instruction { |
| 55 | + program_id, |
| 56 | + accounts: vec![ |
| 57 | + AccountMeta::new(pda, false), |
| 58 | + AccountMeta::new(test_user.pubkey(), false), |
| 59 | + AccountMeta::new(payer.pubkey(), true), |
| 60 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 61 | + ], |
| 62 | + data, |
| 63 | + }; |
| 64 | + |
| 65 | + let tx = Transaction::new_signed_with_payer( |
| 66 | + &[ix], |
| 67 | + Some(&payer.pubkey()), |
| 68 | + &[&payer], |
| 69 | + svm.latest_blockhash(), |
| 70 | + ); |
| 71 | + |
| 72 | + let _ = svm.send_transaction(tx).is_ok(); |
| 73 | + |
| 74 | + let data = borsh::to_vec(&IncrementPageVisits {}).unwrap(); |
| 75 | + |
| 76 | + let ix = Instruction { |
| 77 | + program_id, |
| 78 | + accounts: vec![ |
| 79 | + AccountMeta::new(pda, false), |
| 80 | + AccountMeta::new(payer.pubkey(), true), |
| 81 | + ], |
| 82 | + data, |
| 83 | + }; |
| 84 | + |
| 85 | + let tx = Transaction::new_signed_with_payer( |
| 86 | + &[ix], |
| 87 | + Some(&payer.pubkey()), |
| 88 | + &[&payer], |
| 89 | + svm.latest_blockhash(), |
| 90 | + ); |
| 91 | + |
| 92 | + let _ = svm.send_transaction(tx).is_ok(); |
| 93 | + |
| 94 | + // read page visits |
| 95 | + let account_info = svm.get_account(&pda).unwrap(); |
| 96 | + let read_page_visits = PageVisits::try_from_slice(&account_info.data).unwrap(); |
| 97 | + assert_eq!(read_page_visits.page_visits, 1); |
| 98 | +} |
0 commit comments