Upgradable Contracts

Sample implementation of upgradable contracts

A smart contract upgrade is an action that can arbitrarily change the code executed in an address while preserving storage and balance.

Overview

  • A major limitation of smart contract upgrades: while it’s possible to arbitrarily change the code of a contract, only storage-compatible changes can be done to its state variables. Operations such as reordering variables, inserting new variables, changing the type of a variable, or even changing the inheritance chain of a contract can potentially break storage. The only safe change is appending state variables after any existing ones.

  • Under the unstructured proxy pattern, implementation contracts cannot have constructors. Once the implementation contract has been created, there is no way to invoke its constructor code anymore. This means that proxies cannot call into the constructor to initialize their state.

  • To work around this, constructors need to be changed into regular functions, usually called initializers. Since these are regular functions, they do get compiled into the contract, and can be delegate-called by the proxy to initialize it when it is deployed. However, since they are also regular functions, they need additional logic to ensure they can be called only once.

References

https://eips.ethereum.org/EIPS/eip-1967

https://eips.ethereum.org/EIPS/eip-1822

Last updated