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.
By the end of this tutorial, you will:
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:
Before you start coding, you'll need to set up your development environment. Follow these steps:
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:
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 developmentThis command deploys your contract to the local Ganache blockchain. Once the deployment is successful, you will see your contract address.
After deploying your token, you can interact with it using web3.js. First, install web3.js:
npm install web3Next, 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.jsTo master creating ERC-20 tokens, consider the following exercises:
By practicing these exercises, you’ll deepen your understanding of Ethereum smart contracts and gain hands-on experience in blockchain development.
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 LiveLive DEX discussion, signals & support
Connect directly with traders across 63+ blockchains, get early listing updates and multichain swap news.
Share
More blog from here
Discover the best multi-chain DEX platforms of 2026. Uncover features, fees, and security. Start trading with confidence today!
Learn to set up your crypto wallet easily with our step-by-step guide. Secure your digital assets today with CryptoEx!
Discover crucial smart contract risks and learn how to protect your investments. Read our comprehensive security guide now!
Dive deep into Ethereum price trends for insights and strategies. Explore metrics, catalysts, and market implications today!
Learn how to create your own crypto token easily. Step-by-step guide for beginners to launch your token today!