カスタム Solana プログラムをテストするには、コンパイル済みの .so ファイルを SVM に読み込み、プログラムが想定するアカウントをセットアップし、そのプログラムに対して instructions を送信します。
LiteSVM はこれらすべてをインプロセスで処理するため、validator は不要です。
互換性のある Kit Plugin を利用できるよう、Codama JS Renderer でプログラムクライアントを生成することをおすすめします。
プログラムの読み込み
@my-program/sdk のインポートは、Codama が生成した独自プログラム用クライアントのプレースホルダーです。
これら 2 つの例は説明用のスケルトンです。実行する前に、プラグイン、instructions、アカウントの名前をご自身のものに置き換えてください。
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");
}
プログラムテストの完全なセットアップ
説明用のスケルトン: myProgramPlugin、testInstruction、myAccount は、ご自身で生成したクライアントに由来します。
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
デフォルトプログラムの利用
litesvm() クライアントには、標準のプログラムがすでに読み込まれています。System Program、BPF ローダー、Vote、Compute Budget、Stake、Address Lookup Table、Token、Token-2022、Associated Token Account、Memo、および ed25519、secp256k1、secp256r1 のプリコンパイルです。
テストで呼び出すそれ以外のプログラムだけを読み込んでください。
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");
cargo build-sbf で Solana プログラムをビルドし、.so ファイルを生成します。
基本的な流れは常に同じです。
- プログラムの読み込み:
addProgramFromFile を使ってコンパイル済みプログラムを読み込み、
クライアントから myProgramPlugin() をインポートします
- 設定: 必要に応じて署名検証などの設定を変更します。
sysvar、ビルトイン、プリコンパイル、デフォルトプログラムはすでに読み込まれています
- アカウントのセットアップ:
setAccount でデータアカウントに事前にデータを設定します
- 送信:
client.myProgram.testInstruction({ /* args */ }).sendTransaction() を使用します。
トランザクションを手動で組み立てる必要はありません
- 検証: 実行後のアカウントの状態を確認します
(
client.myProgram.myAccount.fetch)
このスケルトンが動作したら、instructions とアサーションを追加して、本格的なテストスイートを構築できます。