// SPDX-License-Identifier: GPL-3.0-or-later // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . pragma solidity ^0.7.0; import "../openzeppelin/IERC20.sol"; import "../../contracts/interfaces/IAsset.sol"; import "../../contracts/interfaces/IWETH.sol"; abstract contract AssetHelpers { // solhint-disable-next-line var-name-mixedcase IWETH private immutable _weth; // Sentinel value used to indicate WETH with wrapping/unwrapping semantics. The zero address is a good choice for // multiple reasons: it is cheap to pass as a calldata argument, it is a known invalid token and non-contract, and // it is an address Pools cannot register as a token. address private constant _ETH = address(0); constructor(IWETH weth) { _weth = weth; } // solhint-disable-next-line func-name-mixedcase function _WETH() internal view returns (IWETH) { return _weth; } /** * @dev Returns true if `asset` is the sentinel value that represents ETH. */ function _isETH(IAsset asset) internal pure returns (bool) { return address(asset) == _ETH; } /** * @dev Translates `asset` into an equivalent IERC20 token address. If `asset` represents ETH, it will be translated * to the WETH contract. */ function _translateToIERC20(IAsset asset) internal view returns (IERC20) { return _isETH(asset) ? _WETH() : _asIERC20(asset); } /** * @dev Same as `_translateToIERC20(IAsset)`, but for an entire array. */ function _translateToIERC20(IAsset[] memory assets) internal view returns (IERC20[] memory) { IERC20[] memory tokens = new IERC20[](assets.length); for (uint256 i = 0; i < assets.length; ++i) { tokens[i] = _translateToIERC20(assets[i]); } return tokens; } /** * @dev Interprets `asset` as an IERC20 token. This function should only be called on `asset` if `_isETH` previously * returned false for it, that is, if `asset` is guaranteed not to be the ETH sentinel value. */ function _asIERC20(IAsset asset) internal pure returns (IERC20) { return IERC20(address(asset)); } }