ERC20 for beginners

On the beginning of August, 2019 TrustNodes published that Ethereum have crossed the 200,000 smart contracts bar deployed on their blockchain, https://www.trustnodes.com/2019/08/06/ethereum-surpasses-200000-smart-contracts.


Most of the smart contracts deployed are ERC20 tokens and I thought it would be interesting to learn more about those.


At the time that I write this article (23 September 2019), there are 214,692 ERC20 (EtherScan.io Tracker: https://etherscan.io/tokens) smart contracts on the Ethereum blockchain.


ERC20 is a claim, a story – not a proof! In a blatant approach, I would call it a lie.


ERC20 is a standard written for tokens on the Ethereum blockchain. But, we need to know that no one checks the code of those tokens. So it might be that the code is correct and it does the things that we refer as ERC20, but, it might be that the code is not correct and that it does different things.


In this article we will learn how to look on different ERC20 tokens and learn on their solidity program (smart contract).


The ERC20 standard means that the token has, at least, the following functions:


totalSupply(), balanceOf(address _owner), transfer(address _to, uint256 _value), transferFrom(address _from, address _to, uint256 _value), approve(address _spender, uint256 _value), allowance(address _owner, address _spender).


Before we dive in, let me make several remarks:



  1. Solidity is a very good programing language. It is pretty simple to understand. In this article I will walk you through easy tools to understand the ERC20 token activity and to be able to decide if it suits your investment rules.

  2. https://etherscan.io/ is the place to explore the Ethereum blockchain.

  3. I will follow two ERC20 tokens, during this article: 1UP the Uptrennd token and A-Token (www.A-Token.org) which is the token that I have developed.

  4. In my culture the saying goes (my translation): "The strict man (or woman) cannot teach and the shy man (or woman) cannot learn" – feel free to ask, to investigate and to learn more.


Let us move a little further:


This is a sort of a bank ledger:































Account number

Balance in US$

10001

100

10020

345

….

….

….

….

13478

674


 


Any ERC20 token is primarily a list of Ethereum accounts that have some balance of this token. Very similar to the above bank ledger.


When you open EtherScan.io (in another window) and search for Uptrennd, you get a list of 1Ups tokens. The needed one is the one with the Uptrennd.com site connected to it.


When we open the 1UP token we can go to the "Holders" section:


#balances" target="_blank">https://etherscan.io/token/0x07597255910a51509ca469568b048f2597e72504#balances


(Please do open this link and see the resembles to the bank ledger)


Searching "A-Token(A)" will connect you to the A-Token. The "Holders" list is here:


#balances" target="_blank">https://etherscan.io/token/0xf0b3ba2dd4b2ef75d727a4045d7fbcc415b77bf0#balances


 


An account ledger in the bank - describes the activity within an account:


Account number 10001:



























































activity

Date

In

Out

Balance

Salary

1.9.2019

2000

 

2000

Internet

5.9.19

 

30

1970

Cellular

8.9.19

 

40

1930

Taxes

15.9.19

 

300

1630

…..

……

…….

……

……

…..

……

…..

…..

…..

…..

……

……

……

……


 


Each account has IN/OUT activities that make the current balance. The same goes with each token.


For example this account for the A-Token: https://etherscan.io/token/0xf0b3ba2dd4b2ef75d727a4045d7fbcc415b77bf0?a=0x4bb4e256d852607ecbceb3f351a8058818377d75


Or this account for the 1UP:


https://etherscan.io/token/0x07597255910a51509ca469568b048f2597e72504?a=0xa27b212fe1471865e44c9179bee5238ec7692517


That means that the fundamental functions needed to be programmed are the IN/OUT activities.


In order for us to check the solidity code of an ERC20 token we need to look for the green v check in the contract section of the smart contract on the EtherScan.io site.


#code" target="_blank">https://etherscan.io/address/0x07597255910a51509ca469568b048f2597e72504#code


is the link for the 1UP token and, it has the green v check that proves the code is an "exact match".


For the A-Token:


#code" target="_blank">https://etherscan.io/address/0xf0b3ba2dd4b2ef75d727a4045d7fbcc415b77bf0#code


and, of course, it has the green v that proves that the code is an "exact match".


Never work with any token that you cannot check its smart contract!


The fundamental activities (IN/OUT) can be covered with those functions that are a must in an ERC20 token:



  1. Transfer by the account owner.

  2. Approve that someone else is allowed to make a transfer.

  3. Then use this approved value with the transferFrom function.


The rest of the ERC20 functions are tools to get information from the smart contract.


So now, we will make a comparison of the ERC20 as written on both tokens. If they are doing the "right" activity then those are correct ERC20 tokens.


transfer(address _to, uint256 _value):


1UP code:


function transfer(address _to, uint256 _value) public returns (bool success) {


        require(balances[msg.sender] >= _value);


        balances[msg.sender] -= _value;


        balances[_to] = _value;


        emit Transfer(msg.sender, _to, _value);


        return true;


    }


A-Token code:


function transfer(address _to, uint256 _amount) external returns(bool success) {


                                require(_amount > 0);


                                require(_amount <= balances[msg.sender]);


                                require (_to != address(0));


                                balances[msg.sender] = balances[msg.sender].sub(_amount);


                                balances[_to] = balances[_to].add(_amount);


                                if(tokenHoldersMap[_to] != true) {


                                                tokenHolders.push(_to);


                                                tokenHoldersMap[_to] = true;


                                }


                                emit Transfer(msg.sender, _to, _amount);


                                return true;


                }


What is the goal of a transfer function? The function is moving tokens from the sender account to another (accepting) account. This is what we want that such a function will do:



  1. Check that the sender has enough tokens to send. Written in the 1UP:         require(balances[msg.sender] >= _value); written in the A-Token:                      require(_amount <= balances[msg.sender]);

  2. Subtract the proper value (amount) from sender account. Written in !UP: balances[msg.sender] -= _value; Written in A-Token:                     balances[msg.sender] = balances[msg.sender].sub(_amount);

  3. Add the proper value (amount) to the receiving account. Written in 1UP:         balances[_to] = _value; Written in A-Token:                                                     balances[_to] = balances[_to].add(_amount);


 


That's that!


 We can see that both functions do what is expected of them. Although written slightly different from each other, both function are ERC20 in their execution – moving tokens from sender account to the other (accepting) account.


(The A-Token code for this function adds new accounts to another list (database) that will be used on its' split function. The split function will multiply each account balance thus lowering the price of each A-Token. Same as shares splitting).


The following code:  balances[to] = _value; will not be an ERC20! Because it will not add the value to the right account. It is a tiny change in the code. And wallets and smart contracts can approach this function as an ERC20. But, and it is a big but – this is not an ERC20 function.


What we see here is that it is a must to check the claims that the token is an ERC20 one!


The benefit of the function having the same name and having the same arguments (even if carrying different arguments names) allow wallets and other smart contracts to interact with those functions using the same code. This is the main benefit of ERC20 tokens. And for this ability, to use the same coding on smart contracts and wallets this ERC20 standard was developed.


approve(address _spender, uint256 _value)


1UP code:


function approve(address _spender, uint256 _value) public returns (bool success) {


        require(balances[msg.sender] >= _value);


        allowed[msg.sender][_spender] = _value;


        emit Approval(msg.sender, _spender, _value);


        return true;


    }


A-Token code:


function approve(address _spender, uint256 _amount) external returns(bool success) {


                                require(_spender != address(0));


                                require(_amount > 0);


                                require(_amount <= balances[msg.sender]);


        allowed[msg.sender][_spender] = _amount;


                                emit Approval(msg.sender, _spender, _amount);


                                return true;


                }


Written the same and serve the genuine target of such ERC20 function, to allow a third party use of the account (part or all) tokens.


transferFrom(address _from, address _to, uint256 _value)


1UP code:


function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {


        uint256 allowance = allowed[_from][msg.sender];


        require(balances[_from] >= _value && allowance >= _value);


        balances[_to] = _value;


        balances[_from] -= _value;


        if (allowance < MAX_UINT256) {


            allowed[_from][msg.sender] -= _value;


        }


        emit Transfer(_from, _to, _value);


        return true;


    }


A-Token code:


function transferFrom(address _from, address _to, uint256 _amount) external returns(bool success) {


                                require(_from != address(0));


                                require(_to != address (0));


                                require(_amount > 0);


                                require(_amount <= balances[_from]);


                                require(_amount <= allowed[_from][msg.sender]);


                                balances[_from] = balances[_from].sub(_amount);


                                balances[_to] = balances[_to].add(_amount);


                                allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_amount);


                                if(tokenHoldersMap[_to] != true) {


                                                tokenHolders.push(_to);


                                                tokenHoldersMap[_to] = true;


                                }


                                


                                emit Transfer(_from, _to, _amount);


                                return true;


                }


Both tokens use a genuine ERC20 function that moves token from the right account to the right destination account and updates the allowance database with the proper amount (value).


totalSupply() 


1UP code:


A-Token code:


function totalSupply() external constant returns (uint) {


        return _totalSupply  - balances[address(0)];


    }


Weird and considered as a mistake, the totalSupply function is not part of the 1UP code. While this function is just an informative one, this might hurt future communication with other smart contracts or wallets and harm the token.


As we can see this does not prevent 1UP from being an ERC20 token.


balanceOf(address _owner)


1UP code:


function balanceOf(address _owner) public view returns (uint256 balance) {


        return balances[_owner];


    }


A-Token code:


function balanceOf(address _addr) external constant returns(uint256 balance) {


                                return balances[_addr];


                }


Both tokens use the same code and allow users, wallets and smart contracts interact and get information about an account balance.


 


allowance(address _owner, address _spender)


1UP code:


  function allowance(address _owner, address _spender) public view returns (uint256 remaining) {


        return allowed[_owner][_spender];


    }


A-Token code:


                function allowance(address _owner, address _spender) external constant returns(uint256 remaining) {


                                require(_owner != address(0));


                                require(_spender != address(0));


                                return allowed[_owner][_spender];


                }


We have covered the six functions that are needed to define an ERC20 token.


Is our job ended?!


NO.


After we have searched those six functions we need to check the other functions that are in the smart contract. Those other functions can do certain things that we might like or dislike.


Usually we do not like that the smart contact gives unique powers to preferred accounts. For example: a major NO is the ability of a smart contract manager to print more tokens. For example: a major NO is the ability of a smart contract manager to move tokens from other people accounts. Those examples can be written into an ERC20 token.


Thus we have to know the powers given to such accounts.


In the A-Token smart contract there is no unique power or increased ability to any unique account – all accounts have the same privileges.


In the 1UP smart contract there are unique powers that were set to the owner. I trust Jeff and the Uptrennd stuff, so, I am not worried about those powers. If I were to invest in 1Ups, this is what I would do, I would ask for answers to my questions before I invest.


It is beyond our article scope to learn about the extra functions written for both ERC20 tokens that we have used for this article. And, anyway, this article becomes too long and I believe it should be ended here.


Conclusions:



  1. ERC20 is a claim.

  2. To prove that a token meets ERC20 standards  – you have to check its' code.

  3. Never invest with a token that its' smart contract code is not an "exact match" on EtherScan.io.

  4. We only invest when we totally understand the ERC20 token smart contract.


 


 


Arik Schenkler


www.A-Token.org                         


About year 2000, some 400 years ago, Arik had developed a centralized electronic money called: InternetDollar.


Arik was known in this industry and well trusted.


With the development of the A-Token, Arik wishes to return to his cryptocurrency love.


Cryptocurrency is FREEDOM!


Do Your Idea!

COMMENT

4

ATMODEPTH #Indiegamedev
31 Oct

Ok, now i know why people like this post :-)

Very nice!

1 REPLY

COMMUNITY DETAILS

Ethereum

22299

Subscriber

1

Online

Ethereum is an open-source, public, blockchain-based distributed computing platform and operating system featuring smart contract functionality.

RELATED COMMUNITIES

Bitcoin

41278 subscribers

Bitcoin Cash

26581 subscribers

Litecoin

25998 subscribers

Cryptocurrency Mining

25129 subscribers

Trading & TA

22669 subscribers

Zcash

18122 subscribers

Uptrennd Optimization

15127 subscribers

© 2020 UpTrennd.com