cargo add --dev litesvm solana-sdk@4 と cargo add --dev solana-system-interface --features bincode が必要です
完全なテスト
use litesvm::LiteSVM;
use solana_sdk::{
signature::{Keypair, Signer},
transaction::Transaction,
};
use solana_system_interface::instruction as system_instruction;
#[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());
}
重要なポイント
- アカウントの作成:
Keypair::new() を使用してテスト用アカウントを作成します
- 資金の追加:
airdrop() を使用して、アカウントに SOL を補充します
- システム命令: SOL の転送には、(
solana-system-interface の) system_instruction::transfer() を使用します
- 残高の確認: トランザクションの実行後に残高を確認し、転送が正しく行われたことを検証します
- トランザクション手数料: トランザクション手数料のため、Alice の残高は 9 SOL をわずかに下回ります