Created
August 11, 2018 10:35
-
-
Save yhuag/a9785040fd996e2b1fb5ab97e187b486 to your computer and use it in GitHub Desktop.
Delegate call to Math with Solidity "delegatecall"
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 Math { | |
// Answer | |
uint public ans; | |
// Module #0 | |
function plus(uint _n1, uint _n2) public { ans = _n1 + _n2; } | |
// Module #1 | |
function minus(uint _n1, uint _n2) public { ans = _n1 - _n2; } | |
// Module #2 | |
function multiply(uint _n1, uint _n2) public { ans = _n1 * _n2; } | |
// Module #3 | |
function divide(uint _n1, uint _n2) public { ans = _n1 / _n2; } | |
} | |
contract DelegateCall { | |
// Local Answer | |
uint public ans; | |
// Target module | |
address public target; | |
// Target module setter | |
function setTarget(address _tar) public { target = _tar; } | |
// Module caller | |
function callPlus(uint256 x,uint256 y) public { | |
if (!target.delegatecall(bytes4(keccak256("plus(uint256,uint256)")),x,y)) revert(); | |
} | |
function callMinus(uint256 x,uint256 y) public { | |
if (!target.delegatecall(bytes4(keccak256("minus(uint256,uint256)")),x,y)) revert(); | |
} | |
function callMultiply(uint256 x,uint256 y) public { | |
if (!target.delegatecall(bytes4(keccak256("multiply(uint256,uint256)")),x,y)) revert(); | |
} | |
function callDivide(uint256 x,uint256 y) public { | |
if (!target.delegatecall(bytes4(keccak256("divide(uint256,uint256)")),x,y)) revert(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment