> ## 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.

# Bắt đầu

> Cài đặt LiteSVM và viết các bài kiểm thử đầu tiên của bạn.

Hướng dẫn này đề cập những kiến thức cơ bản: cách tạo tài khoản, nạp tiền cho tài khoản và
gửi giao dịch.

<Steps titleSize="h2">

<Step title="Cài đặt các phụ thuộc">
<Tabs>
<Tab title="Rust">

Thêm LiteSVM vào các dev dependencies của bạn:

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

Một số crate của Solana giúp cải thiện trải nghiệm kiểm thử bằng cách cung cấp các
kiểu dữ liệu và tiện ích thiết yếu. Các ví dụ trong trang này sử dụng crate tiện ích
`solana-sdk` (được ghim ở nhánh 4.x mà LiteSVM 0.18 sử dụng) và crate giao diện
System Program:

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

<Warning>
  Hãy dùng `solana-sdk@4`, không phải phiên bản chính mới nhất. Bản `solana-sdk` mới hơn sẽ kéo
  theo một bản sao thứ hai của các component crate (`solana-keypair`,
  `solana-transaction` và các crate khác), khiến các kiểu dữ liệu không còn khớp với những kiểu mà
  LiteSVM mong đợi. Nếu muốn, bạn có thể thêm từng component crate riêng lẻ
  mà LiteSVM re-export thay vì dùng `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="Tạo tệp kiểm thử">
<Tabs>
<Tab title="Rust">

Trong workspace của bạn, hãy tạo một thư mục `tests`.

```bash
mkdir tests
```

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

Tạo một tệp TypeScript mới cho bài kiểm thử của bạn. Các bài kiểm thử dùng `await` ở cấp cao nhất, vì vậy
hãy đặt `"type": "module"` trong `package.json` của bạn trước (nếu không, `tsx` sẽ báo lỗi
`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="Tạo và nạp tiền cho tài khoản">
<Tabs>
<Tab title="Rust">

Hãy viết bài kiểm thử LiteSVM đầu tiên của bạn.

Trong thư mục `tests`, hãy tạo một tệp mới và sao chép đoạn mã này:

```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">

Sao chép đoạn mã này vào tệp kiểm thử của bạn:

```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="Chạy bài kiểm thử">
<Tabs>
<Tab title="Rust">

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

Bạn sẽ thấy:

```
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
```

Bạn sẽ thấy:

```
Balance: 5000000000n lamports
```

</Tab>
</Tabs>

<Info>
  Bạn vừa tạo bài kiểm thử LiteSVM đầu tiên, bao gồm việc tạo
  một tài khoản và nạp tiền cho tài khoản đó.
</Info>

</Step>

<Step title="Thực thi giao dịch">
Bây giờ hãy tạo một giao dịch chuyển tiền.

<Tabs>
<Tab title="Rust">

Tạo một tệp kiểm thử mới và sao chép đoạn mã này:

```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">

Tạo một tệp mới và sao chép đoạn mã này:

```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>
  Bài kiểm thử này bao gồm việc thực thi lệnh chuyển tiền từ
  System Program.
</Info>

</Step>

<Step title="Chạy tất cả bài kiểm thử">
<Tabs>
<Tab title="Rust">

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

Bạn sẽ thấy:

```
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
```

Bạn sẽ thấy:

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

</Tab>
</Tabs>

<Info>
  Bạn đã tạo các bài kiểm thử LiteSVM để tạo và nạp tiền cho tài khoản cũng như thực thi giao dịch.
</Info>
</Step>

</Steps>

## Các bước tiếp theo

**[Tại sao chọn LiteSVM →](/developers/docs/tools/litesvm/core-concepts)** Tìm hiểu lý do bạn nên
dùng LiteSVM để kiểm thử

<Tabs>
  <Tab title="Rust">
    **[Kiểm thử chương trình của bạn →](/developers/docs/tools/litesvm/testing-your-program)** Tìm hiểu
    cách kiểm thử chương trình Solana của riêng bạn <br /> **[Ví dụ
    →](/developers/docs/tools/litesvm/examples)** Xem các giải pháp sao chép và dán cho những
    tình huống phổ biến và các ví dụ repository đầy đủ <br /> **[Tài liệu tham khảo API
    →](/developers/docs/tools/litesvm/api-reference)** Tìm hiểu các tính năng nâng cao và cấu hình
    tùy chỉnh
  </Tab>
  <Tab title="TypeScript">
    **[Kiểm thử chương trình của bạn →](/developers/docs/tools/litesvm/typescript/getting-started)**
    Tìm hiểu cách kiểm thử chương trình Solana của riêng bạn bằng LiteSVM cho TypeScript{" "}
    <br /> **[Ví dụ →](/developers/docs/tools/litesvm/typescript/examples)** Xem
    các giải pháp sao chép và dán cho những tình huống phổ biến <br /> **[Tài liệu tham khảo API
    →](/developers/docs/tools/litesvm/typescript/api-reference)** Tìm hiểu các tính năng nâng cao
    và cấu hình tùy chỉnh
  </Tab>
</Tabs>
