Create your own ERC-20 Token using Solidity and HardHat on Avalanche Test Network
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:
namereturns the name of the token (e.g., Avalanche)
symbolreturns the symbol of the token (e.g., AVAX)
decimalsreturns the number of decimals the token uses
totalSupplyreturns the total number initially supplied to the token
balanceOfreturns the balance of an account
transfertransfers a certain amount of tokens to an address
transferFromtransfers a certain amount of tokens from a beneficiary address to a recipient address
approvewithdraws tokens from the owner's address up to a certain amount of tokens
allowancereturns 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:
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-waffleWe can use this plugin to build smart contract tests using Waffle in Hardhat, taking advantage of both.
ethereum-waffleThe most advanced framework for testing smart contracts.
chaiChai 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-ethersThis plugin brings to Hardhat the Ethereum library
ethers.js, which allows you to interact with the Ethereum blockchain in a simple way.
@openzeppelin/contractsA 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.
initialSupplyis a parameter that represent initial supply.
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.
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
We need to make one more configruation before deploying contract.
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:
- Network Name: Avalanche FUJI C-Chain
- New RPC URL: https://api.avax-test.network/ext/bc/C/rpc
- Explorer: https://testnet.snowtrace.io/
To add LordToken to MetaMask click Import tokens then write Token Contract Address and click Add Custom Token button.
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