A simple ERC20 Smart Contract
This tutorial introduces you to interact with All That Node product to deploy sample ERC20 smart contract to Ethereum Network.
Last updated
This tutorial introduces you to interact with All That Node product to deploy sample ERC20 smart contract to Ethereum Network.
Last updated
Install Node.js LTS version (v16.15.0)
Sign up to All That Node (ATN) and secure your account
Download your own IDE - recommend Visual Studio Code
An Ethereum account with Metamask or other equivalent wallet
Install Git
on your computer
This tutorial uses Ethereum Kovan Network, which allows for developers to test the contract before deployment on the main Ethereum network. AllThatNode supports the feature called Faucet for giving away some Kovan Ether (kETH) that can be only used in the testnet.
To claim your small amounts of testnet Ether, click to the Faucet button on the top of the ATN site.
As of April 2022, AllThatNode Faucet currently supports the following protocols and testnets. The faucet has planned to support more networks in that ATN plans to become the faucet aggregator which fully supports the multi-chain ecosystem development.
Ethereum: Goerli Testnet, Kovan Testnet, Rinkeby Testnet, and Ropsten Testnet.
Terra: Bombay Testnet
Celo: Alfajores Testnet
Solana: Devnet, Testnet
Polygon: Mumbai Testnet
Klaytn: Baobab Testnet
Choose your preferred network below. In this tutorial, you are expected to deploy the test contract to Kovan test network. Input your Ethereum Address to the form below, then click Claim your tokens
below. The prompt will show up and require you to pass the CAPTCHA process.
The faucet is required to keep a certain amount of balance to maintain the service, so please consider donating your remaining testnet Ether after finishing your testing and troubleshooting.
Please note that you cannot request to claim your testnet Ether once a day per network due to the limited resources as stated above.
You are shown succeeded transaction hash after clicking Claim your tokens button. To see the network status, please click the given link to the Etherscan address. When it comes to Kovan testnet, you are given 0.1 Kovan testnet Ether from the Faucet.
The amount you are given could be differed from each network according to current status and remaining balance.
you should manually enable the option to show test network. If you click Show/hide test networks
button, you are shown the wallet preferences that can turn on seeing test networks.
After turning the option, you can now view the activated testnets if pressing the Ethereum Mainnet
oval icon at the right top. Please toggle to Kovan Test Network
to access the net. You will be able to see some micro-amount of testnet Ether for testing your contract in the testnet.
We will use Hardhat
framework to set up the development project that helps to build Ethereum based project easily. To note, please name your Hardhat project you are now presently working on, as this tutorial gives a general setup guide.
We'll use the npm
CLI to install Hardhat. The Node.js package manager is a JavaScript package management and online repository.
Run the following commands in a new terminal.
Select Create a sample project
option to proceed.
Please note that if you are in Mac or Linux environment, Hardhat will automatically ask you to install Ethers.js and Waffle plugins that help you to interact with Ethereum blockchain.
If you are in Windows environment, you should manually install the dependencies. Please refer to the command below if you wishes to install the dependencies in a manual fashion.
Hardhat looks for the closest hardhat.config.js
file starting in the current working directory when it is launched. The hardhat.config.js
will be initialized as shown below.
You are recommended to install Solidity + Hardhat plugin in VSCode or Hardhat plugin in Intellij to render IDE to highlight the grammar syntax.
In this tutorial, we're going to make a basic smart contract that implements a transferrable token that follows ERC20 compliance. The most common purpose of token contracts is to trade or store value. The tutorial won't go through the contract's Solidity code in detail.
The contract will have the following logic.
The entire supply of tokens is fixed and cannot be modified.
The full supply is assigned to the contract's deployment address.
Tokens can be given to anyone.
Tokens can be transferred by anyone who has at least one token.
The token isn't divisible. You are allowed to transfer token in an Integer
fashion.
The ERC20 establishes a standard for Fungible Tokens, which means that each Token has a trait that makes it identical to another Token (in terms of type and value). An ERC-20 Token, for example, functions similarly to ETH, meaning that one Token is and will always be equal to all other Tokens.
We will use the OpenZeppelin framework to build the sample token that conforms to ERC20 standard. The OpenZeppelin Framework offers security technologies for developing, automating, and running decentralized applications. The framework is a library for creating safe smart contracts. Build on a strong foundation of code that has been verified by the community.
Install OpenZeppelin library using npm
or yarn
package manager to use the boilerplate contracts.
Once you have installed, create a new directory contracts
, and then a file ATNToken.sol
within that directory.
You are supposed to alter the file name to something more appropriate for you. Be sure that you have node_modules directory in root folder and it includes @openzeppelin/contracts
that you had installed earlier.
Note that contracts are used via inheritance, and we're using ERC20 for both the basic standard implementation and the optional extensions for name, symbol, and decimals.
The contract also creates an initialSupply of tokens, which will be assigned to the contract's deployment address. ERC20 utilizes a decimal value of 18 by default. You must override the decimals()
method in your contract to utilize a different number.
Unless you have a compelling reason not to, you'll generally want to utilize a de-facto standard that designates a decimals value of 18
, as Ether and other ERC20 token contracts do. You are expected to provide a number property 1000 * (10 ** 18)
when minting 1000 tokens.
To see how the code is organized, look at the OpenZeppelin documentation.
Run npx hardhat compile
in your terminal to compile the contract. Compile is an example of a built-in task.
When you compile your contracts, the contract was translated to the bytecode in order to operate on the Ethereum Virtual Machine (EVM) by full nodes. During compilation, the function names and input arguments are hashed. As a result, in order for another account to invoke a function, the function name and parameters must first be provided.
A list of the contract's functions and parameters is known as the ABI, Application Binary Interface in JSON format. The ABI is used by an account that wants to utilize a smart contract's function to hash the function specification and generate the EVM bytecode needed to invoke the function.
This is then included in a transaction's data field, and interpreted by the EVM at the destination account using the code. Each contract would have a ABI file, and ABI files are stored in artifacts directory in JSON type.
Create a new directory called test
inside our project root directory and create a new file called ATNToken-test.js
. The example test for the contract above is as follows.
To get further explanation, recommend to see medium article and official documentation.
On your terminal run npx hardhat test
. You should see the following output.
If you run test command without compiling contract precedingly, Hardhat automatically compiles the contract and testing the contract.
Hardhat is pre-installed with a local Ethereum development network. You may use it to deploy contracts, perform tests, and debug your code. It's the default network for Hardhat, so you don't have to do anything to make it function.
Create a new directory called scripts
inside our project root directory and create a new file called deploy.js
. The example test for the contract above is as follows.
Make sure that your contract name is to be the same as the parameter which is to be injected in
getContractFactory
. In addition, do not forget to use async/await pattern when running main function to properly handle potential errors.
Executing a script without the —network
flag causes the code to execute against an embedded instance of Hardhat Network. Please be aware that resulting in the deployment being terminated of a local network when Hardhat scripts end.
To deploy to a remote network such as a mainnet or any testnet, you need to add a network entry to your hardhat.config.js
file. We’ll use Kovan for this example.
To deploy to live network, you are required to export AllThatNode API KEY in a AllThatNode dashboard and wallet private key from Metamask.
Make sure your API KEY and private key aren't exposed to the internet (e.g. commit to the GitHub). Anyone with your private keys can steal any assets held in your account. In this tutorial, you are going to use dotenv
framework to hide your secrets and inject the information in runtime.
Go to AllThatNode site and click to protocols.
2. On protocol page, click Ethereum
and Create New Project
button.
3. Name the project with your preferred title and create new project.
4. Recheck your plan and create new project. AllThatNode is free for all developers and it supports 10,000 daily request per a day.
5. You will be able to see new Ethereum project on Dashboard. Click the Ethereum project.
6. Scroll down the page on Dashboard. You are able to check API Key and RPC URL. Click to copy the RPC URL of Kovan test network (the URL includes API Key)
We need to add the account on a hardhat configuration to use for deploying the contract. To access the wallet, your private key should be injected. Note again, be careful with your private key, it gives access to your wallet and will spend its testnet Ether to deploy the contract.
Open up your Metamask wallet extension, and click three dots at the upper right corner.
Click on the “Account Details” button.
Click “Export Private Key”.
Enter your password and click “Confirm”.
Your private key is revealed. Click to copy it, and save it somewhere safe.
Click “Done” to close the screen.
.env
Install dotenv
dependencies using npm or yarn library.
In your project's root folder, create a file .env
Open the .env
file and copy-paste the following contents:
Please note that .env file should not be revealed to anywhere (e.g. push it to GitHub)
3. Any file that loads the .env
file by placing at the top will have access to the variables in that file by using process.env
which implies hardhat.config.js
will now work and load the .env
variables and values correctly.
Finally, run the following command. If everything went well, you should see the deployed address.
Users dealing with smart contracts benefit from source code verification because it gives transparency. Etherscan will compare the compiled bytecode to the deployed bytecode on the blockchain when you submit the source code. A smart contract, should provide end users additional information about what they are "digitally signing" for and allow them to audit the code to independently verify that it performs what it says it does.
You are required to install additional dependencies to help your contracts to be verified at Etherscan. Run the command as the following.
If the installation is done, making a modest change to the file hardhat.config.js
. We must additionally load the module using the require()
method after the plugin has been installed. There should already be a require()
function call at the top of the file; all we have to do now is add require("@nomiclabs/hardhat-etherscan");
beneath it. Refer to the following code.
You are required to retrieve API Key from Etherscan. Sign up to Etherscan here and create your account. After logged into Etherscan, click your profile and scroll down to access API Key page.
In API Key page, you might be able to see +Add
button. Click the button to create new API Key.
After creating new API Key, click the Key to copy the secret. Then, open up your .env
file and paste your API Key on the file.
Then, add the Etherscan property on hardhat.config.js
file as follows.
We now have everything we need to validate our smart contract, having accomplished all of the previous processes of building a smart contract, a deployment script, and modifying the config file. Let's run following command to deploy our contract on the Kovan test network.
You should designate which network to be deployed on after --network
flag. The name should be pre-declared on the networks property at hardhat.config.js
.
That's all there is to it; congrats! With Hardhat and AllThatNode, you've created, deployed, and validated your own ERC20 smart contract!
You may use the same contract code on AllThatNode to deploy it on any other EVM-compatible chains, such as Polygon and Avalanche, in the same way you did on the Ethereum test network.