Skip to content

Instantly share code, notes, and snippets.

@tomw1808
Last active April 29, 2018 09:12
Show Gist options
  • Save tomw1808/bc0557ea6de24d4fa99035cd017aaf9d to your computer and use it in GitHub Desktop.
Save tomw1808/bc0557ea6de24d4fa99035cd017aaf9d to your computer and use it in GitHub Desktop.
Example of a contract calling another contract and exceptions are thrown. One time with a named contract call, another time with a low level function call with call.value()()
pragma solidity ^0.4.21;
contract A {
/**
* Example of a named contract call exception
*/
function someAException() public {
B myContractB = new B();
myContractB.myB();
}
/**
* Example of address.call.value()()
*
*/
function someABoolean() public {
B myContractB = new B();
//attention here, the call will execute because call.value() only returns a boolean
bool successfulCall = address(myContractB).call.value(0 ether)(bytes4(keccak256("myB()")));
if(!successfulCall) {
//something went wrong
} else {
//it worked
}
}
}
contract B {
function myB() public payable{
require(msg.value == 1 ether);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment