Upgradeable Contract

Why Upgradeable Contract ?

Upgradeable is literally upgradeable.

The smart contract on the blockchain cannot be changed. That is the core and advantage of the contract implemented in code.

However, it is not just good that the code cannot be changed. (especially to developers)

Any program can have a bug. Either it's trivial, or it's fatal.

The point is code is written by a developer. It can eventually cause errors because it is written by a human.

If a bug is found while the service is already being deployed and in progress, it will be very difficult problem.

In particular, in decentralized Web3 Dapp, it is the same level as forced termination of the servi

To solve these problems, an Upgradeable Contract appears.

What is Upgradeable Contract ?

To upgrade an unchangeable smart contract, use the Proxy pattern.

A schematic diagram of the Proxy pattern is as follows.

  1. The user (client) sends a request to the Proxy contract.

  2. Proxy Contract delivers the request to a contract containing the actual implementation and logic.

The important concept is, after all, the two sentences right above.

If a function needs to be added to the contract, or if issues and bugs are found in the function, we don't have to fix the Proxy contract.

In other words, the client only needs to call as usual, and only changes the implementation contract address and sets it to the Proxy contract.

It's like using an interface, the calling partial code doesn't change, but only the internal implementation.

But How?

We know the concept, but what is the implementation? First of all, there's a proposition here.

Even if a function of another contract is called, it affects the storage of the contract which is called, but does not affect the storage of the contract which called another.

In other words, even if you call a contract containing logic through Proxy contract, only the data of the contract containing logic changes

The data in the Proxy contract does not change.

For this magic, Solidity uses a friend named delegatecall and call.

Call and degatecall correspond to opcode (machine language), not functions.

Internally, Solidity uses call and degatecall when calling other contracts.

Call is the opcode we usually use to call a contract.

Delegatecall uses code from other contracts, but the execution environment allows it to be performed on existing contracts.

When Contract A calls Contract B, when using Contract B, if it uses Contract B's code, it changes Contract A's Storage.

Sources…

Last updated