Các bộ kiểm thử Solana khởi động solana-test-validator tốn hàng giây cho mỗi bài kiểm thử —
bạn phải chờ tiến trình khởi động, kết nối RPC và các vòng lặp mạng.
LiteSVM chạy các bài kiểm thử tương tự trong mili giây. Không có tiến trình bên ngoài, không có mạng,
tất cả đều trong bộ nhớ bên trong trình chạy kiểm thử của bạn.
Đã kiểm thử với: @solana/kit@6.8.0, @solana/kit-plugin-litesvm@0.10.0,
@solana/kit-plugin-signer@0.10.0, @solana-program/system@0.12.0,
@solana-program/token@0.13.0. Các gói plugin này còn mới và đang phát triển
nhanh chóng — nếu bạn gặp sự không tương thích, hãy kiểm tra
testing/package.json
để biết các phiên bản chính xác mà tài liệu và bộ kiểm thử đang sử dụng.
Bạn tạo một client Kit từ @solana/kit bằng cách cài đặt các plugin với
.use(): một plugin signer
(@solana/kit-plugin-signer),
plugin LiteSVM
(@solana/kit-plugin-litesvm),
và bất kỳ plugin chương trình nào bạn cần. Kết quả hiển thị client.sendTransaction(),
client.rpc, và quyền truy cập trực tiếp vào trạng thái tài khoản, đồng hồ và sysvars — tất cả
mà không cần rời khỏi tiến trình của bạn.
Bắt Đầu Nhanh
Cài Đặt Các Phụ Thuộc
pnpm add @solana/kit @solana/kit-plugin-litesvm @solana/kit-plugin-signer @solana-program/system
Tạo Client Của Bạn
import { createClient, generateKeyPairSigner, lamports } from "@solana/kit";import { litesvm } from "@solana/kit-plugin-litesvm";import { signer } from "@solana/kit-plugin-signer";// Payer first, then the LiteSVM transport.const mySigner = await generateKeyPairSigner();const client = createClient().use(signer(mySigner)).use(litesvm());// Fund the payerclient.svm.airdrop(client.payer.address, lamports(5_000_000_000n));// Check the balanceconst balance = client.svm.getBalance(client.payer.address);console.log("Balance:", balance, "lamports");
createClient() trả về một shell rỗng — mỗi .use(plugin) bổ sung
thêm các khả năng vào nó. Plugin signer() phải đến trước litesvm()
vì plugin transport cần một payer để gắn vào. signer(mySigner) là
cách viết tắt để cài đặt cùng một keypair làm cả payer và identity — sử dụng
payer() hoặc identity() từ cùng gói nếu bạn muốn tách biệt các
vai trò đó. Thứ tự này tương tự cho solanaRpc() khi bạn đang dùng mạng thật.
Muốn thiết lập một dòng cho các thử nghiệm nhanh? Gói litesvm() bao gồm
airdrop, vì vậy bạn có thể nối tiếp
.use(generatedPayerWithSol(lamports(10_000_000_000n))) từ
@solana/kit-plugin-signer để tự động tạo và nạp tiền cho một payer.
Hiểu Về Client
Sau .use(signer(...)).use(litesvm()), client hiển thị:
| Thuộc Tính | Mô Tả |
|---|---|
client.payer | TransactionSigner bạn đã cài đặt với plugin signer() (cũng được đặt là client.identity) |
client.identity | Wallet signer sở hữu các tài khoản, token và quyền hạn |
client.svm | Instance LiteSVM trực tiếp để quản lý tài khoản, giao dịch và cấu hình |
client.rpc | Giao diện RPC tương thích Kit (tập con: getAccountInfo, getBalance, getEpochSchedule, getLatestBlockhash, getMinimumBalanceForRentExemption, getMultipleAccounts, getSlot, requestAirdrop) |
client.sendTransaction(s) | Lập kế hoạch, ký và gửi các lệnh dưới dạng giao dịch |
client.airdrop | Yêu cầu SOL từ faucet LiteSVM |
client.getMinimumBalance | Tính toán số lamport tối thiểu để miễn rent |
Sử Dụng client.svm
Truy cập trực tiếp vào LiteSVM để:
- Quản lý tài khoản (
setAccount,getAccount,airdrop) - Thực thi giao dịch (
sendTransaction,simulateTransaction) - Cấu hình (
withSigverify,withBlockhashCheck, v.v.) - Thao tác thời gian (
warpToSlot,getClock)
import { address, lamports } from "@solana/kit";// Fund an accountclient.svm.airdrop(address, lamports(1_000_000_000n));// Set account state directlyclient.svm.setAccount({address: myAddress,data: new Uint8Array([1, 2, 3]),executable: false,lamports: lamports(1_000_000n),programAddress: address("11111111111111111111111111111111"),space: 3n});// Get the latest blockhashconst blockhash = client.svm.latestBlockhash();
Sử Dụng client.rpc
Giao diện RPC tương thích Kit cho các thao tác tiêu chuẩn:
// Get account info (returns base64-encoded data)const { value: account } = await client.rpc.getAccountInfo(address).send();// Get multiple accounts in one callconst { value: accounts } = await client.rpc.getMultipleAccounts([addr1, addr2, addr3]).send();// Get latest blockhashconst { value: blockhash } = await client.rpc.getLatestBlockhash().send();
Lớp RPC chỉ hỗ trợ mã hóa base64. Yêu cầu các mã hóa khác sẽ gây ra lỗi.
Cấu Hình
Cấu hình LiteSVM bằng cách sử dụng các phương thức builder trên client.svm:
const client = createClient().use(signer(mySigner)).use(litesvm());// Configure for testingclient.svm.withSigverify(false) // Skip signature verification.withBlockhashCheck(false) // Skip blockhash validation.withSysvars() // Enable Clock, Rent, etc..withBuiltins() // Enable ed25519, secp256k1.withTransactionHistory(100n); // Store transaction history
Gửi Giao Dịch
Thêm các plugin chương trình sau transport — chúng gắn các builder lệnh
vào client để bạn không bao giờ phải thao tác trực tiếp với TransactionMessage thô:
import { createClient, generateKeyPairSigner, lamports } from "@solana/kit";import { litesvm } from "@solana/kit-plugin-litesvm";import { signer } from "@solana/kit-plugin-signer";import { systemProgram } from "@solana-program/system";const mySigner = await generateKeyPairSigner();const client = createClient().use(signer(mySigner)).use(litesvm()).use(systemProgram());client.svm.airdrop(client.payer.address, lamports(10_000_000_000n));const recipient = await generateKeyPairSigner();// Build and send in one callawait client.system.instructions.transferSol({source: client.payer,destination: recipient.address,amount: lamports(1_000_000_000n)}).sendTransaction();
Luồng SVM → RPC
Bạn có thể đặt trạng thái qua SVM và đọc nó qua RPC:
import { address, lamports } from "@solana/kit";// Set account directly via SVMclient.svm.setAccount({address: myAddress,data: new Uint8Array([0xde, 0xad, 0xbe, 0xef]),executable: false,lamports: lamports(1_000_000n),programAddress: address("11111111111111111111111111111111"),space: 4n});// Read via RPC (returns base64-encoded data)const { value } = await client.rpc.getAccountInfo(myAddress).send();console.log("Data:", value?.data); // ['3q2+7w==', 'base64']
Tiếp Theo
Phần này bao gồm client cốt lõi — tạo tài khoản, gửi lệnh và
đọc trạng thái. Nếu các bài kiểm thử của bạn liên quan đến token SPL (mint, ATA, chuyển khoản), hướng dẫn
kiểm thử token cho thấy cách plugin
tokenProgram() thu gọn ~40 dòng thiết lập thành 3 lần gọi. Để xem tài liệu API đầy đủ,
hãy xem tài liệu API.
Is this page helpful?