Coinmonks
Published in

Coinmonks

Coding Upgradable Smart Contracts

As Developers, we always wanted to upgrade our code to introduce new features and fixing bugs too. talking about blockchain, whatever is written on blockchain is immutable. even Smart contracts, once deployed on blockchain network we can’t alter or modify it. in the past few years, we have seen many attacks like DAO, Re-entrancy attacks, Parity Wallet hacks caused a loss of millions of ether only because of small bugs in smart contracts. So testing smart contracts tens of times before deploying is very essential to avoid these vulnerabilities. What if, we wanted to introduce new features to our deployed smart contract? since it is not even possible with blockchain, Right? but somehow, there is a way to upgrade our contracts. how? “A way of Upgrading without actually upgrading.” seems funny? yeah give me a minute, you might be aware of Call diversion feature in your Smartphone, once call diversion is activated for your mobile number with your preferred new number, every call that comes to your mobile number will be diverted to your preferred mobile number. in the same way, an upgradeable contract works. your old contract handles the data and redirects every function call that comes to your old contract address to the newly deployed contract ie.. an upgraded one.

for good reason, gochain provides a CLI protocol to Upgrading contracts. So what are you waiting for? let's go.

In the demonstration, I’m gonna upgrade the simple greeter contract from v1 to v2 .in the version v1, there is no getter method to return greeting. in v2, getter method is added as a bug fix or new feature whatever you call it is. code available here.

Ubuntu Desktop With gochain web3 library.

1.gochain Web3 library

curl -LSs https://raw.githubusercontent.com/gochain-io/web3/master/install.sh | sh

2. Gochain wallet and some GO-tokens.

Go to https://explorer.gochain.io/wallet/create and store your privatekey somewhere else safe.

For doing any operations on network, you need tokens. so inorder to get testnet tokens, join their official telegram channel https://t.me/gochain_testnet and ask them to provide some testnet tokens by pasting your previously created wallet address**

Steps

a). Export private key to your Env. variables (require root user privileges )

sudo -s

export WEB3_PRIVATE_KEY=0x...

Check if your private key is correct by this command $ web3 myaddress if this command returns the wallet address that you created as in pre-requisites, then you are good to go.

b). Use the Go-Chain testnet

$ export WEB3_NETWORK=testnet

a). Compiling Contract

copy greeter.sol file to your directory, then

web3 contract build greeter.sol

b). Deploying contract

web3 contract deploy --upgradeable greeter.bin

Once deployed, you will get contract address, add this address to env. variables

export WEB3_ADDRESS=0x...

Deploying upgradable contract actually deploy two contracts. original greeter contract and proxy contract which will redirect calls to upgraded contract. check where our proxy contract redirecting calls and storage with this command

web3 contract target

c). Writing Transaction (Greeting)

web3 contract call --abi greeter.abi --function greet "Hi Guys, This is from Old Contract"

Once transaction went through, we successfully added greeting on blockchain. but still, there is no option to return the added greeting. thats why we are trying to upgrade contract with the new code.

Copy greeterv2.sol to your directory and then,

web3 contract build greeterv2.sol
web3 contract deploy greeter.bin

Once deployed, it will return the newly deployed contract address. using this address we can upgrade our old contract greeterto greeterv2. run below command to finish one last step

web3 contract upgrade --to 0x.... //add your newly deployed contract address here

Now you can call a new contract with the same address. in the old contract, there is no getter function for variable, in the new contract, we added getter function getGreeting. now you can call it using this command.

web3 contract call --abi greeter.abi --function getGreeting>Hi Guys, This is from Old Contract

Upgradeable contracts also include the ability to pause & resume execution. if you found any bug in contract you can simply pause (or resume later) the contract with these commands

web3 contract pause
web3 contract resume

Once paused, if you call the function you will get unmarshalling empty output string till you resume contract.

BOOM…

Get Best Software Deals Directly In Your Inbox

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Salman Dabbakuti

Curious about decentralized future. Working on Blockchain Technologies. Hit me up, would love to interact!