SOL 转账

完整测试

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());
}

关键要点

  1. 创建账户:使用 Keypair::new() 创建测试账户
  2. 充值:使用 airdrop() 向账户充值 SOL
  3. 系统指令:使用 system_instruction::transfer() 进行 SOL 转账
  4. 余额验证:在交易完成后检查余额以确认转账
  5. 交易费用:由于交易费用,Alice 的余额将略低于 9 SOL

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有