Last active
April 16, 2018 01:00
-
-
Save zabirauf/a0d2456278bf388138f95da2804ce5f6 to your computer and use it in GitHub Desktop.
Example of a ethereum smart contract that can be upgraded
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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