Developing Upgradeable ERC20 Smart Contract

Posted By : Neeraj Kumar | 30-Oct-2018

Introduction to Upgradeable ERC20 Smart Contract:
 

ERC20 is a token standard which contains functions and events that an Ethereum token contract has to implement. As soon as this contract gets deployed over the Ethereum network and gets instantiated a special transaction occur at node which assigns an address to the contract which now holds a state as a key-value pair onto the network mapped to the address of the contract. This address of the contract never changes for a deployed contract and all its state mapped to it only. Now, if the token contract needed an upgrade (i.e. changes in the source code as it was originally) it will have to be re-deployed and will receive a new state as mapped to a new ethereum contract address. Now the question arises how we will be able to interact with the upgraded contract if the contract address is changing in every update with new contract state, and the answer is, use a proxy contract address. Let’s see how this can be done.
 

Zeppelin-OS for developing upgradeable smart contracts:
 

Zeppelin OS: It’s a platform to develop, deploy and operate smart contract projects on Ethereum and every other EVM and eWASM-powered blockchain. Using Zeppelin-OS one can deploy a new version of the contract to the blockchain, allow other contracts to use them, link your project to EVM contracts already deployed and allow the usage of industry standard tested contracts of the zeppelin to be used with it.  Let’s start with developing one:
 

First, make sure that you have Node.JS and npm installed in your system, then after that let’s install zos globally,

    > $ npm install --global zos
 

And now let’s create a blank node Js project with  

> $ mkdir SampleUpgradeableTokenContract

    > $ cd SampleUpgradeableTokenContract

    > $ npm init --y
 

Now let’s zos initialize this project as zos project,

    > $ zos init  SampleUpgradeableTokenContract
 

Installing zos-lib and zeppelin-zos npm modules

    > $ npm install zos-lib openzeppelin-zos
 

Linking the standard openzeppelin library with you project, very important step if you want to use openzeppelin smart contracts in contracts in your project

    > $ zos link openzeppelin-zos
 

Now let’s write an upgradeable ERC20 smart contract
 

pragma solidity ^0.4.24;

import "zos-lib/contracts/migrations/Migratable.sol";
import "openzeppelin-zos/contracts/token/ERC20/StandardBurnableToken.sol";


contract UpgradeableERC20 is Migratable, StandardBurnableToken {

    string public name;
    string public symbol;
    uint8 public decimals;

    function initialize(address _sender, string _name, string _symbol, uint8 _decimals) 
    public 
    isInitializer("UpgradeableERC20", "0.1") 
    {
        require(_sender != address(0), "Null address not allowed.");
        require(bytes(_name).length > 0, "Name should be provided.");
        require(bytes(_name).length > 0, "Symbol should be provided.");
        require(_decimals >= 0, "Decimals should be a non-negative integer quantity.");

        // token parameters initialization
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        // calculating total supply value
        uint256 _totalSupplyValue = 1000000000 * (10 ** uint256 (decimals));
        
        // setting token's total supply
        totalSupply_ = _totalSupplyValue;

        // updating the token balance of the owner
        balances[_sender] = balances[_sender].add(_totalSupplyValue);
    }

    function() public payable {
        revert("This contract does not support recieving ether.");
    }
}

 

The migratable smart contract that we have extended from the zos-lib is the one responsible for handling the upgradeable life cycle of our smart contract by maintaining the version of smart contract in each update, internally zeppelin-os maintains a proxy contracts which has this smart contract set in the setter of the proxy contract and each time we update or existing smart contract the address of the new smart contract gets set in the setter of the proxy contract and all the state of this old smart contract gets migrated over to new updated one whereas the address of the proxy contract remains we are able to interact with it without needing to change the interacting contracts address.

 

Now let’s discuss how we can deploy this upgradeable Smart contract.
 

Add the contract to the project by

    > $ zos add UpgradeableERC20
 

Push the standard library onto your Ethereum network if you haven’t already

(Note:- Ropsten testnet and the mainnet already have this library pushed on them so no need to push it there.)
 

    > $ zos push --deploy-stdlib -- network <nameOfYourNetwork>
 

Now initialize your smart contract by giving the initializer arguments
 

    > $ zos create UpgradeableERC20 --init initialize --args <yourEthereumAccountAddress>,<tokenName>,TOK,18 --network <nameOfYourNetwork>
 

This will create an upgradeable ERC20 smart contract with
 

Token name: <anyTokenNameYouProvide>

Token symbol: TOK

Token Decimals: 18
 

At this, you point you have a fully operational ERC20 smart contract.
 

If you want to add any functionality or remove bug just do that change and use following commands to push and register that change as an update:
 

    > $ zos push --network <nameOfYourNetwork>
 

Now update the contract
 

    > $ zos update <tokenNamePrevioslyProvided> --network <nameOfYourNetwork>
 

Conclusion:

This way we have made our ERC20 smart contract an upgradeable one, now whenever we need to make an update we can do that in a fly.

 

About Author

Author Image
Neeraj Kumar

Neeraj is a JAVA Developer who possesses skill set in: Data Structures, Core Java, Java Enterprise Edition (Servlets, JSP, Standard Java Beans), Spring (Spring-core, spring-MVC, spring-Data-Access, Spring-Data-JPA), Hibernate, JPA, HTML, CSS, JavaScri

Request for Proposal

Name is required

Comment is required

Sending message..