커스텀 Solana 프로그램을 테스트하려면 컴파일된 .so 파일을 SVM에 로드하고, 프로그램이 필요로 하는 계정을 설정한 후, 해당 프로그램에 명령어를 전송해야 합니다.
LiteSVM은 이 모든 과정을 인-프로세스로 처리하므로 — validator가 필요하지 않습니다. 호환 가능한 Kit 플러그인에 접근할 수 있도록
Codama JS Renderer를 사용하여 프로그램 클라이언트를 생성하는 것을 권장합니다.
프로그램 로드
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");}
완전한 프로그램 테스트 설정
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
기본 프로그램 사용
표준 프로그램이 필요한 테스트의 경우:
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
cargo build-sbf로 Solana 프로그램을 빌드하여 .so 파일을 생성하세요.
패턴은 항상 동일합니다:
- 프로그램 로드:
addProgramFromFile을 사용하여 컴파일된 프로그램을 로드하고 클라이언트에서 myProgramPlugin()을 임포트합니다 - 설정: 필요에 따라 sysvar, 빌트인, 프리컴파일을 활성화합니다
- 계정 설정:
setAccount로 데이터 계정을 미리 채웁니다 - 전송:
client.myProgram.testInstruction({..}).sendTransaction()을 사용합니다 — 수동 트랜잭션 빌드가 필요 없습니다 - 검증: 실행 후 계정 상태를 확인합니다
(
client.myProgram.myAccount.fetch)
이 기본 구조가 동작하면, 더 많은 명령어와 어설션을 추가하여 완전한 테스트 스위트를 구성할 수 있습니다.
Is this page helpful?