> ## 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-token لاختبار رموز SPL

[عرض `litesvm-token` على crates.io](https://crates.io/crates/litesvm-token)

## التثبيت

تأكد من توفر جميع الاعتماديات المطلوبة:

```bash
cargo add --dev litesvm litesvm-token solana-sdk@4 spl-token spl-associated-token-account
cargo add --dev solana-system-interface --features bincode
```

## أساسيات رموز SPL

في سولانا، يتم إنشاء token account عبر عملية من خطوتين.

<Steps>
<Step title="إنشاء Mint Account">
- لا يحتوي على رصيد من الرموز 
- يحتفظ بجميع المعلومات العامة للرمز، مثل إجمالي المعروض والخانات العشرية والصلاحية (authority) وغيرها
- يوجد Mint Account واحد لكل رمز
- المالك هو Token Program (‏Tokenkeg أو Token-2022)
</Step>

<Step title="إنشاء Token Account">
- يخزّن رصيد رمز SPL معيّن 
- يحتفظ بـ Mint Account لتحديد أي رمز SPL موجود في هذا الحساب 
- يمكن وجود عدة token accounts لـ Mint Account واحد 
- المالك هو من يملك التحكم في الرموز داخل هذا الحساب 
</Step>
</Steps>

## أنواع Token Accounts

### Token Account العادي

يخزّن token account رصيدك من رمز SPL معيّن:

```rust
// Can create token accounts at ANY address
let token_account = Keypair::new();  // Random address
let create_ix = system_instruction::create_account(
    &payer.pubkey(),
    &token_account.pubkey(),  // Any address you want
    rent,
    165,  // Token account size
    &spl_token::id(),
);
let init_ix = spl_token::instruction::initialize_account(
    &spl_token::id(),
    &token_account.pubkey(),
    &mint,
    &owner.pubkey(),
)?;
```

<Columns cols={2}>
  <Card title="المزايا" icon="check">
    - يمكن إنشاء عدة حسابات لنفس mint/المالك
    - مرن - يمكن استخدام أي عنوان
  </Card>

  <Card title="العيوب" icon="x">
    - غير حتمي - يجب تتبّع العناوين يدويًا
    - على المستلم أن يخبرك بالحساب الذي يجب الإرسال إليه
    - يسبب التباسًا عند وجود عدة حسابات
  </Card>
</Columns>

<Info>
  **متى تستخدمه:** الحسابات المؤقتة/حسابات الضمان (escrow)، والحسابات المملوكة للبرامج ذات المنطق المخصص، وعندما تحتاج إلى عدة حسابات لنفس الرمز، واستراتيجيات DeFi المتقدمة.
</Info>

### Associated Token Account (ATA)

ATA هو token account على عنوان PDA حتمي:

```rust
// ATA address is ALWAYS the same for owner + mint
let ata = get_associated_token_address(&owner.pubkey(), &mint);
// Address derived from: [owner_pubkey, token_program_id, mint]
```

كيفية اشتقاق عنوان ATA:

```rust
// The ATA address is a PDA of the Associated Token Program;
// the account itself is owned by the Token Program
let (ata, bump) = Pubkey::find_program_address(
    &[
        owner.as_ref(),
        spl_token::id().as_ref(),
        mint.as_ref(),
    ],
    &spl_associated_token_account::id(),  // ATA program
);
```

<Columns cols={2}>
  <Card title="المزايا" icon="check">
    - حساب أساسي واحد لكل زوج مالك/mint
    - حتمي - يمكن لأي شخص حساب العنوان
    - يبسّط المدفوعات - يكفي عنوان المحفظة + mint
    - عُرف قياسي في جميع تطبيقات سولانا
  </Card>

  <Card title="العيوب" icon="x">
    - يمكن امتلاك ATA واحد فقط لكل زوج مالك/mint (بحكم التصميم)
  </Card>
</Columns>

<Info>
  **موصى به لمعظم الحالات:** تطبيقات المحافظ، وبروتوكولات DeFi، ومقتنيات NFT، وأنظمة الدفع، وأي عمليات نقل رموز موجهة للمستخدم.
</Info>

## مثال سريع

إليك مثالًا كاملًا لإنشاء mint للرمز وسكّ الرموز:

```rust
use litesvm::LiteSVM;
use litesvm_token::{
    get_spl_account,
    spl_token::{native_mint::DECIMALS, state::Account as TokenAccount},
    CreateAccount, CreateMint, MintTo, Transfer,
};
use solana_sdk::{
    native_token::LAMPORTS_PER_SOL,
    signature::{Keypair, Signer},
};

#[test]
fn test_create_and_mint_tokens() {
    let mut svm = LiteSVM::new();

    // Create payer account and fund it
    let payer = Keypair::new();
    svm.airdrop(&payer.pubkey(), 10 * LAMPORTS_PER_SOL).unwrap();

    // Create a new SPL token mint with the payer as the mint authority
    let mint = CreateMint::new(&mut svm, &payer)
        .authority(&payer.pubkey())
        .decimals(DECIMALS)
        .send()
        .unwrap();

    // Create a token account for the payer
    let token_account = CreateAccount::new(&mut svm, &payer, &mint)
        .owner(&payer.pubkey())
        .send()
        .unwrap();

    // Mint tokens into the payer's token account
    MintTo::new(&mut svm, &payer, &mint, &token_account, 1000)
        .owner(&payer)
        .send()
        .unwrap();

    // Verify balance
    let token_account: TokenAccount = get_spl_account(&svm, &token_account).unwrap();
    let account_balance = token_account.amount;
    assert_eq!(account_balance, 1000)
}
```

## المفاهيم الأساسية

### الخانات العشرية للرمز

تستخدم معظم الرموز خانات عشرية لتمثيل الكميات الكسرية:

```rust
// SOL has 9 decimals
let one_sol = 10_u64.pow(9);  // 1_000_000_000 lamports

// USDC has 6 decimals
let one_usdc = 10_u64.pow(6); // 1_000_000 micro-USDC

// Always account for decimals in calculations
let amount_tokens = 100;
let amount_raw = amount_tokens * 10_u64.pow(decimals as u32);
```

### العلاقات بين الحسابات

فهم العلاقات بين الحسابات أمر بالغ الأهمية:

- **Mint Account**: يعرّف الرمز (المعروض، والخانات العشرية، والصلاحيات)
- **Token Account**: يحتفظ بالرموز لمالك معيّن
- **Associated Token Account**: token account حتمي لزوج المالك + mint
- **Mint Authority**: يمكنها إنشاء رموز جديدة
- **Freeze Authority**: يمكنها تجميد token accounts (اختياري)

## استكشاف الأخطاء وإصلاحها

### الأخطاء الشائعة

| الخطأ                       | السبب                                               | الحل                                |
| --------------------------- | --------------------------------------------------- | --------------------------------------- |
| `AccountNotFound`           | محاولة استخدام حساب غير موجود         | تأكد من إنشاء الحساب قبل استخدامه    |
| `AccountAlreadyInitialized` | محاولة تهيئة حساب مهيأ بالفعل | تحقق من وجود الحساب قبل إنشائه |
| `InsufficientFunds`         | لا توجد lamports كافية لدفع rent أو رموز كافية للتحويل | تأكد من توفر التمويل أو السكّ الكافي       |
| `OwnerMismatch`             | الحساب مملوك لبرنامج خاطئ                      | تحقق من معرّف البرنامج الصحيح عند الإنشاء |
