home Blockchain, Tutorial How to Create an ERC-20 Token on Ethereum

How to Create an ERC-20 Token on Ethereum

Creating your own cryptocurrency can seem like a daunting task, but with the right tools and guidance, you can create an ERC-20 token on Ethereum easily. This step-by-step guide will walk you through the process, from setting up your development environment to deploying your token on the Sepolia Test Network.

What You’ll Learn

  • How to set up a development environment for creating and deploying an ERC-20 token.
  • Step-by-step instructions on coding, compiling, and deploying your token.
  • How to verify the availability of your token name.

Prerequisites

Before we dive into the technical details, make sure you have the following:

  • Basic understanding of blockchain technology and smart contracts.
  • Node.js and npm installed on your computer.
  • A code editor, such as Visual Studio Code.
  • MetaMask or any other Ethereum wallet.
  • Test Ether from the Sepolia Test Network (you can get this from a faucet).

Step 1: Install Truffle and Ganache

Truffle is a development framework for Ethereum, and Ganache is a personal blockchain for Ethereum development. Open your terminal and run the following commands:

npm install -g truffle
npm install -g ganache-cli

Step 2: Set Up a Truffle Project

Create a directory for your project and initialize it with Truffle:

mkdir TechbreezeToken
cd TechbreezeToken
truffle init

Step 3: Create the Token Contract

In the contracts directory, create a new file named TechbreezeToken.sol and add the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract TechbreezeToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("Techbreeze Token", "TBZ") {
        _mint(msg.sender, initialSupply);
    }
}

This Solidity code defines an ERC-20 token named “Techbreeze Token” with the symbol “TBZ”.

Step 4: Install OpenZeppelin

OpenZeppelin is a library for secure smart contract development. Install it using npm:

npm install @openzeppelin/contracts

Step 5: Configure the Deployment Script

In the migrations directory, create a new file named 2_deploy_contracts.js:

const TechbreezeToken = artifacts.require("TechbreezeToken");

module.exports = function(deployer) {
    const initialSupply = web3.utils.toWei('1000000', 'ether'); // 1 million tokens with 18 decimals
    deployer.deploy(TechbreezeToken, initialSupply);
};

Step 6: Generate a Secure Mnemonic

A secure mnemonic (seed phrase) is crucial for managing your wallet and deploying contracts securely. Here’s how to generate a mnemonic:

Using a Secure Mnemonic for Your Wallet

  • Use a Reliable Wallet: Use a well-known and secure wallet like MetaMask or hardware wallets that provide a mnemonic phrase when creating a new wallet.
  • Manual Generation Using Libraries: You can generate a mnemonic using libraries like bip39 in JavaScript:
const bip39 = require('bip39');
const hdkey = require('hdkey');
const Wallet = require('ethereumjs-wallet').default;

// Generate a mnemonic phrase
const mnemonic = bip39.generateMnemonic();
console.log("Generated Mnemonic: ", mnemonic);

// Validate the mnemonic phrase
const isValid = bip39.validateMnemonic(mnemonic);
console.log("Is the mnemonic valid? ", isValid);

// Derive seed from mnemonic
const seed = bip39.mnemonicToSeedSync(mnemonic);
console.log("Derived Seed: ", seed.toString('hex'));

// Derive HD wallet using the seed
const hdWallet = hdkey.fromMasterSeed(seed);

// Derive the first account using the standard BIP44 path for Ethereum
const path = "m/44'/60'/0'/0/0";
const node = hdWallet.derive(path);
const wallet = Wallet.fromPrivateKey(node.privateKey);

// Get the address from the wallet
const address = `0x${wallet.getAddress().toString('hex')}`;
console.log("Ethereum Address: ", address);

Step 7: Install and Configure HDWalletProvider

Install the @truffle/hdwallet-provider package to handle wallet and provider functionalities:

npm install @truffle/hdwallet-provider

Step 8: Configure Truffle

In the truffle-config.js file, configure the networks and compiler settings:

const HDWalletProvider = require('@truffle/hdwallet-provider');
require('dotenv').config();

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 8545,
      network_id: "*", // Match any network id
    },
    sepolia: {
      provider: () => new HDWalletProvider(
        process.env.MNEMONIC,
        `https://eth-sepolia.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
      ),
      network_id: 11155111,
      gas: 5500000,
      confirmations: 2,
      timeoutBlocks: 200,
      skipDryRun: true
    }
  },
  compilers: {
    solc: {
      version: "0.8.21",
    }
  }
};

Create a .env file in the root directory of your project and add your mnemonic and Alchemy project ID:

MNEMONIC="your securely generated mnemonic here"
ALCHEMY_API_KEY="your alchemy api key here"

Step 9: Start Ganache

In a new terminal window, start Ganache CLI:

ganache-cli

Step 10: Deploy the Contract

Compile and deploy the contract to the local blockchain:

truffle compile
truffle migrate --network development

Step 11: Interact with the Contract

You can interact with your deployed contract using Truffle Console:

truffle console --network development

In the Truffle console, check the balance of your account:

let token = await TechbreezeToken.deployed();
let balance = await token.balanceOf(accounts[0]);
console.log(balance.toString());

Deploying Your ERC-20 Token on the Sepolia Test Network

  1. Get some test Ether from a Sepolia faucet:
    • Obtain test Ether from a Sepolia faucet and transfer it to the wallet address derived from the mnemonic phrase that you have added to the .env file. This ensures the account has sufficient funds to deploy the contract. You can use faucets like Sepolia Faucet 1 or Sepolia Faucet 2.
  2. Replace the MNEMONIC and ALCHEMY_API_KEY in your .env file.
  3. Deploy the contract:
truffle migrate --network sepolia

By following these steps, you now know how to create an ERC-20 token on Ethereum and deploy it on the Sepolia Test Network. This guide has covered all the essential aspects, from generating a mnemonic to interacting with your deployed token.

For more detailed guides and insights into blockchain development, visit our comprehensive blockchain tutorials. We offer a range of articles and resources to help you navigate the world of blockchain and cryptocurrency development.


Discover more from Techbreeze IT Solutions

Subscribe to get the latest posts sent to your email.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Techbreeze IT Solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading