Is the immutability of smart contracts a scam

November 18, 2022

Hey, I know the title of this article confused you, it would be painful to discover that something we believe is a hoax, but luckily smart contracts are immutable, the concepts of upgradable and migration do not kill the smart contract immutability, they help a lot when you want to use the same address for another contract or to get data from one contract to another, let's understand what it is and how it works.

Upgradable Smart Contracts

Ethereum smart contracts are immutable by default, once we create them there is no way to modify them.

In certain cases this can cause problems, imagine that a vulnerability is discovered in a smart contract, what can be done? To solve this type of problem, the concept of updatable smart contracts is used, there are 2 methods for this:

  • Migration
  • Proxies

Migration

Is when you deploy your new contract not connected to old contract in any way, by social convention you tell to everbody that you deployed new contract. More faithful to blockchain values, but not always efficient, in case of a vulnerability for example, the vulnerable contract will still be active at the same address, and this address may be known and being used by third-party contracts that also cannot be updated.

How to perform the migration

It is important to be prepared to migrate the contract at any time, as the contract may undergo updates and so on.

A migration is based on 2 steps:

  • Retrieving the data to migrate
  • Writing of the data of the new contract

Step 1: Data Recovery

Data retrieval is done through the getters methods of the contract, but private data can be used through the getStorageAt function. It is important to use events in the data for better tracking when recovering. An easy way to retrieve data is using the Google BigQuery API or using the ethereum-etl tool.

Step 2: Data writing

After recovering the data, you need to start a new contract. For simple variables you can set the values through the contract constructor.

Migratory concerns

When migrating a contract, two major concerns arise: How much will the migration cost? What is the impact on trades?

Proxies - Upgradable Smart Contracts

An upgradable smart contract uses a proxy pattern. The latter involves deploying proxy contracts and implementation contracts:

Upgradable Smart Contract

You can see in the image that the user interacts with a proxy, and the proxy forwards the calls to a contract, only the owner of the proxy contract can change the address of the contract to which calls are forwarded. This is how the concept of proxy-upgradable contracts works, you deploy a contract and a proxy contract that forwards calls to your contract. If you have a problem with your contract, you can simply deploy another one, and update the contract address inside the proxy contract, so you don't even need to release the new contract to the public.

Migration Upgrade VS Proxy Upgrade

Okay, now you know the 2 methods to deal with broken contracts, it's time to know which method use. Which method to use depends a lot on the situation, each method is applicable under certain conditions.

Contract migration is more secure and reliable, so it is better to use it in most cases, but in cases where the contract needs frequent upgrades and an fixed address, you should use proxy upgrade, despite spending more gas fees.