Solana documentatieLiteSVMTypeScriptVoorbeelden

Programma Testen

Het testen van een aangepast Solana-programma betekent het laden van het gecompileerde .so-bestand in de SVM, het instellen van de verwachte accounts en het versturen van instructies daartegen. LiteSVM regelt dit alles in-process — geen validator nodig. We raden aan om programmaclients te genereren met de Codama JS Renderer zodat je toegang hebt tot een compatibele Kit Plugin.

Een Programma Laden

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 = 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 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");
}

Volledige Programmatest Instellen

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 = 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 accounts
const 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 transaction
await client.myProgram.testInstruction({..}).sendTransaction();
// Verify state changes
const updatedAccount = client.myProgram.myAccount.fetch(dataAccountAddress);
// assert on decoded account

Standaardprogramma's Gebruiken

Voor tests die standaardprogramma's vereisen:

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

Bouw je Solana-programma met cargo build-sbf om het .so-bestand te genereren.

Het patroon is altijd hetzelfde:

  1. Programma Laden: Gebruik addProgramFromFile om gecompileerde programma's te laden en importeer myProgramPlugin() vanuit je client
  2. Configuratie: Schakel sysvars, builtins en precompiles in waar nodig
  3. Account Instellen: Vul data-accounts vooraf in met setAccount
  4. Versturen: Gebruik client.myProgram.testInstruction({..}).sendTransaction() — geen handmatige transactieopbouw
  5. Verificatie: Controleer de accountstatus na uitvoering (client.myProgram.myAccount.fetch)

Zodra dit basisskelet werkt, kun je een volledige testsuite uitbreiden door meer instructies en beweringen toe te voegen.

Is this page helpful?

Inhoudsopgave

Pagina Bewerken
© 2026 Solana Foundation. Alle rechten voorbehouden.