feat: Add executor and selector to Swap

Add tests to Swap
Modify ExecutionDispatcher and TychoRouter to account for these changes

--- don't change below this line ---
ENG-4041 Took 57 minutes


Took 10 seconds
This commit is contained in:
Diana Carvalho
2025-01-28 12:19:07 +00:00
parent dfa0f7d176
commit c2347ac79e
7 changed files with 176 additions and 86 deletions

View File

@@ -51,28 +51,21 @@ contract ExecutionDispatcher {
* protocol-specific data required by the executor.
*/
// slither-disable-next-line dead-code
function _callExecutor(uint256 amount, bytes calldata data)
internal
returns (uint256 calculatedAmount)
{
address executor;
bytes4 decodedSelector;
bytes memory protocolData;
(executor, decodedSelector, protocolData) =
_decodeExecutorAndSelector(data);
function _callExecutor(
address executor,
bytes4 selector,
uint256 amount,
bytes calldata data
) internal returns (uint256 calculatedAmount) {
if (!executors[executor]) {
revert ExecutionDispatcher__UnapprovedExecutor();
}
bytes4 selector = decodedSelector == bytes4(0)
? IExecutor.swap.selector
: decodedSelector;
selector = selector == bytes4(0) ? IExecutor.swap.selector : selector;
// slither-disable-next-line low-level-calls
(bool success, bytes memory result) = executor.delegatecall(
abi.encodeWithSelector(selector, amount, protocolData)
abi.encodeWithSelector(selector, amount, data)
);
if (!success) {
@@ -87,16 +80,4 @@ contract ExecutionDispatcher {
calculatedAmount = abi.decode(result, (uint256));
}
// slither-disable-next-line dead-code
function _decodeExecutorAndSelector(bytes calldata data)
internal
pure
returns (address executor, bytes4 selector, bytes memory protocolData)
{
require(data.length >= 24, "Invalid data length");
executor = address(uint160(bytes20(data[:20])));
selector = bytes4(data[20:24]);
protocolData = data[24:];
}
}

View File

@@ -1,40 +0,0 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
library Swap {
/// Returns the InToken index into an array of tokens
function tokenInIndex(bytes calldata swap)
internal
pure
returns (uint8 res)
{
res = uint8(swap[0]);
}
/// The OutToken index into an array of tokens
function tokenOutIndex(bytes calldata swap)
internal
pure
returns (uint8 res)
{
res = uint8(swap[1]);
}
/// The relative amount of token quantity routed into this swap
function splitPercentage(bytes calldata swap)
internal
pure
returns (uint24 res)
{
res = uint24(bytes3(swap[2:5]));
}
/// Remaining bytes are interpreted as protocol data
function protocolData(bytes calldata swap)
internal
pure
returns (bytes calldata res)
{
res = swap[5:];
}
}

View File

@@ -11,7 +11,7 @@ import "@permit2/src/interfaces/IAllowanceTransfer.sol";
import "./ExecutionDispatcher.sol";
import "./CallbackVerificationDispatcher.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import {Swap} from "./Swap.sol";
import {LibSwap} from "../lib/LibSwap.sol";
error TychoRouter__WithdrawalFailed();
error TychoRouter__AddressZero();
@@ -29,7 +29,7 @@ contract TychoRouter is
using SafeERC20 for IERC20;
using LibPrefixLengthEncodedByteArray for bytes;
using Swap for bytes;
using LibSwap for bytes;
//keccak256("NAME_OF_ROLE") : save gas on deployment
bytes32 public constant EXECUTOR_SETTER_ROLE =
@@ -185,8 +185,12 @@ contract TychoRouter is
currentAmountIn = split > 0
? (amounts[tokenInIndex] * split) / 0xffffff
: remainingAmounts[tokenInIndex];
currentAmountOut =
_callExecutor(currentAmountIn, swapData.protocolData());
currentAmountOut = _callExecutor(
swapData.executor(),
swapData.executorSelector(),
currentAmountIn,
swapData.protocolData()
);
amounts[tokenOutIndex] += currentAmountOut;
remainingAmounts[tokenOutIndex] += currentAmountOut;
remainingAmounts[tokenInIndex] -= currentAmountIn;