طرق إرسال المعاملات ومحاكاتها وإدارتها.
sendTransaction
sendTransaction(tx: Transaction): TransactionMetadata | FailedTransactionMetadata
تنفيذ معاملة. يُعيد بيانات وصفية عند النجاح أو تفاصيل الخطأ عند الفشل.
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());
خصائص نتيجة المعاملة مثل err()، وcomputeUnitsConsumed()،
وlogs()، وsignature() هي دوال getter، وليست خصائص. احرص دائمًا
على استدعائها بالأقواس.
طرق TransactionMetadata
| الطريقة | القيمة المُعادة | الوصف |
|---|---|---|
signature() | Uint8Array | بايتات توقيع المعاملة |
computeUnitsConsumed() | bigint | وحدات الحوسبة المستخدمة |
logs() | string[] | سجلات البرنامج |
innerInstructions() | InnerInstruction[] | التعليمات الداخلية (CPI) |
returnData() | TransactionReturnData | البيانات المُعادة من البرامج |
prettyLogs() | string | السجلات المنسّقة بالألوان |
simulateTransaction
simulateTransaction(tx: Transaction): SimulatedTransactionInfo | FailedTransactionMetadata
محاكاة معاملة دون تعديل الحالة.
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);
المحاكاة لا تُغيّر الحالة. استخدمها للتحقق من المعاملات قبل إرسالها.
إدارة Blockhash
latestBlockhash
latestBlockhash(): Blockhash
الحصول على الـ blockhash الحالي.
const blockhash = client.svm.latestBlockhash();console.log("Blockhash:", blockhash);
getLatestBlockhash (عبر client.rpc)
الحصول على الـ blockhash مع معلومات مدة الصلاحية لبناء المعاملات يدويًا.
استخدم مساعد RPC إذ يُعيد كلًا من blockhash وlastValidBlockHeight.
import {createTransactionMessage,setTransactionMessageLifetimeUsingBlockhash} from "@solana/kit";const { value: blockhashLifetime } = await client.rpc.getLatestBlockhash().send();const tx = setTransactionMessageLifetimeUsingBlockhash(blockhashLifetime,createTransactionMessage({ version: 0 }));
عند استخدام إضافات البرامج مثل systemProgram() أو tokenProgram()،
لن تحتاج أبدًا إلى جلب الـ blockhash بنفسك — إذ يتولى client.sendTransaction() (و
مساعد .sendTransaction() في سلاسل التعليمات) ذلك تلقائيًا.
expireBlockhash
expireBlockhash(): LiteSVM
الانتقال إلى blockhash جديد. مفيد لاختبار سيناريوهات انتهاء صلاحية الـ 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
سجل المعاملات
فعّل سجل المعاملات لاسترداد المعاملات بعد الإرسال:
// 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?