How to create an ERC20 token
Simple summary of how to deploy your own Ethereum smart contract.
Prerequisites
- Node.js and npm installed
- Truffle framework installed
- MetaMask or another Ethereum wallet
- Test ETH for gas fees
Step 1: Install Dependencies
npm install -g truffle
npm install @openzeppelin/contracts
Step 2: Initialize Truffle Project
mkdir my-token
cd my-token truffle init
Step 3: Create ERC20 Token Contract
Create contracts/MyToken.sol:
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); } }
Step 4: Configure Migration
Create migrations/2_deploy_token.js:
const MyToken = artifacts.require("MyToken");
module.exports = function (deployer) { deployer.deploy(MyToken, 1000000); // Initial supply: 1,000,000 tokens };
Step 5: Configure Network
Update truffle-config.js with your network settings:
module.exports = {
networks: { ropsten: { provider: () => new HDWalletProvider(mnemonic, https://ropsten.infura.io/v3/YOUR_PROJECT_ID), network_id: 3, gas: 5500000, } } };
Step 6: Compile and Deploy
<h1 id="heading-8">Compile</h1>
truffle compile
<h1 id="heading-9">Deploy to testnet</h1> truffle migrate --network ropsten
Your ERC20 token is now deployed!
