Simple proxy
Course: Upgradable contracts - chapter 14
Simple implementation
Section titled “Simple implementation”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)
Complex implementation
Section titled “Complex implementation”Ref - https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/proxy/Proxy.sol
Proxy.sol
Section titled “Proxy.sol”$1bUsing Proxy.sol
Section titled “Using Proxy.sol”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; }}Problems with this approach -
Section titled “Problems with this approach -”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?