Metodi per inviare, simulare e gestire le transazioni.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
Esegue una transazione. Restituisce metadati in caso di successo o dettagli sull'errore in caso di fallimento.
const result = client.svm.sendTransaction(signedTx);// Check for errorsif ("err" in result && result.err()) {console.error("Transaction failed:", result.err());return;}// Access result properties (note: these are getter functions)console.log("Signature:", result.signature());console.log("Compute units:", result.computeUnitsConsumed());console.log("Logs:", result.logs());
Le proprietà del risultato della transazione come err(), computeUnitsConsumed(),
logs() e signature() sono funzioni getter, non proprietà. Chiamale
sempre con le parentesi.
Metodi di TransactionMetadata
| Metodo | Restituisce | Descrizione |
|---|---|---|
signature() | Uint8Array | Byte della firma della transazione |
computeUnitsConsumed() | bigint | Unità di calcolo utilizzate |
logs() | string[] | Log del programma |
innerInstructions() | InnerInstruction[] | Istruzioni interne (CPI) |
returnData() | TransactionReturnData | Dati restituiti dai programmi |
prettyLogs() | string | Log formattati con colori |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
Simula una transazione senza modificare lo stato.
const simResult = client.svm.simulateTransaction(signedTx);if ("err" in simResult && simResult.err()) {console.error("Simulation failed:", simResult.err());return;}// Use .meta() to get TransactionMetadataconst meta = simResult.meta();console.log("Would use", meta.computeUnitsConsumed(), "compute units");console.log("Logs:", meta.logs());// Get post-execution account statesconst postAccounts = simResult.postAccounts();console.log("Post-state accounts:", postAccounts);
La simulazione non modifica lo stato. Usala per verificare le transazioni prima di inviarle.
Gestione del Blockhash
latestBlockhash
latestBlockhash(): Blockhash
Ottieni il blockhash corrente.
const blockhash = client.svm.latestBlockhash();console.log("Blockhash:", blockhash);
getLatestBlockhash (tramite client.rpc)
Ottieni il blockhash con le informazioni sul ciclo di vita per costruire le transazioni manualmente.
Usa l'helper RPC poiché restituisce sia blockhash che lastValidBlockHeight.
import {createTransactionMessage,setTransactionMessageLifetimeUsingBlockhash} from "@solana/kit";const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();const tx = setTransactionMessageLifetimeUsingBlockhash(blockhashLifetime,createTransactionMessage({ version: 0 }));
Quando si utilizzano plugin di programma come systemProgram() o tokenProgram(), non
è mai necessario recuperare il blockhash manualmente — client.sendTransaction() (e
l'helper .sendTransaction() sulle catene di istruzioni) lo gestiscono automaticamente.
expireBlockhash
expireBlockhash(): LiteSVM
Avanza a un nuovo blockhash. Utile per testare scenari di scadenza del blockhash.
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 recuperare le transazioni dopo l'invio:
// Enable history storage (stores last N transactions)client.svm.withTransactionHistory(100n);// Send a transactionconst result = client.svm.sendTransaction(signedTx);// Later, retrieve it by signature (base58 string)// Note: getTransaction expects a base58-encoded signature string
Is this page helpful?