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

# Token-Tests

> Teste SPL-Token mit @solana-program/token und LiteSVM

Das Testen von Token-Logik auf Solana bedeutet, dass du Mints, token accounts und
Authorities einrichten musst, bevor du die Transfer-Logik testen kannst, um die es dir eigentlich geht. Das
`tokenProgram()`-Plugin aus `@solana-program/token` reduziert diese Einrichtung auf
wenige Methodenaufrufe — und übernimmt die ATA-Erstellung automatisch.

[`@solana-program/token` auf npm ansehen](https://www.npmjs.com/package/@solana-program/token)

## Installation

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

## Einen Mint erstellen

```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
```

## Token minten

`mintToATA` gibt Token an den Associated Token Account eines Owners aus. Falls der ATA
nicht existiert, wird er in derselben Transaktion erstellt.

```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();
```

Der Parameter `decimals` ist eine Sicherheitsprüfung – er muss exakt mit den Decimals des Mints
übereinstimmen, andernfalls lehnt das Token Program die Transaktion mit `Custom program error: #18` (`0x12`,
`MintDecimalsMismatch`) ab. Dadurch wird
verhindert, dass versehentlich 1000-mal zu viele Token geminted werden.

## Token übertragen

`transferToATA` überträgt Token zwischen Nutzern. Der ATA des Empfängers wird bei Bedarf
automatisch erstellt.

```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();
```

Die `authority` ist der Signer, dem die Quell-Token gehören. Das Plugin leitet
sowohl den Quell- als auch den Ziel-ATA aus deren Adressen + dem Mint ab.

## Guthaben überprüfen

Um Token-Guthaben auszulesen, leite die ATA-Adresse mit
`findAssociatedTokenPda` ab und rufe die dekodierten Konten ab:

```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
```

## Decimals

Token-Beträge werden immer in Rohwerten angegeben. 1000 Token mit 9 Decimals sind
`1_000_000_000_000n`, nicht `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>
  Verwende für Token-Beträge immer `BigInt`. Der `Number`-Typ von JavaScript verliert oberhalb von 2^53
  an Präzision.
</Warning>

## Plugin-Referenz

| Methode                                      | Beschreibung                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| `client.token.instructions.createMint()`    | Einen neuen SPL-Token-Mint erstellen                                  |
| `client.token.instructions.mintToATA()`     | Token an den ATA eines Owners minten (erstellt den ATA bei Bedarf)        |
| `client.token.instructions.transferToATA()` | Token an den ATA eines Empfängers übertragen (erstellt den ATA bei Bedarf) |
| `client.token.accounts.mint.fetch()`        | Einen mint account abrufen und dekodieren                              |
| `client.token.accounts.token.fetch()`       | Einen token account abrufen und dekodieren                             |

## Häufige Fehler

| Fehler                                                               | Ursache                             | Lösung                                                    |
| ------------------------------------------------------------------- | --------------------------------- | ------------------------------------------------------ |
| `Custom program error: #18` (`0x12`, `MintDecimalsMismatch`)        | `decimals` stimmt nicht mit dem Mint überein | Verwende denselben Decimals-Wert, den du an `createMint` übergeben hast |
| `Account not found at address: ...`                                 | Konten existieren noch nicht         | Stelle sicher, dass der Mint erstellt wurde, bevor du abrufst oder mintest  |
| `Attempt to debit an account but found no record of a prior credit` | Der Payer hat kein SOL, um Fee zu bezahlen  | Sende zuerst SOL per Airdrop an den Payer                         |
| `Custom program error: #4` (`OwnerMismatch`)                       | Die signierende `authority` ist nicht der Owner des Quell-token account | Übergib den tatsächlichen Owner als `authority`     |
