Unlocking the Power of Upgradable Smart Contracts

The blockchain’s immutability property is both a blessing and a curse. While it ensures the integrity of the network, it also makes it challenging to fix vulnerabilities or add new features to deployed smart contracts. Traditional smart contract patterns don’t allow for hot fixes, forcing developers to deploy a new contract every time they want to make changes. This can lead to a huge overhead as the codebase grows, requiring data migration from the old contract to the new one.

The Solution: Upgradability Patterns

To overcome this limitation, various upgradability patterns have been introduced. Among them, the proxy pattern stands out as the truest form of upgradability. This approach allows the client to interact with the same contract (proxy), while the underlying logic can be changed (upgraded) whenever needed without losing any previous data.

Types of Proxy Patterns

There are three types of proxy patterns:

  • Diamond Pattern: An upgradeable proxy pattern that uses multiple logic contracts (facets) instead of one. It allows adding or removing methods from any facet in a single transaction.
  • Transparent Proxy Pattern: A pattern that separates the logic layer from the storage layer, enabling identical function signatures to exist in the proxy and logic contracts.
  • Universal Upgradeable Proxy Standard (UUPS): A pattern that triggers upgrades via the logic contract rather than the proxy contract, providing a unique storage slot to store the address of the logic contract.

Comparing Proxy Patterns

Each proxy pattern has its pros and cons. The UUPS pattern is considered more gas-efficient, but the decision to use it depends on various factors, including business requirements.

Smart Contract Upgrade Demo with UUPS Proxy Pattern

Let’s demonstrate how to set up and deploy an upgradable Pizza contract using the UUPS proxy pattern, leveraging Hardhat and OpenZeppelin’s UUPS library contracts.

Setup with Hardhat and OpenZeppelin

We’ll deploy a simple smart contract called Pizza and upgrade it to PizzaV2 using the UUPS proxy pattern. First, we need to install Hardhat and OpenZeppelin’s UUPS library contracts.

Making the Implementation and Proxy Contracts

Create a new file called Pizza.sol inside the contracts directory and add the necessary code. Compile and deploy the Pizza contract, and then create a new file called PizzaV2.sol to add new functionality.

Upgrading the Contract

Once the PizzaV2 contract is deployed, we can upgrade the deployed Pizza contract to PizzaV2. Run the upgrade script to execute the upgrade, and verify the PizzaV2 contract from the terminal.

Closing Thoughts

While the UUPS pattern offers several advantages, it’s essential to be aware of its caveats, such as the risk of newer implementations excluding the upgradeTo method, which may permanently kill the ability to upgrade the smart contract. Despite these warnings, UUPS is a gas-efficient proxy pattern that can be beneficial in real-world projects.

Get Started with LogRocket

LogRocket is a powerful tool for monitoring and debugging web and mobile apps. Join organizations like Bitso and Coinsquare who use LogRocket to proactively monitor their Web3 apps. Start monitoring for free today!

Leave a Reply

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