DEV Community

Cover image for Is it possible to upgrade a smart contract? no? think again.
Javier Acrich
Javier Acrich

Posted on • Updated on

Is it possible to upgrade a smart contract? no? think again.

Are smart contracts really upgradable?

"TL;DR" Not really. but...
Smart Contracts are pieces of software that live in the blockchain. They can execute actions according to a series of parameters already programmed. All of this in an immutable, transparent, and completely secure way. The fact that these smart contracts are distributed in thousands of machines makes them censorship-resistant, transparent, efficient, secure, and inexpensive.

  • Transparency - The data on the blockchain is available for everyone to see.
  • Efficiency - No room for (mis-) interpretations or lost documents. "code is law" is a popular expression that describes this behavior.
  • Cost reduction - As middlemen are cut out, the costs are significantly reduced as well.
  • Security - Cryptography is used to secure transactions and prevents attacks

What is OpenZeppelin?

OpenZeppelin is a company that offers a variety of services for developing distributed applications.
It also offers some free standard base smart contracts that are audited for security reasons and they are one of the most used smart contracts among developers. When building a new smart contract often you have to follow standards like ERC20 or EC721. Instead of reinventing the wheel, you can just inherit your contract from one of the openZeppelin contracts to save time and start programming right away your specific business rules.

Why upgrade a Contract?

By design, smart contracts are immutable. On the other hand, software quality heavily depends on the ability to upgrade and patch source code in order to produce iterative releases. Even though blockchain-based software profits significantly from the technology’s immutability, still a certain degree of mutability is needed for bug fixing and potential product improvements. OpenZeppelin Upgrades solves this apparent contradiction by providing an easy-to-use, simple, robust, and opt-in upgrade mechanism for smart contracts that can be controlled by any type of governance, be it a multi-sig wallet, a simple address or a complex DAO.

How do I upgrade a Contract?

The basic idea is to use a proxy for upgrades. The first contract is a simple wrapper or "proxy" with which users interact directly and is in charge of forwarding transactions to and from the second contract, which contains the logic. The key concept to understand is that the logic contract can be replaced while the proxy or the access point is never changed. Both contracts are still immutable in the sense that their code cannot be changed, but the logic contract can simply be swapped by another contract. The wrapper can thus point to different logic implementation and in doing so, the software is "upgraded".

OpenZeppelin provides a plugin that allows us to upgrade a contract easily.

image

Diagram 1: contracts relation and users

When we deploy our implementation contract with the Upgrades plugin we are also deploying 2 more contracts. The Proxy Contract and the ProxyAdmin Contract.

The Proxy Contract will be the contract that your clients will connect to. You won't have to update this contract since it doesn't hold any of the logic. Just the state.
The Implementation Contract will hold the logic (that you can later upgrade if you find bugs or need to add features )
But you won't actually “upgrade” it. We will just make the Proxy Contract point to a new V2 Contract with fixed bugs or more features than V1. And since the client is connected to the Proxy Client, you won't have to ask them to switch to the V2 Contract. What a relief…

You also deploy the ProxyAdmin Contract whose only responsibility is to change the Proxy Contract to point to a newer Implementation Contract. Only an admin owner can do this trick.

There is a special function in Solidity called DELEGATECALL that allows you to call another contract using the context of the caller. Not the callee. This allows the discrimination between state and logic which makes possible the “upgrade”

My name is Javier Acrich
And I work at Santex as a full-stack software developer.

Top comments (0)