Transfer SOL

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

Kluczowe punkty

  1. Tworzenie konta: Użyj Keypair::new(), aby tworzyć konta testowe
  2. Finansowanie: Użyj airdrop(), aby zasilić konta SOL
  3. Instrukcje systemowe: Użyj system_instruction::transfer() do transferów SOL
  4. Weryfikacja salda: Sprawdź salda po transakcji, aby zweryfikować transfer
  5. Opłaty transakcyjne: Saldo Alice będzie nieco mniejsze niż 9 SOL ze względu na opłaty transakcyjne

Is this page helpful?

Spis treści

Edytuj stronę