CREATE YOUR ERC20 TOKENS

Tech Geek
7 min readAug 3, 2018

THE DIFFERENCE BETWEEN COIN AND TOKEN

It’s important to keep in mind that there are 2 types of cryptocurrencies: one is a coin, and the other is a token. The main difference between them is that coins have their own blockchains, while tokens use the blockchain or some other backbone that has already been built.

WHAT IS AN ERC20 TOKEN?

Ethereum token standard, is a technical standard used for smart contracts on the Ethereum blockchain. When you create a token, it can be easily exchanged on the internet and backed up with a digital asset.

ERC20 Ethereum tokens are built on ERC (Ethereum Request for Comments). This is a process used in the Ethereum community to define and specify different standards. Once something is defined as a standard and qualifies as an ERC, it becomes a standard across the platform.

ERC20 tokens exist on the Ethereum platform. Ethereum itself consists of blockchains that are capable of storing transactions and a virtual machine (EVM) designed for running smart contracts.

Tokens live on the Ethereum blockchain. They benefit from its technology. Tokens are not independent and rely on Ethereum’s blockchain and platform. The native currency on Ethereum platform is Ether. Besides Ether, it can also support other tokens, which can work like currencies.

STRUCTURE OF THE ERC20 TOKEN

A token can be created by a smart contract. This contract is not only responsible for creating tokens, but also for managing transactions involving the token and keeping track of each tokens-holder’s balance.

ERC stands for Ethereum Request for Comments. ERC20 defines 6 obligatory functions your smart contract should implement, plus 3 optional ones.

Optional functions include:

  • name
  • symbols
  • decimals
1     string public constant name = "Token Name";
2 string public constant symbol = "SYMBOL";
3 uint8 public constant decimals = 18; // 18 is the most common number of decimal places

Mandatory ones are trickier:

  • Total supply — the amount of tokens that exist at the moment
  • Balance of — shows the balance for the address
  • Transfer — sends a certain amount of tokens to the address
  • Transfer from — used for exchanging tokens among users who have these tokens
  • Approve — verifies that your the wallet address is eligible to send tokens to another user
  • Allowance — shows whether or not a user has a balance sufficient to send tokens to someone else

These 6 functions are recognized and identified by other smart contracts. In a nutshell, the ERC20 token plays well with other smart contracts, and that’s why it is so popular.

// ----------------------------------------------------------------------------
2 // ERC Token Standard #20 Interface
3 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
4 // ----------------------------------------------------------------------------
5 contract ERC20Interface {
6 function totalSupply() public constant returns (uint);
7 function balanceOf(address tokenOwner) public constant returns (uint balance);
8 function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
9 function transfer(address to, uint tokens) public returns (bool success);
10 function approve(address spender, uint tokens) public returns (bool success);
11 function transferFrom(address from, address to, uint tokens) public returns (bool success);
12
13 event Transfer(address indexed from, address indexed to, uint tokens);
14 event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
15 }

THE CODE

pragma solidity ^0.4.24;
2
3 // ----------------------------------------------------------------------------
4 // 'FIXED' 'Example Fixed Supply Token' token contract
5 //
6 // Symbol : FIXED
7 // Name : Example Fixed Supply Token
8 // Total supply: 1,000,000.000000000000000000
9 // Decimals : 18
10 //
11 // Enjoy.
12 //
13 // (c) BokkyPooBah / Bok Consulting Pty Ltd 2018. The MIT Licence.
14 // ----------------------------------------------------------------------------
15
16
17 // ----------------------------------------------------------------------------
18 // Safe maths
19 // ----------------------------------------------------------------------------
20 library SafeMath {
21 function add(uint a, uint b) internal pure returns (uint c) {
22 c = a + b;
23 require(c >= a);
24 }
25 function sub(uint a, uint b) internal pure returns (uint c) {
26 require(b <= a);
27 c = a - b;
28 }
29 function mul(uint a, uint b) internal pure returns (uint c) {
30 c = a * b;
31 require(a == 0 || c / a == b);
32 }
33 function div(uint a, uint b) internal pure returns (uint c) {
34 require(b > 0);
35 c = a / b;
36 }
37 }
38
39
40 // ----------------------------------------------------------------------------
41 // ERC Token Standard #20 Interface
42 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
43 // ----------------------------------------------------------------------------
44 contract ERC20Interface {
45 function totalSupply() public constant returns (uint);
46 function balanceOf(address tokenOwner) public constant returns (uint balance);
47 function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
48 function transfer(address to, uint tokens) public returns (bool success);
49 function approve(address spender, uint tokens) public returns (bool success);
50 function transferFrom(address from, address to, uint tokens) public returns (bool success);
51
52 event Transfer(address indexed from, address indexed to, uint tokens);
53 event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
54 }
55
56
57 // ----------------------------------------------------------------------------
58 // Contract function to receive approval and execute function in one call
59 //
60 // Borrowed from MiniMeToken
61 // ----------------------------------------------------------------------------
62 contract ApproveAndCallFallBack {
63 function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
64 }
65
66
67 // ----------------------------------------------------------------------------
68 // Owned contract
69 // ----------------------------------------------------------------------------
70 contract Owned {
71 address public owner;
72 address public newOwner;
73
74 event OwnershipTransferred(address indexed _from, address indexed _to);
75
76 constructor() public {
77 owner = msg.sender;
78 }
79
80 modifier onlyOwner {
81 require(msg.sender == owner);
82 _;
83 }
84
85 function transferOwnership(address _newOwner) public onlyOwner {
86 newOwner = _newOwner;
87 }
88 function acceptOwnership() public {
89 require(msg.sender == newOwner);
90 emit OwnershipTransferred(owner, newOwner);
91 owner = newOwner;
92 newOwner = address(0);
93 }
94 }
95
96
97 // ----------------------------------------------------------------------------
98 // ERC20 Token, with the addition of symbol, name and decimals and a
99 // fixed supply
100 // ----------------------------------------------------------------------------
101 contract FixedSupplyToken is ERC20Interface, Owned {
102 using SafeMath for uint;
103
104 string public symbol;
105 string public name;
106 uint8 public decimals;
107 uint _totalSupply;
108
109 mapping(address => uint) balances;
110 mapping(address => mapping(address => uint)) allowed;
111
112
113 // ------------------------------------------------------------------------
114 // Constructor
115 // ------------------------------------------------------------------------
116 constructor() public {
117 symbol = "FIXED";
118 name = "Example Fixed Supply Token";
119 decimals = 18;
120 _totalSupply = 1000000 * 10**uint(decimals);
121 balances[owner] = _totalSupply;
122 emit Transfer(address(0), owner, _totalSupply);
123 }
124
125
126 // ------------------------------------------------------------------------
127 // Total supply
128 // ------------------------------------------------------------------------
129 function totalSupply() public view returns (uint) {
130 return _totalSupply.sub(balances[address(0)]);
131 }
132
133
134 // ------------------------------------------------------------------------
135 // Get the token balance for account `tokenOwner`
136 // ------------------------------------------------------------------------
137 function balanceOf(address tokenOwner) public view returns (uint balance) {
138 return balances[tokenOwner];
139 }
140
141
142 // ------------------------------------------------------------------------
143 // Transfer the balance from token owner's account to `to` account
144 // - Owner's account must have sufficient balance to transfer
145 // - 0 value transfers are allowed
146 // ------------------------------------------------------------------------
147 function transfer(address to, uint tokens) public returns (bool success) {
148 balances[msg.sender] = balances[msg.sender].sub(tokens);
149 balances[to] = balances[to].add(tokens);
150 emit Transfer(msg.sender, to, tokens);
151 return true;
152 }
153
154
155 // ------------------------------------------------------------------------
156 // Token owner can approve for `spender` to transferFrom(...) `tokens`
157 // from the token owner's account
158 //
159 // https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
160 // recommends that there are no checks for the approval double-spend attack
161 // as this should be implemented in user interfaces
162 // ------------------------------------------------------------------------
163 function approve(address spender, uint tokens) public returns (bool success) {
164 allowed[msg.sender][spender] = tokens;
165 emit Approval(msg.sender, spender, tokens);
166 return true;
167 }
168
169
170 // ------------------------------------------------------------------------
171 // Transfer `tokens` from the `from` account to the `to` account
172 //
173 // The calling account must already have sufficient tokens approve(...)-d
174 // for spending from the `from` account and
175 // - From account must have sufficient balance to transfer
176 // - Spender must have sufficient allowance to transfer
177 // - 0 value transfers are allowed
178 // ------------------------------------------------------------------------
179 function transferFrom(address from, address to, uint tokens) public returns (bool success) {
180 balances[from] = balances[from].sub(tokens);
181 allowed[from][msg.sender] = allowed[from][msg.sender].sub(tokens);
182 balances[to] = balances[to].add(tokens);
183 emit Transfer(from, to, tokens);
184 return true;
185 }
186
187
188 // ------------------------------------------------------------------------
189 // Returns the amount of tokens approved by the owner that can be
190 // transferred to the spender's account
191 // ------------------------------------------------------------------------
192 function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
193 return allowed[tokenOwner][spender];
194 }
195
196
197 // ------------------------------------------------------------------------
198 // Token owner can approve for `spender` to transferFrom(...) `tokens`
199 // from the token owner's account. The `spender` contract function
200 // `receiveApproval(...)` is then executed
201 // ------------------------------------------------------------------------
202 function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
203 allowed[msg.sender][spender] = tokens;
204 emit Approval(msg.sender, spender, tokens);
205 ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
206 return true;
207 }
208
209
210 // ------------------------------------------------------------------------
211 // Don't accept ETH
212 // ------------------------------------------------------------------------
213 function () public payable {
214 revert();
215 }
216
217
218 // ------------------------------------------------------------------------
219 // Owner can transfer out any accidentally sent ERC20 tokens
220 // ------------------------------------------------------------------------
221 function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
222 return ERC20Interface(tokenAddress).transfer(owner, tokens);
223 }
224 }

source : https://theethereum.wiki/w/index.php/ERC20_Token_Standard

--

--

Tech Geek

I’m a software developer from India, currently working with blockchain.