Ethereum Token Development for Beginner Web3 Developers

This blog offers a simple, step-by-step guide for beginners in web3 development on how to develop and deploy an ERC-20 token on the Ethereum blockchain using OpenZeppelin, Hardhat, Infura, and MetaMask, including deployment to the Linea testnet

by Kingsley OkonkwoMay 21, 2024
Ethereum Token Development for Beginner Web3 Developers

As a beginner in web3 development, developing and deploying your own token is one of the most popular ways to get started building on-chain software, improve your Solidity skills and understand the web3 development lifecycle.

In Ethereum, there are various token standards, which are the predefined set of rules that tokens must follow to ensure compatibility, compliance, and interoperability with the Ethereum network and other dapps.

These standards include ERC-20 for fungible tokens, ERC-721 for non-fungible tokens (NFTs), ERC-1155 for multi-token types, ERC-777 for advanced token functionalities, ERC-1400 for security tokens, and ERC-4626 for tokenized vaults.

In this blog, we will develop an ERC-20 token by leveraging OpenZeppelin, a library that provides secure and tested implementations of standard smart contract templates. We will walk through setting up your development environment, writing the token contract, and deploying it on the Linea layer 2 network.

Step 1: Set up your development environment with Hardhat


Before you start developing your Ethereum token, you'll need to set up your development environment. In this blog, we will be using Hardhat. Follow the steps below:
  1. Install Node.js and npm: Node.js is a JavaScript runtime, and npm is its package manager. Download and install them from the official Node.js website.

  2. Create a new Hardhat project: Open your terminal, create a new directory for your project, and navigate into it. Then, run:


mkdir my-token

cd my-token

npm init -y

npm install --save-dev hardhat

npx hardhat

  1. Install necessary Hardhat plugins and OpenZeppelin contracts:

npm install @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts

Step 2: Create your Ethereum token


Follow these steps to create a simple ERC-20 token:
  1. Create a token contract: In your Hardhat project, navigate to the contracts directory and create a new file named MyToken.sol. Open this file and define your token contract:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {

    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {

        _mint(msg.sender, initialSupply);

    }

}

This smart contract creates an ERC-20 token named "MyToken" with the symbol "MTK" using OpenZeppelin's library. When deployed, it mints an initial supply of tokens specified by the deployer and assigns them to the deployer's address.

  1. Write a deployment script: In the scripts directory, create a new file named deploy.js and add the following code to deploy your contract:

async function main() {

  const [deployer] = await ethers.getSigners();

  console.log("Deploying contracts with the account:", deployer.address);

  const MyToken = await ethers.getContractFactory("MyToken");

  const myToken = await MyToken.deploy(1000000);

  console.log("MyToken deployed to:", myToken.address);

}

main()

  .then(() => process.exit(0))

  .catch((error) => {

    console.error(error);

    process.exit(1);

  });

The above script deploys the "MyToken" smart contract using Hardhat and ethers.js, initializing it with a supply of 1,000,000 tokens. It retrieves the deployer's address and deploys the contract.

  1. Compile and deploy your contract locally: In your terminal, compile and deploy your contract to a local blockchain provided by Hardhat:

npx hardhat compile

npx hardhat run scripts/deploy.js --network localhost

Step 3: Deploy your token contract to Linea testnet


Now that we have developed and deployed our ERC-20 token locally, it's time to deploy it to the Linea testnet using Infura and MetaMask. Follow these steps to get your token live on the Linea testnet.
  1. Set up an Infura account: Infura provides easy access to the various blockchain networks and their testnets. Create a new project, select Linea Sepolia testnet from the menu of supported networks, and copy out the project ID.

  2. Configure MetaMask: Open your MetaMask wallet and navigate to the network section. In the dropdown menu, ensure that “show test networks” is toggled on. Then select Linea Sepolia testnet.

  3. Get Linea Sepolia ETH: When deploying your contract, you need Linea Sepolia ETH to pay for gas fees. You can get some Linea ETH by visiting the official Linea Sepolia faucet.

  4. Update Hardhat configuration: In your Hardhat project, update your hardhat.config.js to include the Linea testnet configuration:


require("@nomiclabs/hardhat-ethers");

require("dotenv").config();

module.exports = {

  solidity: "0.8.0",

  networks: {

    lineaTestnet: {

      url: `https://linea-sepolia.infura.io/v3/${process.env.INFURA_PROJECT_ID}`,

      accounts: [`0x${process.env.PRIVATE_KEY}`],

      chainId: 59141

    }

  }

};

Ensure your .env file contains the necessary environment variables:


INFURA_PROJECT_ID=your_infura_project_id

PRIVATE_KEY=your_private_key
  1. Deploy your contract to Linea Sepolia testnet

Now, to deploy your token contract to the Linea testnet, run this command:


npx hardhat compile

npx hardhat run scripts/deploy.js --network lineaTestnet

This script will deploy your ERC-20 token contract to the Linea testnet using the account connected to MetaMask.

Advance your web3 dapp development with MetaMask Developer


By mastering the basics of Ethereum token development, setting up your development environment, and adhering to best practices, you can successfully develop and deploy your own Ethereum token. Whether creating a new cryptocurrency or tokenizing an asset, Ethereum offers the tools and ecosystem to bring your ideas to life.

Visit the MetaMask Developer portal for more advanced development needs, such as Sign-in-with-Ethereum (SIWE), support for EIP-6963, and seamless integration with MetaMask via Snaps. It offers a comprehensive suite of tools, APIs, and SDKs designed to streamline user onboarding and enable developers to forge unique, permissionless wallet connections and in-dapp experiences tailored for MetaMask users.

Receive our Newsletter