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

ICTT: native to erc20 bridge chapter #142

Merged
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
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
---
title: Native to ERC-20 Token Bridge
description: TBD
updated: 2024-05-31
authors: [ashucoder9]
title: Native to ERC-20 Token Bridge Overview
description: Learn how to transfer native Avalanche L1 tokens to the C-Chain as ERC-20 tokens.
updated: 2024-09-09
authors: [owenwahlgren]
icon: Book
---

import ICTTsetup from "@/content/common/avalanche-starter-kit/create-default-blockchain.mdx";


## Bridge a Avalanche L1's Native Token to the C-Chain

The following code example will show you how to send a Avalanche L1's native token to the C-Chain using Interchain Messaging and Foundry. This demo is conducted on a local network run by the CLI, but can be applied to Fuji Testnet and Avalanche Mainnet directly.

**All token bridge contracts and interfaces implemented in this example implementation are maintained in the [teleporter-token-bridge](https://github.com/ava-labs/teleporter-token-bridge/tree/main/contracts/src) repository.**

If you prefer full end-to-end testing written in Golang for bridging ERC20s, native tokens, or any combination of the two, you can view the test workflows directly in the [teleporter-token-bridge](https://github.com/ava-labs/teleporter-token-bridge/tree/main/tests/flows) repository.

Deep dives on each template interface can be found [here](https://github.com/ava-labs/teleporter-token-bridge/blob/main/contracts/README.md).

_Disclaimer: The teleporter-token-bridge contracts used in this tutorial are under active development and are not yet intended for production deployments. Use at your own risk._

## What we have to do

1. Create an Avalanche L1 and Deploy on Local Network
2. Deploy Wrapped Native Token C-chain
3. Deploy the Interchain Token Transferer Contracts on C-chain and Avalanche L1
4. Register Remote Token with Home Transferer
5. Add Collateral and Start Sending Tokens

<ICTTsetup />

## Parameter Management

As you deploy the teleporter contracts, keeping track of their addresses will make testing and troubleshooting much easier. The parameters you should keep track of include:

| Parameter | Network | Description |
| :---------------------------- | :------ | :-------------------------------------------------------------------------------------------------------------------------------------------------- |
| Funded Address (with 1000000) | Both | The public address you use to deploy contracts, and send tokens through the bridge. Used as the `teleporterManager` constructor parameter in this example. |
| Interchain Messaging Registry | C-Chain | Address of the TeleporterRegistry contract on C-Chain deployed by the CLI |
| Interchain Messaging Registry | Avalanche L1 | Address of the TeleporterRegistry contract on Avalanche L1 deployed by the CLI |
| Wrapped Native Token | Avalanche L1 | Address of the wrapped token contract for your Avalanche L1's native token to be deployed on the Avalanche L1 |
| Native Token Home | Avalanche L1 | Address of the bridge's Home contract to be deployed on the Avalanche L1 |
| ERC20 Remote | C-Chain | Address of the bridge's remote contract to be deployed on the C-Chain |
| Avalanche L1 Blockchain ID | Avalanche L1 | Hexadecimal representation of the Avalanche L1's Blockchain ID. Returned by `avalanche subnet describe <subnetName>`. |
| C-Chain Blockchain ID | C-Chain | Hexadecimal representation of the C-Chain's Blockchain ID on the selected network. Returned by `avalanche primary describe`. |
ICTT is also capable of bridging native tokens from Avalanche L1s to the C-Chain as ERC-20 tokens. This process involves deploying a `NativeTokenHome` contract on the Avalanche L1, and a `ERC20TokenRemote` contract on the C-Chain. The `NativeTokenHome` contract will be used to bridge the native token to the C-Chain as an ERC-20 token.

This example will cover native to ERC-20 token bridging between `myblockchain` and the C-Chain, but the process can be adapted to bridge the C-Chain's native asset, `AVAX`, to any L1 as an ERC-20 token.

### What we will do
1. Deploy a Wrapped Native Token to `myblockchain`
2. Deploy the `NativeTokenHome` contract on `myblockchain`
3. Deploy the `ERC20TokenRemote` contract on the C-Chain
4. Register `ERC20TokenRemote` on C-Chain with `NativeTokenHome` on `myblockchain`
4. Perform a transfer of the native token on `myblockchain` to the C-Chain as an ERC-20 token
Original file line number Diff line number Diff line change
@@ -1,116 +1,34 @@
---
title: Deploy a ICTT contracts
description: deploy remote, home and wrapped native tokens
title: Deploy Wrapped Native Token Contracts
description: Deploy the wrapped native token contract to the Avalanche L1 blockchain.
updated: 2024-05-31
authors: [ashucoder9]
authors: [ashucoder9, owenwahlgren]
icon: Book
---
import { Step, Steps } from 'fumadocs-ui/components/steps';


## Wrapped Native Token

On your Avalanche L1, deploy a wrapped token contract for your native token. When we configured the Avalanche L1 earlier, we named the token `NATV`. This is reflected in line 19 of our [example wrapped token contract](https://github.com/ava-labs/avalanche-interchain-token-transfer/blob/main/contracts/src/WrappedNativeToken.sol).

```
forge create --rpc-url myblockchain --private-key $PK src/5-native-to-erc20-token-bridge/ExampleWNATV.sol:WNATV
```

Export the "Deployed to" address as an environment variables.
<Steps>
<Step>
### Deploy Wrapped Native Token `ExampleWNATV`

```bash
export WRAPPED_ERC20_HOME_SUBNET=<"Deployed to" address>
forge create --rpc-url myblockchain --private-key $PK src/5-native-to-erc20-interchain-token-transfer/ExampleWNATV.sol:WNATV
```
```

```bash
[⠊] Compiling...
[] Compiling 7 files with Solc 0.8.18
[] Solc 0.8.18 finished in 778.12ms
[] Compiling 7 files with Solc 0.8.18
[] Solc 0.8.18 finished in 371.02ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922
Transaction hash: 0x054e7b46b221c30f400b81df0fa2601668ae832054cf8e8b873f4ba615fa4115
```

## Native Token Home

To bridge the token out of your Avalanche L1, you'll need to first deploy a _home_ contract on your Avalanche L1 that implements the `INativeTokenBridge` interface, and inherits the properties of the `TeleporterTokenHome` contract standard.

Using the [`forge create`](https://book.getfoundry.sh/reference/forge/forge-create) command, we will deploy the [NativeTokenHome.sol](https://github.com/ava-labs/avalanche-interchain-token-transfer/blob/main/contracts/src/TokenHome/NativeTokenHome.sol) contract, passing in the following constructor arguments:

```bash
forge create --rpc-url myblockchain --private-key $PK lib/teleporter-token-bridge/contracts/src/TokenHome/NativeTokenHome.sol:NativeTokenHome --constructor-args $TELEPORTER_REGISTRY_SUBNET $FUNDED_ADDRESS $WRAPPED_ERC20_HOME_SUBNET
```

- Interchain Messaging Registry (for our Avalanche L1)
- Interchain Messaging Manager (our funded address)
- Wrapped Token Address (deployed in the last step)

For example, this foundry command could be entered into your terminal as:

```bash
forge create --rpc-url myblockchain --private-key $PK src/5-native-token-bridge/NativeTokenHome.sol:NativeTokenHome --constructor-args 0xAd00Ce990172Cfed987B0cECd3eF58221471a0a3 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922
```

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

```bash
export ERC20_HOME_BRIDGE_SUBNET=<"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 Avalanche L1, run:

```bash
avalanche subnet describe myblockchain
```

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

`Source Blockchain ID` is in the field: `Local Network BlockchainID (HEX)`.

Using the [`forge create`](https://book.getfoundry.sh/reference/forge/forge-create) command, we will deploy the [ERC20RTokenRemote.sol](https://github.com/ava-labs/avalanche-interchain-token-transfer/blob/main/contracts/src/TokenRemote/ERC20TokenRemote.sol) contract, passing in the following constructor arguments:

```bash
forge create --rpc-url local-c --private-key $PK lib/teleporter-token-bridge/contracts/src/TokenRemote/ERC20TokenRemote.sol:ERC20TokenRemote --constructor-args "(${TELEPORTER_REGISTRY_C_CHAIN}, ${FUNDED_ADDRESS}, ${SUBNET_BLOCKCHAIN_ID_HEX}, ${ERC20_HOME_BRIDGE_SUBNET})" "Wrapped NATV" "WNATV" 18
```

- Interchain Messaging Registry Address **(for C-Chain)**
- Interchain Messaging Manager (our funded address)
- Source Blockchain ID (hexidecimal representation of our Avalanche L1's Blockchain ID)
- Token Home Address (address of NativeTokenHome.sol deployed on Avalanche L1 in the last step)
- 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.)

For example, this contract deployment could be entered into your terminal as:

```bash
forge create --rpc-url local-c --private-key $PK \
lib/teleporter-token-bridge/contracts/src/TokenRemote/ERC20TokenRemote.sol:ERC20TokenRemote \
--constructor-args 0xAd00Ce990172Cfed987B0cECd3eF58221471a0a3 \
0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC \
0xbcb8143686b1f0c765a1404bb94ad13134cafa5cf56f181a3a990ba21b1151b9 \
0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 \
"Wrapped NATV" \
"WNATV" \
18
Deployed to: 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922 // [!code highlight]
Transaction hash: 0x481398f0648cd2298a6e14fcbc474f71feaff9c85eb367100b31bb7a2213232b
```

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

```bash
export ERC20_TOKEN_REMOTE_C_CHAIN=<"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`.
</Step>
<Step>
### Save the Wrapped Native Token Address

```bash
cast send --rpc-url local-c --private-key $PK $ERC20_TOKEN_REMOTE_C_CHAIN "registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)"
export WRAPPED_ERC20_L1=0x52C84043CD9c865236f11d9Fc9F56aa003c1f922
```
</Step>
</Steps>

This file was deleted.

Loading
Loading