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

# Transazioni

> Esegui e simula transazioni con LiteSVM in TypeScript

Metodi per inviare, simulare e gestire le transazioni.

## sendTransaction

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

Esegue una transazione. Restituisce `TransactionMetadata` in caso di successo oppure
`FailedTransactionMetadata` in caso di errore. Per capire quale dei due hai ottenuto, usa
`instanceof FailedTransactionMetadata`. Un risultato di errore dispone di `err()` per
l'errore e di `meta()` per i relativi metadati, inclusi i 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` proviene dal pacchetto `litesvm`, che viene
  installato da `@solana/kit-plugin-litesvm`. Aggiungilo alle tue dipendenze con
  `npm install litesvm` per poterlo importare.
</Info>

<Warning>
  Le proprietà del risultato di una transazione, come `err()`, `computeUnitsConsumed()`,
  `logs()` e `signature()`, sono **funzioni getter**, non proprietà. Chiamale sempre
  con le parentesi.
</Warning>

### Metodi di TransactionMetadata

| Metodo                   | Restituisce                 | Descrizione                 |
| ------------------------ | ----------------------- | --------------------------- |
| `signature()`            | `Uint8Array`            | Byte della firma della transazione |
| `computeUnitsConsumed()` | `bigint`                | Compute unit utilizzate          |
| `logs()`                 | `string[]`              | Log del programma                |
| `innerInstructions()`    | `InnerInstruction[]`    | Istruzioni interne (CPI)    |
| `returnData()`           | `TransactionReturnData` | Dati restituiti dai programmi   |
| `prettyLogs()`           | `string`                | Log formattati con colori  |

## simulateTransaction

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

Simula una transazione senza modificare lo stato.

```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>
  La simulazione non modifica lo stato. Usala per verificare le transazioni prima di inviarle.
</Info>

## Gestione del blockhash

### latestBlockhash

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

Ottiene il blockhash corrente.

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

### getLatestBlockhash (tramite client.rpc)

Ottiene il blockhash insieme alle informazioni sulla sua validità, per costruire le transazioni manualmente.
Usa l'helper RPC, perché restituisce sia `blockhash` sia `lastValidBlockHeight`.

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

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

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

<Info>
  Quando usi plugin di programma come `systemProgram()` o `tokenProgram()`, non
  devi mai recuperare il blockhash da solo: `client.sendTransaction()` (e
  l'helper `.sendTransaction()` sulle catene di istruzioni) se ne occupano al posto tuo.
</Info>

### expireBlockhash

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

Passa a un nuovo blockhash. Utile per testare gli scenari di scadenza del 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
```

## Cronologia delle transazioni

Abilita la cronologia delle transazioni per recuperarle dopo l'invio:

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