👨💻
How to Create Your Own ERC-20 Token

ERC-20 contract with Hardhat and OpenZeppelin

ERC-20 is a standard interface for tokens.
With an ERC-20 contract, you can deploy tokens.
With our tutorial, you will learn how to:
  • Create an ERC-20 contract.
  • Deploy the contract on the Rinkeby(testnet).

Preparations

  • Hardhat development environment to compile, deploy, test, and debug your contract.
    # Hardhat Installation
    npm install --save-dev hardhat
    To use your local installation of Hardhat, you need to use npx to run it in advance.
    npx hardhat
  • OpenZeppelin Contracts library for secure smart contract development.
    ## Openzeppelin Installation
    npm install @openzeppelin/[email protected]

Overview

  1. 1.
    Create an ERC-20 project
  2. 2.
    Compile and deploy on Rinkeby(Testnet)

Step by Step

Create your ERC-20 contract project

Create Hardhat project
# create a sample contract demo
npx hardhat
Then you will see a welcome message and following options in your terminal.
888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888
👷 Welcome to Hardhat v2.8.2 👷‍
? What do you want to do? …
Create a basic sample project
Create an advanced sample project
Create an advanced sample project that uses TypeScript
❯ Create an empty hardhat.config.js
Quit
Select Create an empty hardhat.config.js and create two folders in your terminal command line:
mkdir contracts
mkdir scripts
  • contracts/ is where we’ll keep our ERC-20 smart contract code
  • scripts/ is where we’ll keep scripts to deploy and interact with our smart contract
This will generate Hardhat contract demo with the following structure:
Now create BVExampleToken.sol in the folder contracts and deploy.js in the folder scripts.
Notice: Our contract file - you must match the name of your smart contract file with the name of your token, so if you choose BVExampleToken, you must name your contract file BVExampleToken.sol. Now your project's structure would be like:
.
├── contracts
│ └── BVExampleToken.sol
├── scripts
│ └── deploy.js
├── hardhat.config.js
└── package.json
Go to contracts/ directory and modify your ERC-20 contract here: BVExampleToken.sol
Now open the newly-created file and add following content:
BVExampleToken.sol
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.7.3;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract BVExampleToken is ERC20 {
uint constant _initial_supply = 100 * (10**18);
constructor() ERC20("BVExampleToken", "BV") public {
_mint(msg.sender, _initial_supply);
}
}
Install the library for your project
# install etherjs
npm install --save-dev @nomiclabs/hardhat-ethers '[email protected]^5.0.0'
# install dotenv
npm install dotenv --save
Create a file named .env in your root directory of the project and add your API URL and Metamask PRIVATE KEY to it. Your .env should look like this:
API_URL = "https://eth-rinkeby.blockvision.org/v1/your-api-key"
PRIVATE_KEY = "your-metamask-private-key"
Notice: Your Metamask Private Key MUST NOT be visible. Others may steal your token via your Private key.
Update your hardhat.config.js
/**
* @type import('hardhat/config').HardhatUserConfig
*/
require("dotenv").config();
require("@nomiclabs/hardhat-ethers");
const {API_URL, PRIVATE_KEY} = process.env;
module.exports = {
solidity: "0.7.3",
defaultNetwork: "rinkeby",
networks: {
hardhat: {},
rinkeby: {
url: API_URL,
accounts: [`0x${PRIVATE_KEY}`]
}
},
};

Deploy your ERC-20 contract

Go to scripts/ directory and modify your deploy script here: deploy.js.
Open the newly-created file and add following content:
deploy.js
async function main() {
const [deployer] = await ethers.getSigners();
console.log("Deploying contracts with the account:", deployer.address);
const weiAmount = (await deployer.getBalance()).toString();
console.log("Account balance:", (await ethers.utils.formatEther(weiAmount)));
// make sure to replace the "GoofyGoober" reference with your own ERC-20 name!
const Token = await ethers.getContractFactory("GoofyGoober");
const token = await Token.deploy();
console.log("Token address:", token.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Run on your terminal command line:
npx hardhat run scripts/deploy.js --network rinkeby
Your contract will be compiled and deployed to the Rinkeby network! You would see something similar to this in your terminal output:
Deploying contracts with the account: 0x42f58a73E8D1ABcde3D9ae90D7871515daE61c3b
Account balance: 0.115183382105021384
Token address: 0x31d911BF5945644115bD20382AcAdc399175E117
Check your token Info on EtherScan Rinkeby

Send your own Token

Now you can transfer your own ERC-20 token by importing your ERC-20 token's contract address to the Metamask and check your transaction's information on Etherscan Rinkeby
First go to your MetaMask and import your token with token address.
Copy and paste your token address
Now you can see your "BVExample" Token in your Metamask's Asset list.
After importing your token, you can send your token easily.

Conclusion

This tutorial guided you through the basics of creating and deploying token in the ERC-20 standard.
You also verified your deployed contract online and interacted with it.
This tutorial uses testnet, and the exact same instructions and sequence will work on the mainnet as well.
Now you have your own contract on Rinkeby, in the following document, you will learn how to fetch your data through BlockVision's ERC-20 API service.
Copy link
On this page
ERC-20 contract with Hardhat and OpenZeppelin
Preparations
Overview
Step by Step
Create your ERC-20 contract project
Deploy your ERC-20 contract
Send your own Token
Conclusion