完全なテスト
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());}
重要なポイント
- アカウント作成:
Keypair::new()を使用してテストアカウントを作成する - 資金調達:
airdrop()を使用してアカウントにSOLを入金する - システム命令: SOL転送には
system_instruction::transfer()を使用する - 残高確認: 転送を確認するためにトランザクション後の残高を確認する
- トランザクション手数料: トランザクション手数料のため、Aliceの残高は9 SOLをわずかに下回る
Is this page helpful?