** Build your own cryptocurrency on Ethereum — Part 4 **
🚀 Unleash ERC-20 — Ethereum’s Game Changer! 🚀
Discover ERC-20, the Ethereum Request for Comment standard that defines fungible tokens. Fungible tokens ensure each part is identical; 1 ETH exchanged for another 1 ETH remains the same. NFTs, on the other hand, represent unique items.
Most tokens on Ethereum follow ERC-20, enabling easy support for various tokens in applications like Uniswap.
To dive into the exciting world of ERC-20, you’ll need MetaMask and Sepolia Testnet ETH. Once set up, let’s get started!
Writing the Smart Contract
Using Remix IDE, create a new contract file (e.g., LW3Token.sol) and import the ERC20 standard from OpenZeppelin.
Contract Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract LW3Token is ERC20 {
constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) {
_mint(msg.sender, 10 * 10 ** 18);
}
}
Breaking Down the Contract:
pragma solidity ^0.8.19;
: Specifies Solidity's compiler version. Latest versions offer new features and optimizations.import "
https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol
";
: Imports ERC-20 token standard from OpenZeppelin (OZ), a trusted Ethereum security company. Avoid rewriting the standard from scratch - use OZ's reference contracts.contract LW3Token is ERC20 { ... }
: Defines a new contract, LW3Token, extending the ERC20 standard. It grants access to ERC20's built-in functions, allowing custom logic on top.constructor(string memory _name, string memory _symbol) ERC20(_name, _symbol) { ... }
: Constructor function called during contract deployment. Takes user input _name and _symbol (e.g., Ethereum, ETH). Immediately calls ERC20's constructor, initializing the smart contract with provided values._mint(msg.sender, 10 * 10 ** 18);
: Internal function _mint within ERC20 contract. It mints tokens to msg.sender (contract deployer). 10 * 10 ** 18 represents 10 full LW3Tokens due to Solidity's lack of floating-point support for decimals.
🎯 Congratulations! You’ve mastered ERC-20 essentials! 🎯
Compiling & Deploying:
Compile your contract in Remix, selecting LW3Token.sol. Head to the Deploy and Run Transactions tab with MetaMask connected to Sepolia Testnet.
Select LW3Token.sol, enter _name and _symbol for your cryptocurrency, and transact to deploy!
Copy the contract address from Deployed Contracts and find it on Sepolia Etherscan.
Viewing Tokens in MetaMask
Although you’ve minted tokens, they won’t appear automatically in MetaMask. To fix this:
Copy your contract address.
Open MetaMask, click Import Tokens in Assets.
Enter your Token Contract Address to detect name and decimals.
Click Add, and your balance will show!
Congratulations on deploying your own ERC20 token! Exciting possibilities await! 🎉🌟
Subscribe to my newsletter
Read articles from Awais Sajid directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Awais Sajid
Awais Sajid
As a Cybersecurity student passionate about Hacking and Blockchain security, I strive to develop innovative solutions to protect against emerging threats.