Created
September 18, 2018 21:50
-
-
Save cmditch/91ec92296f90ef0c7c97d0432c95c20c 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; | |
import "../lib/ds-test/src/test.sol"; | |
// Proxy contract | |
/// @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(not(0), masterCopy, 0, calldatasize(), 0, 0) | |
returndatacopy(0, 0, returndatasize()) | |
switch success | |
case 0 { revert(0, returndatasize()) } | |
default { return(0, returndatasize()) } | |
} | |
} | |
} | |
// Proxy contract DSTest | |
contract Foo is Proxyable { | |
function testFunc() public view returns(uint) { | |
return 42; | |
} | |
} | |
contract ProxyTest is DSTest { | |
Foo foo; | |
function setUp() public { | |
Foo fooMaster = new Foo(); | |
Proxy fooProxy = new Proxy(address(fooMaster)); | |
foo = Foo(address(fooProxy)); | |
} | |
function testFoo() public { | |
assertEq(foo.testFunc(), 42); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment