Step-by-Step Guide to Launching an ERC20 Token on Sepolia via HardHat

Wiseman UmanahWiseman Umanah
7 min read

In this article, I will guide you step by step on how to deploy your first ERC20 token on Sepolia using Hardhat. Before we begin, it's important to set up a few things that will be crucial throughout the development process.

  • Ensure Node and npm are already installed. You can install them via the terminal or from their official website.

  • Have a MetaMask wallet.

  • Obtain Sepolia tokens, i.e., Ethereum faucets, for your wallet (you can find these using a Google web faucet).

Project Initialization and Setup

  • Create a new directory and name it whatever you like (e.g., token_creation). Then, in your terminal, change the directory to the one you just created.

  • Initialize npm in the directory using npm init. Without initializing npm, you won't be able to install or work with Hardhat. This step is very important.

  • Install Hardhat with npm install --save-dev hardhat. This command automatically installs Hardhat as a package.

      # Create new directory
      mkdir hardhat_tutorial
    
      # Change to the created directory
      cd hardhat_tutorial
    
      # Initialize npm
      npm init
    
      # Install hardhat packages
      npm install --save-dev hardhat
    

Your First Deployment on Your Local Network with HardHat

  • Initialize Hardhat by running: npx hardhat init. Choose your preferred project language; I'll choose TypeScript. Answer the other prompts with 'y' or 'n' and press Enter. Make sure to install the Hardhat dependencies, which is the last option. This automatically sets up our directory with all the necessary tools to develop, test, and deploy our contract.

    If you chose JavaScript, I will provide the equivalent code in TypeScript where necessary, so don't panic but then when I say .ts use .js.

      # Initialize Hardhat
      npx hardhat init
    

  • Start the local node in another terminal (i.e terminal 2) that’s in the same directory.

      # Start on another terminal (local network)
      npx hardhat node
    
    • Notice that some directories and files were automatically created by Hardhat in the directory. They all play an important role in developing your project, and we will discuss them shortly.

      contracts/: This directory is where you will write your Solidity smart contracts. You can create .sol files here, and they will be compiled by Hardhat. You can add your own contracts or modify any sample contracts.

      hardhat.config.ts : This is the main configuration file for your Hardhat project. It allows you to configure various settings for your Hardhat environment, such as network configurations, compiler settings, and plugins. You can specify which version of Solidity to use, set up different networks (like local, testnet, or mainnet), and include any Hardhat plugins you want to use.

      ignition : This folder contains the configuration and scripts that define how your smart contracts should be deployed. Hardhat Ignition allows you to write deployment scripts in a modular, declarative manner, making it easier to manage complex deployments, handle dependencies, and ensure consistent deployments across different environments (e.g., local, testnet, mainnet).

      test : This directory is where you write your test files. You can create TypeScript or JavaScript test files to ensure that your smart contracts behave as expected.

  • In contracts/, there is a default file called Lock.sol. Similarly, in ignition/modules/, we have Lock.ts or Lock.js created by Hardhat. We can compile this file, deploy it, and test it using the scripts in the test/ directory. These files and directories are the default ones created at the time of writing this article.

      # While your local node is still running in terminal 2
      # Compile the default smart contract
      npx hardhat compile
    
      # Test the script for errors
      npx hardhat test
    
      # Deploy smart contract to local network
      npx hardhat ignition deploy ./ignition/modules/Lock.ts # for typescript
    

    Your output should look like this:

Deploying to Sepolia With our Custom Token

Now we’ll use the same process for creating our new token on Sepolia. Create the files Token.sol in contracts/ and Token.ts in ignition/modules/, and copy the code for these files from my GitHub (Token.sol and Token.ts). You can delete the Lock.sol and Lock.ts files since they are no longer needed. Also, stop the local network by CTRL + C. You should also set your own token name, token symbol, decimals and initial supply in Token.ts

  • Recompile the contract with npx hardhat compile

  • Now in hardhat.config.ts, copy and paste the code below for your specific language. Take a look at the code and notice that some keys must be saved as constant variables somewhere.

    TypeScript:

    ```typescript import {HardhatUserConfig, vars} from "hardhat/config"; import "@nomicfoundation/hardhat-toolbox";

    //https://sepolia.infura.io const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY"); const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY"); const SEPOLIA_RPC = vars.get("SEPOLIA_RPC");

const config: HardhatUserConfig = { solidity: "0.8.20", networks: { sepolia: { url: ${SEPOLIA_RPC}, accounts: SEPOLIA_PRIVATE_KEY ? [SEPOLIA_PRIVATE_KEY] : [], }, }, etherscan: { apiKey: ETHERSCAN_API_KEY, }, };

export default config;


    **JavaScript:**

    ```javascript
    const { vars } = require("hardhat/config");
    require("@nomicfoundation/hardhat-toolbox");

    // https://sepolia.infura.io
    const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY");
    const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY");
    const SEPOLIA_RPC = vars.get("SEPOLIA_RPC");

    const config = {
      solidity: "0.8.20",
      networks: {
        sepolia: {
          url: SEPOLIA_RPC,
          accounts: SEPOLIA_PRIVATE_KEY ? [SEPOLIA_PRIVATE_KEY] : [],
        },
      },
      etherscan: {
        apiKey: ETHERSCAN_API_KEY,
      },
    };

    module.exports = config;

If you have noticed, we have some configuration variable keys to set so let’s do that right away.

  • Create an Account with Etherscan and Obtain your Etherscan Api Key. Navigate to account setting»API Dashboard»Add

    • Add the API key to hardhat

        # Add configuration variable
        npx hardhat vars set ETHERSCAN_API_KEY
      

  • Get your Private Key from your metamask wallet.

    Open MetaMask»Account Details»Show Private Key»Copy Private Key.

    • Add your private key to hardhat. Do not expose your Private key as this could lead to fraudulent activities by attackers

        # Add configuration variable
        npx hardhat vars set SEPOLIA_PRIVATE_KEY
      

  • Sign up on the MetaMask Developer Page, create your API key, and select the Ethereum network. The link might already be formatted like this: https://mainnet.infura.io/v3/<api_key>. If not, create it yourself and replace <api_key> with your actual API key. Notice that the URL points to the Ethereum Mainnet, but we are working with Sepolia, which is Ethereum's test URL. Change "mainnet" to "sepolia," like this: https://sepolia.infura.io/v3/<api_key>.

    • Add RPC as configuration variable

  • You are ready to deploy to sepolia.

  • You have successfully created and deployed a token to Sepolia Network. Confirm that by copying the contract address to sepolia.etherscan.io to see the transactions and effects to the blockchain.

Verifying Your Contracts With HardHat

But wait, we deployed, but the contract is not verified on Sepolia. Why? It's great that everything deployed well and we can see the effects on the blockchain, but why should we verify our contracts?

  • For authenticity: Verification ensures that the deployed contract code matches the source code, providing transparency and trust to users and stakeholders.

  • For security reasons: By verifying contracts, developers can identify and address potential vulnerabilities, reducing the risk of exploits and ensuring the contract behaves as intended.

  • To make it open source: Verification allows the contract code to be publicly accessible, promoting collaboration, peer review, and innovation within the developer community. This openness can lead to improvements and increased trust in the contract's functionality.

Hardhat is the best tool for verifying contracts, and it only takes one step to complete the verification process.

Go back to your terminal and type

npx hardhat ignition deploy ignition/modules/Token.ts --network sepolia --verify

what happens? Contract verified? You can see the difference on your terminal and a link to confirm that the token have been verified on Sepolia Blockchain Explorer is included.

We deployed, verified, and confirmed this on the Sepolia blockchain explorer. Now, let's import our token into our wallet. Copy your token address (Deployed Address), go to your MetaMask Wallet, switch the network to Sepolia, and then select "Import Token."

Now our Sepolia token is in our wallet under the Sepolia Network. We can also perform basic transactions, like sending our token on the Sepolia Network.

The Sepolia network is Ethereum’s test network. Sepolia Tokens are not real tokens that you can swap or sell; they are free faucet tokens for testing and experimenting in a dedicated environment before deploying to the mainnet, which is the live deployment network.

As we conclude our journey of deploying an ERC20 token on the Sepolia network using Hardhat, we've successfully navigated through the essential steps of setting up our development environment, writing and compiling smart contracts, and deploying them to a test network. This process not only equips us with the technical skills needed for blockchain development but also provides a solid foundation for future projects on the Ethereum mainnet. By verifying our contracts and importing our tokens into a wallet, we've ensured transparency and security, paving the way for further experimentation and innovation in the decentralized world.


Complete Code for Cloning and Testing is Available

0
Subscribe to my newsletter

Read articles from Wiseman Umanah directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Wiseman Umanah
Wiseman Umanah

I'm WIseman Umanah, a developer 💻️ who loves to share my knowledge and experiences with others and I'm always looking for ways to improve my skills and learn new things. When I'm not working, you can find me drawing as an artist 🤗😀.