> ## 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 トークンをテストする方法を学びます

[crates.io で `litesvm-token` を見る](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 トークンの基本

Solana では、token account の作成は 2 段階のプロセスです。

<Steps>
<Step title="Mint Account を作成する">
- トークン残高を持たない 
- 総供給量、小数桁数、権限など、トークンのグローバル情報をすべて保持する
- トークン 1 つにつき Mint Account は 1 つ
- オーナーは Token Program(Tokenkeg または Token-2022)
</Step>

<Step title="Token Account を作成する">
- 特定の SPL トークンの残高を保存する 
- Mint Account を保持し、このアカウントにどの SPL トークンが入っているかを定義する 
- 1 つの Mint Account に対して複数の token account を作成できる 
- オーナーは、このアカウント内のトークンを管理する権限を持つ者 
</Step>
</Steps>

## Token Account の種類

### 通常の 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>
  **使用する場面:** 一時的なエスクローアカウント、独自ロジックを持つプログラム所有のアカウント、同じトークンに複数のアカウントが必要な場合、高度な DeFi
  戦略など。
</Info>

### Associated Token Account (ATA)

ATA は、決定論的な PDA アドレスを持つ token account です:

```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 のペアごとに正規のアカウントが 1 つ
    - 決定論的 - 誰でもアドレスを計算できる
    - 支払いが簡単 - ウォレットアドレスと mint だけで済む
    - すべての Solana アプリに共通の標準的な慣例
  </Card>

  <Card title="デメリット" icon="x">
    - オーナーと mint の組み合わせごとに ATA は 1 つしか持てない(設計上の仕様)
  </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**: オーナーと mint のペアに対する
  決定論的な token account
- **Mint Authority**: 新しいトークンを作成できます
- **Freeze Authority**: token account を凍結できます(任意)

## トラブルシューティング

### よくあるエラー

| エラー                       | 原因                                               | 解決策                                |
| --------------------------- | --------------------------------------------------- | --------------------------------------- |
| `AccountNotFound`           | 存在しないアカウントを使用しようとしている         | 使用前にアカウントが作成されていることを確認する    |
| `AccountAlreadyInitialized` | すでに初期化済みのアカウントを初期化しようとしている | 作成前にアカウントが存在するか確認する |
| `InsufficientFunds`         | rent に必要な lamports、または転送に必要なトークンが不足している | 十分な資金/ミント数を確保する       |
| `OwnerMismatch`             | アカウントが誤ったプログラムによって所有されている                      | 作成時に正しいプログラム ID を指定しているか確認する |
