Solana 文档LiteSVMTypeScript示例

SOL 转账

最基础的 Solana 交易:将 SOL 从一个账户转移到另一个账户。这是在测试更复杂的程序逻辑之前,验证 LiteSVM 配置是否正常运行的良好起点。

完整示例

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

预期输出

Sender initial: 10 SOL
Sender final: 8.999995 SOL
Recipient final: 1 SOL

由于交易费用(每个签名 5,000 lamport),发送方的最终余额略低于 9 SOL。

本节介绍了最简单的交易类型。如需测试自定义程序,请参阅 程序测试。如需进行代币转账(铸币、ATA),请参阅 代币测试

Is this page helpful?

Table of Contents

Edit Page
©️ 2026 Solana 基金会版权所有