From de52eb0b0b7a8b93a55e548524b133e0f74058e5 Mon Sep 17 00:00:00 2001 From: Andrea Vargas Date: Wed, 26 Jun 2024 11:49:39 -0600 Subject: [PATCH 1/4] add codespace setup --- .../_INSTRUCTIONS.md | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md index f69bd24..b8723fe 100644 --- a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md +++ b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md @@ -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 From bb879a0c994909a67d7eae75844fe2206bfbc1fe Mon Sep 17 00:00:00 2001 From: Andrea Vargas <43045203+Andyvargtz@users.noreply.github.com> Date: Wed, 26 Jun 2024 20:28:14 +0000 Subject: [PATCH 2/4] copy forge to home --- .devcontainer/Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile index 3cea675..b2928f7 100644 --- a/.devcontainer/Dockerfile +++ b/.devcontainer/Dockerfile @@ -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 + From cabae19b4ccb3c890cef724020bc0eab2a870e37 Mon Sep 17 00:00:00 2001 From: Andrea Vargas <43045203+Andyvargtz@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:44:35 +0000 Subject: [PATCH 3/4] Migrate bridge deployment to CLI --- .../_INSTRUCTIONS.md | 165 +++++++++--------- 1 file changed, 82 insertions(+), 83 deletions(-) diff --git a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md index b8723fe..73fa885 100644 --- a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md +++ b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md @@ -56,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 ``` @@ -96,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... 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= -export TELEPORTER_REGISTRY_C_CHAIN= -export TELEPORTER_REGISTRY_SUBNET= ``` ## Deploy ERC20 Contract on C-chain @@ -147,97 +151,92 @@ 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 +avalanche teleporter bridge deploy ``` - -Export the "Deployed to" address as an environment variables. +Go ```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. +✔ 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 -First, get the `Source Blockchain ID` in hexidecimal format, which in this example is the BlockchainID of your Subnet, run: +Home Deployed to http://127.0.0.1:9650/ext/bc/C/rpc +Home Address: 0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D -```zsh -avalanche subnet describe mysubnet +Remote Deployed to http://127.0.0.1:9650/ext/bc/2u9Hu7Noja3Z1kbZyrztTMZcDeqb6acwyPyqP4BbVDjoT8ZaYc/rpc +Remote Address: 0x0D189a705c1FFe77F1bF3ee834931b6b9B356c05 ``` +Save the Remote contract address in the environment variables. + ```bash -export SUBNET_BLOCKCHAIN_ID_HEX=0x4d569bf60a38e3ab3e92afd016fe37f7060d7d63c44e3378f42775bf82a7642d +export ERC20_REMOTE_SUBNET=<"Remote address"> ``` -`Source Blockchain ID` is in the field: `Local Network BlockchainID (HEX)`. - -Do the same for the C-Chain: - -```zsh -avalanche primary describe -``` +## Get Balances -`Destination Blockchain ID` is in the field: `BlockchainID (HEX)`. +Before transfering some funds Cross-Chain, check the current balances of both the ERC20 Home token and the Remote one. ```bash -export C_CHAIN_BLOCKCHAIN_ID_HEX=0x55e1fcfdde01f9f6d4c16fa2ed89ce65a8669120a86f321eef121891cab61241 +avalanche key list --local --keys ewoq --subnets c,mysubnet --tokens $ERC20_HOME_C_CHAIN,$ERC20_REMOTE_SUBNET ``` -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: - -- 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.) - -```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 ++--------+------+---------+--------------------------------------------+---------------+------------------+---------------+ +| 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 | ++--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+ ``` -Note the address the remote contract was "Deployed to". - -export ERC20_TOKEN_REMOTE_SUBNET=<"Deployed to" address> - -## Register Remote Bridge with Home Bridge +## Bridge the Token Cross-chain -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`. +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 mysubnet --private-key $PK $ERC20_TOKEN_REMOTE_SUBNET "registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)" +avalanche key transfer ``` -### Approve tokens for the Home Bridge contract - -You can increase/decrease the numbers here as per your requirements. (All values are mentioned in wei) - -```bash -cast send --rpc-url local-c --private-key $PK $ERC20_HOME_C_CHAIN "approve(address, uint256)" $ERC20_HOME_BRIDGE_C_CHAIN 2000000000000000000000 +``` +✔ 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 ``` -## Bridge the Token Cross-chain +## 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: -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 | ++--------+------+----------+--------------------------------------------+---------------+-----------------+---------------+ ``` From e1402a57c0dfbc9b6f60143c6043ca98d3cdb610 Mon Sep 17 00:00:00 2001 From: Andrea Vargas <43045203+Andyvargtz@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:47:04 +0000 Subject: [PATCH 4/4] remove duplicated balance check --- src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md index 73fa885..91da093 100644 --- a/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md +++ b/src/8-erc20-to-erc20-token-bridge/_INSTRUCTIONS.md @@ -219,10 +219,6 @@ Enter the address of the Bridge on mysubnet: 0x0D189a705c1FFe77F1bF3ee834931b6b9 Amount to send (TOKEN units): 100 ``` -## 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: - ## Get New Balances Before transfering some funds Cross-Chain, check the current balances of both the ERC20 Home token and the Remote one.