# [tokens] TokenTracker.sol

> Encrypted ERC (eERC) システムにおいて、対応するERC20トークンの登録・識別・ブラックリスト管理を行うコントラクトです。

- URL: https://cardene.dev/books/encrypted-erc/2b6eb3
- 本: Encrypted ERC (https://cardene.dev/books/encrypted-erc.md)
- 章: 22 / 32
- 前の章: https://cardene.dev/books/encrypted-erc/e1e0aa.md
- 次の章: https://cardene.dev/books/encrypted-erc/318347.md
- 公開日: 2025-07-07
- 更新日: 2025-07-07
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, smartcontract, avalanch

## 概要

Encrypted ERC (eERC) システムにおいて、対応する**ERC20**トークンの登録・識別・ブラックリスト管理を行うコントラクトです。

* **Converter Mode**
  既存の**ERC20**を暗号化トークンへ変換する時のハブとして機能し、外部トークンとeERCの紐付けをトラッキングします。
* **Standalone Mode**
  新たに暗号化されたトークンを発行するときに機能し、`tokenId` に0をに割り当ててます。
  これは、既存の**ERC20**と異なり、コントラクトアドレスが存在しないためです。

どちらのモードでもトークンの一意なIDの付与と不正トークンのブラックリスト化が行えるため、運用者 (Owner) は `setTokenBlacklist` により随時リスクのあるトークンを遮断できます。

## コード

https://github.com/ava-labs/EncryptedERC/blob/main/contracts/tokens/TokenTracker.sol

```solidity
// (c) 2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.

// SPDX-License-Identifier: Ecosystem

pragma solidity 0.8.27;

import {Ownable2Step, Ownable} from "@openzeppelin/contracts/access/Ownable2Step.sol";
import {TokenBlacklisted, InvalidOperation} from "../errors/Errors.sol";

/**
 * @title TokenTracker
 * @notice Contract for tracking ERC20 tokens in the encrypted ERC system
 * @dev This contract manages:
 *      1. Token registration and identification
 *      2. Token blacklisting for security
 *      3. Contract Mode (converter vs standalone)
 *
 * The contract can operate in two modes:
 * - Converter Mode: Wraps existing ERC20 tokens into encrypted tokens
 * - Standalone Mode: Operates as a standalone encrypted token
 */
contract TokenTracker is Ownable2Step {
    ///////////////////////////////////////////////////
    ///                   State Variables           ///
    ///////////////////////////////////////////////////

    /// @notice The next available token ID
    /// @dev Token IDs start from 1, with 0 reserved for the standalone version
    uint256 public nextTokenId = 1;

    /// @notice Indicates if the contract is operating in converter mode
    bool public isConverter;

    /// @notice Mapping from token address to token ID
    mapping(address tokenAddress => uint256 tokenId) public tokenIds;

    /// @notice Mapping from token ID to token address
    mapping(uint256 tokenId => address tokenAddress) public tokenAddresses;

    /// @notice Array of all registered token addresses
    address[] public tokens;

    /// @notice Mapping to track blacklisted tokens
    mapping(address tokenAddress => bool isBlacklisted)
        public blacklistedTokens;

    ///////////////////////////////////////////////////
    ///                   Modifiers                 ///
    ///////////////////////////////////////////////////

    /**
     * @notice Ensures the function is only called in converter mode
     * @dev Reverts with InvalidOperation if called in standalone mode
     */
    modifier onlyForConverter() {
        if (!isConverter) {
            revert InvalidOperation();
        }
        _;
    }

    /**
     * @notice Ensures the function is only called in standalone mode
     * @dev Reverts with InvalidOperation if called in converter mode
     */
    modifier onlyForStandalone() {
        if (isConverter) {
            revert InvalidOperation();
        }
        _;
    }

    /**
     * @notice Ensures the token is not blacklisted
     * @param tokenAddress Address of the token to check
     * @dev Reverts with TokenBlacklisted if the token is blacklisted
     */
    modifier revertIfBlacklisted(address tokenAddress) {
        if (blacklistedTokens[tokenAddress]) {
            revert TokenBlacklisted(tokenAddress);
        }
        _;
    }

    ///////////////////////////////////////////////////
    ///                   Constructor               ///
    ///////////////////////////////////////////////////

    /**
     * @notice Initializes the TokenTracker contract
     * @param isConverter_ Determines if the contract operates in converter mode
     * @dev Sets the initial mode of operation and initializes the owner
     */
    constructor(bool isConverter_) Ownable(msg.sender) {
        isConverter = isConverter_;
    }

    ///////////////////////////////////////////////////
    ///                   External                  ///
    ///////////////////////////////////////////////////

    /**
     * @notice Sets the blacklist status of a token
     * @param token Address of the token to blacklist/unblacklist
     * @param blacklisted True to blacklist, false to unblacklist
     * @dev Only the owner can call this function
     */
    function setTokenBlacklist(
        address token,
        bool blacklisted
    ) external onlyOwner {
        blacklistedTokens[token] = blacklisted;
    }

    /**
     * @notice Returns an array of all registered token addresses
     * @return Array of token addresses
     * @dev Used for enumeration and listing all supported tokens
     */
    function getTokens() external view returns (address[] memory) {
        return tokens;
    }

    ///////////////////////////////////////////////////
    ///                   Internal                  ///
    ///////////////////////////////////////////////////

    /**
     * @notice Adds a new token to the tracker
     * @param tokenAddress Address of the token to add
     * @dev This function:
     *      1. Assigns a new token ID
     *      2. Updates the token mappings
     *      3. Adds the token to the tokens array
     *      4. Increments the next token ID
     */
    function _addToken(address tokenAddress) internal {
        uint256 newTokenId = nextTokenId;
        tokenIds[tokenAddress] = newTokenId;
        tokenAddresses[newTokenId] = tokenAddress;
        tokens.push(tokenAddress);
        nextTokenId++;
    }
}
```

## State

```solidity
uint256 public nextTokenId = 1;
bool    public isConverter;
mapping(address => uint256) public tokenIds;
mapping(uint256 => address) public tokenAddresses;
address[] public tokens;
mapping(address => bool) public blacklistedTokens;
```

* `nextTokenId`
    * 次に発行するトークンID。
    * Converterモードでは運用では1 から自動増分。
    * Standaloneモードでは0がコントラクト自身に割り当てられます。

* `isConverter`
    * `true` ならConverter Mode、`false` ならStandalone Mode。

* `tokenIds`
    * 「トークンアドレス ⇒ 一意のID」を管理するマッピング配列。
    * 既に登録済みトークンかを即座に判定できます。

* `tokenAddresses`
    * 「ID ⇒ トークンアドレス」を管理するマッピング配列。
    * IDを指定して、元の**ERC20**コントラクトを取得する時に使用します。

* `tokens`
    * 登録済みトークンアドレスを管理する配列。

* `blacklistedTokens`
    * トークンコントラクトのアドレスごとにブラックリストに登録されているかを管理するマッピング配列。。
    * `true`の場合 で取引ができないトークンとみなされます。

## Modifiers

### `onlyForConverter`

```solidity
modifier onlyForConverter() {
    if (!isConverter) { revert InvalidOperation(); }
    _;
}
```

Converter運用時のみ許可。
Standaloneの場合 `InvalidOperation` エラーを返します。

### `onlyForStandalone`

```solidity
modifier onlyForStandalone() {
    if (isConverter) { revert InvalidOperation(); }
    _;
}
```

Standalone運用時のみ許可。
Converterの場合 `InvalidOperation` エラーを返します。

### `revertIfBlacklisted`

```solidity
modifier revertIfBlacklisted(address tokenAddress) {
    if (blacklistedTokens[tokenAddress]) {
        revert TokenBlacklisted(tokenAddress);
    }
    _;
}
```

特定のトークンがブラックリストに登録されている場合、`TokenBlacklisted` エラーを返します。

## External Functions

### `constructor`

```solidity
constructor(bool isConverter_) Ownable(msg.sender) {
    isConverter = isConverter_;
}
```

デプロイ時に運用モードを決定し、コントラクトのデプロイを実行しているアドレスを `owner` に設定します。

**引数**

* `isConverter_` (`bool`)
    * `true` で Converter Mode、`false` で Standalone Mode

### `setTokenBlacklist`

```solidity
function setTokenBlacklist(
    address token,
    bool blacklisted
) external onlyOwner {
    blacklistedTokens[token] = blacklisted;
}
```

`owner` が任意トークンをブラックリストへ追加／解除する関数。
なんらかの不正などを見つかったトークンを即座に取引できなくするための管理機能です。
`owner` アドレスからのみ実行可能です。

**引数**

* `token` (`address`)
    * 対象トークンのコントラクトアドレス
* `blacklisted` (`bool`)
    * `true` で追加、`false` で解除

### `getTokens`

```solidity
function getTokens() external view returns (address[] memory) {
    return tokens;
}
```

登録済みトークンの全てのアドレス一覧を取得する関数。

## Internal Functions

### `_addToken`

```solidity
function _addToken(address tokenAddress) internal {
    uint256 newTokenId = nextTokenId;
    tokenIds[tokenAddress]   = newTokenId;
    tokenAddresses[newTokenId] = tokenAddress;
    tokens.push(tokenAddress);
    nextTokenId++;
}
```

Converter Modeで新たに**ERC20**トークンを暗号化されたトークンへ変更して登録する関数。

1. `nextTokenId` を読み取り `newTokenId` に確定
2. アドレス ↔ ID 相互マッピングを更新
3. 配列 `tokens` にアドレスを追記
4. `nextTokenId` をインクリメント

**引数**

* `tokenAddress` (`address`)
    * 登録する**ERC20**コントラクトアドレス
