Join
May 28, 2026
Login

Building a smart world — Weekly smart contracts[ ERC20 Token ]

Building a smart world — Weekly smart contracts[ ERC20 Token ]

Ethereum smart contract using Solidity language

I would strongly recommend reading the below two Medium posts in sequence if you like to understand ERC20 token contract in depth.

Medium post 1 by Jim McDonald

Medium post 2 by Jim McDonald

What is ERC20 token?

Some quick facts about ERC20 Token

– Ethereum blockchain native token Ether [ETH] is not ERC20 Token
– ERC-20 token as such works only on Ethereum blockchain but can be migrated to other blockchains by taking a snapshot of token balance at any time on the Ethereum netork and migrating the same balance to another blockchain network. E.g. EOS
– All ERC20 addresses begin with 0x. If it doesn’t begin with 0x, then it is not an ERC20 address.
– All ERC20 addresses are also Ethereum addresses and can, therefore, receive Ether.
– You cannot pay transaction fees or gas cost in Ethereum network with your ERC20 token. You need ETH for that!
– ERC20 tokens are often created and marketed using ICOs, but they don’t have to be.

For all the development of token and testing, you need one dev tool called Remix which is free here

Token Code functions

The ERC20 interface code has the following mandatory functions which need to be implemented.

***a public function can be invoked from outside the token contract***

***view function basically means a constant function, i.e. the contract’s internal state will not be changed by the function

function totalSupply() public view returns (uint) :-
This function returns the total amount of tokens that exist in the Ethereum network at the moment.
function balanceOf(address tokenOwner) public view returns (uint balance) :-
This function returns the account balance of the token owner's account. It takes the address of the owner as a function parameter.
function allowance(address tokenOwner, address spender) public constant returns (uint remaining):-
This function returns amount with the token owner which a user/spender is allowed to spend/withdraw. It takes the address of the token owner and address of the user/spender as function parameters. Actually the amount returned is the amount set in approve function.
function transfer(address to, uint tokens) public returns (bool success) :-
This function sends specified amount of tokens to a address from the token contract. It takes the recepient address and the amount of tokens as function parameters. The function returns a boolean value which indicates the success or failure of the transaction.
function approve(address spender, uint tokens) public returns (bool success) :-
This function approves that the specified account address is eligible to spend the tokens specified. It takes address of the user which needs approval and the token amount to be approved for transfer/spend.The function returns a boolean value which indicates the success or failure of the approval.
function transferFrom(address from, address to, uint tokens) public returns (bool success) :-
This function transfers the amount of tokens from the 'from' address to the 'to' address. It takes from address, to address and the amount of tokens to be transferred as function parameters. The function returns boolean value which indicates the success or failure of the transaction.

Token Code events

***an event is how token contract let know the outside world or front-end apps to know changes triggered within the contract***

event Transfer(address indexed from, address indexed to, uint tokens) :-
This event gets triggered from the transfer and transferFrom function implementations.
event Approval(address indexed tokenOwner, address indexed spender, uint tokens) :- This event gets triggered from the approve function implementation.

The ERC20 interface code has the following optional variables which also have default function implementations in solidity and they return the variable values.

string public constant name = "Token Name" :-
name denotes the token name and can be any string data
string public constant symbol = "SYM" :-
symbol denotes the symbol used to represent token
uint8 public constant decimals = 18 :-
decimals denote the number of max decimal positions a token value can be displayed.

Some hints to understand the contract logic

mapping(address => uint256) balances;

The balances object will hold the token balance of each of the owner account addresses. This can be considered as a single dimension array of account address and their respective account balances.

mapping(address => mapping (address => uint256)) allowed;

The allowed object will hold all the account addresses which are approved to withdraw from a account address along with the balances approved to withdraw. This can be considered as a two dimensional array of account addresses

msg.sender ( address ): sender of the message (current call)   

We have many references to msg.sender which is a global variable reserved in Solidity in this contract. msg. sender will be the person who currently connecting with the contract. There are contracts connecting with other contracts. In that case, the contract that is creating the call would be the msg.sender.

balances[owner] = balances[owner].sub(numTokens);

We use SafeMath library to give a industrial level security for our smart contract. SafeMath is a Solidity library aimed at dealing with one way hackers have been known to break contracts: integer overflow attack. SafeMath protects against this by testing for overflow before performing the arithmetic action, thus removing the danger of overflow attack.

Hands-on using Solidity & Remix

Solidity is a contract-oriented, high-level language for implementing smart contracts. It was influenced by C++, Python, and JavaScript and is designed to target the Ethereum Virtual Machine (EVM).

You can write the code and compile and run the code all in a web-based IDE called Remix.

Remix is a powerful, open source tool written in JavaScript that helps you write Solidity contracts straight from the browser. You can open Remix IDE at remix.ethereum.org. You can find the remix documentation for the latest version here

Once you open the link above you are in the IDE. Click on the + icon which says ‘Create New File in the Browser Storage Explorer’. Give a file name you like with .sol extension. I have named MyToken.sol

Here is the full contract code you can copy paste to remix window. It is automatically saved.

Deploying and Executing the Smart Contract using Remix

Published at Mon, 10 Feb 2020 21:31:11 +0000

{flickr|100|campaign}

Previous Article

Using Azure Pipelines to Generate a .NET Package from a Solidity Contract’s ABI

Next Article

Matt Odell on Educating Bitcoin (BTC)ers

You might be interested in …