feat: ExecutorTransferMethods helper contract
- Also sketch its use in USV2 (missing proper decoding)
This commit is contained in:
committed by
Diana Carvalho
parent
27cebdb3e1
commit
147ba68392
52
foundry/src/executors/ExecutorTransferMethods.sol
Normal file
52
foundry/src/executors/ExecutorTransferMethods.sol
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
import "@interfaces/IExecutor.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@permit2/src/interfaces/IAllowanceTransfer.sol";
|
||||
import "@permit2/src/interfaces/IAllowanceTransfer.sol";
|
||||
|
||||
error ExecutorTransferMethods__InvalidPermit2();
|
||||
|
||||
contract ExecutorTransferMethods {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
IAllowanceTransfer public immutable permit2;
|
||||
|
||||
enum TransferMethod {
|
||||
TRANSFER,
|
||||
TRANSFERFROM,
|
||||
TRANSFERPERMIT2,
|
||||
NONE
|
||||
}
|
||||
|
||||
constructor(address _permit2) {
|
||||
if (_permit2 == address(0)) {
|
||||
revert ExecutorTransferMethods__InvalidPermit2();
|
||||
}
|
||||
permit2 = IAllowanceTransfer(_permit2);
|
||||
}
|
||||
|
||||
function _transfer(
|
||||
IERC20 tokenIn,
|
||||
address receiver,
|
||||
uint256 amount,
|
||||
TransferMethod method
|
||||
) internal {
|
||||
if (method == TransferMethod.TRANSFER) {
|
||||
tokenIn.safeTransfer(receiver, amount);
|
||||
} else if (method == TransferMethod.TRANSFERFROM) {
|
||||
tokenIn.safeTransferFrom(msg.sender, receiver, amount);
|
||||
} else if (method == TransferMethod.TRANSFERPERMIT2) {
|
||||
// Permit2.permit is called from the TychoRouter
|
||||
permit2.transferFrom(
|
||||
msg.sender,
|
||||
receiver, // Does this work if receiver is not address(this)?
|
||||
uint160(amount),
|
||||
address(tokenIn)
|
||||
);
|
||||
} else {
|
||||
// Funds are likely already in pool. Do nothing.
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,20 +4,23 @@ pragma solidity ^0.8.26;
|
||||
import "@interfaces/IExecutor.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "@uniswap-v2/contracts/interfaces/IUniswapV2Pair.sol";
|
||||
import "./ExecutorTransferMethods.sol";
|
||||
|
||||
error UniswapV2Executor__InvalidDataLength();
|
||||
error UniswapV2Executor__InvalidTarget();
|
||||
error UniswapV2Executor__InvalidFactory();
|
||||
error UniswapV2Executor__InvalidInitCode();
|
||||
|
||||
contract UniswapV2Executor is IExecutor {
|
||||
contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
|
||||
using SafeERC20 for IERC20;
|
||||
|
||||
address public immutable factory;
|
||||
bytes32 public immutable initCode;
|
||||
address private immutable self;
|
||||
|
||||
constructor(address _factory, bytes32 _initCode) {
|
||||
constructor(address _factory, bytes32 _initCode, address _permit2)
|
||||
ExecutorTransferMethods(_permit2)
|
||||
{
|
||||
if (_factory == address(0)) {
|
||||
revert UniswapV2Executor__InvalidFactory();
|
||||
}
|
||||
@@ -35,17 +38,18 @@ contract UniswapV2Executor is IExecutor {
|
||||
payable
|
||||
returns (uint256 calculatedAmount)
|
||||
{
|
||||
IERC20 tokenIn;
|
||||
address target;
|
||||
address receiver;
|
||||
bool zeroForOne;
|
||||
IERC20 tokenIn;
|
||||
TransferMethod method;
|
||||
|
||||
(tokenIn, target, receiver, zeroForOne) = _decodeData(data);
|
||||
(tokenIn, target, receiver, zeroForOne, method) = _decodeData(data);
|
||||
|
||||
_verifyPairAddress(target);
|
||||
|
||||
calculatedAmount = _getAmountOut(target, givenAmount, zeroForOne);
|
||||
tokenIn.safeTransfer(target, givenAmount);
|
||||
_transfer(tokenIn, target, givenAmount, method);
|
||||
|
||||
IUniswapV2Pair pool = IUniswapV2Pair(target);
|
||||
if (zeroForOne) {
|
||||
@@ -62,7 +66,8 @@ contract UniswapV2Executor is IExecutor {
|
||||
IERC20 inToken,
|
||||
address target,
|
||||
address receiver,
|
||||
bool zeroForOne
|
||||
bool zeroForOne,
|
||||
TransferMethod method
|
||||
)
|
||||
{
|
||||
if (data.length != 61) {
|
||||
@@ -72,6 +77,8 @@ contract UniswapV2Executor is IExecutor {
|
||||
target = address(bytes20(data[20:40]));
|
||||
receiver = address(bytes20(data[40:60]));
|
||||
zeroForOne = uint8(data[60]) > 0;
|
||||
// TODO properly decode, assume encoded using just 1 byte.
|
||||
method = TransferMethod.TRANSFER;
|
||||
}
|
||||
|
||||
function _getAmountOut(address target, uint256 amountIn, bool zeroForOne)
|
||||
|
||||
Reference in New Issue
Block a user