0% found this document useful (0 votes)
50 views15 pages

UNIT 5 ERC Token

The document describes how to create an ERC20 token on the Ethereum blockchain. ERC20 is a technical standard that defines rules for writing smart contracts for fungible tokens on Ethereum. It specifies functions like totalSupply(), balanceOf(), transfer(), and approve() that allow tokens to be sent, received and tracked. The document provides sample smart contract code to implement an ERC20 compliant QKC token, including setting the initial supply, transferring tokens between addresses, and approving other contracts to transfer tokens.

Uploaded by

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

UNIT 5 ERC Token

The document describes how to create an ERC20 token on the Ethereum blockchain. ERC20 is a technical standard that defines rules for writing smart contracts for fungible tokens on Ethereum. It specifies functions like totalSupply(), balanceOf(), transfer(), and approve() that allow tokens to be sent, received and tracked. The document provides sample smart contract code to implement an ERC20 compliant QKC token, including setting the initial supply, transferring tokens between addresses, and approving other contracts to transfer tokens.

Uploaded by

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

ERC20 Token

How to Create an ERC20 Token


• ERC stands for Ethereum Request for Comment, and 20 is the
proposal identifier number. ERC-20 was designed to improve the ETH
network.
• ERC-20 is one of the most significant ERCs. It has emerged as the
technical standard for writing smart contracts on the Ethereum
blockchain network, used for token implementation. ERC-20 contains
a set of rules that all Ethereum based tokens must follow.
• ERC-20 defines tokens as blockchain-based assets that can be
sent/received and have value. ERC-20 tokens are similar to Bitcoin
and Litecoin in many aspects.
• However, the most significant difference is that instead of running on
their own blockchain network, ERC-20 coins run on Ethereum’s
blockchain network and use gas as the transaction fee.
• Before the emergence of ERC-20, everyone who created tokens had to reinvent the
wheel, which means all tokens were different from each other. For example, if a
developer wanted to work with another token, they had to understand the entire
smart contract code of that token due to the lack of any specific structure or guidelines
for building new tokens.
• This was particularly painful for wallets and exchange platforms -  adding different
types of tokens required developers to go through the code of each and every token
and understand it in order to handle those tokens on their platforms.
• Needless to say, it was rather difficult to add new tokens to any app. Today wallets and
exchanges use the ERC-20 standard to integrate various standardized tokens onto their
platforms and also facilitate easy exchange between ERC-20 tokens and other tokens.
The ERC-20 token standard has made interaction between tokens almost seamless and
painless.
• ERC-20 is a standard or guideline for creating new tokens. The standard defines six
mandatory functions that a smart contract should implement and three optional ones.
• To start you can give your token a name, a symbol, and mention how dividable your
token is, by specifying the decimals. ERC specifies a set of mandatory functions, which
are a bit more complex and listed below:

• totalSupply: A method that defines the total supply of your tokens, When this limit is
reached the smart contract will refuse to create new tokens.
• balanceOf: A method that returns the number of tokens a wallet address has.
• transfer: A method that takes a certain amount of tokens from the total supply and
gives it to a user.
• transferFrom: Another type of transfer method which is used to
transfer tokens between users.
• approve: This method verifies whether a smart contract is allowed to
allocate a certain amount of tokens to a user, considering the total
supply.
• allowance: This method is exactly the same as the approved method
except that it checks if one user has enough balance to send a certain
amount of tokens to another
• C20 standard defines a set of functions to be implemented by all ERC20 tokens so as to
allow integration with other contracts, wallets, or marketplaces. This set of functions is
rather short and basic.

• function totalSupply() public view returns (uint256);


• function balanceOf(address tokenOwner) public view returns (uint);
• function allowance(address tokenOwner, address spender)
• public view returns (uint);
• function transfer(address to, uint tokens) public returns (bool);
• function approve(address spender, uint tokens) public returns (bool);
• function transferFrom(address from, address to, uint tokens) public returns (bool);
• ERC20 functions allow an external user, say a crypto-wallet app, to find out a user’s balance and transfer funds from one user to another with

• The smart contract defines two specifically defined events:

• event Approval(address indexed tokenOwner, address indexed spender,


• uint tokens);
• event Transfer(address indexed from, address indexed to,
• uint tokens);
• These events will be invoked or emitted when a user is granted rights to withdraw tokens from an account, and after the tokens are actually tra

• In addition to standard ERC20 functions, many ERC20 tokens also feature additional fields and some have become a de-facto part of the ERC

• string public constant name;


• string public constant symbol;
• uint8 public constant decimals;
• Here are a few points regarding ERC20 and Solidity nomenclature:

• A public function can be accessed outside of the contract itself


• view basically means constant, i.e. the contract’s internal state will not be changed by the function
• An event is Solidity’s way of allowing clients e.g. your application frontend to be notified on specific occurrences within the contract
• Most Solidity language constructs should be clear if you already possess essential Java/JavaScript skills.
CREATE ERC-20 TOKEN
• deploy our contract on the Ropsten testnet. To get started, you will
need the Metamask browser extension to create an ETH wallet and
some test ETH, which you can get by going to the Ropsten faucet.
You'll need to select Ropsten Test Network on your Metamask wallet
and copy-paste the wallet address into the text field in the faucet,
then click Send me test Ether.
• pragma solidity ^0.4.24;

• //Safe Math Interface

• contract SafeMath {

• function safeAdd(uint a, uint b) public pure returns (uint c) {
• c = a + b;
• require(c >= a);
• }

• function safeSub(uint a, uint b) public pure returns (uint c) {
• require(b <= a);
• c = a - b;
• }


• function safeMul(uint a, uint b) public pure returns (uint c) {
• c = a * b;
• require(a == 0 || c / a == b);
• }

• function safeDiv(uint a, uint b) public pure returns (uint c) {
• require(b > 0);
• c = a / b;
• }
•}


• //ERC Token Standard #20 Interface

• contract ERC20Interface {
• function totalSupply() public constant returns (uint);
• function balanceOf(address tokenOwner) public constant returns (uint balance);
• function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
• function transfer(address to, uint tokens) public returns (bool success);
• function approve(address spender, uint tokens) public returns (bool success);
• function transferFrom(address from, address to, uint tokens) public returns (bool success);

• event Transfer(address indexed from, address indexed to, uint tokens);
• event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
•}


• //Contract function to receive approval and execute function in one call

• contract ApproveAndCallFallBack {
• function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
•}

• //Actual token contract

• contract QKCToken is ERC20Interface, SafeMath {
• string public symbol;
• string public name;
• uint8 public decimals;
• uint public _totalSupply;

• mapping(address => uint) balances;
• mapping(address => mapping(address => uint)) allowed;

• constructor() public {
• symbol = "QKC";
• name = "QuikNode Coin";
• decimals = 2;
• _totalSupply = 100000;
• balances[YOUR_METAMASK_WALLET_ADDRESS] = _totalSupply;
• emit Transfer(address(0), YOUR_METAMASK_WALLET_ADDRESS, _totalSupply);
• }

• function totalSupply() public constant returns (uint) {
• return _totalSupply - balances[address(0)];
• }

• function balanceOf(address tokenOwner) public constant returns (uint balance) {
• return balances[tokenOwner];
• }


• function transfer(address to, uint tokens) public returns (bool success) {
• balances[msg.sender] = safeSub(balances[msg.sender], tokens);
• balances[to] = safeAdd(balances[to], tokens);
• emit Transfer(msg.sender, to, tokens);
• return true;
• }

• function approve(address spender, uint tokens) public returns (bool success) {
• allowed[msg.sender][spender] = tokens;
• emit Approval(msg.sender, spender, tokens);
• return true;
• }

• function transferFrom(address from, address to, uint tokens) public returns (bool success) {
• balances[from] = safeSub(balances[from], tokens);
• allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
• balances[to] = safeAdd(balances[to], tokens);
• emit Transfer(from, to, tokens);
• return true;
• }

• function allowance(address tokenOwner, address spender) public constant returns (uint remaining) {
• return allowed[tokenOwner][spender];
• }

• function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
• allowed[msg.sender][spender] = tokens;
• emit Approval(msg.sender, spender, tokens);
• ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
• return true;
• }

• function () public payable {
• revert();
• }
•}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy