feat: fix input decoding in usv3 executor and execution dispatcher
This commit is contained in:
@@ -7,6 +7,7 @@ import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
|
||||
import "@uniswap/v3-updated/CallbackValidationV2.sol";
|
||||
|
||||
error UniswapV3Executor__InvalidDataLength();
|
||||
error UniswapV3Executor__InvalidFactory();
|
||||
|
||||
contract UniswapV3Executor is IExecutor {
|
||||
using SafeERC20 for IERC20;
|
||||
@@ -16,16 +17,22 @@ contract UniswapV3Executor is IExecutor {
|
||||
1461446703485210103287273052203988822378723970342;
|
||||
|
||||
address public immutable factory;
|
||||
address private immutable self;
|
||||
|
||||
constructor(address _factory) {
|
||||
if (_factory == address(0)) {
|
||||
revert UniswapV3Executor__InvalidFactory();
|
||||
}
|
||||
factory = _factory;
|
||||
self = address(this);
|
||||
}
|
||||
|
||||
// slither-disable-next-line locked-ether
|
||||
function swap(
|
||||
uint256 amountIn,
|
||||
bytes calldata data
|
||||
) external payable returns (uint256 amountOut) {
|
||||
function swap(uint256 amountIn, bytes calldata data)
|
||||
external
|
||||
payable
|
||||
returns (uint256 amountOut)
|
||||
{
|
||||
(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
@@ -58,23 +65,19 @@ contract UniswapV3Executor is IExecutor {
|
||||
}
|
||||
}
|
||||
|
||||
function handleCallback(
|
||||
bytes calldata msgData
|
||||
) external returns (bytes memory result) {
|
||||
int256 amount0Delta;
|
||||
int256 amount1Delta;
|
||||
function handleCallback(bytes calldata msgData)
|
||||
external
|
||||
returns (bytes memory result)
|
||||
{
|
||||
// Skip first 4 bytes of function selector and decode the two int256 values
|
||||
(int256 amount0Delta, int256 amount1Delta) =
|
||||
abi.decode(msgData[4:68], (int256, int256));
|
||||
|
||||
(amount0Delta, amount1Delta) = abi.decode(
|
||||
msgData[:64],
|
||||
(int256, int256)
|
||||
);
|
||||
bytes calldata remainingData = msgData[68:];
|
||||
|
||||
(uint256 amountOwed, address tokenOwed) =
|
||||
_verifyUSV3Callback(amount0Delta, amount1Delta, remainingData);
|
||||
|
||||
bytes calldata remainingData = msgData[64:];
|
||||
(uint256 amountOwed, address tokenOwed) = _verifyUSV3Callback(
|
||||
amount0Delta,
|
||||
amount1Delta,
|
||||
remainingData
|
||||
);
|
||||
IERC20(tokenOwed).safeTransfer(msg.sender, amountOwed);
|
||||
return abi.encode(amountOwed, tokenOwed);
|
||||
}
|
||||
@@ -84,28 +87,23 @@ contract UniswapV3Executor is IExecutor {
|
||||
int256 amount1Delta,
|
||||
bytes calldata data
|
||||
) internal view returns (uint256 amountIn, address tokenIn) {
|
||||
// Skip the first 64 bytes (32 bytes offset + 32 bytes length)
|
||||
data = data[64:];
|
||||
|
||||
tokenIn = address(bytes20(data[0:20]));
|
||||
address tokenOut = address(bytes20(data[20:40]));
|
||||
uint24 poolFee = uint24(bytes3(data[40:43]));
|
||||
|
||||
// slither-disable-next-line unused-return
|
||||
CallbackValidationV2.verifyCallback(
|
||||
factory,
|
||||
tokenIn,
|
||||
tokenOut,
|
||||
poolFee
|
||||
);
|
||||
CallbackValidationV2.verifyCallback(factory, tokenIn, tokenOut, poolFee);
|
||||
|
||||
amountIn = amount0Delta > 0
|
||||
? uint256(amount0Delta)
|
||||
: uint256(amount1Delta);
|
||||
amountIn =
|
||||
amount0Delta > 0 ? uint256(amount0Delta) : uint256(amount1Delta);
|
||||
|
||||
return (amountIn, tokenIn);
|
||||
}
|
||||
|
||||
function _decodeData(
|
||||
bytes calldata data
|
||||
)
|
||||
function _decodeData(bytes calldata data)
|
||||
internal
|
||||
pure
|
||||
returns (
|
||||
@@ -128,11 +126,11 @@ contract UniswapV3Executor is IExecutor {
|
||||
zeroForOne = uint8(data[83]) > 0;
|
||||
}
|
||||
|
||||
function _makeV3CallbackData(
|
||||
address tokenIn,
|
||||
address tokenOut,
|
||||
uint24 fee
|
||||
) internal pure returns (bytes memory) {
|
||||
return abi.encodePacked(tokenIn, tokenOut, fee);
|
||||
function _makeV3CallbackData(address tokenIn, address tokenOut, uint24 fee)
|
||||
internal
|
||||
view
|
||||
returns (bytes memory)
|
||||
{
|
||||
return abi.encodePacked(tokenIn, tokenOut, fee, self);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user