feat: hardcode callback and swap selection in dispatcher

This commit is contained in:
royvardhan
2025-02-24 10:56:17 +05:30
parent d859a7ea97
commit 58116e074a
4 changed files with 22 additions and 28 deletions

View File

@@ -55,7 +55,6 @@ contract Dispatcher {
// slither-disable-next-line delegatecall-loop // slither-disable-next-line delegatecall-loop
function _callExecutor( function _callExecutor(
address executor, address executor,
bytes4 selector,
uint256 amount, uint256 amount,
bytes calldata data bytes calldata data
) internal returns (uint256 calculatedAmount) { ) internal returns (uint256 calculatedAmount) {
@@ -63,10 +62,9 @@ contract Dispatcher {
revert Dispatcher__UnapprovedExecutor(); revert Dispatcher__UnapprovedExecutor();
} }
selector = selector == bytes4(0) ? IExecutor.swap.selector : selector;
// slither-disable-next-line controlled-delegatecall,low-level-calls // slither-disable-next-line controlled-delegatecall,low-level-calls
(bool success, bytes memory result) = executor.delegatecall( (bool success, bytes memory result) = executor.delegatecall(
abi.encodeWithSelector(selector, amount, data) abi.encodeWithSelector(IExecutor.swap.selector, amount, data)
); );
if (!success) { if (!success) {
@@ -83,18 +81,16 @@ contract Dispatcher {
} }
function _handleCallback(bytes calldata data) internal { function _handleCallback(bytes calldata data) internal {
bytes4 selector = bytes4(data[data.length - 4:]);
address executor = address(uint160(bytes20(data[data.length - 24:]))); address executor = address(uint160(bytes20(data[data.length - 24:])));
if (!executors[executor]) { if (!executors[executor]) {
revert Dispatcher__UnapprovedExecutor(); revert Dispatcher__UnapprovedExecutor();
} }
selector =
selector == bytes4(0) ? ICallback.handleCallback.selector : selector;
// slither-disable-next-line controlled-delegatecall,low-level-calls // slither-disable-next-line controlled-delegatecall,low-level-calls
(bool success, bytes memory result) = (bool success, bytes memory result) = executor.delegatecall(
executor.delegatecall(abi.encodeWithSelector(selector, data)); abi.encodeWithSelector(ICallback.handleCallback.selector, data)
);
if (!success) { if (!success) {
revert( revert(

View File

@@ -286,10 +286,7 @@ contract TychoRouter is AccessControl, Dispatcher, Pausable, ReentrancyGuard {
: remainingAmounts[tokenInIndex]; : remainingAmounts[tokenInIndex];
currentAmountOut = _callExecutor( currentAmountOut = _callExecutor(
swapData.executor(), swapData.executor(), currentAmountIn, swapData.protocolData()
swapData.executorSelector(),
currentAmountIn,
swapData.protocolData()
); );
amounts[tokenOutIndex] += currentAmountOut; amounts[tokenOutIndex] += currentAmountOut;
remainingAmounts[tokenOutIndex] += currentAmountOut; remainingAmounts[tokenOutIndex] += currentAmountOut;

View File

@@ -16,10 +16,11 @@ import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol";
import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol"; import {Actions} from "@uniswap/v4-periphery/src/libraries/Actions.sol";
import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol"; import {IV4Router} from "@uniswap/v4-periphery/src/interfaces/IV4Router.sol";
import {PathKey} from "@uniswap/v4-periphery/src/libraries/PathKey.sol"; import {PathKey} from "@uniswap/v4-periphery/src/libraries/PathKey.sol";
import {ICallback} from "@interfaces/ICallback.sol";
error UniswapV4Executor__InvalidDataLength(); error UniswapV4Executor__InvalidDataLength();
contract UniswapV4Executor is IExecutor, V4Router { contract UniswapV4Executor is IExecutor, V4Router, ICallback {
using SafeERC20 for IERC20; using SafeERC20 for IERC20;
using CurrencyLibrary for Currency; using CurrencyLibrary for Currency;
@@ -176,6 +177,16 @@ contract UniswapV4Executor is IExecutor, V4Router {
} }
} }
function handleCallback(bytes calldata data)
external
returns (bytes memory)
{
verifyCallback(data);
return _unlockCallback(data);
}
function verifyCallback(bytes calldata) public view onlyPoolManager {}
function _pay(Currency token, address, uint256 amount) internal override { function _pay(Currency token, address, uint256 amount) internal override {
IERC20(Currency.unwrap(token)).safeTransfer( IERC20(Currency.unwrap(token)).safeTransfer(
address(poolManager), amount address(poolManager), amount

View File

@@ -7,11 +7,10 @@ import "./TychoRouterTestSetup.sol";
contract DispatcherExposed is Dispatcher { contract DispatcherExposed is Dispatcher {
function exposedCallExecutor( function exposedCallExecutor(
address executor, address executor,
bytes4 selector,
uint256 amount, uint256 amount,
bytes calldata data bytes calldata data
) external returns (uint256 calculatedAmount) { ) external returns (uint256 calculatedAmount) {
return _callExecutor(executor, selector, amount, data); return _callExecutor(executor, amount, data);
} }
function exposedSetExecutor(address target) external { function exposedSetExecutor(address target) external {
@@ -83,10 +82,7 @@ contract DispatcherTest is Constants {
hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c"; hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c";
uint256 givenAmount = 15 ether; uint256 givenAmount = 15 ether;
uint256 amount = dispatcherExposed.exposedCallExecutor( uint256 amount = dispatcherExposed.exposedCallExecutor(
0xe592557AB9F4A75D992283fD6066312FF013ba3d, 0xe592557AB9F4A75D992283fD6066312FF013ba3d, givenAmount, data
IExecutor.swap.selector,
givenAmount,
data
); );
assert(amount == 35144641819); assert(amount == 35144641819);
} }
@@ -110,10 +106,7 @@ contract DispatcherTest is Constants {
hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c"; hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c";
uint256 givenAmount = 15 ether; uint256 givenAmount = 15 ether;
uint256 amount = dispatcherExposed.exposedCallExecutor( uint256 amount = dispatcherExposed.exposedCallExecutor(
0xe592557AB9F4A75D992283fD6066312FF013ba3d, 0xe592557AB9F4A75D992283fD6066312FF013ba3d, givenAmount, data
bytes4(0),
givenAmount,
data
); );
assert(amount == 35144641819); assert(amount == 35144641819);
} }
@@ -127,10 +120,7 @@ contract DispatcherTest is Constants {
hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593"; hex"5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593";
vm.expectRevert(); vm.expectRevert();
dispatcherExposed.exposedCallExecutor( dispatcherExposed.exposedCallExecutor(
0xe592557AB9F4A75D992283fD6066312FF013ba3d, 0xe592557AB9F4A75D992283fD6066312FF013ba3d, 0, data
IExecutor.swap.selector,
0,
data
); );
} }
@@ -138,7 +128,7 @@ contract DispatcherTest is Constants {
bytes memory data = hex"aabbccdd1111111111111111"; bytes memory data = hex"aabbccdd1111111111111111";
vm.expectRevert(); vm.expectRevert();
dispatcherExposed.exposedCallExecutor( dispatcherExposed.exposedCallExecutor(
0x5d622C9053b8FFB1B3465495C8a42E603632bA70, bytes4(0), 0, data 0x5d622C9053b8FFB1B3465495C8a42E603632bA70, 0, data
); );
} }
} }