chore: Renamings + comment fixes

- Renamed ExecutorTransferMethods to TokenTransfer to avoid leaking information about how the transfer happens into the executor. The executor shouldn't care if there are multiple methods or one single method that takes care of everything.
- Also renamed TransferMethod to TransferType to match the rust encoding
This commit is contained in:
TAMARA LIPOWSKI
2025-04-10 11:02:04 -04:00
committed by Diana Carvalho
parent 4a20fa6215
commit 4bc29f283b
6 changed files with 63 additions and 65 deletions

View File

@@ -5,14 +5,14 @@ import "@interfaces/IExecutor.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@permit2/src/interfaces/IAllowanceTransfer.sol";
error ExecutorTransferMethods__InvalidPermit2();
error TokenTransfer__InvalidPermit2();
contract ExecutorTransferMethods {
contract TokenTransfer {
using SafeERC20 for IERC20;
IAllowanceTransfer public immutable permit2;
enum TransferMethod {
enum TransferType {
// Assume funds are in the TychoRouter - transfer into the pool
TRANSFER,
// Assume funds are in msg.sender's wallet - transferFrom into the pool
@@ -25,7 +25,7 @@ contract ExecutorTransferMethods {
constructor(address _permit2) {
if (_permit2 == address(0)) {
revert ExecutorTransferMethods__InvalidPermit2();
revert TokenTransfer__InvalidPermit2();
}
permit2 = IAllowanceTransfer(_permit2);
}
@@ -35,20 +35,18 @@ contract ExecutorTransferMethods {
address sender,
address receiver,
uint256 amount,
TransferMethod method
TransferType transferType
) internal {
if (method == TransferMethod.TRANSFER) {
if (transferType == TransferType.TRANSFER) {
tokenIn.safeTransfer(receiver, amount);
} else if (method == TransferMethod.TRANSFERFROM) {
} else if (transferType == TransferType.TRANSFERFROM) {
// slither-disable-next-line arbitrary-send-erc20
tokenIn.safeTransferFrom(sender, receiver, amount);
} else if (method == TransferMethod.TRANSFERPERMIT2) {
} else if (transferType == TransferType.TRANSFERPERMIT2) {
// Permit2.permit is already called from the TychoRouter
permit2.transferFrom(
sender, receiver, uint160(amount), address(tokenIn)
);
} else {
// Funds are likely already in pool. Do nothing.
}
}
}

View File

@@ -4,14 +4,14 @@ 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";
import "./TokenTransfer.sol";
error UniswapV2Executor__InvalidDataLength();
error UniswapV2Executor__InvalidTarget();
error UniswapV2Executor__InvalidFactory();
error UniswapV2Executor__InvalidInitCode();
contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
contract UniswapV2Executor is IExecutor, TokenTransfer {
using SafeERC20 for IERC20;
address public immutable factory;
@@ -19,7 +19,7 @@ contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
address private immutable self;
constructor(address _factory, bytes32 _initCode, address _permit2)
ExecutorTransferMethods(_permit2)
TokenTransfer(_permit2)
{
if (_factory == address(0)) {
revert UniswapV2Executor__InvalidFactory();
@@ -42,14 +42,15 @@ contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
address target;
address receiver;
bool zeroForOne;
TransferMethod method;
TransferType transferType;
(tokenIn, target, receiver, zeroForOne, method) = _decodeData(data);
(tokenIn, target, receiver, zeroForOne, transferType) =
_decodeData(data);
_verifyPairAddress(target);
calculatedAmount = _getAmountOut(target, givenAmount, zeroForOne);
_transfer(tokenIn, msg.sender, target, givenAmount, method);
_transfer(tokenIn, msg.sender, target, givenAmount, transferType);
IUniswapV2Pair pool = IUniswapV2Pair(target);
if (zeroForOne) {
@@ -67,7 +68,7 @@ contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
address target,
address receiver,
bool zeroForOne,
TransferMethod method
TransferType transferType
)
{
if (data.length != 62) {
@@ -77,7 +78,7 @@ contract UniswapV2Executor is IExecutor, ExecutorTransferMethods {
target = address(bytes20(data[20:40]));
receiver = address(bytes20(data[40:60]));
zeroForOne = uint8(data[60]) > 0;
method = TransferMethod(uint8(data[61]));
transferType = TransferType(uint8(data[61]));
}
function _getAmountOut(address target, uint256 amountIn, bool zeroForOne)

View File

@@ -5,14 +5,14 @@ import "@interfaces/IExecutor.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
import "@interfaces/ICallback.sol";
import {ExecutorTransferMethods} from "./ExecutorTransferMethods.sol";
import {TokenTransfer} from "./TokenTransfer.sol";
error UniswapV3Executor__InvalidDataLength();
error UniswapV3Executor__InvalidFactory();
error UniswapV3Executor__InvalidTarget();
error UniswapV3Executor__InvalidInitCode();
contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
contract UniswapV3Executor is IExecutor, ICallback, TokenTransfer {
using SafeERC20 for IERC20;
uint160 private constant MIN_SQRT_RATIO = 4295128739;
@@ -24,7 +24,7 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
address private immutable self;
constructor(address _factory, bytes32 _initCode, address _permit2)
ExecutorTransferMethods(_permit2)
TokenTransfer(_permit2)
{
if (_factory == address(0)) {
revert UniswapV3Executor__InvalidFactory();
@@ -50,7 +50,7 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
address receiver,
address target,
bool zeroForOne,
TransferMethod method
TransferType transferType
) = _decodeData(data);
_verifyPairAddress(tokenIn, tokenOut, fee, target);
@@ -60,7 +60,7 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
IUniswapV3Pool pool = IUniswapV3Pool(target);
bytes memory callbackData =
_makeV3CallbackData(tokenIn, tokenOut, fee, method);
_makeV3CallbackData(tokenIn, tokenOut, fee, transferType);
{
(amount0, amount1) = pool.swap(
@@ -98,10 +98,10 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
address tokenIn = address(bytes20(msgData[132:152]));
require(
uint8(msgData[171]) <= uint8(TransferMethod.NONE),
uint8(msgData[171]) <= uint8(TransferType.NONE),
"InvalidTransferMethod"
);
TransferMethod method = TransferMethod(uint8(msgData[171]));
TransferType transferType = TransferType(uint8(msgData[171]));
address sender = address(bytes20(msgData[172:192]));
verifyCallback(msgData[132:]);
@@ -109,7 +109,7 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
uint256 amountOwed =
amount0Delta > 0 ? uint256(amount0Delta) : uint256(amount1Delta);
_transfer(IERC20(tokenIn), sender, msg.sender, amountOwed, method);
_transfer(IERC20(tokenIn), sender, msg.sender, amountOwed, transferType);
return abi.encode(amountOwed, tokenIn);
}
@@ -146,7 +146,7 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
address receiver,
address target,
bool zeroForOne,
TransferMethod method
TransferType transferType
)
{
if (data.length != 85) {
@@ -158,17 +158,17 @@ contract UniswapV3Executor is IExecutor, ICallback, ExecutorTransferMethods {
receiver = address(bytes20(data[43:63]));
target = address(bytes20(data[63:83]));
zeroForOne = uint8(data[83]) > 0;
method = TransferMethod(uint8(data[84]));
transferType = TransferType(uint8(data[84]));
}
function _makeV3CallbackData(
address tokenIn,
address tokenOut,
uint24 fee,
TransferMethod method
TransferType transferType
) internal pure returns (bytes memory) {
return abi.encodePacked(
tokenIn, tokenOut, fee, uint8(method), msg.sender, self
tokenIn, tokenOut, fee, uint8(transferType), msg.sender, self
);
}