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

# トランザクションヘルパー

> 豊富なエラーハンドリングでトランザクションを実行し、結果を分析します

## トランザクションの送信

`TransactionHelpers` トレイトは、instructions を送信して結果を分析するための便利なメソッドを提供します。

### 単一の instruction を送信する

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    // Create a transfer instruction
    let transfer_ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        LAMPORTS_PER_SOL,
    );

    // Send and get result (returns Result<TransactionResult, TransactionError>)
    let result = svm.send_instruction(transfer_ix, &[&alice]).unwrap();

    // Assert it succeeded
    result.assert_success();
}
```

### 複数の instructions を送信する

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();
    let carol = svm.create_funded_account(0).unwrap();

    // Create multiple transfer instructions
    let instructions = vec![
        system_instruction::transfer(&alice.pubkey(), &bob.pubkey(), LAMPORTS_PER_SOL),
        system_instruction::transfer(&alice.pubkey(), &carol.pubkey(), LAMPORTS_PER_SOL),
    ];

    // Send all in one transaction
    let result = svm.send_instructions(&instructions, &[&alice]).unwrap();
    result.assert_success();
}
```

## TransactionResult

`send_instruction` メソッドと `send_instructions` メソッドは、トランザクションのメタデータに便利なテスト用ユーティリティを組み合わせた `TransactionResult` を返します。

### 成功をアサートする

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    let ix = system_instruction::transfer(&alice.pubkey(), &bob.pubkey(), LAMPORTS_PER_SOL);
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    // Panics with detailed logs if the transaction failed
    result.assert_success();
}
```

<Info>
  `assert_success()` は、トランザクションが失敗した場合にトランザクションログ全体を表示してパニックするため、デバッグがはるかに簡単になります。
</Info>

### 失敗をアサートする

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

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

    let alice = svm.create_funded_account(1 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    // Try to transfer more than Alice has
    let ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        100 * LAMPORTS_PER_SOL, // Alice only has 1 SOL
    );
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    // This should fail
    result.assert_failure();
}
```

### 特定のエラーをアサートする

`assert_log_error` は、トランザクションログ内のメッセージを確認します。エラー自体(例: `InstructionError(0, Custom(1))`)と照合するには、`assert_error` を使用します。これはログではなく、エラー文字列と比較します:

```rust
result.assert_error("InstructionError");
```

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

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

    let alice = svm.create_funded_account(1 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    let ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        100 * LAMPORTS_PER_SOL,
    );
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    // Assert a specific message is present in the transaction logs
    result.assert_log_error("insufficient lamports");
}
```

### エラーコードをアサートする

カスタムプログラムエラーの場合は、`assert_error` を使ってエラーの `Custom(<code>)` 部分をアサートします:

```rust
// Assert a specific custom error code from your program
result.assert_error("Custom(6000)");
```

<Warning>
  `litesvm-utils` 0.4.0 では `assert_error_code` を使用しないでください。この関数はエラー内の `custom program error: 0x…` を探しますが、実際のエラーは `InstructionError(0, Custom(6000))` という形式のため、常にパニックします。代わりに `assert_error("Custom(6000)")` を使用するか、Anchor プログラムの場合は `assert_anchor_error` を使用してください。
</Warning>

### Anchor エラーをアサートする

Anchor プログラムの場合は、エラー名でアサートします:

```rust
// Assert an Anchor-specific error
result.assert_anchor_error("InsufficientFunds");
```

## トランザクションの検査

### トランザクションログを確認する

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    let ix = system_instruction::transfer(&alice.pubkey(), &bob.pubkey(), LAMPORTS_PER_SOL);
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    // Check if a specific message is in the logs
    if result.has_log("Transfer") {
        println!("Found transfer log!");
    }

    // Find a log matching a pattern
    if let Some(log) = result.find_log("Program") {
        println!("Found log: {}", log);
    }

    // Get all logs
    let logs = result.logs();
    for log in logs {
        println!("{}", log);
    }
}
```

### デバッグ用にログを出力する

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    let ix = system_instruction::transfer(&alice.pubkey(), &bob.pubkey(), LAMPORTS_PER_SOL);
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    // Print formatted logs for debugging
    result.print_logs();
}
```

### コンピュートユニットを確認する

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

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

    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    let ix = system_instruction::transfer(&alice.pubkey(), &bob.pubkey(), LAMPORTS_PER_SOL);
    let result = svm.send_instruction(ix, &[&alice]).unwrap();

    let compute_units = result.compute_units();
    println!("Compute units consumed: {}", compute_units);

    // Assert compute usage is within expected bounds
    assert!(compute_units < 10_000, "Transaction used too many compute units");
}
```

<Info>
  コンピュートユニットの監視は最適化に役立ちます。Solana ではトランザクションあたりのコンピュートユニットの上限が 140万に制限されており、使用量が多いほどコストも高くなります。
</Info>

## 完全な例

トランザクションヘルパーを網羅的に示す例です:

```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_transaction_workflow() {
    let mut svm = LiteSVM::new();

    // Setup accounts
    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL).unwrap();
    let bob = svm.create_funded_account(0).unwrap();

    // Test successful transfer
    let transfer_ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        2 * LAMPORTS_PER_SOL,
    );

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

    // Verify success
    result.assert_success();

    // Check compute usage
    let cu = result.compute_units();
    println!("Transfer used {} compute units", cu);

    // Verify balances
    svm.assert_sol_balance(&bob.pubkey(), 2 * LAMPORTS_PER_SOL);

    // Test expected failure
    let bad_transfer = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        100 * LAMPORTS_PER_SOL, // More than Alice has
    );

    let fail_result = svm.send_instruction(bad_transfer, &[&alice]).unwrap();
    fail_result.assert_failure();

    // Print logs for debugging
    fail_result.print_logs();

    println!("Transaction workflow test passed!");
}
```

## エラーの種類

`TransactionError` 列挙型は、構造化されたエラー情報を提供します:

| バリアント                   | 説明                               |
| ------------------------- | ----------------------------------------- |
| `ExecutionFailed(String)` | メッセージ付きでトランザクションの実行に失敗しました |
| `BuildError(String)`      | トランザクションの構築に失敗しました           |
| `AssertionFailed(String)` | 結果に対するアサーションが失敗しました         |
