All ‘smart contracts’ are by default, immutable.
This means that once a ‘smart-contract’ is deployed, its data and programmability cannot be altered, ever. This derived feature of immutability puts code in the supreme place and no one can modify it.
However, as the usefulness of smart contracts is expanding, the need and emergence of its added functionalities have also increased. For instance, the introduction of Upgradable Smart Contracts that enables to update of the functionality of smart contracts. Developers can achieve desired or altered output using this method which is not possible in simple smart contracts. This functionality opens up a new window of endless possibilities for developing decentralized applications on blockchain.
In this article, we will discuss the Upgradable Smart Contracts and their functions, whilst maintaining the policy of immutability, decentralization and transparency.
Before going any further, let’s recap what smart contracts are and how they function.
Smart Contracts- their genesis, functions and future
To understand the intricacies of this new phenomenon, it is crucial to get the basics of smart contracts right.
Smart contracts are programs deployed on blockchain networks that execute automatically when the predefined conditions are met. The unique point about Smart Contracts is that it does not require any approval from the developer or intermediary. Smart Contracts’ sole aim is to achieve immutability in a specific application and this derived feature puts code in supreme place.
Upgradable Smart Contracts
Upgradable Smart Contracts are developed by separating the code of logic and state data (storage) in two different executions. This makes it possible to update the logic of the contract while leaving the state unchanged. In a typical smart contract, both these functions are executed in sync which puts certain limitations on its upgradability.
This method of separating a smart contract’s logic and state is called a ‘proxy pattern’ which is most popular for developing upgradable smart contracts. Here, two different contracts: a proxy contract and an implementation contract are deployed within one agreement. A proxy contract is constant where the state is stored and the implementation contract includes the logic.
How does it work?
When the smart contract upgrade takes place, developers are required to direct user interaction from the proxy contract to the new implementation contract where logic is updated. Proxy contracts act as a bridge between users and logic by forwarding calls to the implementation contract.
The implementation contract is changeable by calling the function _upgradeTo(newAddress) in the proxy contract. In this process, the data remains untouched as the implementation contract does not store any data itself and just returns the logical output.
However, the use of proxy patterns brings several drawbacks to the smart contracts such as increased gas cost, posing challenges in logic updation, and the perceived issue of security risks as storage is separated. These apparent pitfalls have led the blockchain landscape to the creation of standard EVM proxy patterns like ERC tokens.
Types of Proxy Patterns in Upgradable Smart Contracts
The popular proxy pattern types are as below which are tailored in accordance with the unique needs and uses within the smart contracts.
1. Transparent Proxy Pattern
Transparent proxy pattern involves a proxy contract that forwards all calls to the logic contract without any interference or modification. This proxy aims to provide a seamless user experience by adapting the straightforward approach to upgrade functionality of a smart contract. It is deployed within the EIP-1538 upgrade on Ethereum as a standard implementation.
2. Universal Upgradeable Proxy Standard (UUPS)
UUPS is a standardized proxy pattern that enhances gas efficiency and extensibility compared to the Transparent Proxy Pattern. It was introduced in the EIP-1822 as a standard approach for upgradable smart contracts. UUPS requires a single storage slot for the logic contract address, making upgrades more gas-efficient and enabling more flexibility in upgrade mechanisms.
3. Eternal Storage Proxy Pattern
Eternal Storage pattern is responsible for separating and storing all data independently of the logic contract like a pure proxy pattern. However, this pattern stores data in a contract called ‘Eternal Storage’ instead of a proxy contract. This functionality ensures that state variables within the contracts are migrated to the new logic contract without much effort. It preserves data integrity across upgrades and provides flexibility in smart contract upgrades.
4. Diamond Proxy Pattern
The Diamond Proxy Pattern was introduced in EIP-2535 as the most efficient proxy type for smart contracts. It follows a modular and upgradeable architecture that consists of multiple facets. Each facet represents a separate module for the implementation of smart contract. The diamond proxy contract acts as the entry point, dispatching calls to the appropriate facets based on function selectors. It optimizes gas usage by minimizing storage duplication and allowing for flexible upgrades to the smart contract.
5. Proxy Delegate Pattern
This pattern involves a proxy contract with minimal logic that delegates all calls to a separate delegate contract housing the main logic. While the Delegate pattern itself does not inherit upgradeability, it can be combined with Transparent Proxy Pattern or UUPS Proxy Pattern to achieve upgradeability while keeping the logic separate from the proxy.
All these above proxy patterns have their own pros and cons. They are used within the smart contracts according to need of upgradability and trade-offs it could neglect while executed.
Why Upgradable Smart Contracts Are Needed
So now we know what are the upgradable smart contracts and how they are developed, the question must arise that why upgradable smart contracts are needed. It can be used in a number of cases such as fixing an existing bug, adding features or updating logical conditions. Some of them are as discussed below;
Bug Fixes: Launching fix for bugs and unintentional errors is the key-reason behind opting for upgradable smart contracts. If such bugs occur in normal smart contracts, the developer would have to replace the whole code and deploy it on a separate address. Thus the separation enabled by the proxy patterns helps in cost-minimizing for post-deployment fixes.
Feature Addition & Modification: When a smart contract needs constant feature updates or modifications in existing functionality. A smart contract may require to be updated when the source of logic changes. The upgradable smart contracts are also useful when a developer wants to enhance user experience and improvise their applications.
Maintaining Relevance: The data and variables used in smart contracts are often changed thus the upgradability feature of smart contracts can help it sync the latest states. In addition, the blockchain landpace is fast-paced and numerous developments are introduced on regular basis. To keep up, upgradable smart contracts seem better than typical smart contracts.
Also Read : 5 Common Smart Contract Vulnerabilities & How to Prevent them
Final words
Upgradable smart contracts have opened a new window of experiments and exploration for developers. It allows flexibility and adaptability while not compromising with the core feature of immutability. To remind again, upgradability does not mean mutability as the deployed smart contracts can not be edited in any way. The use of proxy patterns in smart contracts have bring massive opportunities to develop and deploy novel decentralized applications.
FAQs
How upgradable smart contracts work?
Upgradable smart contract uses a tailored solution called ‘proxy patterns’ that allows to separate logic and state in two different smart contracts. At the time of upgrade, the logic contract could be changed within the proxy contract.
What is a proxy smart contract?
A proxy smart contract is a bridge to interact with another smart contract consisting of core features and functionality execution.
Are upgradable smart contracts redeployed?
No, upgradable smart contracts are not redeployed, at least not the proxy contract. An implementation contract consisting of the logic is replaced within the proxy contract hence the redeployment does not take place.