Skip to content

Commit 42f3341

Browse files
authored
ICCT Course: Add Scaling Decimals Chapter (#180)
2 parents 5e7dad2 + 05f7296 commit 42f3341

File tree

3 files changed

+188
-1
lines changed

3 files changed

+188
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: Scaling with TokenRemote
3+
description: Learn how to handle token scaling with TokenRemote contracts when bridging assets with different decimal systems.
4+
updated: 2024-10-04
5+
authors: [owenwahlgren]
6+
icon: Calculator
7+
---
8+
9+
## Math Example
10+
11+
Token scaling is a crucial part of cross-chain token transfers, especially when dealing with varying decimal denominations between the home and remote assets. This chapter will provide a math example to demonstrate how the scaling works with `TokenRemote` contracts.
12+
13+
In this example, let's assume we are bridging an ERC-20 token from a home chain where the token uses 6 decimal places to a remote chain as the native token that uses 18 decimal places (e.g., USDC on Avalanche).
14+
15+
The key variables here are:
16+
- `_homeTokenDecimals = 6`
17+
- `_tokenDecimals = 18`
18+
- `_tokenMultiplier` is calculated as:
19+
20+
<Mermaid chart={`
21+
sequenceDiagram
22+
participant Home as TokenHome.sol (_homeTokenDecimals)
23+
participant Remote as TokenRemote.sol (_tokenDecimals)
24+
participant Multiplier as Token Multiplier (_tokenMultiplier)
25+
26+
Home->>Remote: Calculate _tokenDecimals - _homeTokenDecimals
27+
Remote->>Multiplier: 18 - 6 = 12
28+
Multiplier-->>Multiplier: 10^12
29+
`} />
30+
31+
This multiplier helps scale the token amounts when transferring between chains.
32+
33+
### Scenario 1: Transferring from Home (6 decimals) to Remote (18 decimals)
34+
35+
When transferring tokens from the home chain (6 decimals) to the remote chain (18 decimals), the token amount is multiplied by `_tokenMultiplier` to normalize the denomination. If `multiplyOnRemote = true`, we perform the following:
36+
37+
For example, if we transfer **100 USDC** (which is represented as `100 × 10^6` in the 6-decimal system), it will be scaled as follows on the remote chain:
38+
39+
```
40+
100 × 10^6 × 10^{12} = 100 × 10^{18}
41+
```
42+
Thus, the value on the remote chain would be 100 × 10^{18}, equivalent to 100 USDC in 18 decimals.
43+
44+
### Scenario 2: Transferring from Remote (18 decimals) to Home (6 decimals)
45+
46+
When transferring tokens back from the remote chain (18 decimals) to the home chain (6 decimals), the token amount is divided by `_tokenMultiplier`. If `multiplyOnRemote = true`, the reverse scaling applies:
47+
48+
For example, transferring `100 × 10^{18}` (which is 100 USDC in the 18-decimal system) back to the home chain would scale down:
49+
50+
```
51+
100 × 10^{18} ÷ 10^{12} = 100 × 10^6
52+
```
53+
54+
This results in 100 × 10^6 USDC, which correctly represents 100 USDC in the 6-decimal system.
55+
56+
By applying this multiplier, tokens retain their value across chains with different decimal systems.
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: Example USDC as Native Token (DIY)
3+
description: Learn how to transfer USDC to a new Avalanche L1 and use it as a native token via ICTT.
4+
updated: 2024-09-03
5+
authors: [owenwahlgren]
6+
icon: Terminal
7+
---
8+
import { Step, Steps } from 'fumadocs-ui/components/steps';
9+
10+
In this section, you will learn how to transfer USDC from Avalanche’s C-Chain to a new Avalanche L1 using Interchain Token Transfers (ICTT) and set it up to act as the **native token** on the new L1. This guide will take you through the steps of configuring a local network environment, deploying the necessary contracts, and transferring tokens.
11+
12+
13+
<Steps>
14+
<Step>
15+
### Create a new blockchain and Deploy on Local Network
16+
17+
Use the **Avalanche CLI** to create a new blockchain where you will deploy USDC as the native token.
18+
19+
20+
```bash
21+
avalanche blockchain create myblockchain
22+
```
23+
```bash
24+
avalanche blockchain deploy myblockchain
25+
```
26+
</Step>
27+
<Step>
28+
### Acquire USDC On Fuji C-Chain
29+
30+
The address for USDC on Fuji C-Chain is [`0x5425890298aed601595a70ab815c96711a31bc65`](https://testnet.snowtrace.io/token/0x5425890298aed601595a70ab815c96711a31bc65).
31+
For convience we have already deployed a `TokenHome` to the C-Chain for USDC with the address [`0x546526F786115af1FE7c11aa8Ac5682b8c181E3A`](https://testnet.snowtrace.io/address/0x546526F786115af1FE7c11aa8Ac5682b8c181E3A)
32+
33+
You can use the [Core Faucet to get some USDC](https://core.app/en/tools/testnet-faucet/?subnet=c&token=usdcc) on Fuji.
34+
```bash
35+
export USDC=0x5425890298aed601595a70ab815c96711a31bc65
36+
export USDC_HOME_C_CHAIN=0x546526F786115af1FE7c11aa8Ac5682b8c181E3A
37+
```
38+
</Step>
39+
<Step>
40+
### Deploy Interchain Token Transfer Contracts
41+
Set up the remote transferer contracts for transferring tokens between the C-Chain and the newly created L1.
42+
43+
- `NativeTokenRemote` Contract on `myblockchain`
44+
```bash
45+
forge create --rpc-url myblockchain --private-key $PK lib/avalanche-interchain-token-transfer/contracts/src/TokenRemote/NativeTokenRemote.sol:NativeTokenRemote --constructor-args "($TELEPORTER_REGISTRY_L1, $FUNDED_ADDRESS, $C_CHAIN_BLOCKCHAIN_ID_HEX, $USDC_HOME_C_CHAIN, 6)" "USDC" 100000000000000000000 0
46+
```
47+
48+
_Note: When deploying the `NativeTokenRemote` contract on the L1, ensure that the **initial amount** matches the native token amount that was minted when the blockchain was created. This ensures consistency between the native token supply and the remote token counterpart._
49+
50+
```bash
51+
export NATIVE_TOKEN_REMOTE_L1="Deployed To Address"
52+
```
53+
</Step>
54+
<Step>
55+
### Granting Native Minting Rights to NativeTokenRemote Contract
56+
To ensure that the `NativeTokenRemote` contract can mint native tokens on the L1 when USDC tokens are transferred from the `C-Chain`, the contract must be granted **minting rights**. This is done by adding the `NativeTokenRemote contract` address to the `Native Minter Precompile`.
57+
58+
1. You will need to interact with the `Native Minter Precompile`, which resides at a fixed address on all Avalanche L1s:
59+
**Native Minter Precompile Address**: `0x0200000000000000000000000000000000000001`
60+
61+
2. Use the following command to grant the `NativeTokenRemote` contract minting rights by setting it as an **enabled** address on the Native Minter Precompile:
62+
63+
```bash
64+
cast send --rpc-url myblockchain --private-key $PK 0x0200000000000000000000000000000000000001 "setEnabled(address)" $NATIVE_TOKEN_REMOTE_L1
65+
```
66+
67+
- `$NATIVE_TOKEN_REMOTE_L1`: The deployed address of the `NativeTokenRemote` contract on your L1.
68+
69+
Once this step is completed, the `NativeTokenRemote` contract will have the necessary permissions to mint native tokens when USDC tokens are transferred from the C-Chain.
70+
</Step>
71+
<Step>
72+
### Register Remote Token with Home Transferer
73+
Register the remote token on the home chain so that it recognizes the transferer contracts.
74+
75+
```bash
76+
cast send --rpc-url myblockchain --private-key $PK $NATIVE_TOKEN_REMOTE_L1 "registerWithHome((address, uint256))" "(0x0000000000000000000000000000000000000000, 0)"
77+
```
78+
</Step>
79+
<Step>
80+
### Collateralize and Transfer Tokens
81+
Add collateral to the transferer contract on the home chain, and then send the USDC tokens across chains.
82+
83+
<Accordions>
84+
<Accordion title="What is Collateral?">
85+
Collateral in this context refers to the amount of the token that is locked in the `Home Transferer contract` on the source chain (`C-Chain`) to back the value of the token on the destination chain (`myblockchain`). This ensures that for every token minted on the remote chain, there’s an equivalent token locked as collateral on the home chain.
86+
</Accordion>
87+
<Accordion title="Why is Collateral Important?">
88+
Collateralization ensures that the total supply of the token remains consistent across both chains. When tokens are sent from the `home chain`, they are locked as collateral, and a corresponding number of tokens is minted on the remote chain. If tokens are sent back to the home chain, the collateral is unlocked, and the minted tokens on the remote chain are burned.
89+
</Accordion>
90+
</Accordions>
91+
92+
- **Approve Tokens for Transfer**
93+
Approve a certain number of tokens to be used by the Home Transferer.
94+
95+
```bash
96+
cast send --rpc-url local-c --private-key $PK $USDC "approve(address, uint256)" $USDC_HOME_C_CHAIN 2000000000000000000000
97+
```
98+
99+
- **Add Collateral and Send Tokens**
100+
Add collateral to the transferer contract.
101+
```bash
102+
cast send --rpc-url local-c --private-key $PK $USDC_HOME_C_CHAIN "addCollateral(bytes32, address, uint256)" $L1_BLOCKCHAIN_ID_HEX $NATIVE_TOKEN_REMOTE_L1 100000000000000000000
103+
```
104+
You can also confirm whether the Transferer is collateralized now by running the below command:
105+
106+
```bash
107+
cast call --rpc-url myblockchain $NATIVE_TOKEN_REMOTE_L1 "isCollateralized()(bool)"
108+
```
109+
Send tokens to the L1
110+
```bash
111+
cast send --rpc-url local-c --private-key $PK $USDC_HOME_C_CHAIN "send((bytes32, address, address, address, uint256, uint256, uint256, address), uint256)" "(${L1_BLOCKCHAIN_ID_HEX}, ${NATIVE_TOKEN_REMOTE_L1}, ${FUNDED_ADDRESS}, ${USDC}, 0, 0, 250000, 0x0000000000000000000000000000000000000000)" 1000000000000000000000
112+
```
113+
</Step>
114+
115+
<Step>
116+
Check Balance
117+
```bash
118+
cast balance --rpc-url myblockchain $FUNDED_ADDRESS
119+
```
120+
</Step>
121+
</Steps>
122+
123+
---
124+
125+
### Conclusion
126+
127+
Follow the steps above to transfer a USDC token from the C-Chain to your custom Avalanche L1 and use it as the native token. This exercise will demonstrate how Avalanche’s **Interchain Token Transfer (ICTT)** system works, ensuring that tokens are properly locked, transferred, and minted across multiple chains.
128+
129+
For more detailed information, refer to the [official Avalanche ICTT documentation](/course/interchain-token-transfer).

content/course/interchain-token-transfer/meta.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"---Send and Call---",
2323
"...12-send-and-call",
2424
"---Cross-Chain Token Swaps---",
25-
"...13-cross-chain-token-swaps"
25+
"...13-cross-chain-token-swaps",
26+
"---Scaling Token Decimals---",
27+
"...14-scaling-decimals"
2628
]
2729
}
2830

0 commit comments

Comments
 (0)