Hızlı Başlangıç Kılavuzu
Bu kılavuz temel konuları ele almaktadır: nasıl hesap oluşturulur, hesaba nasıl para yüklenir ve bir işlem nasıl gönderilir.
Bağımlılıkları Yükle
LiteSVM'yi geliştirme bağımlılıklarınıza ekleyin:
cargo add --dev litesvm
Birkaç Solana crate'i, temel türler ve yardımcı programlar sağlayarak test
deneyimini geliştirir. solana-sdk kolaylık crate'ini veya bireysel bileşen
crate'lerini kullanabilirsiniz:
cargo add --dev solana-sdk
Test Dosyası Oluştur
Çalışma alanınızın içinde tests için bir klasör oluşturun.
mkdir tests
Hesap Oluşturun ve Fonlayın
İlk LiteSVM testinizi yazalım.
tests klasörünüzün içinde yeni bir dosya oluşturun ve bu kodu kopyalayın:
use litesvm::LiteSVM;use solana_sdk::signature::{Keypair, Signer};#[test]fn create_account() {// Create the test environmentlet mut svm = LiteSVM::new();// Create a test accountlet user = Keypair::new();// Fund the account with SOLsvm.airdrop(&user.pubkey(), 1_000_000_000).unwrap();// Check the balancelet balance = svm.get_balance(&user.pubkey()).unwrap();assert_eq!(balance, 1_000_000_000);println!("Account funded with {} SOL", balance as f64 / 1e9);}
Testinizi Çalıştırın
cargo test create_account -- --show-output
Şunu görmelisiniz:
running 1 testtest create_account ... oksuccesses:---- create_account stdout ----Account funded with 1 SOLsuccesses:create_account
Hepsi bu! LiteSVM ile ilk testinizi oluşturdunuz; bu test bir hesap oluşturmayı ve fonlamayı kapsamaktadır.
Bir İşlem Gerçekleştirin
Şimdi bir transfer işlemi oluşturalım.
Yeni bir test dosyası oluşturun ve bu kodu kopyalayın:
use litesvm::LiteSVM;use solana_sdk::{signature::{Keypair, Signer},system_instruction,transaction::Transaction,};#[test]fn test_transfer() {let mut svm = LiteSVM::new();// Create two accountslet alice = Keypair::new();let bob = Keypair::new();// Fund Alicesvm.airdrop(&alice.pubkey(), 2_000_000_000).unwrap();// Create transfer instructionlet transfer = system_instruction::transfer(&alice.pubkey(),&bob.pubkey(),1_000_000_000, // 1 SOL);// Build and sign transactionlet tx = Transaction::new_signed_with_payer(&[transfer],Some(&alice.pubkey()),&[&alice],svm.latest_blockhash(),);// Send it (execution happens immediately)svm.send_transaction(tx).unwrap();// Check new balancesassert_eq!(svm.get_balance(&bob.pubkey()).unwrap(), 1_000_000_000);assert!(svm.get_balance(&alice.pubkey()).unwrap() < 1_000_000_000);println!("Transfer successful!");}
Test tamamlandı! Bu test, System Program'dan transfer talimatının çalıştırılmasını kapsamaktadır.
Tüm Testleri Çalıştırın
cargo test -- --show-output
Şunu görmelisiniz:
running 1 testtest create_account ... oksuccesses:---- create_account stdout ----Account funded with 1 SOLsuccesses:create_accounttest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04sRunning tests/test_transfer.rs (target/debug/deps/test_transfer-f174954e7483f36b)running 1 testtest test_transfer ... oksuccesses:---- test_transfer stdout ----Transfer successful!successes:test_transfertest result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
Hepsi bu! Artık hesap oluşturup fonlayan ve işlemleri gerçekleştiren litesvm testlerini başarıyla yazdınız.
Sonraki Adımlar
LiteSVM Neden Kullanılır? → Test için neden LiteSVM kullanmak isteyeceğinizi öğrenin
Programınızı Test Etme →
Kendi Solana programınızı nasıl test edeceğinizi öğrenin
Örnekler
→ Yaygın senaryolar için hazır çözümleri ve
tam depo örneklerini inceleyin
API Referansı
→ Gelişmiş özellikleri ve özel
yapılandırmaları öğrenin
Is this page helpful?