Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

133 ictt course add multi hop chapter #140

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
---
title: Tokens on Multiple Chains
description: TBD
title: Overview of Multi-hop Transfers
description: Learn about how ICTT supports multi-hop transfers between spoke chains.
updated: 2024-05-31
authors: [ashucoder9]
authors: [owenwahlgren]
icon: Book
---

The token bridge also supports "multi-hop" transfers, where tokens can be transferred between spoke chains. To illustrate, consider two spokes _S<sub>a</sub>_ and _S<sub>b</sub>_ that are both connected to the same remote _H_. A multi-hop transfer from _S<sub>a</sub>_ to _S<sub>b</sub>_ first gets routed from _S<sub>a</sub>_ to _H_, where spoke balances are updated, and then _H_ automatically routes the transfer on to _S<sub>b</sub>_.
owenwahlgren marked this conversation as resolved.
Show resolved Hide resolved

In this example, our _S<sub>a</sub>_ chain is `myblockchain`, our _S<sub>b</sub>_ chain is `myblockchain2`, and _H_ chain is the Avalanche C-Chain.

### What we will do

1. Deploy a new local blockchain `myblockchain2`
2. Deploy a new `ERC20TokenRemote` contract to `myblockchain2`
3. Register the new `ERC20TokenRemote` contract with the `TokenHome` contract on the Avalanche C-Chain
3. Perform a multi-hop transfer of `TOK` from `myblockchain` to `myblockchain2`, with a 'hop' through the Avalanche C-Chain


Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: Deploy Additional Blockchain for Multi-hop
description: Set up a new blockchain to test ICTT multi-hop transfers.
updated: 2024-05-31
authors: [owenwahlgren]
icon: Book
---
import { Step, Steps } from 'fumadocs-ui/components/steps';

<Steps>
<Step>
### Create a new blockchain `myblockchain2` for multi-hop transfer

```bash
avalanche blockchain create myblockchain2
```
```bash
✔ Subnet-EVM
✔ I want to use defaults for a test environment
✔ Chain ID: 2
✔ Token Symbol: chain2
prefunding address 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC with balance 1000000000000000000000000
✓ Successfully created blockchain configuration
```
</Step>
<Step>
### Deploy the new blockchain `myblockchain2`
```bash
avalanche blockchain deploy myblockchain2
```
```bash
✔ Local Network
```
</Step>
<Step>
### Get the `Teleporter Registry Address` of `myblockchain2`

```bash
avalanche blockchain describe myblockchain2
```

```
+-------------------------------------------------------------------------------------------+
| TELEPORTER |
+---------------+------------------------------+--------------------------------------------+
| Local Network | Teleporter Messenger Address | 0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf |
| +------------------------------+--------------------------------------------+
| | Teleporter Registry Address | 0xa3493940a13b426BD2f7dA6E55A39c060C0e6020 | // [!code highlight]
+---------------+------------------------------+--------------------------------------------+
```
</Step>
<Step>
### Save the `Teleporter Registry Address`

Most other environment variables we will need are already set in the devcontainer or from the [previous section](/course/interchain-token-transfer/06-erc-20-to-erc-20-bridge/01-erc-20-to-erc-20-bridge).
```bash
export TELEPORTER_REGISTRY_CHAIN2=0xa3493940a13b426BD2f7dA6E55A39c060C0e6020
```
</Step>
<Step>

### Get the `Blockchain ID (hex)` of `myblockchain2`


```
+---------------------------------------------------------------------------------------------------------------+
| MYBLOCKCHAIN2 |
+---------------+-----------------------------------------------------------------------------------------------+
| Name | myblockchain2 |
+---------------+-----------------------------------------------------------------------------------------------+
| VM ID | qDNV9vtxZYYNqm7TSa9KnDTRabGxtBLv6vd888791J9a89kTF |
+---------------+-----------------------------------------------------------------------------------------------+
| VM Version | v0.6.9 |
+---------------+--------------------------+--------------------------------------------------------------------+
| Local Network | ChainID | 2 |
| +--------------------------+--------------------------------------------------------------------+
| | SubnetID | 2KhsQJhH3VqS7WWMreodAAHpGfGCUNuGQDSaP2vUT29p1HELBV |
| +--------------------------+--------------------------------------------------------------------+
| | Owners (Threhold=1) | P-custom18jma8ppw3nhx5r4ap8clazz0dps7rv5u9xde7p |
| +--------------------------+--------------------------------------------------------------------+
| | BlockchainID (CB58) | 2ZempAyezixFRdJhJribDdj2wiK6YFF63CkXKaimfPb3hWeWVH |
| +--------------------------+--------------------------------------------------------------------+
| | BlockchainID (HEX) | 0xcdd5b2b99ae462c32a8e4ea47e94f2c7804519353558fd4127cf7ae11d8a6e52 | // [!code highlight]
+---------------+--------------------------+--------------------------------------------------------------------+

```
</Step>
<Step>

### Save the Source Blockchain ID

```bash
export SOURCE_BLOCKCHAIN2_ID_HEX=0xcdd5b2b99ae462c32a8e4ea47e94f2c7804519353558fd4127cf7ae11d8a6e52
```

</Step>

<Step>
### Add `myblockchain2` RPC to `foundry.toml` config
```toml
[rpc_endpoints]
local-c = "http://localhost:9650/ext/bc/C/rpc"
myblockchain = "http://localhost:9650/ext/bc/myblockchain/rpc"
myblockchain2 = "http://localhost:9650/ext/bc/myblockchain2/rpc" // [!code highlight]
```
</Step>
</Steps>
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Deploy Token Remote for Multi-hop
description: Deploy and configure the ERC20TokenRemote contract on the second blockchain.
updated: 2024-05-31
authors: [owenwahlgren]
icon: Book
---
import { Step, Steps } from 'fumadocs-ui/components/steps';


<Steps>
<Step>
### Deploy the Remote Contract

Using the [`forge create`](https://book.getfoundry.sh/reference/forge/forge-create) command, we will deploy the `ERC20TokenRemote.sol` contract, passing in the following constructor arguments:

- Interchain Messaging Registry Address **(for `myblockchain2`)**
- Interchain Messaging Manager (our funded address)
- Source Blockchain ID (hexidecimal representation of Avalanche C-Chain)
- Token Home Address (address of `NativeTokenHome.sol` deployed on Avalanche C-Chain in the [Deploy a Home Contract](/course/interchain-token-transfer/06-erc-20-to-erc-20-bridge/03-deploy-home) section)
- Token Name (input in the constructor of the [wrapped token contract](https://github.com/ava-labs/avalanche-interchain-token-transfer/blob/main/contracts/src/WrappedNativeToken.sol))
- Token Symbol (input in the constructor of the [wrapped token contract](https://github.com/ava-labs/avalanche-interchain-token-transfer/blob/main/contracts/src/WrappedNativeToken.sol))
- Token Decimals (uint8 integer representing number of decimal places for the ERC20 token being created. Most ERC20 tokens follow the Ethereum standard, which defines 18 decimal places.)

```bash
forge create --rpc-url myblockchain2 --private-key $PK \
lib/avalanche-interchain-token-transfer/contracts/src/TokenRemote/ERC20TokenRemote.sol:ERC20TokenRemote \
--constructor-args "(${TELEPORTER_REGISTRY_CHAIN2}, ${FUNDED_ADDRESS}, ${C_CHAIN_BLOCKCHAIN_ID_HEX}, ${ERC20_HOME_C_CHAIN}, 18)" "TOK" "TOK" 18
```

</Step>
<Step>

### Save the Remote Contract Address

Note the address the remote contract was "Deployed to".

```bash
export ERC20_TOKEN_REMOTE_CHAIN2=<"Deployed to" address>
```

</Step>
<Step>

### Register Remote Contract with Home Contract

After deploying the bridge contracts, you'll need to register the remote bridge by sending a dummy message using the `registerWithHome` method. This message includes details which inform the Home Bridge about your destination blockchain and bridge settings, eg. `initialReserveImbalance`.

```bash
cast send --rpc-url myblockchain2 --private-key $PK $ERC20_TOKEN_REMOTE_CHAIN2 \
"registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)"
```

</Step>
<Step>

### Approve tokens for the Token Remote contract on `myblockchain`
Now that our `ERC20TokenRemote` contract is deployed and configured on `myblockchain2`, we need to prepare `myblockchain` for the token transfer.
We will approve the `ERC20TokenRemote` contract to spend a certain amount of tokens on behalf of the sender.

You can increase/decrease the numbers here as per your requirements. (All values are mentioned in wei)

```bash
cast send --rpc-url myblockchain --private-key $PK $ERC20_TOKEN_REMOTE_L1 \
"approve(address, uint256)" $ERC20_TOKEN_REMOTE_L1 2000000000000000000000
```

</Step>
</Steps>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
title: Multi-hop Transfer
description: Perform the multi-hop transfer of `TOK` from `myblockchain` to `myblockchain2`, with a 'hop' through the Avalanche C-Chain.
updated: 2024-05-31
authors: [owenwahlgren]
icon: Book
---

import { Step, Steps } from 'fumadocs-ui/components/steps';

<Steps>
<Step>

### Transfer the Token Cross-chain with a Multi-hop Transfer

Now that all the bridge contracts have been deployed and configured, send the ERC-20 token from `myblockchain` to `myblockchain2` with the [`cast send`](https://book.getfoundry.sh/reference/cast/cast-send) foundry command.

The token will be routed through the Avalanche C-Chain and then to `myblockchain2`.

```bash
cast send --rpc-url myblockchain --private-key $PK $ERC20_TOKEN_REMOTE_L1 \
"send((bytes32, address, address, address, uint256, uint256, uint256, address), uint256)" \
"(${SOURCE_BLOCKCHAIN2_ID_HEX}, ${ERC20_TOKEN_REMOTE_CHAIN2}, ${FUNDED_ADDRESS}, ${ERC20_C_CHAIN}, 0, 0, 250000, ${FUNDED_ADDRESS})" 500000000000000
```

</Step>
<Step>

### Check Balance

To confirm the token was bridged from C-Chain to a Avalanche L1, we will check the recipient's balance of wrapped tokens on the Avalanche L1 with the [`cast call`](https://book.getfoundry.sh/reference/cast/cast-call?highlight=cast%20call#cast-call) foundry command:
owenwahlgren marked this conversation as resolved.
Show resolved Hide resolved

```bash
cast call --rpc-url myblockchain2 $ERC20_TOKEN_REMOTE_CHAIN2 "balanceOf(address)(uint)" $FUNDED_ADDRESS
```

```bash
500000000000000 [5e14]
```
</Step>
</Steps>
2 changes: 2 additions & 0 deletions content/course/interchain-token-transfer/meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"...05-avalanche-interchain-token-transfer",
"---ERC-20 to ERC-20 Token Bridge---",
"...06-erc-20-to-erc-20-bridge",
"---ERC-20 Multi-Hop Transfer---",
"...07-tokens-on-multiple-chains",
"---Send and Call---",
"...12-send-and-call",
"---Cross-Chain Token Swaps---",
Expand Down
Loading