Metody wysyłania, symulowania i zarządzania transakcjami.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
Wykonuje transakcję. Zwraca metadane w przypadku sukcesu lub szczegóły błędu w przypadku niepowodzenia.
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());
Właściwości wyniku transakcji, takie jak err(), computeUnitsConsumed(),
logs() i signature(), to funkcje getter, a nie właściwości. Zawsze
wywołuj je z nawiasami.
Metody TransactionMetadata
| Metoda | Zwraca | Opis |
|---|---|---|
signature() | Uint8Array | Bajty podpisu transakcji |
computeUnitsConsumed() | bigint | Zużyte jednostki obliczeniowe |
logs() | string[] | Logi programu |
innerInstructions() | InnerInstruction[] | Instrukcje wewnętrzne (CPI) |
returnData() | TransactionReturnData | Dane zwrócone przez programy |
prettyLogs() | string | Sformatowane logi z kolorami |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
Symuluje transakcję bez modyfikowania stanu.
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);
Symulacja nie zmienia stanu. Używaj jej do sprawdzania transakcji przed wysłaniem.
Zarządzanie blockhashami
latestBlockhash
latestBlockhash(): Blockhash
Pobiera aktualny blockhash.
const blockhash = client.svm.latestBlockhash();console.log("Blockhash:", blockhash);
getLatestBlockhash (przez client.rpc)
Pobiera blockhash wraz z informacjami o czasie ważności, przydatny do ręcznego budowania transakcji.
Użyj helpera RPC, ponieważ zwraca zarówno blockhash, jak i lastValidBlockHeight.
import {createTransactionMessage,setTransactionMessageLifetimeUsingBlockhash} from "@solana/kit";const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();const tx = setTransactionMessageLifetimeUsingBlockhash(blockhashLifetime,createTransactionMessage({ version: 0 }));
Gdy używasz wtyczek programowych, takich jak systemProgram() lub tokenProgram(), nigdy
nie musisz samodzielnie pobierać blockhasha — client.sendTransaction() (oraz
helper .sendTransaction() w łańcuchach instrukcji) robią to za Ciebie.
expireBlockhash
expireBlockhash(): LiteSVM
Przechodzi do nowego blockhasha. Przydatne do testowania scenariuszy wygasania blockhasha.
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
Historia transakcji
Włącz historię transakcji, aby móc pobierać transakcje po ich wysłaniu:
// 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?