> ## Documentation Index
> Fetch the complete documentation index at: https://solana.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# المعاملات

> تنفيذ المعاملات ومحاكاتها باستخدام LiteSVM في Go

طرق لإرسال المعاملات ومحاكاتها وإدارتها. تُدعم المعاملات القديمة (legacy)
والمعاملات ذات الإصدارات v0 وما بعدها.

تقبل `litesvm-go` بايتات المعاملات المشفرة بصيغة bincode والناتجة عن
`(*solana.Transaction).MarshalBinary` من
[`gagliardetto/solana-go`](https://github.com/gagliardetto/solana-go).

## SendLegacyTransaction

```go
func (s *LiteSVM) SendLegacyTransaction(txBytes []byte) (*TxOutcome, error)
```

أرسل `Transaction` قديمة (legacy) مشفرة بصيغة bincode. عند النجاح تُثبَّت المعاملة
في دفتر الأستاذ داخل الذاكرة.

```go
out, err := svm.SendLegacyTransaction(txBytes)
if err != nil {
    t.Fatal(err)
}

if !out.IsOk() {
    t.Fatalf("tx failed: %s\nlogs: %v", out.Error(), out.Logs())
}

t.Logf("signature: %s", out.Signature())
t.Logf("compute:   %d CU", out.ComputeUnits())
t.Logf("fee:       %d lamports", out.Fee())
```

<Warning>
  لا يكون الخطأ المُرجَع غير فارغ (non-nil) إلا إذا تعذّر فك ترميز البايتات أو حدث
  عطل داخلي. أما المعاملة التي *تُنفَّذ وتفشل* فلا تزال تُرجع `*TxOutcome`
  غير فارغ؛ لذا تحقق دائمًا من `IsOk()` قبل استخدام الحالة الخاصة بالنجاح فقط.
</Warning>

## SendVersionedTransaction

```go
func (s *LiteSVM) SendVersionedTransaction(txBytes []byte) (*TxOutcome, error)
```

الشكل نفسه كما في `SendLegacyTransaction`، ولكن لرسائل v0 وما بعدها. تنتج `MarshalBinary` في `solana-go`
البايتات الصحيحة في كلتا الحالتين.

## SimulateLegacyTransaction / SimulateVersionedTransaction

```go
func (s *LiteSVM) SimulateLegacyTransaction(txBytes []byte) (*TxOutcome, error)
func (s *LiteSVM) SimulateVersionedTransaction(txBytes []byte) (*TxOutcome, error)
```

نفّذ المعاملة دون تثبيت الحالة. يُرجَع الشكل نفسه `*TxOutcome`، كما تُملأ
`PostAccounts()` بالحالة المتوقعة بعد التنفيذ للحسابات القابلة للكتابة.

```go
sim, err := svm.SimulateLegacyTransaction(txBytes)
if err != nil {
    t.Fatal(err)
}

if !sim.IsOk() {
    t.Fatalf("sim failed: %s", sim.Error())
}

t.Logf("would use %d compute units", sim.ComputeUnits())
t.Logf("logs: %v", sim.Logs())

posts, err := sim.PostAccounts()
if err != nil {
    t.Fatal(err)
}
for _, p := range posts {
    t.Logf("%s: %d lamports", p.Address, p.Account.Lamports)
}
```

<Info>
  المحاكاة لا تغيّر الحالة. استخدمها لتقدير ميزانيات الحوسبة أو للتحقق من
  السجلات دون تلويث دفتر الأستاذ لديك.
</Info>

## TxOutcome

تُرجع كل عملية إرسال / محاكاة `*TxOutcome` يحمل البيانات الوصفية نفسها
بغض النظر عن النجاح أو الفشل. تتعامل جميع دوال الوصول بأمان مع المستقبِل `nil`
وتُرجع القيم الصفرية.

| الدالة                | القيمة المُرجَعة                            | الوصف                                               |
| --------------------- | ---------------------------------- | --------------------------------------------------------- |
| `IsOk()`              | `bool`                             | نجحت المعاملة                                     |
| `Error()`             | `string`                           | سبب الفشل بصيغة JSON الخاصة بـ Agave wire-format، مثل `"BlockhashNotFound"` (فارغ عند النجاح) |
| `Signature()`         | `solana.Signature`                 | توقيع المعاملة                                     |
| `ComputeUnits()`      | `uint64`                           | وحدات الحوسبة المستهلكة                                    |
| `Fee()`               | `uint64`                           | الرسوم المحتسبة                                               |
| `Logs()`              | `[]string`                         | سجلات البرنامج                                              |
| `ReturnData()`        | `(solana.PublicKey, []byte, bool)` | بيانات الإرجاع المعيّنة عبر `set_return_data`؛ تكون `ok=false` إذا لم توجد |
| `InnerInstructions()` | `InnerInstructionsList`            | استدعاءات CPI مجمّعة حسب التعليمة ذات المستوى الأعلى                     |
| `PostAccounts()`      | `([]PostAccount, error)`           | الحالة بعد التنفيذ - للمحاكاة فقط                      |

### InnerInstruction

```go
type InnerInstruction struct {
    Instruction solana.CompiledInstruction // indices point into the transaction's account table
    StackHeight uint16                     // 1 for top-level, higher for deeper CPIs
}

type InnerInstructionsList [][]InnerInstruction
```

تُرجع `InnerInstructions()` شريحة (slice) مفهرسة بحسب التعليمة ذات المستوى الأعلى
التي أطلقت كل دفعة من استدعاءات CPI.

### PostAccount

```go
type PostAccount struct {
    Address solana.PublicKey
    Account *solana.Account
}
```

تُرجعها `PostAccounts()` في عمليات المحاكاة.

## إدارة Blockhash

### LatestBlockhash

```go
func (s *LiteSVM) LatestBlockhash() (solana.Hash, error)
```

اقرأ الـ blockhash الحالي ومرّره إلى `solana.NewTransaction(...)` عند
بناء المعاملات.

```go
blockhash, err := svm.LatestBlockhash()
if err != nil {
    t.Fatal(err)
}

tx, _ := solana.NewTransaction(
    []solana.Instruction{ix},
    blockhash,
    solana.TransactionPayer(payer),
)
_ = tx // sign, marshal and send it
```

### ExpireBlockhash

```go
func (s *LiteSVM) ExpireBlockhash() error
```

تقدَّم إلى ما بعد الـ blockhash الحالي بحيث تُرجع `LatestBlockhash` التالية قيمة جديدة.
عندها تفشل المعاملات الموقّعة بـ blockhash أقدم بالخطأ
`"BlockhashNotFound"`، وهو أمر مفيد لاختبار الحالات الحدّية لانتهاء صلاحية الـ blockhash.

```go
bh1, err := svm.LatestBlockhash()
if err != nil {
    t.Fatal(err)
}
if err := svm.ExpireBlockhash(); err != nil {
    t.Fatal(err)
}
bh2, err := svm.LatestBlockhash()
if err != nil {
    t.Fatal(err)
}
// bh1 != bh2
_, _ = bh1, bh2
```

## سجل المعاملات

سجل المعاملات مفعّل افتراضيًا، وهو ما يكشف عمليات إعادة الإرسال المكررة (replays): فإرسال
المعاملة الموقّعة نفسها مرتين يُرجع نتيجة تحمل الخطأ
`AlreadyProcessed`. اضبط السعة (أو عطّل إزالة التكرار) عبر
`SetTransactionHistory`:

```go
// Cap history at 100 entries.
if err := svm.SetTransactionHistory(100); err != nil {
    t.Fatal(err)
}

// Disable dedup entirely (allows replaying identical transactions).
if err := svm.SetTransactionHistory(0); err != nil {
    t.Fatal(err)
}
```

ابحث عن معاملة سابقة بواسطة التوقيع (يحتفظ السجل افتراضيًا بآخر 32 معاملة، بما فيها المعاملات التي نُفِّذت وفشلت):

```go
prior := svm.GetTransaction(sig) // nil if unknown
if prior != nil {
    t.Logf("found: %s, fee=%d", prior.Signature(), prior.Fee())
}
```

## BuildTransferTx (دالة مساعدة للاختبار)

```go
func BuildTransferTx(
    payerSeed [32]byte,
    to solana.PublicKey,
    lamports uint64,
    blockhash solana.Hash,
) ([]byte, error)
```

تُنتج `Transaction` قديمة (legacy) موقّعة ومشفرة بصيغة bincode تنقل `lamports`
من keypair المشتق من `payerSeed` إلى `to`، باستخدام `blockhash`. البايتات
المُرجَعة جاهزة لتمريرها إلى `SendLegacyTransaction`. الدافع هو
keypair المشتق من الـ seed، لذا موّل عنوانه أولًا (على سبيل المثال عبر
`Airdrop`)؛ وإلا ستفشل النتيجة بالخطأ `"InsufficientFundsForFee"`.

```go
var seed [32]byte
copy(seed[:], somePayerSeedBytes)

bh, err := svm.LatestBlockhash()
if err != nil {
    t.Fatal(err)
}
txBytes, err := litesvm.BuildTransferTx(seed, recipient, 1_000_000_000, bh)
if err != nil {
    t.Fatal(err)
}

out, err := svm.SendLegacyTransaction(txBytes)
if err != nil {
    t.Fatal(err)
}
if !out.IsOk() {
    t.Fatalf("tx failed: %s", out.Error())
}
```

<Info>
  وُجدت هذه الدالة لتهيئة الاختبارات. وعندما تبني معاملات حقيقية، ففضّل مسار `solana-go`
  الموضح أعلاه - فهو يتسع ليشمل المعاملات متعددة التعليمات ورسائل v0
  والبرامج العشوائية.
</Info>
