Setting up the ERC-20 Project
To setup a project that uses Hardhat for ERC-20 contract development, do the following:
-
Create a new directory and enter it.
mkdir my-token cd my-token
-
Initialize a new Node.js project.
npm init -y
The directory should contain a single file called
package.json
. -
Install Hardhat by running the following command in the
my-token
directory.npm install --save-dev hardhat
The
package.json
file will now have ahardhat
section underdevDependencies
. -
Create a Hardhat project by running the following command. Choose the Create an empty hardhat.config.js option.
npx hardhat
The directory will have a file called
hardhat.config.js
with the following contents./** @type import('hardhat/config').HardhatUserConfig */ module.exports = { solidity: "0.8.17", };
The number
0.8.17
specifies the version of the Solidity compiler. -
Install the dotenv package in your project directory.
npm install dotenv --save
-
Create a file called
.env
in the project directory with the following contents.API_URL = "https://eth-goerli.g.alchemy.com/v2/your-api-key" PRIVATE_KEY = "your-metamask-private-key"
Follow these instructions to export your private key from Metamask. The
API_URL
needs to be copied from your Alchemy account.NOTE: If you are going to push the project code to a public Github/Gitlab repository, remember to add the
.env
file to your.gitignore
. -
Install Ethers.js by running the following command
npm install --save-dev @nomiclabs/hardhat-ethers "ethers@^5.0.0"
-
Update the
hardhat.config.js
file to have the following content./** * @type import('hardhat/config').HardhatUserConfig */ require('dotenv').config(); require("@nomiclabs/hardhat-ethers"); const { API_URL, PRIVATE_KEY } = process.env; module.exports = { solidity: "0.8.17", defaultNetwork: "goerli", networks: { hardhat: {}, goerli: { url: API_URL, accounts: [`0x${PRIVATE_KEY}`] } }, }
-
Your ERC-20 token will be based on the implementation by OpenZeppelin. Install the Node.js package containing OpenZeppelin's contracts by running the following command in the project directory.
npm install @openzeppelin/contracts
The installed contracts can be found in the
node_modules
directory in your project directory. The path will benode_modules/@openzeppelin/contracts/
. We will be inheriting the ERC-20 implementation atnode_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol
.