0% found this document useful (0 votes)
11 views5 pages

Practical No 7BLC

The document provides an overview of ERC20 tokens, which are standards for creating fungible tokens on the Ethereum blockchain. It outlines the purpose, key features, and structure of an ERC20 token contract, including mandatory and optional functions. Additionally, it includes a Java code example demonstrating the creation and functionality of an ERC20 token, concluding with a successful creation of the token.

Uploaded by

weginwarsohum111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

Practical No 7BLC

The document provides an overview of ERC20 tokens, which are standards for creating fungible tokens on the Ethereum blockchain. It outlines the purpose, key features, and structure of an ERC20 token contract, including mandatory and optional functions. Additionally, it includes a Java code example demonstrating the creation and functionality of an ERC20 token, concluding with a successful creation of the token.

Uploaded by

weginwarsohum111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No 5

AIM : Write a program for Creating ERC20 token


Theory:

What is ERC20?

An ERC20 token is a standard used for creating and issuing smart contracts on the
Ethereum blockchain. Smart contracts can then be used to create smart property or
tokenized assets that people can invest in. ERC stands for Ethereum request for
comment,and the ERC20 standard was implemented in 2015

ERC20 is a technical standard used for smart contracts on the Ethereum blockchain to
implement fungible tokens. "ERC" stands for Ethereum Request for Comment, and "20"
is the proposal identifier.

ERC20 defines a common set of rules for how tokens behave, allowing compatibility with
wallets, exchanges, and dApps (decentralized applications).

Purpose of ERC20 Tokens

ERC20 tokens can represent:

 Currency-like assets (e.g., USD Coin, DAI)


 Voting rights in DAOs
 Points in reward systems
 In-game assets or credits

They are widely used for crowdfunding (ICOs), DeFi, NFT marketplaces (for payment
tokens), and more.

Key Features of an ERC20 Token

The ERC20 standard defines six mandatory functions and three optional metadata
functions:

Function Description
totalSupply() Returns the total number of tokens in existence.
balanceOf(address) Returns the token balance of a specific address.
transfer(address, uint256) Transfers tokens from the sender to another
address.
approve(address, uint256) Allows another address to spend a certain amount
of tokens on the sender’s behalf.
transferFrom(address, address, Transfers tokens on behalf of someone else (used
uint256)
with approve).
allowance(address, address) Returns how much an address is allowed to spend
on behalf of another.

Optional functions include:

 name(): The name of the token (e.g., "MyToken").


 symbol(): Token ticker (e.g., "MYT").
 decimals(): The number of decimal places (usually 18 like Ether).

� Structure of an ERC20 Token Contract

An ERC20 token contract typically includes:

 Token metadata (name, symbol, decimals)


 Variables to track balances and allowances
 Functions to transfer, approve, and query token data
 Events like Transfer and Approval to notify clients (wallets, dApps)

Code:

import java.util.HashMap;

import java.util.Map;

public class ERC20Token {

private String name;

private String symbol;

private int decimals;

private Map<String, Integer> balances;

public ERC20Token(String name, String symbol, int decimals) {

this.name = name;

this.symbol = symbol;

this.decimals = decimals;

this.balances = new HashMap<>();


}

public void transfer(String from, String to, int amount) {

int balance = balances.getOrDefault(from, 0);

if (balance < amount) {

System.out.println("Insufficient balance");

return;

balances.put(from, balance - amount);

balances.put(to, balances.getOrDefault(to, 0) + amount);

System.out.println("Transfer successful: " + amount + " from " + from + " to " +
to);

public int balanceOf(String address) {

return balances.getOrDefault(address, 0);

public String getName() {

return name;

public String getSymbol() {

return symbol;

}
public int getDecimals() {

return decimals;

public static void main(String[] args) {

ERC20Token token = new ERC20Token("MyToken", "MTK", 18);

// Set initial balances

token.balances.put("Alice", 1000);

token.balances.put("Bob", 500);

token.balances.put("Charlie", 200);

// Perform some transfers

token.transfer("Alice", "Bob", 200);

token.transfer("Charlie", "Alice", 100);

token.transfer("Bob", "Charlie", 50);

// Print final balances

System.out.println("Final Balances:");

System.out.println("Alice: " + token.balanceOf("Alice"));

System.out.println("Bob: " + token.balanceOf("Bob"));

System.out.println("Charlie: " + token.balanceOf("Charlie"));

}
Output:

Conclusion: Successfully Created ERC20 token.

You might also like