Skip to content

Latest commit

 

History

History
440 lines (387 loc) · 14.3 KB

SafeERC20.md

File metadata and controls

440 lines (387 loc) · 14.3 KB

SafeERC20 (SafeERC20.sol)

View Source: contracts/openzeppelin/SafeERC20.sol

SafeERC20 contract

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.

Functions


safeTransfer

function safeTransfer(IERC20 token, address to, uint256 value) internal nonpayable

Arguments

Name Type Description
token IERC20
to address
value uint256
Source Code
function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

safeTransferFrom

function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal nonpayable

Arguments

Name Type Description
token IERC20
from address
to address
value uint256
Source Code
function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        callOptionalReturn(
            token,
            abi.encodeWithSelector(token.transferFrom.selector, from, to, value)
        );
    }

safeApprove

function safeApprove(IERC20 token, address spender, uint256 value) internal nonpayable

Arguments

Name Type Description
token IERC20
spender address
value uint256
Source Code
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'
        // solhint-disable-next-line max-line-length
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

safeIncreaseAllowance

function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal nonpayable

Arguments

Name Type Description
token IERC20
spender address
value uint256
Source Code
function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender).add(value);
        callOptionalReturn(
            token,
            abi.encodeWithSelector(token.approve.selector, spender, newAllowance)
        );
    }

safeDecreaseAllowance

function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal nonpayable

Arguments

Name Type Description
token IERC20
spender address
value uint256
Source Code
function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance =
            token.allowance(address(this), spender).sub(
                value,
                "SafeERC20: decreased allowance below zero"
            );
        callOptionalReturn(
            token,
            abi.encodeWithSelector(token.approve.selector, spender, newAllowance)
        );
    }

callOptionalReturn

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).

function callOptionalReturn(IERC20 token, bytes data) private nonpayable

Arguments

Name Type Description
token IERC20 The token targeted by the call.
data bytes The call data (encoded using abi.encode or one of its variants).
Source Code
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.

        // A Solidity high level call has three parts:
        //  1. The target address is checked to verify it contains contract code
        //  2. The call itself is made, and success asserted
        //  3. The return value is decoded, which in turn checks the size of the returned data.
        // solhint-disable-next-line max-line-length
        require(address(token).isContract(), "SafeERC20: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = address(token).call(data);
        require(success, "SafeERC20: low-level call failed");

        if (returndata.length > 0) {
            // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }

Contracts