Launching Your Own Token with ERC-20
๐ What is an ERC-20 Token?
ERC-20 is the technical standard for fungible tokens on the Ethereum blockchain. These tokens:
Are interchangeable (1 token = 1 token)
Follow a standard set of functions
Can be used for currencies, utility tokens, governance, and more
๐งฐ What You Need
Tool Purpose
Node.js + npm JavaScript runtime and package manager
Hardhat Smart contract development environment
MetaMask Wallet for testing and deploying
Solidity Programming language for Ethereum
OpenZeppelin Secure prebuilt contract libraries
Ethereum network Testnet like Goerli, or local network
๐ ️ Step 1: Project Setup
1. Initialize Your Project
mkdir my-token
cd my-token
npm init -y
npm install --save-dev hardhat
npx hardhat
Choose “Create a basic sample project”.
๐ฆ Step 2: Install OpenZeppelin Contracts
npm install @openzeppelin/contracts
OpenZeppelin provides secure and battle-tested ERC-20 templates.
๐ Step 3: Create Your Token Contract
Inside the contracts/ folder, create MyToken.sol:
// 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 * 10 ** decimals());
}
}
Explanation:
"MyToken": Token name
"MTK": Token symbol
initialSupply: How many tokens you want at launch
๐งช Step 4: Create Deployment Script
Create scripts/deploy.js:
const hre = require("hardhat");
async function main() {
const MyToken = await hre.ethers.getContractFactory("MyToken");
const initialSupply = 1000000; // 1 million tokens
const token = await MyToken.deploy(initialSupply);
await token.deployed();
console.log(`Token deployed to: ${token.address}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
๐งฌ Step 5: Compile and Deploy
Compile:
npx hardhat compile
Run on Local Network:
npx hardhat node
In a second terminal, deploy:
npx hardhat run scripts/deploy.js --network localhost
To deploy on Goerli testnet, configure hardhat.config.js and use:
npx hardhat run scripts/deploy.js --network goerli
๐ Step 6: Interact with Your Token
After deployment, you can:
View it in MetaMask (add custom token with the contract address)
Send and receive tokens using MetaMask
Write a frontend with Web3.js or Ethers.js to connect to your token
✅ Key ERC-20 Functions (Built-in via OpenZeppelin)
balanceOf(address) – View balance
transfer(address to, uint256 amount) – Send tokens
approve(address spender, uint256 amount) – Allow another address to spend
transferFrom(address from, address to, uint256 amount) – Move allowed tokens
totalSupply() – Get total supply
๐ง Bonus: Make It More Advanced
You can easily extend your token by adding features like:
Burnable tokens
Mintable tokens
Access control (only owner can mint)
Capped supply
Example:
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, ERC20Burnable, Ownable {
constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
_mint(msg.sender, initialSupply * 10 ** decimals());
}
function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
๐ฆ Summary
Step Task
1️⃣ Set up Hardhat and project structure
2️⃣ Write your ERC-20 smart contract
3️⃣ Compile and deploy to Ethereum
4️⃣ Interact using wallet or frontend
5️⃣ Optionally add advanced features
Learn Blockchain Course in Hyderabad
Read More
NFT Marketplace Clone Step-by-Step
Create a Personal Blockchain on Python
Comments
Post a Comment