The Power of Native Hedera Tokens as ERC-20 Tokens: A step-by-step guide
In this tutorial, you’ll learn how to make Hedera native tokens work like Ethereum's ERC-20 tokens using the Hashio JSON-RPC instance.
Prerequisites
Basic understanding of TypeScript and Solidity.
Get a Hedera testnet account.
Have
ts-node
installed.
Table of Contents
Project Setup
To make the setup process simple, you'll use a pre-configured token wrapper project from the token-wrapper
repository.
Open a terminal window and navigate to your preferred directory where your project will live. Run the following command to clone the repo and install dependencies to your local machine:
git clone https://github.com/Swiss-Digital-Assets-Institute/token-wrapper.git
cd token-wrapper
npm install
The dotenv
package is used to manage environment variables in a separate .env
file, which is loaded at runtime. This helps protect sensitive information like your private keys and API secrets, but it's still best practice to add .env
to your .gitignore
to prevent you from pushing your credentials to GitHub.
Project Configuration
In this step, you will update and configure the Hardhat configuration file that defines tasks, stores Hedera account private key information, and Hashio Testnet RPC URL. First, rename the .env.example
file to .env
. and update the .env
files with the following code.
Environment Variables
The .env
file defines environment variables used in the project. The MY_ACCOUNT_ID
and MY_PRIVATE_KEY
variables contains the ECDSA Account ID and DER Encoded Private Key, respectively for the Hedera Testnet account. The HEX_ENCODED_PRIVATE_KEY
variable contains the HEX Encoded Private Key.
The JSON_RPC_RELAY_URL
variable contains the HashIO Testnet endpoint URL. This is the JSON-RPC instance that will submit the transactions to the Hedera test network to test, create and deploy your smart contract.
MY_ACCOUNT_ID =
MY_PRIVATE_KEY =
HEX_ENCODED_PRIVATE_KEY =
JSON_RPC_RELAY_URL = https://testnet.hashio.io/api
Project Contents
In this step, you'll examine the descriptions of the project contents in your existing project. If you don't need to review the project contents, you can proceed directly to Running the project.
This directory contains compiled smart contracts used for generating TypeScript bindings with Typechain. We compile these contracts using Hardhat, a versatile Ethereum development tool. You'll find the configuration in hardhat.config.js
.
Think of Hardhat as your all-in-one environment for Ethereum development tasks like compiling, deploying, testing, and debugging.
Running the project
Now that you have your project set up and configured, we can run it.
To do so, run the following command:
npm run compile
ts-node scripts/ERC20.ts
The first command compiles the smart contracts and generates the typescript bindings. The second command runs the ERC20 script.
To see the transactions on the Hedera network, you can use the Hedera Testnet Explorer.
Note: At the top of the explorer page, remember to switch the network to TESTNET before you search for the transaction.
Congratulations! 🎉 You have successfully learned to use native Hedera tokens as ERC20 tokens. Feel free to reach out if you have any questions:
Last updated
Was this helpful?