SOL Transfer

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 accounts
let alice = Keypair::new();
let bob = Keypair::new();
// Fund alice with 10 SOL
svm.airdrop(&alice.pubkey(), 10_000_000_000).unwrap();
// Create transfer instruction
let transfer_ix = system_instruction::transfer(
&alice.pubkey(),
&bob.pubkey(),
1_000_000_000, // 1 SOL
);
// Build transaction
let tx = Transaction::new_signed_with_payer(
&[transfer_ix],
Some(&alice.pubkey()),
&[&alice],
svm.latest_blockhash(),
);
// Send and verify
let result = svm.send_transaction(tx).unwrap();
// Check balances
assert_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

  1. Account Creation: Use Keypair::new() to create test accounts
  2. Funding: Use airdrop() to fund accounts with SOL
  3. System Instructions: Use system_instruction::transfer() for SOL transfers
  4. Balance Verification: Check balances after transaction to verify the transfer
  5. Transaction Fees: Alice's balance will be slightly less than 9 SOL due to transaction fees

Is this page helpful?

Inhaltsverzeichnis

Seite bearbeiten
© 2026 Solana Foundation. Alle Rechte vorbehalten.