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

# Transaksi

> Jalankan dan simulasikan transaksi dengan LiteSVM di TypeScript

Metode untuk mengirim, mensimulasikan, dan mengelola transaksi.

## sendTransaction

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

Menjalankan transaksi. Mengembalikan `TransactionMetadata` jika berhasil, atau
`FailedTransactionMetadata` jika gagal. Periksa hasil yang Anda dapatkan dengan
`instanceof FailedTransactionMetadata`. Hasil yang gagal memiliki `err()` untuk
error dan `meta()` untuk metadatanya, termasuk 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` berasal dari paket `litesvm`, yang diinstal oleh
  `@solana/kit-plugin-litesvm`. Tambahkan paket tersebut ke dependensi Anda sendiri dengan
  `npm install litesvm` agar Anda dapat mengimpornya.
</Info>

<Warning>
  Properti hasil transaksi seperti `err()`, `computeUnitsConsumed()`,
  `logs()`, dan `signature()` adalah **fungsi getter**, bukan properti. Selalu
  panggil dengan tanda kurung.
</Warning>

### Metode TransactionMetadata

| Metode                   | Nilai Kembali                 | Deskripsi                 |
| ------------------------ | ----------------------- | --------------------------- |
| `signature()`            | `Uint8Array`            | Byte tanda tangan transaksi |
| `computeUnitsConsumed()` | `bigint`                | Compute unit yang digunakan          |
| `logs()`                 | `string[]`              | Log program                |
| `innerInstructions()`    | `InnerInstruction[]`    | Instruksi inner (CPI)    |
| `returnData()`           | `TransactionReturnData` | Data kembalian dari program   |
| `prettyLogs()`           | `string`                | Log terformat dengan warna  |

## simulateTransaction

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

Simulasikan transaksi tanpa mengubah state.

```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>
  Simulasi tidak mengubah state. Gunakan untuk memeriksa transaksi sebelum dikirim.
</Info>

## Manajemen Blockhash

### latestBlockhash

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

Dapatkan blockhash saat ini.

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

### getLatestBlockhash (melalui client.rpc)

Dapatkan blockhash beserta informasi masa berlakunya untuk membangun transaksi secara manual.
Gunakan helper RPC karena helper ini mengembalikan `blockhash` dan `lastValidBlockHeight` sekaligus.

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

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

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

<Info>
  Saat menggunakan plugin program seperti `systemProgram()` atau `tokenProgram()`, Anda
  tidak perlu mengambil blockhash sendiri — `client.sendTransaction()` (dan
  helper `.sendTransaction()` pada rantai instruksi) akan menanganinya untuk Anda.
</Info>

### expireBlockhash

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

Maju ke blockhash baru. Berguna untuk menguji skenario kedaluwarsa blockhash.

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

## Riwayat Transaksi

Aktifkan riwayat transaksi untuk mengambil transaksi setelah dikirim:

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