-
Notifications
You must be signed in to change notification settings - Fork 12k
Add no-return-data ERC20 support to SafeERC20. #1655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
139b69a
92e75e5
9192661
d03b819
7e6919d
3460346
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,78 @@ import "../../math/SafeMath.sol"; | |
|
||
/** | ||
* @title SafeERC20 | ||
* @dev Wrappers around ERC20 operations that throw on failure. | ||
* @dev Wrappers around ERC20 operations that throw on failure (when the token | ||
* contract returns false). Tokens that return no value (and instead revert or | ||
* throw on failure) are also supported, non-reverting calls are assumed to be | ||
* successful. | ||
* To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract, | ||
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc. | ||
*/ | ||
library SafeERC20 { | ||
using SafeMath for uint256; | ||
|
||
function safeTransfer(IERC20 token, address to, uint256 value) internal { | ||
require(token.transfer(to, value)); | ||
callAndAssertSuccess(token, abi.encodeWithSignature("transfer(address,uint256)", to, value)); | ||
} | ||
|
||
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { | ||
require(token.transferFrom(from, to, value)); | ||
callAndAssertSuccess(token, abi.encodeWithSignature("transferFrom(address,address,uint256)", from, to, value)); | ||
} | ||
|
||
function safeApprove(IERC20 token, address spender, uint256 value) internal { | ||
// safeApprove should only be called when setting an initial allowance, | ||
// or when resetting it to zero. To increase and decrease it, use | ||
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance' | ||
require((value == 0) || (token.allowance(address(this), spender) == 0)); | ||
require(token.approve(spender, value)); | ||
callAndAssertSuccess(token, abi.encodeWithSignature("approve(address,uint256)", spender, value)); | ||
} | ||
|
||
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { | ||
uint256 newAllowance = token.allowance(address(this), spender).add(value); | ||
require(token.approve(spender, newAllowance)); | ||
callAndAssertSuccess(token, abi.encodeWithSignature("approve(address,uint256)", spender, newAllowance)); | ||
} | ||
|
||
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { | ||
uint256 newAllowance = token.allowance(address(this), spender).sub(value); | ||
require(token.approve(spender, newAllowance)); | ||
callAndAssertSuccess(token, abi.encodeWithSignature("approve(address,uint256)", spender, newAllowance)); | ||
} | ||
|
||
/** | ||
* @dev Performs a function call on a token, asserting its success, which is defined by a) the call succeeding, | ||
* and b) the return data being either empty or true. | ||
* @param token The token targeted byt he call. | ||
* @param data The call data (encoded using abi.encode or one of its variants). | ||
*/ | ||
function callAndAssertSuccess(IERC20 token, bytes memory data) private { | ||
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since | ||
// we're implementing it ourselves. | ||
|
||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success,) = address(token).call(data); | ||
require(success); | ||
|
||
require(getBooleanReturnValue()); | ||
} | ||
|
||
/** | ||
* @dev Parses the return value of the last contract call and attempts to return a boolean value out of it. | ||
*/ | ||
function getBooleanReturnValue() private pure returns (bool result) { | ||
// We need to use inline assembly here, since it is the only way we can utilize the returndatasize opcode. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is true for the opcode, but we could be using the I don't feel so comfortable having this function use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe it's a good time to introduce that bytes conversion library. 👀 |
||
|
||
// solhint-disable-next-line no-inline-assembly | ||
assembly { | ||
switch returndatasize() | ||
case 0 { // No return value - assume success | ||
result := 1 | ||
} | ||
case 32 { // Standard ERC20 - read the returned 32 byte value | ||
returndatacopy(0, 0, 32) | ||
result := mload(0) | ||
} | ||
default { // Other return sizes are unsupported | ||
revert(0, 0) | ||
} | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.