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

# Szybki start

> Dowiedz się, jak używać crate'a litesvm-token do testowania z tokenami SPL

[Zobacz `litesvm-token` na crates.io](https://crates.io/crates/litesvm-token)

## Instalacja

Upewnij się, że masz wszystkie wymagane zależności:

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

## Podstawy SPL Token

W Solanie utworzenie token account to proces dwuetapowy.

<Steps>
<Step title="Utwórz Mint Account">
- Nie przechowuje salda tokenów 
- Zawiera wszystkie globalne informacje o tokenie, takie jak całkowita podaż, liczba miejsc dziesiętnych (decimals), uprawnienia (authority) itp.
- Na każdy token przypada jeden Mint Account
- Właścicielem (owner) jest Token Program (Tokenkeg lub Token-2022)
</Step>

<Step title="Utwórz Token Account">
- Przechowuje saldo konkretnego tokena SPL 
- Zawiera Mint Account, który określa, jaki token SPL znajduje się na tym koncie 
- Jednemu Mint Account może odpowiadać wiele token accounts 
- Właściciel (owner) to podmiot, który kontroluje tokeny na tym koncie 
</Step>
</Steps>

## Rodzaje Token Accounts

### Zwykły Token Account

Token account przechowuje Twoje saldo konkretnego tokena 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="Zalety" icon="check">
    - Można utworzyć wiele kont dla tego samego mint/owner
    - Elastyczność – można użyć dowolnego adresu
  </Card>

  <Card title="Wady" icon="x">
    - Brak determinizmu – adresy trzeba śledzić ręcznie
    - Odbiorca musi podać, na które konto wysłać środki
    - Wiele kont powoduje zamieszanie
  </Card>
</Columns>

<Info>
  **Kiedy używać:** Konta tymczasowe/escrow, konta należące do programu z niestandardową
  logiką, gdy potrzebujesz wielu kont dla tego samego tokena, zaawansowane
  strategie DeFi.
</Info>

### Associated Token Account (ATA)

ATA to token account pod deterministycznym adresem 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]
```

Jak wyprowadzany jest adres 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="Zalety" icon="check">
    - Jedno kanoniczne konto na parę owner/mint
    - Deterministyczny – każdy może obliczyć adres
    - Upraszcza płatności – wystarczy adres portfela + mint
    - Standardowa konwencja we wszystkich aplikacjach Solana
  </Card>

  <Card title="Wady" icon="x">
    - Można mieć tylko JEDNO ATA na parę owner/mint (zgodnie z założeniem)
  </Card>
</Columns>

<Info>
  **Zalecane w większości przypadków:** aplikacje portfelowe, protokoły DeFi, posiadanie
  NFT, systemy płatności i wszelkie transfery tokenów po stronie użytkownika.
</Info>

## Szybki przykład

Oto kompletny przykład tworzenia mintu tokena i emisji tokenów:

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

## Kluczowe pojęcia

### Miejsca dziesiętne tokena (decimals)

Większość tokenów używa miejsc dziesiętnych do reprezentowania wartości ułamkowych:

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

### Relacje między kontami

Zrozumienie relacji między kontami jest kluczowe:

- **Mint Account**: definiuje token (podaż, decimals, uprawnienia)
- **Token Account**: przechowuje tokeny konkretnego właściciela
- **Associated Token Account**: deterministyczny token account dla
  pary owner+mint
- **Mint Authority**: może tworzyć nowe tokeny
- **Freeze Authority**: może zamrażać token accounts (opcjonalnie)

## Rozwiązywanie problemów

### Typowe błędy

| Błąd                       | Przyczyna                                               | Rozwiązanie                                |
| --------------------------- | --------------------------------------------------- | --------------------------------------- |
| `AccountNotFound`           | Próba użycia konta, które nie istnieje         | Upewnij się, że konto zostało utworzone przed użyciem    |
| `AccountAlreadyInitialized` | Próba zainicjowania konta, które jest już zainicjowane | Przed utworzeniem sprawdź, czy konto już istnieje |
| `InsufficientFunds`         | Za mało lamport na rent lub za mało tokenów do transferu | Zadbaj o wystarczające środki lub wykonaj mint odpowiedniej ilości tokenów       |
| `OwnerMismatch`             | Konto należy do niewłaściwego programu                      | Podczas tworzenia sprawdź, czy użyto poprawnego ID programu |
