Giao dịch Solana cơ bản nhất: chuyển SOL từ tài khoản này sang tài khoản khác. Đây là
bài kiểm thử đầu tiên phù hợp để xác minh thiết lập LiteSVM của bạn hoạt động đúng trước khi kiểm thử logic
chương trình phức tạp hơn.
Ví dụ đầy đủ
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";
// Payer first, then the LiteSVM transport, then program plugins.
const mySigner = await generateKeyPairSigner();
const client = createClient()
.use(signer(mySigner))
.use(litesvm())
.use(systemProgram());
const recipient = await generateKeyPairSigner();
// Airdrop SOL to the payer
client.svm.airdrop(client.payer.address, lamports(10_000_000_000n));
// Check initial balance
const senderInitial = client.svm.getBalance(client.payer.address) ?? 0n;
console.log("Sender initial:", Number(senderInitial) / 1e9, "SOL");
// Build and send the transfer
await client.system.instructions
.transferSol({
source: client.payer,
destination: recipient.address,
amount: lamports(1_000_000_000n) // 1 SOL
})
.sendTransaction();
// Check final balances
const senderFinal = client.svm.getBalance(client.payer.address) ?? 0n;
const recipientFinal = client.svm.getBalance(recipient.address) ?? 0n;
console.log("Sender final:", Number(senderFinal) / 1e9, "SOL");
console.log("Recipient final:", Number(recipientFinal) / 1e9, "SOL");
Kết quả mong đợi
Sender initial: 10 SOL
Sender final: 8.999995 SOL
Recipient final: 1 SOL
Số dư cuối cùng của người gửi thấp hơn 9 SOL một chút do phí giao dịch
(5.000 lamport cho mỗi chữ ký).
Phần này bao quát loại giao dịch đơn giản nhất. Để kiểm thử với các chương trình tùy chỉnh, xem
Kiểm thử chương trình. Để
kiểm thử chuyển token (mint, ATA), xem
Kiểm thử token.