İşlemleri göndermek, simüle etmek ve yönetmek için kullanılan metotlar.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
Bir işlemi çalıştırır. Başarılı olursa TransactionMetadata, başarısız olursa
FailedTransactionMetadata döndürür. Hangisini aldığınızı
instanceof FailedTransactionMetadata ile kontrol edebilirsiniz. Başarısız bir sonucun hatayı veren err() ve
loglar dahil metadata’sını veren meta() metotları vardır.
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());
}
FailedTransactionMetadata, @solana/kit-plugin-litesvm tarafından kurulan
litesvm paketinden gelir. İçe aktarabilmek için npm install litesvm ile
paketi kendi bağımlılıklarınıza ekleyin.
err(), computeUnitsConsumed(),
logs() ve signature() gibi işlem sonucu özellikleri birer getter fonksiyonudur, özellik değildir. Her zaman
parantezlerle çağırın.
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
Durumu değiştirmeden bir işlemi simüle eder.
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);
}
Simülasyon durumu değiştirmez. İşlemleri göndermeden önce kontrol etmek için kullanın.
Blockhash Yönetimi
latestBlockhash
latestBlockhash(): Blockhash
Geçerli blockhash’i alır.
const blockhash = client.svm.latestBlockhash();
console.log("Blockhash:", blockhash);
getLatestBlockhash (client.rpc üzerinden)
İşlemleri manuel olarak oluşturmak için geçerlilik süresi bilgisiyle birlikte blockhash’i alır.
Hem blockhash hem de lastValidBlockHeight değerini döndürdüğü için RPC yardımcısını kullanın.
import {
createTransactionMessage,
setTransactionMessageLifetimeUsingBlockhash
} from "@solana/kit";
const { value: blockhashLifetime } = await client.rpc
.getLatestBlockhash()
.send();
const tx = setTransactionMessageLifetimeUsingBlockhash(
blockhashLifetime,
createTransactionMessage({ version: 0 })
);
systemProgram() veya tokenProgram() gibi program eklentilerini kullanırken
blockhash’i kendiniz almanız gerekmez — client.sendTransaction() (ve
talimat zincirlerindeki .sendTransaction() yardımcısı) bunu sizin için halleder.
expireBlockhash
Yeni bir blockhash’e geçer. Blockhash süresinin dolması senaryolarını test etmek için kullanışlıdır.
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
İşlem Geçmişi
İşlemleri gönderdikten sonra geri almak için işlem geçmişini etkinleştirin:
// 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