Create your own ERC-20 Token using Solidity and HardHat on Avalanche Test Network

Yoorbit
6 min readApr 27, 2022

--

Photo by Nenad Novaković on Unsplash

In this article, you’ll learn how to create your own Avalanche token (ERC20 standard), set the initial balances so that they can be modified easily later, and deploy the smart contract to the live blockchain.

Table of Content

  • What is Blockchain?
  • What is Avalanche?
  • What is smart contract?
  • What is ERC-20 token?
  • The ERC-20 standard
  • Creating an ERC-20 token
  • Deploying a token on the Avalanche testnet

What is Blockchain?

Blockchain is a new way of conducting transactions. Instead of banks or governments keeping track, the system is a distributed database spread across thousands of computers around the world. Every ten minutes or so, a block of transactions is added to the ledger, which everyone can see. This information is permanent and verifiable. This makes it perfect for running smart contracts and creating cryptocurrencies.

What is Avalanche?

Avalanche is an open-source platform for building decentralized applications and enterprise blockchain systems that provides interoperability with existing legacy systems while scaling to the global financial network. Avalanche’s interoperability, scalability, and finality allow you to build smart contracts in Solidity over trillions of dollars of existing financial infrastructure, making it the platform of choice for smart contract-driven enterprise applications.

What is smart contract?

A smart contract is an immutable computer program that lives in the blockchain. Its source code is public and can be read by anyone. It runs exactly as programmed without any possibility of downtime, censorship, fraud or third-party interference.

Solidity is a statically typed, contract-oriented programming language for writing Ethereum smart contracts. The Solidity compiler targets the Ethereum Virtual Machine (EVM). It was designed to target the EVM. Solc source code

What is ERC-20 token?

The ERC-20 introduces a standard for Fungible Tokens, in other words, they have a property that makes each Token be exactly the same (in type and value) of another Token. For example, an ERC-20 Token acts just like the ETH, meaning that 1 Token is and will always be equal to all the other Tokens.

An ERC-20 token must be able to:

  • Transfer tokens from one account to another
  • Return the balance of an account
  • Return the total tokens available in the token
  • Transfer tokens to an account

ERC-20 is a token standard used for Ethereum smart contracts. It specifies 6 public methods & 3 public events that an Ethereum token contract must implement

Simple implementation of the ERC20 Standard token:

  • name returns the name of the token (e.g., Avalanche)
  • symbol returns the symbol of the token (e.g., AVAX)
  • decimals returns the number of decimals the token uses
  • totalSupply returns the total number initially supplied to the token
  • balanceOf returns the balance of an account
  • transfer transfers a certain amount of tokens to an address
  • transferFrom transfers a certain amount of tokens from a beneficiary address to a recipient address
  • approve withdraws tokens from the owner's address up to a certain amount of tokens
  • allowance returns the number of tokens withdrawable from the owner's account
  • Transfer, which must be triggered when tokens are transferred
  • Approval, which must be triggered when an account is approved to collect a certain amount of tokens

Creating ERC-20 Token

Step 1: Project Setup

Okay, let’s move on to the next step. We’ll need to cd into the directory that we want to work on and run the following commands:

mkdir lord_token
cd lord_token
npm install --save-dev hardhat

Now, we should have a hardhat. Let’s get a sample project going by running the command below:

npx hardhat init

We’ll go with the option of creating a sample project. Accept all requests.

After that we need to install couple of dependency. We can install these dependencies using this command:

npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts
  • @nomiclabs/hardhat-waffle We can use this plugin to build smart contract tests using Waffle in Hardhat, taking advantage of both.
  • ethereum-waffle The most advanced framework for testing smart contracts.
  • chai Chai is an assertion library, similar to Node’s built-in assert. It makes testing much easier by giving you lots of assertions you can run against your code.
  • @nomiclabs/hardhat-ethers This plugin brings to Hardhat the Ethereum library ethers.js, which allows you to interact with the Ethereum blockchain in a simple way.
  • ethers A complete Ethereum wallet implementation and utilities in JavaScript (and TypeScript).
  • @openzeppelin/contracts A library for secure smart contract development. Build on a solid foundation of community-vetted code.

To make sure everything is working, run:

npx hardhat test

We will see a passed test result in our console.

Step 2: Writing Smart Contract

Next, under the contracts directory, we’ll create a file called LordToken.sol. We can write an ERC-20 Token smart contract by inheriting OpenZepplin contracts.

  • initialSupply is a parameter that represent initial supply.
_mint(msg.sender, initialSupply);

above code is going to call when contract deployed and send LordToken amount of initialSupply to contract creator.

We can compile contract writing below command:

npx hardhat compile

After compile process finished, we are going to see new folder named artifacts. In this folder we have compiled ABI files.

Step 3: Deploying Contract

HardHat has sample deploy script in default. You can see that script under scripts/ folder. Now, we are going to write script like that.

First create javascript file called deploy.js and paste below code

In our LordToken contract we have constructor parameter named initialSupply. We need to send that value during deploy process. Above example that value is 10000000000000000000000. This means we are going to mint 10000 LordToken.

We need to make one more configruation before deploying contract.

Open hardhat.config.jsfile and change that with below code:

in line 2 you need to write your own private key.

For getting private key you need to have MetaMask extension on your browser. Check this tutorial if you don’t have MetaMask.

You can get your private key following this steps.

Write below code to deploy your token:

npx hardhat run scripts/deploy.js --network avalanche_fuji

After deploy proccess finish you are going to see this line on your console

LordToken deployed to: 0x59F5d8Bd31CB8c11176FE710881E6Af04dAF5e2E

This is our contract address. Let’s check our contract on SnowTrace.

Copy and paste token address to SnowTrace search bar and press Enter. And you are going to see that is our token.

Step 4: Add token to MetaMask

First we need to add Avalanche Fuji Testnet to MetaMask. Open your MetaMask, click Network Name right of the MetaMask Fox and click Add Network button. After writing configurations click save and change your netwok from MetaMask extension.

FUJI Testnet Settings:

To add LordToken to MetaMask click Import tokens then write Token Contract Address and click Add Custom Token button.

Conclusions

That’s what we have learned and done so far

  • Learned blockchain, solidity concepts, and smart contracts.
  • Create an ERC20 Token contract
  • Deployed our contract on Avalanche Fuji

Useful Links

--

--

Yoorbit
Yoorbit

Written by Yoorbit

World’s First Rate-to-Earn Social Network

No responses yet