The Basics of Upgradable Proxy Contracts in Ethereum

Bernard Peh
May 21, 2018 · 3 min read
pragma solidity ^0.4.21;

contract Storage {
uint public val;
}
pragma solidity ^0.4.21;

import './Ownable.sol';
import './Storage.sol';

contract Registry is Storage, Ownable {

address public logic_contract;

function setLogicContract(address _c) public onlyOwner returns (bool success){
logic_contract = _c;
return true;
}

function () payable public {
address target = logic_contract;
assembly {
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize)
let result := delegatecall(gas, target, ptr, calldatasize, 0, 0)
let size := returndatasize
returndatacopy(ptr, 0, size)
switch result
case 0 { revert(ptr, size) }
case 1 { return(ptr, size) }
}
}
}
pragma solidity ^0.4.21;

import './Storage.sol';

contract LogicOne is Storage {
function setVal(uint _val) public returns (bool success) {
val = 2 * _val;
return true;
}
}
Registry.at(Registry.address).setLogicContract(LogicOne.address)
LogicOne.at(Registry.address).setVal(2)
Registry.at(Registry.address).setLogicContract(LogicTwo.address)
LogicTwo.at(Registry.address).setVal(2)

Bernard Peh

Written by

Founder of Whale Tech. Blockchain Developer and Technologist.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade