Metode untuk mengirim, mensimulasikan, dan mengelola transaksi.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
Jalankan sebuah transaksi. Mengembalikan metadata saat berhasil atau detail kegagalan saat terjadi error.
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());
Properti hasil transaksi seperti err(), computeUnitsConsumed(),
logs(), dan signature() adalah fungsi getter, bukan properti. Selalu
panggil dengan tanda kurung.
Metode TransactionMetadata
| Metode | Mengembalikan | Deskripsi |
|---|---|---|
signature() | Uint8Array | Byte tanda tangan transaksi |
computeUnitsConsumed() | bigint | Unit komputasi yang digunakan |
logs() | string[] | Log program |
innerInstructions() | InnerInstruction[] | Instruksi inner (CPI) |
returnData() | TransactionReturnData | Data kembalian dari program |
prettyLogs() | string | Log berformat dengan warna |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
Simulasikan transaksi tanpa mengubah state.
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);
Simulasi tidak mengubah state. Gunakan untuk memeriksa transaksi sebelum mengirimnya.
Manajemen Blockhash
latestBlockhash
latestBlockhash(): Blockhash
Dapatkan blockhash saat ini.
const blockhash = client.svm.latestBlockhash();console.log("Blockhash:", blockhash);
getLatestBlockhash (via client.rpc)
Dapatkan blockhash beserta informasi lifetime untuk membangun transaksi secara manual.
Gunakan helper RPC karena mengembalikan baik blockhash maupun lastValidBlockHeight.
import {createTransactionMessage,setTransactionMessageLifetimeUsingBlockhash} from "@solana/kit";const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();const tx = setTransactionMessageLifetimeUsingBlockhash(blockhashLifetime,createTransactionMessage({ version: 0 }));
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.
expireBlockhash
expireBlockhash(): LiteSVM
Maju ke blockhash baru. Berguna untuk menguji skenario kedaluwarsa 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
Riwayat Transaksi
Aktifkan riwayat transaksi untuk mengambil transaksi setelah dikirim:
// 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?