概要
ExampleTxAllowListコントラクトは、Avalanche Subnet-EVMのTxAllowListプリコンパイルの利用方法を示すサンプルコントラクトです。
このコントラクトは、Subnet上でトランザクションを発行できるウォレットアドレスをホワイトリスト形式で管理するためのインターフェースを提供します。
このコントラクトは、Subnetにネイティブに組み込まれているTxAllowListプリコンパイル(アドレス:0x0200...0002)を、Solidityから簡単に操作するためのラッパーとして機能します。
AllowListコントラクトを継承しているため、setAdmin, setEnabled, revokeといった権限管理機能をすべて利用できます。
TxAllowListが有効化されたSubnetでは、ここでEnabled以上の権限を付与されたアドレスしかトランザクションをネットワークに送信できません。
権限のないアドレスからのトランザクションは、EVMで実行される前の段階でネットワークに拒否されます。
このコントラクトのオーナーは、継承した関数を呼び出すことで、Subnet全体でトランザクションの発行が許可されるアドレスのリストを安全に管理できます。
機能
TxAllowListプリコンパイルを操作するためのSolidityインターフェースを提供します。- コントラクトのオーナーが、継承した関数を用いてトランザクション発行者の権限を管理します。
- トランザクション許可リストの動作を確認するためのテスト用関数(
deployContract)を含みます。
フロー
- トランザクション権限の付与とトランザクション発行フロー
トランザクション権限の付与とトランザクション発行フロー
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コード
//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
address constant TX_ALLOW_LIST = 0x0200000000000000000000000000000000000002;TxAllowListプリコンパイルが配置されている固定のコントラクトアドレスを定義した定数。
関数
constructor
constructor() AllowList(TX_ALLOW_LIST) {}コントラクトを初期化する関数。
コントラクトがデプロイされる時に一度だけ実行されます。
親コントラクトであるAllowListのコンストラクタを呼び出し、引数としてTxAllowListプリコンパイルのアドレス(TX_ALLOW_LIST)を渡します。
これにより、このコントラクトから呼び出される権限管理機能が、すべてTxAllowListプリコンパイルに対して実行されるように紐付けられます。
deployContract
function deployContract() public {
new Example();
}新しいExampleコントラクトをデプロイする関数。
この関数は、new Example()という命令を実行して、空のExampleコントラクトをデプロイします。
この関数自体に特別なアクセス制限はありませんが、TxAllowListが有効なSubnetでは、この関数を呼び出すトランザクション自体が、AllowListに登録されていないアドレスからは送信できません。