** Build your own cryptocurrency on Ethereum — Part 4 **

Lesson Banner Image

🚀 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:

  1. pragma solidity ^0.8.19;: Specifies Solidity's compiler version. Latest versions offer new features and optimizations.

  2. 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.

  3. 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.

  4. 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.

  5. _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:

  1. Copy your contract address.

  2. Open MetaMask, click Import Tokens in Assets.

  3. Enter your Token Contract Address to detect name and decimals.

  4. Click Add, and your balance will show!

Congratulations on deploying your own ERC20 token! Exciting possibilities await! 🎉🌟