# [Contract] ExampleTxAllowList

> ExampleTxAllowListコントラクトは、Avalanche Subnet-EVMのTxAllowListプリコンパイルの利用方法を示すサンプルコントラクトです。

- URL: https://cardene.dev/books/avalanche-subnet-precompile/dce04b
- 本: [Avalanche] Subnet Precompile (https://cardene.dev/books/avalanche-subnet-precompile.md)
- 章: 13 / 21
- 前の章: https://cardene.dev/books/avalanche-subnet-precompile/da9d31.md
- 次の章: https://cardene.dev/books/avalanche-subnet-precompile/f70ee4.md
- 公開日: 2025-08-20
- 更新日: 2025-08-20
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, ethereum, smartcontract

## 概要

**ExampleTxAllowList**コントラクトは、Avalanche Subnet-EVMの`TxAllowList`プリコンパイルの利用方法を示すサンプルコントラクトです。
このコントラクトは、Subnet上でトランザクションを発行できるウォレットアドレスをホワイトリスト形式で管理するためのインターフェースを提供します。

このコントラクトは、Subnetにネイティブに組み込まれている`TxAllowList`プリコンパイル（アドレス：`0x0200...0002`）を、Solidityから簡単に操作するためのラッパーとして機能します。
**AllowList**コントラクトを継承しているため、`setAdmin`, `setEnabled`, `revoke`といった権限管理機能をすべて利用できます。

`TxAllowList`が有効化されたSubnetでは、ここで`Enabled`以上の権限を付与されたアドレスしかトランザクションをネットワークに送信できません。
権限のないアドレスからのトランザクションは、EVMで実行される前の段階でネットワークに拒否されます。

このコントラクトのオーナーは、継承した関数を呼び出すことで、Subnet全体でトランザクションの発行が許可されるアドレスのリストを安全に管理できます。

## 機能

- `TxAllowList`プリコンパイルを操作するためのSolidityインターフェースを提供します。
- コントラクトのオーナーが、継承した関数を用いてトランザクション発行者の権限を管理します。
- トランザクション許可リストの動作を確認するためのテスト用関数（`deployContract`）を含みます。

## フロー

- トランザクション権限の付与とトランザクション発行フロー

### トランザクション権限の付与とトランザクション発行フロー

```mermaid
sequenceDiagram
    participant Owner as オーナー
    participant ExampleTxAllowList as ExampleTxAllowListコントラクト
    participant User as 許可されたユーザー
    participant SubnetNode as Subnetノード
    participant TxAllowListPrecompile as TxAllowListプリコンパイル

    Owner->>ExampleTxAllowList: setEnabled(User) を呼び出す
    activate ExampleTxAllowList
    Note right of ExampleTxAllowList: AllowListのロジックが実行される
    ExampleTxAllowList->>TxAllowListPrecompile: プリコンパイルのsetEnabledを呼び出す
    TxAllowListPrecompile-->>ExampleTxAllowList: 成功
    ExampleTxAllowList-->>Owner: 処理完了
    deactivate ExampleTxAllowList

    User->>SubnetNode: deployContract()のトランザクションを送信
    activate SubnetNode
    SubnetNode->>TxAllowListPrecompile: Userがトランザクション発行権限を持つか確認
    activate TxAllowListPrecompile
    TxAllowListPrecompile-->>SubnetNode: 権限あり
    deactivate TxAllowListPrecompile
    SubnetNode-->>User: トランザクションを受け付け、実行
    deactivate SubnetNode
```

## コード

https://github.com/ava-labs/subnet-evm/blob/master/contracts/contracts/ExampleTxAllowList.sol

```sol
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./AllowList.sol";
import "./interfaces/IAllowList.sol";

// Precompiled Allow List Contract Address
address constant TX_ALLOW_LIST = 0x0200000000000000000000000000000000000002;

// ExampleTxAllowList shows how TxAllowList precompile can be used in a smart contract
// All methods of [allowList] can be directly called. There are example calls as tasks in hardhat.config.ts file.
contract ExampleTxAllowList is AllowList {
  constructor() AllowList(TX_ALLOW_LIST) {}

  function deployContract() public {
    new Example();
  }
}

contract Example {}
```

## 変数

### `TX_ALLOW_LIST`

```sol
address constant TX_ALLOW_LIST = 0x0200000000000000000000000000000000000002;
```

`TxAllowList`プリコンパイルが配置されている固定のコントラクトアドレスを定義した定数。

## 関数

### `constructor`

```sol
constructor() AllowList(TX_ALLOW_LIST) {}
```

コントラクトを初期化する関数。
コントラクトがデプロイされる時に一度だけ実行されます。
親コントラクトである`AllowList`のコンストラクタを呼び出し、引数として`TxAllowList`プリコンパイルのアドレス（`TX_ALLOW_LIST`）を渡します。
これにより、このコントラクトから呼び出される権限管理機能が、すべて`TxAllowList`プリコンパイルに対して実行されるように紐付けられます。

### `deployContract`

```sol
function deployContract() public {
    new Example();
}
```

新しい`Example`コントラクトをデプロイする関数。
この関数は、`new Example()`という命令を実行して、空の`Example`コントラクトをデプロイします。
この関数自体に特別なアクセス制限はありませんが、`TxAllowList`が有効なSubnetでは、この関数を呼び出すトランザクション自体が、AllowListに登録されていないアドレスからは送信できません。
