Created
November 30, 2016 20:17
-
-
Save simondlr/cd13348d0f578e2f5cd49cb7f5501801 to your computer and use it in GitHub Desktop.
Testing throws in Solidity
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.4; | |
/* | |
This is for testing if a transaction would throw. | |
Contract calls rethrow when it encounters errors. | |
Raw calls do not. | |
You wrap your contract you want to test in a ThrowProxy. | |
You prime it by calling the fallback function. | |
Then executing it. | |
False will be returned if it threw. | |
True will be return it it did not throw or OOG. | |
*/ | |
contract ThrowProxy { | |
address public target; | |
bytes data; | |
function ThrowProxy(address _target) { | |
target = _target; | |
} | |
//prime the data using the fallback function. | |
function() { | |
data = msg.data; | |
} | |
function execute() returns (bool) { | |
return target.call(data); | |
} | |
} | |
contract Something { | |
function doThrow() { | |
throw; | |
} | |
function doNoThrow() { | |
// | |
} | |
} | |
contract TestSomething { | |
function testThrow() returns (bool) { | |
Something s = new Something(); | |
ThrowProxy t = new ThrowProxy(address(s)); | |
//prime the proxy. | |
Something(address(t)).doThrow(); | |
//execute the call that is supposed to throw. | |
bool r = t.execute(); //r will be false if it threw. r will be true if it didn't. | |
//do assert here. | |
return r; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
address(t).call(bytes4(bytes32(sha3("doThrow()"))));