> ## 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.

# اختبار التوكنات

> اختبر باستخدام توكنات SPL عبر @solana-program/token وLiteSVM

يعني اختبار منطق التوكنات على سولانا إعداد mints وtoken accounts والصلاحيات
قبل أن تتمكن من اختبار منطق التحويل الذي يهمك فعلًا. تختصر إضافة
`tokenProgram()` من `@solana-program/token` هذا الإعداد في
بضع استدعاءات لطرق (methods)، وتتولى إنشاء ATA تلقائيًا.

[عرض `@solana-program/token` على npm](https://www.npmjs.com/package/@solana-program/token)

## التثبيت

```bash
npm install @solana/kit @solana/kit-plugin-litesvm @solana/kit-plugin-signer @solana-program/token
```

## إنشاء Mint

```typescript
import { createClient, generateKeyPairSigner, lamports } from "@solana/kit";
import { litesvm } from "@solana/kit-plugin-litesvm";
import { signer } from "@solana/kit-plugin-signer";
import { tokenProgram } from "@solana-program/token";

const mySigner = await generateKeyPairSigner();
const client = createClient()
  .use(signer(mySigner))
  .use(litesvm())
  .use(tokenProgram());

client.svm.airdrop(client.payer.address, lamports(10_000_000_000n));

const mintAuthority = await generateKeyPairSigner();
const newMint = await generateKeyPairSigner();

await client.token.instructions
  .createMint({
    newMint,
    decimals: 9,
    mintAuthority: mintAuthority.address
  })
  .sendTransaction();

// Verify
const mintAccount = await client.token.accounts.mint.fetch(newMint.address);
console.log(mintAccount.data.supply); // 0n
console.log(mintAccount.data.decimals); // 9
```

## سك التوكنات

تُصدر `mintToATA` التوكنات إلى Associated Token Account الخاص بالمالك. وإذا لم يكن
ATA موجودًا، يتم إنشاؤه ضمن المعاملة نفسها.

```typescript
await client.token.instructions
  .mintToATA({
    mint: newMint.address,
    owner: client.payer.address,
    mintAuthority,
    amount: 1_000_000_000_000n, // 1000 tokens (9 decimals)
    decimals: 9
  })
  .sendTransaction();
```

المعامل `decimals` بمثابة فحص أمان، إذ يجب أن يطابق عدد الخانات العشرية الخاص بالـ mint
تمامًا، وإلا سيرفض Token program المعاملة مع الخطأ `Custom program error: #18` (`0x12`,
`MintDecimalsMismatch`). وهذا
يمنع سك عدد توكنات أكبر بمقدار 1000 ضعف عن غير قصد.

## تحويل التوكنات

تنقل `transferToATA` التوكنات بين المستخدمين. ويُنشأ ATA الخاص بالمستلم
تلقائيًا عند الحاجة.

```typescript
const alice = await generateKeyPairSigner();
const bob = await generateKeyPairSigner();

// Mint to Alice
await client.token.instructions
  .mintToATA({
    mint: newMint.address,
    owner: alice.address,
    mintAuthority,
    amount: 1_000_000_000_000n,
    decimals: 9
  })
  .sendTransaction();

// Transfer 400 tokens from Alice to Bob
await client.token.instructions
  .transferToATA({
    mint: newMint.address,
    authority: alice,
    recipient: bob.address,
    amount: 400_000_000_000n,
    decimals: 9
  })
  .sendTransaction();
```

`authority` هو الموقّع الذي يملك التوكنات المصدر. تشتق الإضافة
كلًا من ATA المصدر وATA الوجهة من عنوانيهما + الـ mint.

## التحقق من الأرصدة

لقراءة أرصدة التوكنات، اشتق عنوان ATA باستخدام
`findAssociatedTokenPda` ثم اجلب الحساب بعد فك ترميزه:

```typescript
import {
  findAssociatedTokenPda,
  TOKEN_PROGRAM_ADDRESS
} from "@solana-program/token";

const [aliceAta] = await findAssociatedTokenPda({
  owner: alice.address,
  mint: newMint.address,
  tokenProgram: TOKEN_PROGRAM_ADDRESS
});

const aliceAccount = await client.token.accounts.token.fetch(aliceAta);
console.log(aliceAccount.data.amount); // 600_000_000_000n
console.log(aliceAccount.data.mint); // newMint.address
console.log(aliceAccount.data.owner); // alice.address
```

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

كميات التوكنات تكون دائمًا بالوحدات الخام. 1000 توكن بـ 9 خانات عشرية تساوي
`1_000_000_000_000n` وليس `1000n`.

```typescript
// USDC has 6 decimals
const oneUSDC = 10n ** 6n; // 1_000_000n

// SOL has 9 decimals
const oneSOL = 10n ** 9n; // 1_000_000_000n
```

<Warning>
  استخدم `BigInt` دائمًا لكميات التوكنات. يفقد `Number` في JavaScript دقته
  فوق 2^53.
</Warning>

## مرجع الإضافة

| الطريقة                                      | الوصف                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| `client.token.instructions.createMint()`    | إنشاء mint جديد لتوكن SPL                                  |
| `client.token.instructions.mintToATA()`     | سك توكنات إلى ATA الخاص بالمالك (ينشئ ATA عند الحاجة)        |
| `client.token.instructions.transferToATA()` | تحويل توكنات إلى ATA الخاص بالمستلم (ينشئ ATA عند الحاجة) |
| `client.token.accounts.mint.fetch()`        | جلب mint account وفك ترميزه                              |
| `client.token.accounts.token.fetch()`       | جلب token account وفك ترميزه                             |

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

| الخطأ                                                               | السبب                             | الحل                                                    |
| ------------------------------------------------------------------- | --------------------------------- | ------------------------------------------------------ |
| `Custom program error: #18` (`0x12`, `MintDecimalsMismatch`)        | قيمة `decimals` لا تطابق الـ mint | استخدم قيمة decimals نفسها التي مررتها إلى `createMint` |
| `Account not found at address: ...`                                 | الحساب غير موجود بعد         | تأكد من إنشاء الـ mint قبل الجلب أو السك  |
| `Attempt to debit an account but found no record of a prior credit` | لا يملك الدافع SOL لدفع الرسوم  | نفّذ airdrop لـ SOL إلى الدافع أولًا                         |
| `Custom program error: #4` (`OwnerMismatch`)                       | الـ `authority` الموقِّع ليس مالك token account المصدر | مرّر المالك الحقيقي بوصفه `authority`     |
