|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC20/utils/SafeERC20.sol)
|
|
|
|
|
|
|
|
pragma solidity ^0.8.19;
|
|
|
|
|
|
|
|
import "../IERC20.sol";
|
|
|
|
import "../extensions/IERC20Permit.sol";
|
|
|
|
import "../../../utils/Address.sol";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @title SafeERC20
|
|
|
|
* @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 IERC20;` statement to your contract,
|
|
|
|
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
|
|
|
|
*/
|
|
|
|
library SafeERC20 {
|
|
|
|
using Address for address;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value,
|
|
|
|
* non-reverting calls are assumed to be successful.
|
|
|
|
*/
|
|
|
|
function safeTransfer(IERC20 token, address to, uint256 value) internal {
|
|
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the
|
|
|
|
* calling contract. If `token` returns no value, non-reverting calls are assumed to be successful.
|
|
|
|
*/
|
|
|
|
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
|
|
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
|
|
|
|
* non-reverting calls are assumed to be successful.
|
|
|
|
*/
|
|
|
|
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
|
|
|
|
uint256 oldAllowance = token.allowance(address(this), spender);
|
|
|
|
forceApprove(token, spender, oldAllowance + value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value,
|
|
|
|
* non-reverting calls are assumed to be successful.
|
|
|
|
*/
|
|
|
|
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
|
|
|
|
unchecked {
|
|
|
|
uint256 oldAllowance = token.allowance(address(this), spender);
|
|
|
|
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
|
|
|
|
forceApprove(token, spender, oldAllowance - value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value,
|
|
|
|
* non-reverting calls are assumed to be successful. Compatible with tokens that require the approval to be set to
|
|
|
|
* 0 before setting it to a non-zero value.
|
|
|
|
*/
|
|
|
|
function forceApprove(IERC20 token, address spender, uint256 value) internal {
|
|
|
|
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
|
|
|
|
|
|
|
|
if (!_callOptionalReturnBool(token, approvalCall)) {
|
|
|
|
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
|
|
|
|
_callOptionalReturn(token, approvalCall);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`.
|
|
|
|
* Revert on invalid signature.
|
|
|
|
*/
|
|
|
|
function safePermit(
|
|
|
|
IERC20Permit token,
|
|
|
|
address owner,
|
|
|
|
address spender,
|
|
|
|
uint256 value,
|
|
|
|
uint256 deadline,
|
|
|
|
uint8 v,
|
|
|
|
bytes32 r,
|
|
|
|
bytes32 s
|
|
|
|
) internal {
|
|
|
|
uint256 nonceBefore = token.nonces(owner);
|
|
|
|
token.permit(owner, spender, value, deadline, v, r, s);
|
|
|
|
uint256 nonceAfter = token.nonces(owner);
|
|
|
|
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
|
|
|
|
* on the return value: the return value is optional (but if data is returned, it must not be false).
|
|
|
|
* @param token The token targeted by the call.
|
|
|
|
* @param data The call data (encoded using abi.encode or one of its variants).
|
|
|
|
*/
|
|
|
|
function _callOptionalReturn(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. We use {Address-functionCall} to perform this call, which verifies that
|
|
|
|
// the target address contains contract code and also asserts for success in the low-level call.
|
|
|
|
|
|
|
|
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
|
|
|
|
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
|
|
|
|
* on the return value: the return value is optional (but if data is returned, it must not be false).
|
|
|
|
* @param token The token targeted by the call.
|
|
|
|
* @param data The call data (encoded using abi.encode or one of its variants).
|
|
|
|
*
|
|
|
|
* This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead.
|
|
|
|
*/
|
|
|
|
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
|
|
|
|
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
|
|
|
|
// we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false
|
|
|
|
// and not revert is the subcall reverts.
|
|
|
|
|
|
|
|
(bool success, bytes memory returndata) = address(token).call(data);
|
|
|
|
return success && (returndata.length == 0 || abi.decode(returndata, (bool))) && address(token).code.length > 0;
|
|
|
|
}
|
|
|
|
}
|