> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# 快速入门

> 安装 LiteSVM 并编写你的第一个测试。

本指南涵盖基础内容：如何创建账户、为账户充值以及发送交易。

<Steps titleSize="h2">

<Step title="安装依赖">
<Tabs>
<Tab title="Rust">

将 LiteSVM 添加到开发依赖中：

```bash
cargo add --dev litesvm
```

多个 Solana crate 通过提供必要的类型和工具来增强测试体验。本页示例使用 `solana-sdk` 便捷 crate（固定为 LiteSVM 0.18 所使用的 4.x 版本系列）以及 System Program 接口 crate：

```bash
cargo add --dev solana-sdk@4
cargo add --dev solana-system-interface --features bincode
```

<Warning>
  请使用 `solana-sdk@4`，而不是最新的主版本。较新的 `solana-sdk` 会引入第二份组件 crate（`solana-keypair`、`solana-transaction` 等），导致类型与 LiteSVM 所期望的类型不再匹配。如果你愿意，也可以改为添加 LiteSVM 重新导出的各个组件 crate，而不使用 `solana-sdk`。
</Warning>

</Tab>
<Tab title="TypeScript">

```bash
npm install @solana/kit @solana/kit-plugin-litesvm @solana/kit-plugin-signer @solana-program/system
```

</Tab>
</Tabs>

</Step>

<Step title="创建测试文件">
<Tabs>
<Tab title="Rust">

在你的工作区中，创建一个 `tests` 文件夹。

```bash
mkdir tests
```

</Tab>
<Tab title="TypeScript">

为你的测试创建一个新的 TypeScript 文件。这些测试使用了顶层 `await`，因此请先在 `package.json` 中设置 `"type": "module"`（否则 `tsx` 会报错：`Top-level await is currently not supported with the "cjs" output format`）：

```bash
npm pkg set type=module
touch basic-test.ts
```

</Tab>
</Tabs>

</Step>

<Step title="创建账户并为其充值">
<Tabs>
<Tab title="Rust">

让我们来编写你的第一个 LiteSVM 测试。

在 `tests` 文件夹中，创建一个新文件并复制以下代码：

```rust tests/create_account.rs
use litesvm::LiteSVM;
use solana_sdk::signature::{Keypair, Signer};

#[test]
fn create_account() {
    // Create the test environment
    let mut svm = LiteSVM::new();

    // Create a test account
    let user = Keypair::new();

    // Fund the account with SOL
    svm.airdrop(&user.pubkey(), 1_000_000_000).unwrap();

    // Check the balance
    let balance = svm.get_balance(&user.pubkey()).unwrap();
    assert_eq!(balance, 1_000_000_000);

    println!("Account funded with {} SOL", balance as f64 / 1e9);
}
```

</Tab>
<Tab title="TypeScript">

将以下代码复制到你的测试文件中：

```typescript basic-test.ts
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 payer and check balance
client.svm.airdrop(client.payer.address, lamports(5_000_000_000n));
const balance = client.svm.getBalance(client.payer.address);
console.log("Balance:", balance, "lamports");
```

</Tab>
</Tabs>

</Step>

<Step title="运行测试">
<Tabs>
<Tab title="Rust">

```bash
cargo test create_account -- --show-output
```

你应该会看到：

```
running 1 test
test create_account ... ok

successes:

---- create_account stdout ----
Account funded with 1 SOL


successes:
    create_account
```

</Tab>
<Tab title="TypeScript">

```bash
npx tsx basic-test.ts
```

你应该会看到：

```
Balance: 5000000000n lamports
```

</Tab>
</Tabs>

<Info>
  你刚刚完成了第一个 LiteSVM 测试，涵盖了创建账户并为其充值。
</Info>

</Step>

<Step title="执行交易">
现在让我们来创建一笔转账交易。

<Tabs>
<Tab title="Rust">

创建一个新的测试文件并复制以下代码：

```rust tests/transfer_test.rs
use litesvm::LiteSVM;
use solana_sdk::{
    signature::{Keypair, Signer},
    transaction::Transaction,
};
use solana_system_interface::instruction as system_instruction;

#[test]
fn test_transfer() {
    let mut svm = LiteSVM::new();

    // Create two accounts
    let alice = Keypair::new();
    let bob = Keypair::new();

    // Fund Alice
    svm.airdrop(&alice.pubkey(), 2_000_000_000).unwrap();

    // Create transfer instruction
    let transfer = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        1_000_000_000, // 1 SOL
    );

    // Build and sign transaction
    let tx = Transaction::new_signed_with_payer(
        &[transfer],
        Some(&alice.pubkey()),
        &[&alice],
        svm.latest_blockhash(),
    );

    // Send it (execution happens immediately)
    svm.send_transaction(tx).unwrap();

    // Check new balances
    assert_eq!(svm.get_balance(&bob.pubkey()).unwrap(), 1_000_000_000);
    assert!(svm.get_balance(&alice.pubkey()).unwrap() < 1_000_000_000);

    println!("Transfer successful!");
}
```

</Tab>
<Tab title="TypeScript">

创建一个新文件并复制以下代码：

```typescript transfer-test.ts
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(2_000_000_000n));

// 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 new balances
const recipientBalance = client.svm.getBalance(recipient.address) ?? 0n;
console.log("Recipient balance:", Number(recipientBalance) / 1e9, "SOL");
console.log("Transfer successful!");
```

</Tab>
</Tabs>

<Info>
  该测试涵盖了执行 System Program 的转账指令。
</Info>

</Step>

<Step title="运行所有测试">
<Tabs>
<Tab title="Rust">

```bash
cargo test -- --show-output
```

你应该会看到：

```
running 1 test
test create_account ... ok

successes:

---- create_account stdout ----
Account funded with 1 SOL


successes:
    create_account

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

     Running tests/transfer_test.rs (target/debug/deps/transfer_test-f174954e7483f36b)

running 1 test
test test_transfer ... ok

successes:

---- test_transfer stdout ----
Transfer successful!


successes:
    test_transfer

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s

   Doc-tests litesvm_quickstart

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
```

</Tab>
<Tab title="TypeScript">

```bash
npx tsx basic-test.ts && npx tsx transfer-test.ts
```

你应该会看到：

```
Balance: 5000000000n lamports
Recipient balance: 1 SOL
Transfer successful!
```

</Tab>
</Tabs>

<Info>
  至此，你已经编写了可创建账户、为账户充值并执行交易的 LiteSVM 测试。
</Info>
</Step>

</Steps>

## 后续步骤

**[为什么选择 LiteSVM →](/developers/docs/tools/litesvm/core-concepts)** 了解为什么要使用 LiteSVM 进行测试

<Tabs>
  <Tab title="Rust">
    **[测试你的程序 →](/developers/docs/tools/litesvm/testing-your-program)** 了解如何测试你自己的 Solana 程序 <br /> **[示例 →](/developers/docs/tools/litesvm/examples)** 查看适用于常见场景的可直接复制的解决方案以及完整的仓库示例 <br /> **[API 参考 →](/developers/docs/tools/litesvm/api-reference)** 了解高级功能和自定义配置
  </Tab>
  <Tab title="TypeScript">
    **[测试你的程序 →](/developers/docs/tools/litesvm/typescript/getting-started)** 了解如何使用 LiteSVM for TypeScript 测试你自己的 Solana 程序{" "}
    <br /> **[示例 →](/developers/docs/tools/litesvm/typescript/examples)** 查看适用于常见场景的可直接复制的解决方案 <br /> **[API 参考 →](/developers/docs/tools/litesvm/typescript/api-reference)** 了解高级功能和自定义配置
  </Tab>
</Tabs>
