Özel bir Solana programını test etmek; derlenmiş .so dosyasını SVM’ye yüklemek, programın beklediği hesapları hazırlamak ve ona talimatlar göndermek anlamına gelir.
LiteSVM bunların hepsini süreç içinde halleder — validator gerekmez. Uyumlu bir Kit Plugin’ine erişebilmeniz için program istemcilerini
Codama JS Renderer ile oluşturmanızı öneririz.
Program Yükleme
@my-program/sdk içe aktarımı, kendi programınız için Codama ile oluşturulmuş bir istemcinin yer tutucusudur. Bu iki örnek yalnızca açıklayıcı iskeletlerdir: çalıştırmadan önce plugin, talimat ve hesap adlarını kendi adlarınızla değiştirin.
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 testing
client.svm
.withSigverify(false)
.withBlockhashCheck(false)
.withSysvars()
.withBuiltins();
// Load program from .so file
const programId = (await generateKeyPairSigner()).address;
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 account
const 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
Açıklayıcı iskelet: myProgramPlugin, testInstruction ve myAccount, kendi oluşturduğunuz istemciden gelir.
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 program
const programId = (await generateKeyPairSigner()).address;
const soPath = '/path/to/target/deploy/my_program.so';
if (fs.existsSync(soPath)) {
client.svm.addProgramFromFile(programId, soPath);
}
// Set up any required data accounts
const dataAccountAddress = (await generateKeyPairSigner()).address;
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 transaction
await client.myProgram.testInstruction({ /* args */ }).sendTransaction();
// Verify state changes
const updatedAccount = client.myProgram.myAccount.fetch(dataAccountAddress);
// assert on decoded account
Varsayılan Programları Kullanma
Bir litesvm() istemcisinde standart programlar zaten yüklüdür: System Program, BPF loader’ları, Vote, Compute Budget, Stake, Address Lookup Table, Token, Token-2022, Associated Token Account, Memo ve ed25519, secp256k1 ile secp256r1 precompile’ları. Yalnızca testinizin çağırdığı diğer programları yükleyin:
const mySigner = await generateKeyPairSigner();
const client = createClient().use(signer(mySigner)).use(litesvm());
// Token, Token-2022, Associated Token Account and Memo are already loaded.
// Add any other program your program calls from its .so file
client.svm.addProgramFromFile(otherProgramId, "/path/to/other_program.so");
.so dosyasını oluşturmak için Solana programınızı cargo build-sbf ile derleyin.
Yöntem her zaman aynıdır:
- Program Yükleme: Derlenmiş programları yüklemek için
addProgramFromFile kullanın ve
istemcinizden myProgramPlugin() öğesini içe aktarın
- Yapılandırma: Gerekirse imza doğrulama gibi ayarları değiştirin;
sysvar’lar, yerleşik programlar, precompile’lar ve varsayılan programlar zaten yüklüdür
- Hesap Kurulumu: Veri hesaplarını
setAccount ile önceden doldurun
- Gönderim:
client.myProgram.testInstruction({ /* args */ }).sendTransaction() kullanın —
elle işlem oluşturmaya gerek yok
- Doğrulama: Çalıştırmadan sonra hesap durumunu kontrol edin
(
client.myProgram.myAccount.fetch)
Bu iskelet çalışmaya başladıktan sonra, daha fazla talimat ve doğrulama ekleyerek eksiksiz bir test paketi oluşturabilirsiniz.