Skip to content

Instantly share code, notes, and snippets.

@yhuag
Created August 11, 2018 10:33
Show Gist options
  • Save yhuag/47dc04b72f05b997f2b325390846215b to your computer and use it in GitHub Desktop.
Save yhuag/47dc04b72f05b997f2b325390846215b to your computer and use it in GitHub Desktop.
Call to Math with solidity "call"
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 Call {
// 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.call(bytes4(keccak256("plus(uint256,uint256)")),x,y)) revert();
}
function callMinus(uint256 x,uint256 y) public {
if (!target.call(bytes4(keccak256("minus(uint256,uint256)")),x,y)) revert();
}
function callMultiply(uint256 x,uint256 y) public {
if (!target.call(bytes4(keccak256("multiply(uint256,uint256)")),x,y)) revert();
}
function callDivide(uint256 x,uint256 y) public {
if (!target.call(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