Upgradeable Contracts

Solidity Programming Language
3 min readOct 6, 2022

--

Upgradeable Contracts

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

Proxy Contracts Methodology I

We need to know the call and delegatecall methods.

**Contract A(the Calling Contract) and Contract B(the Target Contract).

***CALL

****When contract A(caller contract) does a simple CALL on B(target contract), the code actually gets executed in the context of B. It means Storage of B is used. Calls are not capable of preserving the msg.sender or msg.value

***DELEGATE CALL

  • ***When Contract A does a Delegate call on Contact B, the code gets executed in the context of Contract A(the Calling Contract) and not Contract B(the Target Contract).It means the storage of A is used. Moreover, in a Delegate Call msg.sender and msg.value is preserved.
  • ****For example, during a delegate call Contract B(target contract) is aware that the actual caller is not Contract A(caller contract) but an externally owned account(msg.sender).

Proxy Contracts Methodology II

The proxy pattern splits a contract in two: one contract holding the logic and a proxy contract holding the data.

In this pattern, the user interacts with the proxy. The contract holding the logic can be updated. This solution requires mastering delegatecall to allow one contract to use code from another.

Let’s review how delegatecall works.

Proxy Contracts Methodology III

We have separated the logic contract, we can change the logic while keeping the proxy contract same for the user. Proxy Contract in this procedure acts as immutable storage while the Logic Contract will include all the functionalities.

  • to upgrade the “logic of the contract”, we simply need to make the [ proxy contract ] aware of the address of the [ new delegate contract ] .
  • The structure of storage, i.e., the order of state variables must be similar for both Proxy and Logic Contract.

Executing an upgrade is then straightforward. By changing the implementation address in the proxy, it is possible to change the code run upon every call to it, while the address the user interacts with is always the same. State is also preserved, since it is kept in the proxy’s storage, and not on that of the implementation contract.

Universal Upgradeable Proxies I

As an alternative to transparent proxies, EIP1822 defines the universal upgradeable proxy standard, or UUPS for short.

This standard uses the same delegate call pattern, but places upgrade logic in the implementation contract instead of the proxy itself.

And the implementation address itself is kept in the proxy’s storage. Nothing prevents the implementation from actually providing the logic for modifying the proxy’s implementation address. UUPS proposes that all implementation contracts extend from a base proxiable contract.

Checks Description C

A- Variables that should not be constant

B- Functions shadowing

C- initializer() is not called

D- Initialize functions are called multiple times

E- Incorrect variables with the v2

F- Incorrect variables with the proxy

G- State variable initialized

I- Variables that should be constant

H- Extra variables in the proxy

J-Missing variables

K- Extra variables in the v2

Kaynak

--

--

Solidity Programming Language
Solidity Programming Language

Written by Solidity Programming Language

Solidity basics for beginners: Learn the fundamentals of smart contract development and build your first DApp! #Solidity #Foundry #Ethereum #Opcodes #DApps

No responses yet