|
| 1 | +// Copyright (C) 2020 Zerion Inc. <https://zerion.io> |
| 2 | +// |
| 3 | +// This program is free software: you can redistribute it and/or modify |
| 4 | +// it under the terms of the GNU General Public License as published by |
| 5 | +// the Free Software Foundation, either version 3 of the License, or |
| 6 | +// (at your option) any later version. |
| 7 | +// |
| 8 | +// This program is distributed in the hope that it will be useful, |
| 9 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +// GNU General Public License for more details. |
| 12 | +// |
| 13 | +// You should have received a copy of the GNU General Public License |
| 14 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | +// |
| 16 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 17 | + |
| 18 | +pragma solidity 0.8.12; |
| 19 | + |
| 20 | +import { Address } from "@openzeppelin/contracts/utils/Address.sol"; |
| 21 | + |
| 22 | +import { ICaller } from "../interfaces/ICaller.sol"; |
| 23 | +import { IPermit2 } from "../interfaces/IPermit2.sol"; |
| 24 | +import { Base } from "../shared/Base.sol"; |
| 25 | +import { ZeroTarget } from "../shared/Errors.sol"; |
| 26 | +import { Permit2 } from "../shared/Permit2.sol"; |
| 27 | +import { TokensHandler } from "../shared/TokensHandler.sol"; |
| 28 | + |
| 29 | +/** |
| 30 | + * @title Simple caller that passes through any call and forwards return tokens |
| 31 | + * @dev It also works in fixed outputs case, when input token overhead is refunded |
| 32 | + * @dev This contracts uses Permit2 contract in case of Uniswap's Universal Router usage |
| 33 | + */ |
| 34 | +contract SimpleCallerWithPermit2 is ICaller, TokensHandler, Permit2 { |
| 35 | + address internal constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; |
| 36 | + |
| 37 | + /** |
| 38 | + * @notice Sets Permit2 address for the current chain |
| 39 | + * @param permit2 Wrapped Ether address |
| 40 | + */ |
| 41 | + constructor(address universalRouter, address permit2) Permit2(universalRouter, permit2) { |
| 42 | + // solhint-disable-previous-line no-empty-blocks |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @notice Main external function: decodes `callerCallData` bytes, |
| 47 | + * executes external call, and returns tokens back to `msg.sender` (i.e. Router contract) |
| 48 | + * @param callerCallData ABI-encoded parameters: |
| 49 | + * - inputToken Address of the token that should be approved to the allowance target |
| 50 | + * - allowanceTarget Address to approve `inputToken` to |
| 51 | + * - callTarget Address to forward the external call to |
| 52 | + * - callData Call data to be used in the external call |
| 53 | + * - outputToken Address of the token that should be returned |
| 54 | + * @dev Call target cannot be zero |
| 55 | + */ |
| 56 | + function callBytes(bytes calldata callerCallData) external override { |
| 57 | + ( |
| 58 | + address inputToken, |
| 59 | + address allowanceTarget, |
| 60 | + address payable callTarget, |
| 61 | + bytes memory callData, |
| 62 | + address outputToken |
| 63 | + ) = abi.decode(callerCallData, (address, address, address, bytes, address)); |
| 64 | + if (callTarget == address(0)) revert ZeroTarget(); |
| 65 | + |
| 66 | + // Approve tokens to the allowance target, call the call target |
| 67 | + approveAndCall(inputToken, allowanceTarget, callTarget, callData); |
| 68 | + |
| 69 | + // In case of non-zero input token, transfer the remaining amount back to `msg.sender` |
| 70 | + Base.transfer(inputToken, msg.sender, Base.getBalance(inputToken)); |
| 71 | + |
| 72 | + // In case of non-zero output token, transfer the total balance to `msg.sender` |
| 73 | + Base.transfer(outputToken, msg.sender, Base.getBalance(outputToken)); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @dev Approves input tokens (if necessary) and calls the target with the provided call data |
| 78 | + * @dev Approval and allowance check for `address(0)` token address are skipped |
| 79 | + * @param inputToken Address of the token that should be approved to the allowance target |
| 80 | + * @param allowanceTarget Address to approve `inputToken` to |
| 81 | + * @param callTarget Address to forward the external call to |
| 82 | + * @param callData Call data for the call to the target |
| 83 | + */ |
| 84 | + function approveAndCall( |
| 85 | + address inputToken, |
| 86 | + address allowanceTarget, |
| 87 | + address callTarget, |
| 88 | + bytes memory callData |
| 89 | + ) internal { |
| 90 | + uint256 amount = Base.getBalance(inputToken); |
| 91 | + if (inputToken == ETH) { |
| 92 | + Address.functionCallWithValue( |
| 93 | + callTarget, |
| 94 | + callData, |
| 95 | + amount, |
| 96 | + "SC: payable call failed w/ no reason" |
| 97 | + ); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + if (inputToken != address(0) && allowanceTarget != address(0)) { |
| 102 | + address permit2 = getPermit2(); |
| 103 | + |
| 104 | + if (allowanceTarget == getUniversalRouter()) { |
| 105 | + Base.safeApproveMax(inputToken, permit2, amount); |
| 106 | + IPermit2(permit2).approve( |
| 107 | + inputToken, |
| 108 | + allowanceTarget, |
| 109 | + type(uint160).max, |
| 110 | + type(uint48).max |
| 111 | + ); |
| 112 | + } else { |
| 113 | + Base.safeApproveMax(inputToken, allowanceTarget, amount); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + Address.functionCall(callTarget, callData, "SC: call failed w/ no reason"); |
| 118 | + } |
| 119 | +} |
0 commit comments