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

# Testowanie tokenów

> Testuj tokeny SPL za pomocą @solana-program/token i LiteSVM

Testowanie logiki tokenów w Solanie oznacza konieczność skonfigurowania mintów, token accounts i uprawnień, zanim będzie można przetestować logikę transferu, na której naprawdę nam zależy. Plugin
`tokenProgram()` z `@solana-program/token` sprowadza tę konfigurację do
kilku wywołań metod — i automatycznie obsługuje tworzenie ATA.

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

## Instalacja

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

## Tworzenie mintu

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

## Mintowanie tokenów

`mintToATA` wydaje tokeny na Associated Token Account właściciela. Jeśli ATA
nie istnieje, zostanie utworzone w tej samej transakcji.

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

Parametr `decimals` jest zabezpieczeniem — musi dokładnie odpowiadać wartości decimals mintu,
w przeciwnym razie Token program odrzuci transakcję z błędem `Custom program error: #18` (`0x12`,
`MintDecimalsMismatch`). Zapobiega to
przypadkowemu wyemitowaniu 1000 razy większej liczby tokenów.

## Transfer tokenów

`transferToATA` przenosi tokeny między użytkownikami. ATA odbiorcy jest tworzone
automatycznie, jeśli jest potrzebne.

```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` to podpisujący, który jest właścicielem tokenów źródłowych. Plugin wyprowadza
zarówno źródłowe, jak i docelowe ATA z ich adresów oraz mintu.

## Weryfikowanie sald

Aby odczytać salda tokenów, wyprowadź adres ATA za pomocą
`findAssociatedTokenPda` i pobierz zdekodowane konto:

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

Ilości tokenów są zawsze podawane w jednostkach bazowych. 1000 tokenów z 9 miejscami dziesiętnymi to
`1_000_000_000_000n`, a nie `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>
  Zawsze używaj `BigInt` dla ilości tokenów. `Number` w JavaScripcie traci precyzję
  powyżej 2^53.
</Warning>

## Opis pluginu

| Metoda                                      | Opis                                                  |
| ------------------------------------------- | ------------------------------------------------------------ |
| `client.token.instructions.createMint()`    | Tworzy nowy mint tokena SPL                                  |
| `client.token.instructions.mintToATA()`     | Mintuje tokeny na ATA właściciela (tworzy ATA, jeśli jest potrzebne)        |
| `client.token.instructions.transferToATA()` | Przesyła tokeny na ATA odbiorcy (tworzy ATA, jeśli jest potrzebne) |
| `client.token.accounts.mint.fetch()`        | Pobiera i dekoduje mint account                              |
| `client.token.accounts.token.fetch()`       | Pobiera i dekoduje token account                             |

## Typowe błędy

| Błąd                                                               | Przyczyna                             | Rozwiązanie                                                    |
| ------------------------------------------------------------------- | --------------------------------- | ------------------------------------------------------ |
| `Custom program error: #18` (`0x12`, `MintDecimalsMismatch`)        | `decimals` nie odpowiada wartości w mincie | Użyj tej samej wartości decimals, którą przekazano do `createMint` |
| `Account not found at address: ...`                                 | Konto jeszcze nie istnieje         | Upewnij się, że mint został utworzony przed pobraniem lub mintowaniem  |
| `Attempt to debit an account but found no record of a prior credit` | Płatnik nie ma SOL na opłaty  | Najpierw wykonaj airdrop SOL na konto płatnika                         |
| `Custom program error: #4` (`OwnerMismatch`)                       | Podpisujący `authority` nie jest właścicielem źródłowego token account | Przekaż prawdziwego właściciela jako `authority`     |
