# ICM

> この章では、ICM（Interchain Messaging：ブロックチェーン間メッセージング）のMessenger/Registryコントラクトを操作していきます。

- URL: https://cardene.dev/books/avalanche-cli/2cb87e
- 本: Avalanche CLI (https://cardene.dev/books/avalanche-cli.md)
- 章: 13 / 20
- 前の章: https://cardene.dev/books/avalanche-cli/baba25.md
- 次の章: https://cardene.dev/books/avalanche-cli/e0e137.md
- 公開日: 2025-09-08
- 更新日: 2025-09-08
- トピック: Blockchain
- タグ: ブロックチェーン, solidity, ethereum, zennfes2025free

## 概要

この章では、ICM（Interchain Messaging：ブロックチェーン間メッセージング）の**Messenger**/**Registry**コントラクトを操作していきます。
**Messenger**コントラクトは他のL1 Blockchainとメッセージをやり取りし、**Registry**コントラクトは他のL1 Blockchainや**Messenger**コントラクトの管理を行います。

```bash
avalanche icm [subcommand] [flags]
```

## ICM の deploy

指定したL1 Blockchainに**ICM Messenger**コントラクトと**ICM Registry**コントラクトをデプロイします。
ローカルネットワークの場合は、あわせて**C-Chain**にもデプロイされます。
以下のコマンドを実行してください。

```bash
avalanche icm deploy
```

デプロイを行うネットワークの選択を求められるので、「**Local Network**」を選択してください。

```bash
? On what Network do you want to deploy the ICM Messenger?: 
  ▸ Local Network
    Devnet
    Fuji Testnet
    Mainnet
```

デプロイを行うL1 Blockchainの選択を求められるので、「**Blockchain chain1**」を選択してください。

```bash
? Which Blockchain would you like to deploy ICM to?: 
    C-Chain
  ▸ Blockchain chain1
    Custom
```

デプロイを行う時に使用するアカウントの指定を求められるので、「**Use the private key of the Genesis Allocated address...**」か「**Get private key from an existing stored key...**」のどちらかを選択してください。

```bash
? Which private key do you want to use to deploy ICM?: 
  ▸ Use the private key of the Genesis Allocated address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
    Get private key from an existing stored key (created from avalanche key create or avalanche key import)
    Custom
```

L1 Blockchain作成時に既にデプロイされているため、以下のように表示されます。
```bash
ICM Messenger has already been deployed to chain1
ICM Messenger has already been deployed to c-chain
```

以下のようにいくつかオプションがあるので1つずつ簡単に説明します。

```bash
--blockchain string                         deploy ICM into the given CLI blockchain
--blockchain-id string                      deploy ICM into the given blockchain ID/Alias
--c-chain                                   deploy ICM into C-Chain
--cchain-key string                         key to be used to pay fees to deploy ICM to C-Chain
--cluster string                            operate on the given cluster
--deploy-messenger                          deploy ICM Messenger (default true)
--deploy-registry                           deploy ICM Registry (default true)
--devnet                                    operate on a devnet network
--endpoint string                           use the given endpoint for network operations
--force-registry-deploy                     deploy ICM Registry even if Messenger has already been deployed
-f, --fuji                                  testnet                             operate on fuji (alias to testnet
--genesis-key                               use genesis allocated key to fund ICM deploy
--include-cchain                            deploy ICM also to C-Chain
--key string                                CLI stored key to use to fund ICM deploy
-l, --local                                 operate on a local network
-m, --mainnet                               operate on mainnet
--messenger-contract-address-path string    path to a messenger contract address file
--messenger-deployer-address-path string    path to a messenger deployer address file
--messenger-deployer-tx-path string         path to a messenger deployer tx file
--private-key string                        private key to use to fund ICM deploy
--registry-bytecode-path string             path to a registry bytecode file
--rpc-url string                            use the given RPC URL to connect to the subnet
-t, --testnet                               fuji                             operate on testnet (alias to fuji)
--version string                            version to deploy (default "latest")
```

* **`--blockchain string`**
    Avalanche CLIで管理しているL1 Blockchainの名前を指定して、そのL1 BlockchainにICMコントラクトをデプロイします（例: `chain1`）。
* **`--blockchain-id string`**
    ブロックチェーンID/エイリアス（CB58 など）を指定してICMコントラクトをデプロイします。
* **`--c-chain`**
    C-ChainにのみICMコントラクトをデプロイします。
    ローカルでL1 Blockchainと並行デプロイしたい場合は `--include-cchain` を使います。
* **`--cchain-key string`**
    C-Chain側デプロイの手数料支払いに使うキー名（Avalanche CLI保管）を指定します。
* **`--cluster string`**
    操作対象のクラスタ名を指定します（Avalanche CLIで命名したノード集合）。
* **`--deploy-messenger`**（デフォルト: true）
    **ICM Messenger**コントラクトをデプロイします。
    不要なら `--deploy-messenger=false` と指定します。
* **`--deploy-registry`**（デフォルト: true）
    **ICM Registry**コントラクトをデプロイします。
    不要なら `--deploy-registry=false` と指定します。
* **`--devnet`**
    Devnet上で処理を実行します。
* **`--endpoint string`**
    ネットワーク操作に使うエンドポイント（Avalanche CLI制御用の接続先URL）を指定します。
* **`--force-registry-deploy`**
    すでに**Messenger**コントラクトがデプロイ済みでも、**Registry**コントラクトを強制デプロイします。
    再デプロイや再初期化が必要なときに使用します。
* **`-f, --fuji`**
    Fuji上で処理を実行します。
    `--testnet` のエイリアスです。
* **`--genesis-key`**
    L1 Blockchain作成時に生成された鍵を使用してICMコントラクトのデプロイを行います。
* **`--include-cchain`**
    指定したL1 Blockchainに加えてC-ChainにもICMコントラクトをデプロイします。
* **`--key string`**
    Avalanche CLIに保存済みのキー名を、ICMコントラクトのデプロイに使用します。
* **`-l, --local`**
    ローカルネットワーク上で処理を実行します。
* **`-m, --mainnet`**
    Mainnet上で処理を実行します。
* **`--messenger-contract-address-path string`**
    **Messenger**コントラクトアドレスを書き出す／読み込むファイルパスを指定します。
* **`--messenger-deployer-address-path string`**
    **Messenger**コントラクトのデプロイヤーアドレス（デプロイ元EOAなど）を記録するファイルパスを指定します。
* **`--messenger-deployer-tx-path string`**
    **Messenger**コントラクトデプロイ時のトランザクション情報を記録するファイルパスを指定します。
* **`--private-key string`**
    秘密鍵を指定し、ICMコントラクトのデプロイを行います。
* **`--registry-bytecode-path string`**
    **Registry**コントラクトのバイトコードを外部ファイルから読み込む場合のパスを指定します。
* **`--rpc-url string`**
    対象サブネット（L1 Blockchain）に接続するためのRPC URLを直接指定します（例: `http://127.0.0.1:8545`）。
* **`-t, --testnet`**
    **Testnet**上で処理を実行します。
    `--fuji` のエイリアスです。
* **`--version string`**（デフォルト: `"latest"`）
    デプロイするICMコントラクトのバージョンを指定します。

## チェーン間でメッセージの送付

2つのブロックチェーン間でICMメッセージを送信して受信完了まで待機します。
以下のコマンドを実行してください。

```bash
avalanche icm sendMsg <送付元chain name> <送信先chain name> "<Message>" --local
```

- 例

```bash
avalanche icm sendMsg chain1 chain2 "Hello" --local
```

トランザクションのガス代を負担するアドレスの選択を求められるので、「**Use the private key of the Genesis Allocated address...**」か「**Get private key from an existing stored key...**」を選択してください。

```bash
? Which private key do you want to use to pay for fees at source blockchain?: 
    Use the private key of the Genesis Allocated address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
  ▸ Get private key from an existing stored key (created from avalanche key create or avalanche key import)
    Custom
```

「**Get private key from an existing stored key...**」を選択した場合は、「**ewoq**」を選択してください。

```bash
? Which stored key should be used pay for fees at source blockchain?: 
    sample-key
  ▸ ewoq
    cli-awm-relayer
    cli-teleporter-deployer
```

以下のように表示されていれば実行成功です。

```bash
Delivering message "Hello" from source blockchain "chain1" (w7ThnGXmC4ZtUDZE7Wv5bDFzsgnZa4c48Y2EBGJNdUs88Bheo)
Waiting for message to be delivered to destination blockchain "chain2" (SMXGPr8rzQJiFDbiuKbARMNmYBpw6bLDwYPk55s4Z9PpqXWQG)
Message successfully delivered!
```

以下のようにいくつかオプションがあるので1つずつ簡単に説明します。

```bash
--cluster string                operate on the given cluster
--dest-rpc string               use the given destination blockchain rpc endpoint
--destination-address string    deliver the message to the given contract destination address
--devnet                        operate on a devnet network
--endpoint string               use the given endpoint for network operations
-f, --fuji                      testnet                 operate on fuji (alias to testnet
--genesis-key                   use genesis allocated key as message originator and to pay source blockchain fees
--hex-encoded                   given message is hex encoded
--key string                    CLI stored key to use as message originator and to pay source blockchain fees
-l, --local                     operate on a local network
-m, --mainnet                   operate on mainnet
--private-key string            private key to use as message originator and to pay source blockchain fees
--source-rpc string             use the given source blockchain rpc endpoint
-t, --testnet                   fuji                 operate on testnet (alias to fuji)
```

* **`--cluster string`**
    対象のクラスタ名（Avalanche CLIで命名したノード群）を指定します。
* **`--dest-rpc string`**
    宛先ブロックチェーンのRPCエンドポイント（ノードのHTTP/HTTPS URL）を指定します。
* **`--destination-address string`**
    宛先コントラクトアドレスを指定します。メッセージはこのアドレスに送付されます。
* **`--devnet`**
    **Devnet**を対象に処理を実行します。
* **`--endpoint string`**
    Avalanche CLIのネットワーク操作に使う制御用エンドポイントURLを指定します。
* **`-f, --fuji`**
    **Fuji**を対象に処理を実行します（`--testnet` のエイリアス）。
* **`--genesis-key`**
    メッセージ送信者（originator）と送信元L1 Blockchainの手数料支払いに、ローカルに保存されているL1 Blockchain作成時に生成されている鍵を使用します。
* **`--hex-encoded`**
    渡すメッセージが16進（hex）エンコードされた文字列であることを示します。
* **`--key string`**
    Avalanche CLIに保存済みの鍵名を、メッセージ送信者（originator）と送信元L1 Blockchainの手数料支払いに使用します。
* **`-l, --local`**
    ローカルネットワークを対象に処理を実行します。
* **`-m, --mainnet`**
    Mainnetを対象に処理を実行します。
* **`--private-key string`**
    秘密鍵を直接指定し、メッセージ送信者（originator）と送信元L1 Blockchainの手数料支払いに使用します。
* **`--source-rpc string`**
    送信元L1 BlockchainのRPCエンドポイント（ノードURL）を指定します。
* **`-t, --testnet`**
    **Testnet**を対象に処理を実行します（`--fuji` のエイリアス）。
