Skip to content

Simple proxy

Course: Upgradable contracts - chapter 14

pragma solidity ^0.8.0;
contract Proxy {
address public implementation;
constructor(address _implementation) {
implementation = _implementation;
}
fallback() external payable {
// Forward the call to the implementation contract
(bool success, ) = implementation.delegatecall(msg.data);
require(success, "Delegatecall failed");
}
}
contract ImplementationV1 {
uint public num;
address public implementation;
function setNum(uint _num) public {
num = _num;
}
}

Try calling the proxy contract with the following callData

0xcd16ecbf0000000000000000000000000000000000000000000000000000000000000002

💡

This calldata comes from the Keccak-256 hash of setNum(uint256) followed by 1 (32 bytes)

Ref - https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/Proxy.sol

$1b
GPL-3.0
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/proxy/Proxy.sol";
contract StorageProxy is Proxy {
uint256 public num;
address public implementation;
constructor(address _implementation) {
implementation = _implementation;
}
function _implementation() internal view virtual override returns (address) {
return implementation;
}
}
contract ImplementationV1 {
uint256 public num;
address public implementation;
function setNum(uint256 _num) public {
num = _num;
}
}

How do we upgrade implementation (you might say I will add a new function called upgradeImplementation but what if there is a similar function in the implementation contract?