---
# System prepended metadata

title: 'Creating and Testing an ERC20 Token: A Step-by-Step Guide'

---

# Creating and Testing an ERC20 Token

## Introduction
As weeks go by, the journey is becoming more intense and interesting. we have been Learning new concepts, getting familiar with blockchain terminologies, and mastering essential tools.

This week, we transitioned from using Remix IDE a web-based development environment to Hardhat, a more powerful and flexible framework for building and testing smart contracts locally. Hardhat provides better control over deployments, debugging, and automation, making the development process smoother and more efficient.

Our focus this week was primarily on implementing ERC-20 tokens from scratch and testing smart contracts like a pro. I will walk you through how to get started with Hardhat on your local machine and dive deeper into ERC-20 implementation!

### **Installing Hardhat**
To begin using Hardhat, you need to install it on your local machine. Follow these steps:

1. **Check if Node.js is installed in VS Code**: Open your terminal in VS Code and run the following command:
   ```sh
   node --version
   ```
    This will display the installed Node.js version. If     it's not installed, you can download and install it     from the [Node.js official site](https://nodejs.org/).
    
   
2. **Initialize a new project**: Create a new folder and run:
   ```sh
   mkdir ERC20_project 
   cd ERC20_project 
   npm init -y
   ```
3. **Install Hardhat**: Run the following command to install Hardhat as a development dependency:
   ```sh
   npm install --save-dev hardhat
   ```
4. **Set up Hardhat**: Initialize Hardhat in your project by running:
   ```sh
   npx hardhat init
   ```
   Follow the prompts to create a new Hardhat project.
5. **Install dependencies**: To compile and test smart contracts, install the necessary dependencies:
   ```sh
   npm install --save-dev @nomicfoundation/hardhat-toolbox
   ```

Now, you're ready to start developing and testing your ERC-20 smart contracts with Hardhat!

## **Phase 1: Building an ERC20 Token Contract**

The first step in our journey was creating an ERC20 token called **BlockLab Token (BLT)**. This was done using OpenZeppelin’s ERC20 library, which provides a secure and battle-tested implementation of the ERC20 standard.

### **BlockLab Token Contract**
![F2A4EA43-A5AC-437B-875A-3D85FDD529DF_1_201_a](https://hackmd.io/_uploads/SyyCkaEKkl.jpg)


### **Key Features of BlockLab Token**
1. **Token Initialization:** The constructor initializes the token with the name "BlockLab Token" and symbol "BLT".
2. **Minting:** The contract owner can mint additional tokens.
3. **Initial Supply:** The contract mints an initial supply of 100,000 BLT tokens to the deployer.

## **Phase 2: Building a Savings Contract for ERC20 Tokens**

Once our ERC20 token was created, we built a contract to allow users to **deposit and withdraw** their tokens. This contract, called `saveERC20`, interacts with any ERC20 token, including our **BlockLab Token (BLT)**.

### **saveERC20 Contract**
![FF42FF79-4BF2-41F9-8AA1-F4CDC1E1FC4D_1_201_a](https://hackmd.io/_uploads/HyJd034F1e.jpg)


### **Key Features of saveERC20**
1. **Deposits:** Users can deposit ERC20 tokens into the contract.
2. **Withdrawals:** Users can withdraw their deposited tokens.
3. **Balance Tracking:** The contract maintains user balances.
4. **Secure Transactions:** The contract ensures users have enough tokens before allowing deposits and withdrawals.

## **Testing Our ERC20 Token**

Once we built our ERC20 token contract, we learned how to **test** it using Hardhat. This is crucial to ensure that the token functions as expected.

### **Testing with Hardhat**
We wrote several test cases to verify the ERC20 functionalities, such as:
- Checking initial token supply
- Transferring tokens between accounts
- Approving and transferring tokens via `transferFrom`
- Checking token name, symbol, and decimals


![BF778189-03E2-487D-9114-E519FE9634BA_1_201_a](https://hackmd.io/_uploads/rkDETh4KJx.jpg)




## **Conclusion**
This marks another step forward in our blockchain journey. See you next week! 🚀

