Complete Test
tests/transfer_test.rs
use litesvm::LiteSVM;use solana_sdk::{signature::{Keypair, Signer},system_instruction,transaction::Transaction,};#[test]fn test_sol_transfer() {let mut svm = LiteSVM::new();// Create accountslet alice = Keypair::new();let bob = Keypair::new();// Fund alice with 10 SOLsvm.airdrop(&alice.pubkey(), 10_000_000_000).unwrap();// Create transfer instructionlet transfer_ix = system_instruction::transfer(&alice.pubkey(),&bob.pubkey(),1_000_000_000, // 1 SOL);// Build transactionlet tx = Transaction::new_signed_with_payer(&[transfer_ix],Some(&alice.pubkey()),&[&alice],svm.latest_blockhash(),);// Send and verifylet result = svm.send_transaction(tx).unwrap();// Check balancesassert_eq!(svm.get_balance(&bob.pubkey()).unwrap(), 1_000_000_000);assert!(svm.get_balance(&alice.pubkey()).unwrap() < 9_000_000_000);println!("Transfer successful!");println!("Compute units used: {}", result.compute_units_consumed);println!("Transaction logs:\n{}", result.pretty_logs());}
Key Points
- Account Creation: Use
Keypair::new()to create test accounts - Funding: Use
airdrop()to fund accounts with SOL - System Instructions: Use
system_instruction::transfer()for SOL transfers - Balance Verification: Check balances after transaction to verify the transfer
- Transaction Fees: Alice's balance will be slightly less than 9 SOL due to transaction fees
Is this page helpful?