Solidity Simple Upgradable Smart Contract

The smart contract is code immutable but not the states on the contract because it can be modified. The problem is once publishing the smart contract to the Ethereum network, the contract code cannot be changed. There is always some advantage and some drawback. There is a good reason being able to upgrade contracts. The most important part of immutability is making sure that nobody can make changes to the code afterwards. This makes Ethereum and Smart Contracts so incredibly powerful and the contract becomes trustworthy. It’s like burning the logic into the blockchain and nobody can change it or take it down. But recent hacks were all based on very simple programming errors and bugs could be fixed very easily if it was possible to upgrade these contracts. In this tutorial, I will provide you with a tutorial on upgradable smart contract.

To get started, use remix IDE //remix.ethereum.org/ to create your upgradable smart contract. You will be able to understand after this tutorial.

Create a storage contract

First, add a new file call “MetaCoinStorage.sol” and copy paste the following source code. It will assign 10000 coins to the contract creator. This contract will store the states of the meta coin.

Create a proxy contract

After that, create a new file call “MetaCoin.sol”, this contract will call the storage contract methods and handle logics of getBalance.

Deploy Contract

In the run tab, deploy the storage contract first, after that deploy the proxy contract.

Discover and fix the bug

The proxy contract getBalance method has a bug which is “+1” when getting the balance of the address. Erase “+1” on the getBalance method proxy contract and deploy it again.

Now you can try to call the method again from the new proxy contract, you will discover the bugs are fixed.

Solidity Simple Upgradable Smart Contract

Source Code

(Visited 537 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *