Skip to content

Instantly share code, notes, and snippets.

@islishude
Last active August 31, 2019 00:11
Show Gist options
  • Save islishude/667e1c672d13fe74dc3e486fc033b67d to your computer and use it in GitHub Desktop.
Save islishude/667e1c672d13fe74dc3e486fc033b67d to your computer and use it in GitHub Desktop.
differents with lower-level call and call with interface in Solidity
pragma solidity ^0.5.0;
// see https://solidity.readthedocs.io/en/v0.5.9/control-structures.html#error-handling-assert-require-revert-and-exceptions
// When exceptions happen in a sub-call, they “bubble up” (i.e. exceptions are rethrown) automatically.
// Exceptions to this rule are send and the low-level functions call, delegatecall and staticcall –
// those return false as their first return value in case of an exception instead of “bubbling up”.
contract Caller {
constructor() public {}
// trx receipt status is true, no emit Event
// if token address doesn't exists would be true
// existence must be checked prior to calling if desired.
function call_transfer_1(address token) public returns( bool ) {
// call transfer() with low-level call
(bool success,) = token.call(hex"8a4068dd");
return success;
}
// trx receipt status is false,no emit Event
// if token address doesn't exists would be failed
function call_transfer_2(address token) public {
// call transfer() with interface
Callable(token).transfer();
}
// call_looping_1(token, 2) => [false, false] why?
function call_looping_1(address token, uint256 loop) public returns ( bool[] memory) {
bool[] memory result = new bool[](loop);
for(uint256 i = 0; i < loop; i++){
(bool success,) = token.call(abi.encodeWithSelector(hex"493a66c6", loop));
result[i] = success;
}
return result;
}
function call_looping_2(address token, uint256 loop) public {
for (uint256 i = 0; i < loop; i++){
Callable(token).loopping(loop);
}
}
}
interface Callable {
function transfer() external returns (bool);
function loopping(uint256) external returns (uint256);
event Hello(uint256 indexed);
}
contract Callee is Callable {
address payable public owner;
uint256 public nonce = 0;
event Hello(uint256 indexed);
constructor() public {
owner = msg.sender;
}
function transfer() external returns (bool) {
emit Hello(nonce);
require(msg.sender == owner, "not owner");
nonce++;
return true;
}
function loopping(uint256 number) external returns (uint256) {
emit Hello(number);
require(number < 1, "number should less than 1");
nonce++;
return number;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment