|
| 1 | +use litesvm::LiteSVM; |
| 2 | +use solana_instruction::{AccountMeta, Instruction}; |
| 3 | +use solana_keypair::{Keypair, Signer}; |
| 4 | +use solana_program::native_token::LAMPORTS_PER_SOL; |
| 5 | +use solana_pubkey::Pubkey; |
| 6 | +use solana_system_interface::instruction::create_account; |
| 7 | +use solana_transaction::Transaction; |
| 8 | +use transfer_sol_program::processor::TransferInstruction; |
| 9 | + |
| 10 | +#[test] |
| 11 | +fn test_transfer_sol() { |
| 12 | + let mut svm = LiteSVM::new(); |
| 13 | + |
| 14 | + let program_id = Pubkey::new_unique(); |
| 15 | + let program_bytes = include_bytes!("../../../../../target/deploy/transfer_sol_program.so"); |
| 16 | + |
| 17 | + svm.add_program(program_id, program_bytes).unwrap(); |
| 18 | + |
| 19 | + let payer = Keypair::new(); |
| 20 | + svm.airdrop(&payer.pubkey(), LAMPORTS_PER_SOL * 10).unwrap(); |
| 21 | + |
| 22 | + let test_recipient1 = Keypair::new(); |
| 23 | + let test_recipient2 = Keypair::new(); |
| 24 | + let test_recipient3 = Keypair::new(); |
| 25 | + |
| 26 | + let payer_balance_before = svm.get_balance(&payer.pubkey()).unwrap(); |
| 27 | + let recipient_balance_before = svm.get_balance(&test_recipient1.pubkey()).unwrap_or(0); |
| 28 | + |
| 29 | + let data = borsh::to_vec(&TransferInstruction::CpiTransfer(LAMPORTS_PER_SOL)).unwrap(); |
| 30 | + |
| 31 | + let ix = Instruction { |
| 32 | + program_id, |
| 33 | + accounts: vec![ |
| 34 | + AccountMeta::new(payer.pubkey(), true), |
| 35 | + AccountMeta::new(test_recipient1.pubkey(), false), |
| 36 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 37 | + ], |
| 38 | + data, |
| 39 | + }; |
| 40 | + |
| 41 | + let tx = Transaction::new_signed_with_payer( |
| 42 | + &[ix], |
| 43 | + Some(&payer.pubkey()), |
| 44 | + &[&payer], |
| 45 | + svm.latest_blockhash(), |
| 46 | + ); |
| 47 | + |
| 48 | + let _ = svm.send_transaction(tx).is_ok(); |
| 49 | + |
| 50 | + let payer_balance_after = svm.get_balance(&payer.pubkey()).unwrap(); |
| 51 | + let recipient_balance_after = svm.get_balance(&test_recipient1.pubkey()).unwrap_or(0); |
| 52 | + |
| 53 | + assert!(payer_balance_before > payer_balance_after); |
| 54 | + assert!(recipient_balance_before < recipient_balance_after); |
| 55 | + |
| 56 | + let create_ix = create_account( |
| 57 | + &payer.pubkey(), |
| 58 | + &test_recipient2.pubkey(), |
| 59 | + 2 * LAMPORTS_PER_SOL, |
| 60 | + 0, |
| 61 | + &program_id, |
| 62 | + ); |
| 63 | + |
| 64 | + let tx = Transaction::new_signed_with_payer( |
| 65 | + &[create_ix], |
| 66 | + Some(&payer.pubkey()), |
| 67 | + &[&payer, &test_recipient2], |
| 68 | + svm.latest_blockhash(), |
| 69 | + ); |
| 70 | + |
| 71 | + let _ = svm.send_transaction(tx).is_ok(); |
| 72 | + |
| 73 | + let data = borsh::to_vec(&TransferInstruction::ProgramTransfer(LAMPORTS_PER_SOL)).unwrap(); |
| 74 | + |
| 75 | + let ix = Instruction { |
| 76 | + program_id, |
| 77 | + accounts: vec![ |
| 78 | + AccountMeta::new(test_recipient2.pubkey(), true), |
| 79 | + AccountMeta::new(test_recipient3.pubkey(), false), |
| 80 | + AccountMeta::new(solana_system_interface::program::ID, false), |
| 81 | + ], |
| 82 | + data, |
| 83 | + }; |
| 84 | + |
| 85 | + let tx = Transaction::new_signed_with_payer( |
| 86 | + &[ix], |
| 87 | + Some(&payer.pubkey()), |
| 88 | + &[&payer, &test_recipient2], |
| 89 | + svm.latest_blockhash(), |
| 90 | + ); |
| 91 | + |
| 92 | + let _ = svm.send_transaction(tx).is_ok(); |
| 93 | +} |
0 commit comments