Created
November 8, 2018 12:56
-
-
Save wadealexc/b49fe88291342ddbc76e39a099a85f8f to your computer and use it in GitHub Desktop.
Simple delegate-proxy implementation
This file contains 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; | |
contract Proxy { | |
// Default delegatecall target | |
address destination; | |
/** | |
* @dev Constructor. Sets the default destination | |
* @param _dest The address to which this contract will forward execution | |
*/ | |
constructor (address _dest) public { | |
destination = _dest; | |
} | |
/** | |
* @dev Fallback function. Delegates all execution to the default address | |
*/ | |
function () external payable { | |
address target = destination; | |
assembly { | |
calldatacopy(0, 0, calldatasize) // Copy msg.data to memory | |
// Delegatecall the target, sending msg.data | |
let ret := delegatecall(gas, target, 0, calldatasize, 0, 0) | |
returndatacopy(0, 0, returndatasize) // Copy returned data to memory | |
// If the delegatecall failed (ret == 0), revert data. | |
// Otherwise, return data | |
switch ret | |
case 0 { revert(0, returndatasize) } | |
default { return(0, returndatasize) } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment