|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// OpenZeppelin Contracts (last updated v4.9.3) (token/ERC20/utils/SafeERC20.sol) |
| 3 | + |
| 4 | +pragma solidity ^0.8.0; |
| 5 | + |
| 6 | +// ===== BEGIN TRON OVERRIDE ===== |
| 7 | +// Modified for Tron: safeTransfer bypasses return-value check for Tron USDT only. |
| 8 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 9 | +import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol"; |
| 10 | +import "@openzeppelin/contracts/utils/Address.sol"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @title SafeERC20 |
| 14 | + * @dev Wrappers around ERC20 operations that throw on failure (when the token |
| 15 | + * contract returns false). Tokens that return no value (and instead revert or |
| 16 | + * throw on failure) are also supported, non-reverting calls are assumed to be |
| 17 | + * successful. |
| 18 | + * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract, |
| 19 | + * which allows you to call the safe operations as `token.safeTransfer(...)`, etc. |
| 20 | + */ |
| 21 | +library SafeERC20 { |
| 22 | + using Address for address; |
| 23 | + // Tron USDT (TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t) |
| 24 | + // transfer() returns false despite success because the source code discards |
| 25 | + // super.transfer()'s return value without its own `return true`. |
| 26 | + // See: https://gist.github.com/yorhodes/a6eccbeba27ff76355c3d761e84d6a35 |
| 27 | + address private constant TRON_USDT = 0xa614f803B6FD780986A42c78Ec9c7f77e6DeD13C; |
| 28 | + |
| 29 | + /** |
| 30 | + * @dev Transfer `value` amount of `token` from the calling contract to `to`. If `token` returns no value, |
| 31 | + * non-reverting calls are assumed to be successful. |
| 32 | + * |
| 33 | + * For Tron USDT specifically, the return value is ignored because its transfer() |
| 34 | + * returns false on success due to a missing `return true` in the source code. |
| 35 | + */ |
| 36 | + function safeTransfer(IERC20 token, address to, uint256 value) internal { |
| 37 | + if (address(token) == TRON_USDT) { |
| 38 | + (bool success, ) = address(token).call(abi.encodeWithSelector(token.transfer.selector, to, value)); |
| 39 | + require(success, "SafeERC20: ERC20 transfer failed"); |
| 40 | + return; |
| 41 | + } |
| 42 | + // ===== END TRON OVERRIDE ===== |
| 43 | + _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value)); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * @dev Transfer `value` amount of `token` from `from` to `to`, spending the approval given by `from` to the |
| 48 | + * calling contract. If `token` returns no value, non-reverting calls are assumed to be successful. |
| 49 | + */ |
| 50 | + function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal { |
| 51 | + _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @dev Deprecated. This function has issues similar to the ones found in |
| 56 | + * {IERC20-approve}, and its usage is discouraged. |
| 57 | + * |
| 58 | + * Whenever possible, use {safeIncreaseAllowance} and |
| 59 | + * {safeDecreaseAllowance} instead. |
| 60 | + */ |
| 61 | + function safeApprove(IERC20 token, address spender, uint256 value) internal { |
| 62 | + // safeApprove should only be called when setting an initial allowance, |
| 63 | + // or when resetting it to zero. To increase and decrease it, use |
| 64 | + // 'safeIncreaseAllowance' and 'safeDecreaseAllowance' |
| 65 | + require( |
| 66 | + (value == 0) || (token.allowance(address(this), spender) == 0), |
| 67 | + "SafeERC20: approve from non-zero to non-zero allowance" |
| 68 | + ); |
| 69 | + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value)); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @dev Increase the calling contract's allowance toward `spender` by `value`. If `token` returns no value, |
| 74 | + * non-reverting calls are assumed to be successful. |
| 75 | + */ |
| 76 | + function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal { |
| 77 | + uint256 oldAllowance = token.allowance(address(this), spender); |
| 78 | + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @dev Decrease the calling contract's allowance toward `spender` by `value`. If `token` returns no value, |
| 83 | + * non-reverting calls are assumed to be successful. |
| 84 | + */ |
| 85 | + function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal { |
| 86 | + unchecked { |
| 87 | + uint256 oldAllowance = token.allowance(address(this), spender); |
| 88 | + require(oldAllowance >= value, "SafeERC20: decreased allowance below zero"); |
| 89 | + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dev Set the calling contract's allowance toward `spender` to `value`. If `token` returns no value, |
| 95 | + * non-reverting calls are assumed to be successful. Meant to be used with tokens that require the approval |
| 96 | + * to be set to zero before setting it to a non-zero value, such as USDT. |
| 97 | + */ |
| 98 | + function forceApprove(IERC20 token, address spender, uint256 value) internal { |
| 99 | + bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value); |
| 100 | + |
| 101 | + if (!_callOptionalReturnBool(token, approvalCall)) { |
| 102 | + _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0)); |
| 103 | + _callOptionalReturn(token, approvalCall); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @dev Use a ERC-2612 signature to set the `owner` approval toward `spender` on `token`. |
| 109 | + * Revert on invalid signature. |
| 110 | + */ |
| 111 | + function safePermit( |
| 112 | + IERC20Permit token, |
| 113 | + address owner, |
| 114 | + address spender, |
| 115 | + uint256 value, |
| 116 | + uint256 deadline, |
| 117 | + uint8 v, |
| 118 | + bytes32 r, |
| 119 | + bytes32 s |
| 120 | + ) internal { |
| 121 | + uint256 nonceBefore = token.nonces(owner); |
| 122 | + token.permit(owner, spender, value, deadline, v, r, s); |
| 123 | + uint256 nonceAfter = token.nonces(owner); |
| 124 | + require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed"); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement |
| 129 | + * on the return value: the return value is optional (but if data is returned, it must not be false). |
| 130 | + * @param token The token targeted by the call. |
| 131 | + * @param data The call data (encoded using abi.encode or one of its variants). |
| 132 | + */ |
| 133 | + function _callOptionalReturn(IERC20 token, bytes memory data) private { |
| 134 | + // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since |
| 135 | + // we're implementing it ourselves. We use {Address-functionCall} to perform this call, which verifies that |
| 136 | + // the target address contains contract code and also asserts for success in the low-level call. |
| 137 | + |
| 138 | + bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed"); |
| 139 | + require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed"); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement |
| 144 | + * on the return value: the return value is optional (but if data is returned, it must not be false). |
| 145 | + * @param token The token targeted by the call. |
| 146 | + * @param data The call data (encoded using abi.encode or one of its variants). |
| 147 | + * |
| 148 | + * This is a variant of {_callOptionalReturn} that silents catches all reverts and returns a bool instead. |
| 149 | + */ |
| 150 | + function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) { |
| 151 | + // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since |
| 152 | + // we're implementing it ourselves. We cannot use {Address-functionCall} here since this should return false |
| 153 | + // and not revert is the subcall reverts. |
| 154 | + |
| 155 | + (bool success, bytes memory returndata) = address(token).call(data); |
| 156 | + return |
| 157 | + success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token)); |
| 158 | + } |
| 159 | +} |
0 commit comments