# Basic Smart Contracts

*Deploying Your Own ERC-20 Token*

By [The Web3 Book](https://paragraph.com/@theweb3book) · 2024-11-29

solidity, ethereum, web3, blockchain, smartcontracts

---

Let’s explore:

*   What ERC-20 tokens are.
    
*   How to create and deploy your own token.
    
*   Understanding functions like `transfer`, `approve`, and `allowance`.
    

**What is ERC-20 Token?**
=========================

*   \> It stands for “Ethereum Request for Comments.”
    
*   \> A cryptocurrency token on ETH that follows a specific standard set of rules.
    
*   \> All tokens are identical and interchangeable, just like 1USD = 1USD no matter where you are!
    

**Steps to Build:**
===================

1.  **Set up the environment** (preferably Remix IDE).
    
2.  **Write the ERC-20 contract** (using OpenZeppelin’s library).
    
3.  **Breaking it Down.**
    
4.  **Deploying on Sepolia Testnet**.
    
5.  **Test the token by transferring it between two accounts**.
    

**Step 1: Setup**
=================

1.  Visit [Remix IDE](https://remix.ethereum.org/).
    
2.  Create a new file called `Token.sol`.
    

**Step 2: Write the Contract**
==============================

Here’s the code:

    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    
    // Importing OpenZeppelin's ERC-20 library
    import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
    contract Token is ERC20 {
    
        // Constructor is run once when the contract is deployed
        constructor() ERC20("Web3Book", "W3B") {
    
            // That Mints 1000 tokens to the deployer's address
            // 1 Token = 10^18 smallest units (wei)
    
            _mint(msg.sender, 1000 * 10 ** decimals());
        }
    }

**Step 3: Let’s Break it Down.**
================================

1.  **Import OpenZeppelin’s Library**
    

*   `import "@openzeppelin/contracts/token/ERC20/ERC20.sol";`
    
*   This gives access to all the standard functions of an ERC-20 token (like `transfer`, `approve`, etc.), _no more reinventing the wheel!._
    

**2\. Contract Declaration**

*   `contract MyToken is ERC20 {}`
    
*   Specifies that our contract inherits all the ERC-20 functionalities from OpenZeppelin library.
    

**3\. Constructor**

*   `constructor() ERC20("Web3Book", "W3B") {}`
    
*   Setting the name (Web3Book) and symbol (W3B) of the token.
    

**4\. Minting Tokens**

*   `_mint(msg.sender, 1000 * 10 ** decimals());`
    
*   Mints 1000 tokens (with 18 decimals by default) to the deployer’s (our ) wallet when the contract is deployed.
    

**Step 4: Deploy on Sepolia Testnet**
=====================================

1.  **Compile the contract** in Remix.
    

*   Go to the “Solidity Compiler” tab and click **Compile Token.sol**.
    

**2\. Deploy** using Injected Provider (MetaMask).

*   Switch your MetaMask to the Sepolia network. **please!**
    
*   Go to the “Deploy & Run Transactions” tab.
    
*   Select **Injected Provider — MetaMask or Use WalletConnect to connect to Metamask** and then deploy the contract.
    

**3\. Minted Tokens**

*   Now Check your MetaMask wallet — 1000 W3B tokens will appear under the assets section!
    

_You’re rich now! (by knowledge ofcourse!)._

**Step 5: Test the Token**
==========================

1.  **Transfer Tokens**
    

*   Call the `transfer` function (with correct parameters!) to send tokens to another wallet.
    

**2\. Add Token to MetaMask**

*   Copy the contract address and add it in MetaMask as a custom token to view your token balance.
    

**Step 6: Understanding transfer(), approve() & allowance()**
=============================================================

**1\. transfer()**
==================

The `transfer` function is used to send tokens from your wallet to someone else's wallet.

**2\. approve()**
=================

The `approve` function allows someone (a spender) to spend a certain amount of your tokens on your behalf.

**3\. allowance()**
===================

The `allowance` function checks how many tokens a spender is allowed to spend on your behalf.

4\. balanceOf()
===============

It **returns the token balance** of a given Ethereum address.

This was it for your first very basic Smart Contract! 🎉

---

*Originally published on [The Web3 Book](https://paragraph.com/@theweb3book/erc20)*
