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

# Giao dịch

> Thực thi và mô phỏng giao dịch với LiteSVM trong TypeScript

Các phương thức để gửi, mô phỏng và quản lý giao dịch.

## sendTransaction

```typescript
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
```

Thực thi một giao dịch. Trả về `TransactionMetadata` nếu thành công, hoặc
`FailedTransactionMetadata` nếu thất bại. Kiểm tra kết quả nhận được bằng
`instanceof FailedTransactionMetadata`. Kết quả thất bại có `err()` để lấy
lỗi và `meta()` để lấy metadata của giao dịch, bao gồm cả log.

```typescript
import { FailedTransactionMetadata } from "litesvm";

const result = client.svm.sendTransaction(signedTx);

if (result instanceof FailedTransactionMetadata) {
  // Check for errors
  console.error("Transaction failed:", result.err());
  console.error("Logs:", result.meta().logs());
} else {
  // Access result properties (note: these are getter functions)
  console.log("Signature:", result.signature());
  console.log("Compute units:", result.computeUnitsConsumed());
  console.log("Logs:", result.logs());
}
```

<Info>
  `FailedTransactionMetadata` đến từ gói `litesvm`, gói này được
  `@solana/kit-plugin-litesvm` cài đặt. Hãy thêm nó vào các dependency của riêng bạn bằng
  `npm install litesvm` để có thể import.
</Info>

<Warning>
  Các thuộc tính kết quả giao dịch như `err()`, `computeUnitsConsumed()`,
  `logs()` và `signature()` là **hàm getter**, không phải thuộc tính. Luôn
  gọi chúng bằng dấu ngoặc đơn.
</Warning>

### Các phương thức của TransactionMetadata

| Phương thức                   | Giá trị trả về                 | Mô tả                 |
| ------------------------ | ----------------------- | --------------------------- |
| `signature()`            | `Uint8Array`            | Các byte chữ ký của giao dịch |
| `computeUnitsConsumed()` | `bigint`                | Số compute unit đã sử dụng          |
| `logs()`                 | `string[]`              | Log của chương trình                |
| `innerInstructions()`    | `InnerInstruction[]`    | Các lệnh bên trong (CPI)    |
| `returnData()`           | `TransactionReturnData` | Dữ liệu trả về từ các chương trình   |
| `prettyLogs()`           | `string`                | Log được định dạng kèm màu sắc  |

## simulateTransaction

```typescript
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
```

Mô phỏng một giao dịch mà không làm thay đổi trạng thái.

```typescript
import { FailedTransactionMetadata } from "litesvm";

const simResult = client.svm.simulateTransaction(signedTx);

if (simResult instanceof FailedTransactionMetadata) {
  console.error("Simulation failed:", simResult.err());
  console.error("Logs:", simResult.meta().logs());
} else {
  // Use .meta() to get TransactionMetadata
  const meta = simResult.meta();
  console.log("Would use", meta.computeUnitsConsumed(), "compute units");
  console.log("Logs:", meta.logs());

  // Get post-execution account states
  const postAccounts = simResult.postAccounts();
  console.log("Post-state accounts:", postAccounts);
}
```

<Info>
  Mô phỏng không làm thay đổi trạng thái. Hãy dùng nó để kiểm tra giao dịch trước khi gửi.
</Info>

## Quản lý Blockhash

### latestBlockhash

```typescript
latestBlockhash(): Blockhash
```

Lấy blockhash hiện tại.

```typescript
const blockhash = client.svm.latestBlockhash();
console.log("Blockhash:", blockhash);
```

### getLatestBlockhash (qua client.rpc)

Lấy blockhash cùng thông tin về thời hạn hiệu lực để xây dựng giao dịch thủ công.
Hãy dùng helper RPC vì nó trả về cả `blockhash` lẫn `lastValidBlockHeight`.

```typescript
import {
  createTransactionMessage,
  setTransactionMessageLifetimeUsingBlockhash
} from "@solana/kit";

const { value: blockhashLifetime } = await client.rpc
  .getLatestBlockhash()
  .send();

const tx = setTransactionMessageLifetimeUsingBlockhash(
  blockhashLifetime,
  createTransactionMessage({ version: 0 })
);
```

<Info>
  Khi sử dụng các plugin chương trình như `systemProgram()` hoặc `tokenProgram()`, bạn
  không bao giờ cần tự lấy blockhash — `client.sendTransaction()` (và
  helper `.sendTransaction()` trên các chuỗi lệnh) sẽ xử lý việc đó cho bạn.
</Info>

### expireBlockhash

```typescript
expireBlockhash(): void
```

Chuyển sang một blockhash mới. Hữu ích để kiểm thử các kịch bản blockhash hết hạn.

```typescript
const oldBlockhash = client.svm.latestBlockhash();
client.svm.expireBlockhash();
const newBlockhash = client.svm.latestBlockhash();

console.log("Old:", oldBlockhash);
console.log("New:", newBlockhash);
// Blockhashes will be different
```

## Lịch sử giao dịch

Bật lịch sử giao dịch để truy xuất các giao dịch sau khi gửi:

```typescript
// Enable history storage (stores last N transactions)
client.svm.withTransactionHistory(100n);

// Send a transaction
const result = client.svm.sendTransaction(signedTx);

// Later, retrieve it by signature (base58 string)
// Note: getTransaction expects a base58-encoded signature string
```
