CryptoEx DeFi Yield Screener - Live Pool Analytics & 1-Click ZapCryptoEx DEX - Multi-Chain Decentralized Exchange

Creating Your First ERC-20 Token: A Step-by-Step Guide

Back to Blog

Introduction

Creating your first ERC-20 token is an exciting venture into the world of blockchain and cryptocurrency. ERC-20 tokens are fungible tokens built on the Ethereum blockchain, widely used for a variety of applications including fundraising, utility tokens, and more. In this tutorial, we will guide you through the entire process of creating your own ERC-20 token from scratch.

Learning Objectives

By the end of this tutorial, you will:

  • Understand the fundamental principles of ERC-20 tokens.
  • Gain familiarity with Solidity programming language.
  • Learn how to deploy your token on the Ethereum network.
  • Understand how to interact with your token using web3.js.

Syllabus

  1. Understanding ERC-20 Tokens
  2. Setting Up Your Development Environment
  3. Writing the Smart Contract
  4. Deploying the Smart Contract
  5. Interacting with Your Token

Understanding ERC-20 Tokens

ERC-20 is a technical standard used for smart contracts on the Ethereum blockchain for implementing tokens. It defines a common list of rules that all Ethereum tokens must adhere to, ensuring compatibility between the tokens and various platforms. Key functions of ERC-20 tokens include:

  • Transfer: Allows users to transfer tokens.
  • BalanceOf: Provides the balance of tokens held by an address.
  • Approve: Lets users permit another address to withdraw a specific amount of tokens.
  • TransferFrom: Facilitates the transfer of tokens from one address to another.

Setting Up Your Development Environment

Before you start coding, you'll need to set up your development environment. Follow these steps:

  1. Install Node.js: Download and install Node.js from the official website.
  2. Install Truffle: Use npm to install Truffle, a popular development framework for Ethereum.
  3. Install Ganache: Download Ganache, a personal Ethereum blockchain for deploying contracts and testing.
  4. Install MetaMask: Add the MetaMask extension to your browser for managing your Ethereum wallet.

Writing the Smart Contract

Now that your environment is set up, it’s time to write your ERC-20 token smart contract. Create a new file named MyToken.sol and add the following code:

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 code does the following:

  • Imports the ERC20 standard from OpenZeppelin, a library of secure smart contracts.
  • Defines a contract named MyToken.
  • Sets the token name to MyToken and the symbol to MTK.
  • Mints the initial supply of tokens to the contract's creator.

Deploying the Smart Contract

With the smart contract written, you can now deploy it to the Ethereum blockchain. Create a new migration script in the migrations folder named 2_deploy_token.js and add:

const MyToken = artifacts.require("MyToken"); module.exports = function(deployer) { deployer.deploy(MyToken, 1000000 * 10 ** 18); };

This script deploys your token with an initial supply of 1,000,000 tokens. Now, run the following command in your terminal:

truffle migrate --network development

This command deploys your contract to the local Ganache blockchain. Once the deployment is successful, you will see your contract address.

Interacting with Your Token

After deploying your token, you can interact with it using web3.js. First, install web3.js:

npm install web3

Next, create a new JavaScript file called interact.js and include the following code:

const Web3 = require("web3"); const MyToken = require("./build/contracts/MyToken.json"); const web3 = new Web3("http://localhost:7545"); const contract = new web3.eth.Contract(MyToken.abi, "YOUR_CONTRACT_ADDRESS"); contract.methods.totalSupply().call().then(console.log);

Replace YOUR_CONTRACT_ADDRESS with the address of your deployed token contract. Run the script to see the total supply of your tokens:

node interact.js

Exercise / Next Steps for Mastery

To master creating ERC-20 tokens, consider the following exercises:

  • Modify the token's initial supply and name.
  • Implement additional functions such as burn and mint.
  • Explore deploying your token to the Ethereum mainnet.

By practicing these exercises, you’ll deepen your understanding of Ethereum smart contracts and gain hands-on experience in blockchain development.

Conclusion

Congratulations! You've successfully created your first ERC-20 token and interacted with it on the Ethereum blockchain. This foundational knowledge opens the door to numerous possibilities in the world of decentralized finance (DeFi) and tokenization. Stay curious and keep exploring the exciting landscape of blockchain technology!

Join Community

24/7 Live

CryptoEx Official

Live DEX discussion, signals & support

Connect directly with traders across 63+ blockchains, get early listing updates and multichain swap news.

Share

More blog from here

Exploring the Best Multi-Chain DEX Platforms Available Today

Discover the best multi-chain DEX platforms of 2026. Uncover features, fees, and security. Start trading with confidence today!

How to Set Up a Crypto Wallet in Simple Steps

Learn to set up your crypto wallet easily with our step-by-step guide. Secure your digital assets today with CryptoEx!

Essential Smart Contract Risks Every Investor Should Know

Discover crucial smart contract risks and learn how to protect your investments. Read our comprehensive security guide now!

Understanding Ethereum Price Trends: A Deep Dive

Dive deep into Ethereum price trends for insights and strategies. Explore metrics, catalysts, and market implications today!

Beginner's Guide to Creating Your Own Crypto Token

Learn how to create your own crypto token easily. Step-by-step guide for beginners to launch your token today!