Skip to content

Instantly share code, notes, and snippets.

@DanielVF
Created October 19, 2020 14:12
Show Gist options
  • Save DanielVF/186db6809cd9a1a9bfcf1dfbdbf192d9 to your computer and use it in GitHub Desktop.
Save DanielVF/186db6809cd9a1a9bfcf1dfbdbf192d9 to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.0;
// Calling SafeERC20.safeTransfer and SafeERC20.safeTransferFrom can fail
// with a "SafeERC20: ERC20 operation did not succeed" message when the
// underlying cause is running out of gas inside the transfer
// This is because the low level call to the ERC20 is indeed reverted by
// running out of gas, but that revert is caught, and a different revert
// is raised by the safeTransfer method.
// Sample code for remix below.
pragma solidity ^0.6.0;
contract Caller{
function doSafeTransfer(address other) public returns (bytes memory rd) {
bytes memory data = abi.encodeWithSignature("transfer()");
(bool success, bytes memory returndata) = address(other).call(data);
require(success, "SafeERC20: ERC20 operation did not succeed");
return returndata;
}
}
contract Happy{
function transfer() public {
}
}
contract Sad {
uint256 foo;
function transfer() public {
for(uint256 i = 0; i < 400; i++){
foo = i;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment