# [Contract] ClaimTopicsRegistry

> このコントラクトは、特定のトークンを保有・取引するために投資家が持つべき必須クレームの種類（トピック）をリストとして管理するための台帳（レジストリ）です。

- URL: https://cardene.dev/books/erc3643/549e0e
- 本: ERC3643 (https://cardene.dev/books/erc3643.md)
- 章: 17 / 20
- 前の章: https://cardene.dev/books/erc3643/304dbe.md
- 次の章: https://cardene.dev/books/erc3643/ba0689.md
- 公開日: 2025-07-25
- 更新日: 2025-07-25
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, ethereum, smartcontract

## 概要

このコントラクトは、特定のトークンを保有・取引するために投資家が持つべき必須クレームの種類（トピック）をリストとして管理するための台帳（レジストリ）です。
トークン発行者は、このコントラクトを通じて、自身のトークンに適用されるコンプライアンス要件を定義します。

**ClaimTopicsRegistry**コントラクトは、トークンのコンプライアンス検証における「**要件定義書**」の役割を担う、シンプルながらも重要なスマートコントラクトです。
トークン発行者（オーナー）は、`addClaimTopic`関数を使って、例えば「**KYC済み（トピック番号: 1）**」や「**適格投資家（トピック番号: 2）**」といった、必須となる資格証明の種類をリストに追加します。

トークン送付時には、**IdentityRegistry**コントラクトがこのコントラクトの`getClaimTopics`関数を呼び出して必須トピックのリストを取得し、投資家のオンチェーンIDがこれらのトピックのクレームを全て満たしているかを検証します。

このコントラクトは、ロジック部分とストレージ部分（**CTRStorage**）が分離されており、アップグレード可能なプロキシとして使用されることを前提としています。
オーナーは、`removeClaimTopic`を通じて、いつでも要件リストを更新できます。

## 機能

* 必須クレームトピックの追加・削除（オーナー限定）
* 登録されている全必須クレームトピックのリスト取得

## フロー

### 必須クレームトピックの追加

```mermaid
sequenceDiagram
    actor Owner as オーナー (トークン発行者)
    participant CTR as ClaimTopicsRegistry

    Owner->>CTR: addClaimTopic(topicNumber)
    activate CTR
    Note over CTR: onlyOwner修飾子で権限をチェック
    Note over CTR: トピック数が上限(15)未満か、<br>トピックが未登録かなどを検証
    Note over CTR: _claimTopics 配列にトピックを追加
    CTR-->>Owner: 成功 (ClaimTopicAddedイベント発行)
    deactivate CTR
```

## コード

```sol:ClaimTopicsRegistry.sol
// SPDX-License-Identifier: GPL-3.0
//
//                                             :+#####%%%%%%%%%%%%%%+
//                                         .-*@@@%+.:+%@@@@@%%#***%@@%=
//                                     :=*%@@@#=.      :#@@%       *@@@%=
//                       .-+*%@%*-.:+%@@@@@@+.     -*+:  .=#.       :%@@@%-
//                   :=*@@@@%%@@@@@@@@@%@@@-   .=#@@@%@%=             =@@@@#.
//             -=+#%@@%#*=:.  :%@@@@%.   -*@@#*@@@@@@@#=:-              *@@@@+
//            =@@%=:.     :=:   *@@@@@%#-   =%*%@@@@#+-.        =+       :%@@@%-
//           -@@%.     .+@@@     =+=-.         @@#-           +@@@%-       =@@@@%:
//          :@@@.    .+@@#%:                   :    .=*=-::.-%@@@+*@@=       +@@@@#.
//          %@@:    +@%%*                         =%@@@@@@@@@@@#.  .*@%-       +@@@@*.
//         #@@=                                .+@@@@%:=*@@@@@-      :%@%:      .*@@@@+
//        *@@*                                +@@@#-@@%-:%@@*          +@@#.      :%@@@@-
//       -@@%           .:-=++*##%%%@@@@@@@@@@@@*. :@+.@@@%:            .#@@+       =@@@@#:
//      .@@@*-+*#%%%@@@@@@@@@@@@@@@@%%#**@@%@@@.   *@=*@@#                :#@%=      .#@@@@#-
//      -%@@@@@@@@@@@@@@@*+==-:-@@@=    *@# .#@*-=*@@@@%=                 -%@@@*       =@@@@@%-
//         -+%@@@#.   %@%%=   -@@:+@: -@@*    *@@*-::                   -%@@%=.         .*@@@@@#
//            *@@@*  +@* *@@##@@-  #@*@@+    -@@=          .         :+@@@#:           .-+@@@%+-
//             +@@@%*@@:..=@@@@*   .@@@*   .#@#.       .=+-       .=%@@@*.         :+#@@@@*=:
//              =@@@@%@@@@@@@@@@@@@@@@@@@@@@%-      :+#*.       :*@@@%=.       .=#@@@@%+:
//               .%@@=                 .....    .=#@@+.       .#@@@*:       -*%@@@@%+.
//                 +@@#+===---:::...         .=%@@*-         +@@@+.      -*@@@@@%+.
//                  -@@@@@@@@@@@@@@@@@@@@@@%@@@@=          -@@@+      -#@@@@@#=.
//                    ..:::---===+++***###%%%@@@#-       .#@@+     -*@@@@@#=.
//                                           @@@@@@+.   +@@*.   .+@@@@@%=.
//                                          -@@@@@=   =@@%:   -#@@@@%+.
//                                          +@@@@@. =@@@=  .+@@@@@*:
//                                          #@@@@#:%@@#. :*@@@@#-
//                                          @@@@@%@@@= :#@@@@+.
//                                         :@@@@@@@#.:#@@@%-
//                                         +@@@@@@-.*@@@*:
//                                         #@@@@#.=@@@+.
//                                         @@@@+-%@%=
//                                        :@@@#%@%=
//                                        +@@@@%-
//                                        :#%%=
//
/**
 *     NOTICE
 *
 *     The T-REX software is licensed under a proprietary license or the GPL v.3.
 *     If you choose to receive it under the GPL v.3 license, the following applies:
 *     T-REX is a suite of smart contracts implementing the ERC-3643 standard and
 *     developed by Tokeny to manage and transfer financial assets on EVM blockchains
 *
 *     Copyright (C) 2023, Tokeny sàrl.
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

pragma solidity 0.8.17;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "../storage/CTRStorage.sol";
import "../interface/IClaimTopicsRegistry.sol";

contract ClaimTopicsRegistry is IClaimTopicsRegistry, OwnableUpgradeable, CTRStorage {

    function init() external initializer {
        __Ownable_init();
    }

    /**
     *  @dev See {IClaimTopicsRegistry-addClaimTopic}.
     */
    function addClaimTopic(uint256 _claimTopic) external override onlyOwner {
        uint256 length = _claimTopics.length;
        require(length < 15, "cannot require more than 15 topics");
        for (uint256 i = 0; i < length; i++) {
            require(_claimTopics[i] != _claimTopic, "claimTopic already exists");
        }
        _claimTopics.push(_claimTopic);
        emit ClaimTopicAdded(_claimTopic);
    }

    /**
     *  @dev See {IClaimTopicsRegistry-removeClaimTopic}.
     */
    function removeClaimTopic(uint256 _claimTopic) external override onlyOwner {
        uint256 length = _claimTopics.length;
        for (uint256 i = 0; i < length; i++) {
            if (_claimTopics[i] == _claimTopic) {
                _claimTopics[i] = _claimTopics[length - 1];
                _claimTopics.pop();
                emit ClaimTopicRemoved(_claimTopic);
                break;
            }
        }
    }

    /**
     *  @dev See {IClaimTopicsRegistry-getClaimTopics}.
     */
    function getClaimTopics() external view override returns (uint256[] memory) {
        return _claimTopics;
    }
}
```

## 変数

### `_claimTopics`

```solidity
uint256[] internal _claimTopics;
```

このトークンの取引に必須とされるクレームトピックの番号（`uint256`）を格納する動的配列。

## イベント

### `ClaimTopicAdded`

```solidity
event ClaimTopicAdded(uint256 indexed claimTopic);
```

`addClaimTopic`関数によって、新しい必須クレームトピックがレジストリに追加された時に発行されるイベント。

### `ClaimTopicRemoved`

```solidity
event ClaimTopicRemoved(uint256 indexed claimTopic);
```

`removeClaimTopic`関数によって、必須クレームトピックがレジストリから削除された時に発行されるイベント。

## 関数

### `init`

```solidity
function init() external initializer {
    __Ownable_init();
}
```

プロキシコントラクトの初期設定を行う初期化関数。
`initializer`修飾子により、一度だけ呼び出し可能です。
内部で`__Ownable_init`を呼び出し、デプロイ者をコントラクトの所有者として設定します。

### `addClaimTopic`

```solidity
function addClaimTopic(uint256 _claimTopic) external override onlyOwner {
    uint256 length = _claimTopics.length;
    require(length < 15, "cannot require more than 15 topics");
    for (uint256 i = 0; i < length; i++) {
        require(_claimTopics[i] != _claimTopic, "claimTopic already exists");
    }
    _claimTopics.push(_claimTopic);
    emit ClaimTopicAdded(_claimTopic);
}
```

検証に必須となるクレームトピックをレジストリに追加する関数。
`onlyOwner`修飾子により、所有者のみが呼び出し可能です。
登録されているトピック数が15未満であること、および追加しようとしているトピックがまだ登録されていないことを確認した上で、`_claimTopics`配列に新しいトピックを追加します。

**引数**

- `_claimTopic`
    - 追加するクレームトピックの番号。

### `removeClaimTopic`

```solidity
function removeClaimTopic(uint256 _claimTopic) external override onlyOwner {
    uint256 length = _claimTopics.length;
    for (uint256 i = 0; i < length; i++) {
        if (_claimTopics[i] == _claimTopic) {
            _claimTopics[i] = _claimTopics[length - 1];
            _claimTopics.pop();
            emit ClaimTopicRemoved(_claimTopic);
            break;
        }
    }
}
```

必須クレームトピックをレジストリから削除する関数。
`onlyOwner`修飾子により、所有者のみが呼び出し可能です。
`_claimTopics`配列から指定されたトピックを検索し、見つかった場合はその要素を配列の最後の要素と入れ替えてから末尾を削除する、という効率的な方法で削除処理を行います。

**引数**

- `_claimTopic`
    - 削除するクレームトピックの番号。

### `getClaimTopics`

```solidity
function getClaimTopics() external view override returns (uint256[] memory) {
    return _claimTopics;
}
```

レジストリに登録されている全ての必須クレームトピックのリストを返す関数。
`_claimTopics`配列の現在の内容を返します。

**戻り値**

- `uint256[] memory`
    - 登録されているクレームトピックの番号の配列。
