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

16 migrate bridge with cli #17

Merged
merged 4 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ COPY --from=foundry /usr/local/bin/forge /usr/local/bin/forge
COPY --from=foundry /usr/local/bin/cast /usr/local/bin/cast
COPY --from=foundry /usr/local/bin/anvil /usr/local/bin/anvil
COPY --from=foundry /usr/local/bin/chisel /usr/local/bin/chisel

RUN mkdir -p /home/vscode/.foundry/bin
COPY --from=foundry /usr/local/bin/forge /home/vscode/.foundry/bin/forge

182 changes: 92 additions & 90 deletions src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ _Disclaimer: The teleporter-token-bridge contracts used in this tutorial are und

## What we have to do

1. Create a Subnet and Deploy on Local Network
2. Deploy an ERC20 Contract on C-chain
3. Deploy the Bridge Contracts on C-chain and Subnet
4. Register Remote Bridge with Home Bridge
5. Approve Transaction
6. Start Sending Tokens
1. Codespace Environment Setup
2. Create a Subnet and Deploy on Local Network
3. Deploy an ERC20 Contract on C-chain
4. Deploy the Bridge Contracts on C-chain and Subnet
5. Register Remote Bridge with Home Bridge
6. Approve Transaction
7. Start Sending Tokens

## Environment Setup

### Run on Github Codespace

You can run them directly on Github by clicking **Code**, switching to the **Codespaces** tab and clicking **Create codespace on main**. A new window will open that loads the codespace. Afterwards you will see a browser version of VS code with all the dependencies installed. Codespace time out after some time of inactivity, but can be restarted.

## Local Network Environment

Expand Down Expand Up @@ -49,19 +56,30 @@ creating genesis for subnet mysubnet
Enter your subnet's ChainId. It can be any positive integer.
ChainId: 123
Select a symbol for your subnet's native token
Token symbol: TOK
Token symbol: NAT
✔ Low disk use / Low Throughput 1.5 mil gas/s (C-Chain's setting)
✔ Customize your airdrop
Address to airdrop to: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Amount to airdrop (in ASH units): 100
Amount to airdrop (in NAT units): 100
✔ No
✔ Yes
✔ Native Minting
✔ Add
Enter Address : 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
✔ Done
✔ Done
✔ Done

This precompile allows admins to permit designated contracts to mint the native token on your subnet.
For more information visit https://docs.avax.network/subnets/customize-a-subnet#minting-native-coins

✔ Add an address for a role to the allow list
✔ Admin
✔ Enter the address of the account (or multiple comma separated): 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
✔ Confirm Allow List
+---------+--------------------------------------------+
| Admins | 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC |
+---------+--------------------------------------------+
| Manager | |
+---------+--------------------------------------------+
| Enabled | |
+---------+--------------------------------------------+
✔ Yes
✔ No
✓ Successfully created subnet configuration
```
Expand Down Expand Up @@ -89,33 +107,26 @@ Teleporter Registry successfully deployed to c-chain (0x17aB05351fC94a1a67Bf3f56
Teleporter Messenger successfully deployed to mysubnet (0x253b2784c75e510dD0fF1da844684a1aC0aa5fcf)
Teleporter Registry successfully deployed to mysubnet (0x73b1dB7E9923c9d8fd643ff381e74dd9618EA1a5)

using awm-relayer version (v1.3.0)
using awm-relayer version (v1.3.3)
Installing AWM-Relayer v1.3.3
Executing AWM-Relayer...

<lots of node information...>

Browser Extension connection details (any node URL from above works):
RPC URL: http://127.0.0.1:9650/ext/bc/bFjwbbhaSCotYtdZTPDrQwZn8uVqRoL7YbZxCxXY94k7Qhf3E/rpc
Codespace RPC URL: https://humble-cod-j4prxq655qpcpw96-9650.app.github.dev/ext/bc/bFjwbbhaSCotYtdZTPDrQwZn8uVqRoL7YbZxCxXY94k7Qhf3E/rpc
Funded address: 0x834E891749c29d1417f4501B72945B72224d10dB with 600 (10^18)
Funded address: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC with 100 (10^18) - private key: 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027
RPC URL: http://127.0.0.1:9650/ext/bc/2u9Hu7Noja3Z1kbZyrztTMZcDeqb6acwyPyqP4BbVDjoT8ZaYc/rpc
Codespace RPC URL: https://organic-palm-tree-ppr5xxg7xvv2974r-9650.app.github.dev/ext/bc/2u9Hu7Noja3Z1kbZyrztTMZcDeqb6acwyPyqP4BbVDjoT8ZaYc/rpc
Funded address: 0x69AD03393144008463beD1DcB3FD33eb9A7081ba with 600 (10^18)
Funded address: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC with 1000000 (10^18) - private key: 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027
Network name: mysubnet
Chain ID: 123
Currency Symbol: TOK
Currency Symbol: NAT
```

From this output, take note of the following parameters

- Funded Address (with 100 tokens),
- Teleporter Registry on C-chain, and
- Teleporter Registry on Subnet

Set these parameters as environment variables so that we can manage them easily and also use them in the commands later.
From this output, take note of the Funded Address (with 100 tokens) and set the parameter as environment variable so that we can manage them easily and also use them in the commands later.

```bash
export FUNDED_ADDRESS=<Funded Address (with 100 tokens)>
export TELEPORTER_REGISTRY_C_CHAIN=<Teleporter Registry on C-chain>
export TELEPORTER_REGISTRY_SUBNET=<Teleporter Registry on Subnet>
```

## Deploy ERC20 Contract on C-chain
Expand All @@ -140,97 +151,88 @@ cast call --rpc-url local-c --private-key $PK $ERC20_HOME_C_CHAIN "balanceOf(add

## Deploy Bridge Contracts

We will deploy two bridge contracts. One of the source chain (which is C-chain in our case) and another on the destination chain (mysubnet in our case).

### ERC20Home Contract
We will deploy two bridge contracts. One of the source chain (which is C-chain in our case) and another on the destination chain (mysubnet in our case). This will be done by a single command with the Avalanche CLI

```bash
forge create --rpc-url local-c --private-key $PK lib/teleporter-token-bridge/contracts/src/TokenHome/ERC20TokenHome.sol:ERC20TokenHome --constructor-args $TELEPORTER_REGISTRY_C_CHAIN $FUNDED_ADDRESS $ERC20_HOME_C_CHAIN
```

Export the "Deployed to" address as an environment variables.

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

### ERC20 Remote

To ensure the wrapped token is bridged into the destination chain (in this case, C-Chain) you'll need to deploy a _remote_ contract that implements the `IERC20Bridge` interface, as well as inheriting the properties of `TeleporterTokenRemote`. In order for the bridged tokens to have all the normal functionality of a locally deployed ERC20 token, this remote contract must also inherit the properties of a standard `ERC20` contract.

First, get the `Source Blockchain ID` in hexidecimal format, which in this example is the BlockchainID of your Subnet, run:

```zsh
avalanche subnet describe mysubnet
avalanche teleporter bridge deploy
```
Go

```bash
export SUBNET_BLOCKCHAIN_ID_HEX=0x4d569bf60a38e3ab3e92afd016fe37f7060d7d63c44e3378f42775bf82a7642d
```

`Source Blockchain ID` is in the field: `Local Network BlockchainID (HEX)`.
✔ Local Network
✔ C-Chain
✔ Deploy a new Home for the token
✔ An ERC-20 token
Enter the address of the ERC-20 Token: 0x5DB9A7629912EBF95876228C24A848de0bfB43A9
✔ Subnet mysubnet
Downloading Bridge Contracts
Compiling Bridge

Do the same for the C-Chain:
Home Deployed to http://127.0.0.1:9650/ext/bc/C/rpc
Home Address: 0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D

```zsh
avalanche primary describe
Remote Deployed to http://127.0.0.1:9650/ext/bc/2u9Hu7Noja3Z1kbZyrztTMZcDeqb6acwyPyqP4BbVDjoT8ZaYc/rpc
Remote Address: 0x0D189a705c1FFe77F1bF3ee834931b6b9B356c05
```

`Destination Blockchain ID` is in the field: `BlockchainID (HEX)`.
Save the Remote contract address in the environment variables.

```bash
export C_CHAIN_BLOCKCHAIN_ID_HEX=0x55e1fcfdde01f9f6d4c16fa2ed89ce65a8669120a86f321eef121891cab61241
export ERC20_REMOTE_SUBNET=<"Remote address">
```

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

- Teleporter Registry Address **(for C-Chain)**
- Teleporter Manager (our funded address)
- Source Blockchain ID (hexidecimal representation of our Subnet's Blockchain ID)
- Token Home Address (address of NativeTokenHome.sol deployed on Subnet in the last step)
- Token Name (input in the constructor of the [wrapped token contract](./ExampleWNATV.sol))
- Token Symbol (input in the constructor of the [wrapped token contract](./ExampleWNATV.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.)
Before transfering some funds Cross-Chain, check the current balances of both the ERC20 Home token and the Remote one.

```zsh
forge create --rpc-url mysubnet --private-key $PK lib/teleporter-token-bridge/contracts/src/TokenRemote/ERC20TokenRemote.sol:ERC20TokenRemote \
--constructor-args "(${TELEPORTER_REGISTRY_SUBNET}, ${FUNDED_ADDRESS}, ${C_CHAIN_BLOCKCHAIN_ID_HEX}, ${ERC20_HOME_BRIDGE_C_CHAIN})" "TOK" "TOK" 18
```bash
avalanche key list --local --keys ewoq --subnets c,mysubnet --tokens $ERC20_HOME_C_CHAIN,$ERC20_REMOTE_SUBNET
```

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

export ERC20_TOKEN_REMOTE_SUBNET=<"Deployed to" address>

## Register Remote Bridge with Home Bridge

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 mysubnet --private-key $PK $ERC20_TOKEN_REMOTE_SUBNET "registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)"
+--------+------+---------+--------------------------------------------+---------------+------------------+---------------+
| KIND | NAME | SUBNET | ADDRESS | TOKEN | BALANCE | NETWORK |
+--------+------+---------+--------------------------------------------+---------------+------------------+---------------+
| stored | ewoq | mysubnet | 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC | TOK (0x0D18.)| 0 | Local Network |
+ + +----------+--------------------------------------------+---------------+-----------------+---------------+
| | | C-Chain | 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC | TOK (0x5DB9.)| 100000.000000000 | Local Network |
+--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+
```

### Approve tokens for the Home Bridge contract
## Bridge the Token Cross-chain

You can increase/decrease the numbers here as per your requirements. (All values are mentioned in wei)
Now that the bridge contracts have been deployed, transfer some ERC20 tokens TOK from C-Chain to _mysubnet_ with the following command

```bash
cast send --rpc-url local-c --private-key $PK $ERC20_HOME_C_CHAIN "approve(address, uint256)" $ERC20_HOME_BRIDGE_C_CHAIN 2000000000000000000000
avalanche key transfer
```

## Bridge the Token Cross-chain
```
✔ Local Network
✔ C-Chain
✔ Subnet mysubnet
Enter the address of the Bridge on c-chain: 0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D
Enter the address of the Bridge on mysubnet: 0x0D189a705c1FFe77F1bF3ee834931b6b9B356c05
✔ ewoq
✔ Key
✔ ewoq
Amount to send (TOKEN units): 100
```

Now that all the bridge contracts have been deployed, send a native token from your Subnet to C-Chain with the [`cast send`](https://book.getfoundry.sh/reference/cast/cast-send) foundry command.
## Get New Balances

`
Before transfering some funds Cross-Chain, check the current balances of both the ERC20 Home token and the Remote one.

```bash
cast send --rpc-url local-c --private-key $PK $ERC20_HOME_BRIDGE_C_CHAIN "send((bytes32, address, address, address, uint256, uint256, uint256, address), uint256)" "(${SUBNET_BLOCKCHAIN_ID_HEX}, ${ERC20_TOKEN_REMOTE_SUBNET}, ${FUNDED_ADDRESS}, ${ERC20_HOME_C_CHAIN}, 0, 0, 250000, 0x0000000000000000000000000000000000000000)" 1000000000000000000000
avalanche key list --local --keys ewoq --subnets c,mysubnet --tokens $ERC20_HOME_C_CHAIN,$ERC20_REMOTE_SUBNET
```

## Check Balances

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

```zsh
cast call --rpc-url mysubnet $ERC20_TOKEN_REMOTE_SUBNET "balanceOf(address)(uint)" $FUNDED_ADDRESS
```bash
+--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+
| KIND | NAME | SUBNET | ADDRESS | TOKEN | BALANCE | NETWORK |
+--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+
| stored | ewoq | mysubnet | 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC | TOK (0x0D18.) | 100.000000000 | Local Network |
+ + +----------+--------------------------------------------+---------------+-----------------+---------------+
| | | C-Chain | 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC | TOK (0x5DB9.) | 99900.000000000 | Local Network |
+--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+
```