Upgradeable Smart Contracts
A blog on creating upgradeable smart contracts using delegatecall
Originally published on June 17, 2023 on my previous blog.
In this blog, we will look at how to create a simple upgradeable smart contract using the delegate call property of solidity. For this we will follow the simple proxy pattern where one contract will hold the logic for function implementation and another will store the state.
Simple Proxy Pattern
There are three types of proxies that can be used to create an upgradeable smart contract. These are:
- Simple Proxy Pattern
- Transparent Proxy Pattern
- Universal Proxy Pattern
We will follow the simple proxy pattern for this blog. The other two patterns will be covered in future blogs.
The simple proxy pattern is a design pattern that allows for the separation of the storage and logic of a smart contract. This is done by having a proxy contract that delegates all calls to an implementation contract. The proxy contract will store the state of the contract and the implementation contract will store the logic. This allows for the implementation contract to be upgraded without losing the state of the contract.
We will use the following contracts to demonstrate the simple proxy pattern.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract Logic1 {
uint256 public value;
function setValue(uint256 _newValue) external {
value = _newValue;
}
}
contract State {
uint256 public value;
address public logicContract;
constructor(address _receiver) {
logicContract = _receiver;
}
function updateLogicContract(address _logic) external {
logicContract = _logic;
}
function updateValue(uint256 _newValue) external {
(bool success, ) = logicContract.delegatecall(
abi.encodeWithSignature("setValue(uint256)", _newValue)
);
require(success, "Delegate call failed");
}
}
The Logic1 contract is the implementation contract. It contains the logic for the setValue function. The State contract is the proxy contract. It stores the state of the contract and delegates all calls to the implementation contract. Users will interact with the proxy contract which will remain unchanged even if the implementation contract is upgraded.
Remember to follow the same order of variables in the implementation contract and the proxy contract. This is because the proxy contract will store the state of the implementation contract and if the order of variables is different then there will be storage collisions. To understand storage collisions, you need to understand how storage works in solidity. You can read more about it here.
Delegate Call
The delegatecall function is a low level function that allows you to call another contract and execute its code in the context of the calling contract. This means that the called contract will have access to the storage of the calling contract and logic of the called contract will be executed in the context of the calling contract. This is different from the call function which executes the logic of the called contract in the context of the called contract.
In the updateValue function of the State contract, we are using the delegatecall function to call the setValue function of the Logic1 contract. This will execute the logic of the setValue function in the context of the State contract. This means that the Logic1 contract will have access to the storage of the State contract. After the call the state of the State contract will be updated and the state of the Logic1 contract will remain unchanged.
You can try this in remix by deploying the State contract and then calling the updateValue function with a value. You will see that the value of the State contract will be updated and the value of the Logic1 contract will remain unchanged.
Upgrading the Implementation Contract
Now that we have a proxy contract and an implementation contract, we can upgrade the implementation contract without losing the state of the proxy contract.
To do this we will create a new implementation contract and deploy it. Remember to follow the same order of variables and same function signatures in the new implementation contract. This is because the proxy contract is still unchanged.
Let try to update the setValue function in the Logic1 contract. We will change the setValue function to the following:
contract Logic2 {
uint256 value;
function setValue(uint256 _newValue) external {
value += _newValue;
}
}
Deploy the Logic2 contract and copy its address. Now call the updateLogicContract function of the State contract with the address of the Logic2 contract. This will update the implementation contract of the State contract to the Logic2 contract. Now call the updateValue function of the State contract with a value. You will see that now the logic of the Logic2 contract is executed in the context of the State contract and the value of the State contract is updated.
The codes are available on github repo
Comments
Join the discussionNo comments yet. Be the first to comment.