Skip to content

Instantly share code, notes, and snippets.

@zabirauf
Last active April 16, 2018 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zabirauf/a0d2456278bf388138f95da2804ce5f6 to your computer and use it in GitHub Desktop.
Save zabirauf/a0d2456278bf388138f95da2804ce5f6 to your computer and use it in GitHub Desktop.
Example of a ethereum smart contract that can be upgraded
pragma solidity ^0.4.21;
import "./Ownable.sol";
contract UpgradeableContractProxy is Ownable {
address private _currentImplementation;
function UpgradeableContractProxy() public Ownable() {
}
function updateImplementation(address _newImplementation) onlyOwner public {
require(_newImplementation != address(0));
_currentImplementation = _newImplementation;
}
function implementation() public view returns (address) {
return _currentImplementation;
}
function () payable public {
address _impl = implementation();
require(_impl != address(0));
assembly {
// Copy the data sent to the memory address starting 0x40
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize)
// Proxy the call to the contract address with the provided gas and data
let result := delegatecall(gas, _impl, ptr, calldatasize, 0, 0)
// Copy the data returned by the proxied call to memory
let size := returndatasize
returndatacopy(ptr, 0, size)
// Check what the result is, return and revert accordingly
switch result
case 0 { revert(ptr, size) }
case 1 { return(ptr, size) }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment