Created
September 19, 2018 15:59
-
-
Save cmditch/fea2fd934aa62c68eaab8e07d453a3ba to your computer and use it in GitHub Desktop.
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.24; | |
/// @title Proxy - Generic proxy contract allows to execute all transactions applying the code of a master contract. | |
/// @author Stefan George - <[email protected]> | |
// Mix this into contracts you'd like to be "proxyable", | |
// masterCopy must be the first var in any proxied contract in order to work correctly. | |
contract Proxyable { | |
address masterCopy; | |
} | |
contract Proxy is Proxyable { | |
/// @dev Constructor function sets address of master copy contract. | |
/// @param _masterCopy Master copy address. | |
constructor(address _masterCopy) | |
public | |
{ | |
require(_masterCopy != 0); | |
masterCopy = _masterCopy; | |
} | |
/// @dev Fallback function forwards all transactions and returns all received return data. | |
function () | |
external | |
payable | |
{ | |
assembly { | |
let masterCopy := and(sload(0), 0xffffffffffffffffffffffffffffffffffffffff) | |
calldatacopy(0, 0, calldatasize()) | |
let success := delegatecall(sub(gas, 703), masterCopy, 0, calldatasize(), 0, 0) | |
returndatacopy(0, 0, returndatasize()) | |
switch success | |
case 0 { revert(0, returndatasize()) } | |
default { return(0, returndatasize()) } | |
} | |
} | |
} | |
contract Foo is Proxyable { | |
function testFunc() public view returns(uint) { | |
return 42; | |
} | |
} | |
contract ProxyTest { | |
Foo fooMaster; | |
Foo foo; | |
function setUp() public { | |
fooMaster = new Foo(); | |
Proxy fooProxy = new Proxy(address(fooMaster)); | |
foo = Foo(address(fooProxy)); | |
} | |
function testFooMaster() public returns(uint) { | |
return fooMaster.testFunc(); | |
} | |
function testFooProxy() public returns(uint) { | |
return foo.testFunc(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment