> ## 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-utils クレートを使って Solana プログラムのテストを効率化する方法を学びます

[crates.io で `litesvm-utils` を見る](https://crates.io/crates/litesvm-utils)

## インストール

必要な依存関係がすべて揃っていることを確認してください:

```bash
cargo add --dev litesvm-utils solana-sdk@3 spl-token
cargo add --dev solana-system-interface@2 --features bincode
```

<Warning>
  `litesvm-utils` は独自のバージョンの `litesvm` を固定して再エクスポートしています。
  `LiteSVM` は（以下の例のように）`litesvm_utils` からインポートし、
  別途 `litesvm` の依存関係を追加しないでください。追加すると、互換性のない
  2 つのクレートのコピーが混在することになります。
</Warning>

## litesvm-utils とは?

`litesvm-utils` クレートは、Solana プログラムのテストを簡素化するための必須のヘルパートレイトとユーティリティを提供します。使いやすい API により、よくあるテストの定型コードを 30 行以上から、最小限で読みやすいコードに削減します。

<Steps>
<Step title="TestHelpers トレイト">
- 1 回のメソッド呼び出しで資金付きアカウントを作成
- トークンミントと token account を簡単にセットアップ
- 定型コードなしで PDA を導出
- 時間ベースのテストのために slot を管理
</Step>

<Step title="AssertionHelpers トレイト">
- アカウントの状態（存在、クローズ済み、オーナー）をアサート
- トークン残高とミントの供給量を検証
- SOL 残高を確認
- アカウントデータの長さを検証
</Step>

<Step title="TransactionHelpers トレイト">
- 充実した結果処理で instructions を実行
- トランザクションの成功または失敗をアサート
- 特定のエラーを確認
- トランザクションログとコンピュートユニットを検査
</Step>

<Step title="LiteSVMBuilder">
- テスト環境のセットアップに使えるフルエントなビルダーパターン
- メソッドチェーンでプログラムをデプロイ
- 便利な静的ファクトリーメソッド
</Step>
</Steps>

## クイックサンプル

litesvm-utils の威力を示す完全なサンプルです:

```rust
use litesvm_utils::{AssertionHelpers, LiteSVM, Signer, TestHelpers, TransactionHelpers};
use solana_sdk::native_token::LAMPORTS_PER_SOL;
use solana_system_interface::instruction as system_instruction;

#[test]
fn test_with_utils() {
    let mut svm = LiteSVM::new();

    // Create funded accounts in one line
    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    // Create a token mint easily
    let mint = svm.create_token_mint(&alice, 9).unwrap();

    // Create an associated token account (returns Pubkey)
    let alice_ata = svm.create_associated_token_account(&mint.pubkey(), &alice).unwrap();

    // Mint tokens
    svm.mint_to(&mint.pubkey(), &alice_ata, &alice, 1000).unwrap();

    // Assert balances
    svm.assert_token_balance(&alice_ata, 1000);

    // Execute a SOL transfer with rich result handling
    let transfer_ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        LAMPORTS_PER_SOL,
    );

    let result = svm.send_instruction(transfer_ix, &[&alice]).unwrap();
    result.assert_success();

    // Verify the transfer
    svm.assert_sol_balance(&bob.pubkey(), LAMPORTS_PER_SOL);

    // Bob now has SOL, so he can pay for his own associated token account
    let bob_ata = svm.create_associated_token_account(&mint.pubkey(), &bob).unwrap();
    svm.assert_token_balance(&bob_ata, 0);
}
```

## 主なメリット

### litesvm-utils の導入前

```rust
// Creating a funded account manually
let keypair = Keypair::new();
let airdrop_tx = Transaction::new_signed_with_payer(
    &[system_instruction::transfer(
        &payer.pubkey(),
        &keypair.pubkey(),
        lamports,
    )],
    Some(&payer.pubkey()),
    &[&payer],
    svm.latest_blockhash(),
);
svm.send_transaction(airdrop_tx).unwrap();
```

### litesvm-utils の導入後

```rust
// One line to create a funded account
let keypair = svm.create_funded_account(lamports).unwrap();
```

## トレイトの概要

### TestHelpers

| メソッド                                         | 戻り値                | 説明                         |
| ---------------------------------------------- | ---------------------- | ----------------------------------- |
| `create_funded_account(lamports)`              | `Result<Keypair>`      | keypair を作成し、資金を付与します      |
| `create_funded_accounts(count, lamports)`      | `Result<Vec<Keypair>>` | 資金付きの keypair を複数作成します    |
| `create_token_mint(authority, decimals)`       | `Result<Keypair>`      | SPL トークンミントを作成します           |
| `create_token_account(mint, owner)`            | `Result<Keypair>`      | 通常の token account を作成します     |
| `create_associated_token_account(mint, owner)` | `Result<Pubkey>`       | ATA を作成し、そのアドレスを返します |
| `mint_to(mint, account, authority, amount)`    | `Result<()>`           | アカウントにトークンをミントします          |
| `derive_pda(seeds, program_id)`                | `(Pubkey, u8)`         | bump seed 付きで PDA を導出します        |
| `get_pda(seeds, program_id)`                   | `Pubkey`               | PDA アドレスのみを取得します           |
| `get_pda_with_bump(seeds, program_id)`         | `(Pubkey, u8)`         | `derive_pda` のエイリアスです              |
| `get_current_slot()`                           | `u64`                  | 現在の slot を返します                |
| `advance_slot(slots)`                          | `()`                   | 時間を N 個の slot 分進めます            |

### AssertionHelpers

| メソッド                                  | 説明                                                            |
| --------------------------------------- | ---------------------------------------------------------------------- |
| `assert_account_exists(pubkey)`         | アカウントが存在しない場合はパニックを起こします                                       |
| `assert_account_closed(pubkey)`         | アカウントが存在しない、または lamport がゼロでデータもない場合を除き、パニックを起こします |
| `assert_token_balance(account, amount)` | トークン残高を検証します                                                 |
| `assert_sol_balance(pubkey, lamports)`  | SOL 残高を検証します                                                   |
| `assert_mint_supply(mint, supply)`      | ミントの総供給量を検証します                                             |
| `assert_account_owner(pubkey, owner)`   | アカウントのオーナーを検証します                                                 |
| `assert_account_data_len(pubkey, len)`  | データ長を検証します                                                   |

### TransactionHelpers

| メソッド                            | 戻り値                                       | 説明                           |
| --------------------------------- | --------------------------------------------- | ------------------------------------- |
| `send_instruction(ix, signers)`   | `Result<TransactionResult, TransactionError>` | 単一の instruction を送信します            |
| `send_instructions(ixs, signers)` | `Result<TransactionResult, TransactionError>` | 複数の instructions を 1 つのトランザクションで送信します |
| `send_transaction_result(tx)`     | `Result<TransactionResult, TransactionError>` | 生の `Transaction` を送信します             |

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

### よくあるエラー

| エラー               | 原因                                       | 解決策                          |
| ------------------- | ------------------------------------------- | --------------------------------- |
| `AccountNotFound`   | 存在しないアカウントを使用しようとしている | 先に `create_funded_account` を使用してください |
| `InsufficientFunds` | トランザクションに必要な lamport が不足している         | 初期の資金額を増やしてください   |
| `OwnerMismatch`     | アカウントが誤ったプログラムによって所有されている              | 正しいプログラム ID を確認してください         |
| `AssertionFailed`   | 残高または状態が期待値と一致しない     | テストのロジックを確認してください             |
