Özel bir Solana programını test etmek, derlenmiş .so dosyasını SVM'e yüklemek, programın beklediği hesapları ayarlamak ve programa karşı talimatlar göndermek anlamına gelir. LiteSVM tüm bunları işlem içinde gerçekleştirir — validator gerekmez. Uyumlu bir Kit Eklentisine erişebilmek için program istemcilerini
Codama JS Renderer ile oluşturmanızı öneririz.
Program Yükleme
import { createClient, address, generateKeyPairSigner } from "@solana/kit";import { litesvm } from "@solana/kit-plugin-litesvm";import { signer } from "@solana/kit-plugin-signer";import { myProgramPlugin } from "@my-program/sdk";import * as fs from "node:fs";const mySigner = await generateKeyPairSigner();const client = createClient().use(signer(mySigner)).use(litesvm()).use(myProgramPlugin());// Configure for program testingclient.svm.withSigverify(false).withBlockhashCheck(false).withSysvars().withBuiltins();// Load program from .so fileconst programId = address("YourProgramId111111111111111111111111111");const soPath = "/path/to/target/deploy/my_program.so";if (fs.existsSync(soPath)) {client.svm.addProgramFromFile(programId, soPath);console.log("Program loaded:", programId);// Verify program accountconst programAccount = client.svm.getAccount(programId);if (programAccount.exists) {console.log(" Executable:", programAccount.executable);console.log(" Data size:", programAccount.data.length, "bytes");}} else {console.log("Program file not found:", soPath);console.log("Build with: cargo build-sbf");}
Eksiksiz Program Test Kurulumu
import { createClient, address, generateKeyPairSigner, lamports } from '@solana/kit';import { litesvm } from '@solana/kit-plugin-litesvm';import { signer } from '@solana/kit-plugin-signer';import { myProgramPlugin } from '@my-program/sdk';import * as fs from 'node:fs';const mySigner = await generateKeyPairSigner();const client = createClient().use(signer(mySigner)).use(litesvm()).use(myProgramPlugin());client.svm.withSigverify(false).withBlockhashCheck(false).withSysvars().withBuiltins().withPrecompiles().withTransactionHistory(100n);// Load programconst programId = address('YourProgramId111111111111111111111111111');const soPath = '/path/to/target/deploy/my_program.so';if (fs.existsSync(soPath)) {client.svm.addProgramFromFile(programId, soPath);}// Set up any required data accountsconst dataAccountAddress = address('DataAccount111111111111111111111111111');const minBalance = client.svm.minimumBalanceForRentExemption(100n);client.svm.setAccount({address: dataAccountAddress,data: new Uint8Array(100),executable: false,lamports: lamports(minBalance),programAddress: programId,space: 100n,});// Send the transactionawait client.myProgram.testInstruction({..}).sendTransaction();// Verify state changesconst updatedAccount = client.myProgram.myAccount.fetch(dataAccountAddress);// assert on decoded account
Varsayılan Programları Kullanma
Standart programlar gerektiren testler için:
const mySigner = await generateKeyPairSigner();const client = createClient().use(signer(mySigner)).use(litesvm());// Add all default programs (System, BPF Loader, etc.)client.svm.withDefaultPrograms();// Or add specific programs you need// The Token program, Associated Token program, etc. would need// to be added manually via addProgramFromFile if needed
.so dosyasını oluşturmak için Solana programınızı cargo build-sbf ile derleyin.
Kalıp her zaman aynıdır:
- Program Yükleme: Derlenmiş programları yüklemek için
addProgramFromFilekullanın ve istemcinizden myProgramPlugin() içe aktarın - Yapılandırma: Gerektiğinde sysvar'ları, yerleşikleri ve ön derlemeleri etkinleştirin
- Hesap Kurulumu:
setAccountile veri hesaplarını önceden doldurun - Gönderme:
client.myProgram.testInstruction({..}).sendTransaction()kullanın — manuel işlem oluşturma gerekmez - Doğrulama: Yürütme sonrasında hesap durumunu kontrol edin
(
client.myProgram.myAccount.fetch)
Bu iskelet çalışır hale geldikten sonra daha fazla talimat ve doğrulama ekleyerek kapsamlı bir test paketi oluşturabilirsiniz.
Is this page helpful?