feat: Support uniswap v4 callback in TychoRouter

Make TychoRouter inherit from SafeCallback and then delegatecall to the UniswapV4 executor
Add a test for this. I had to update the block of our forked network in the tests. Because of this I had to update all the asserts in previous tests

Had to change the optimizer_runs in foundry.toml because of weird Yul errors when compiling

--- don't change below this line ---
ENG-4223 Took 1 hour 21 minutes

Took 7 seconds


Took 35 seconds
This commit is contained in:
Diana Carvalho
2025-02-12 19:30:21 +00:00
parent 29eb50d0a1
commit 591d73ba71
7 changed files with 101 additions and 30 deletions

View File

@@ -15,6 +15,8 @@ import "@uniswap/v3-updated/CallbackValidationV2.sol";
import "./ExecutionDispatcher.sol";
import "./CallbackVerificationDispatcher.sol";
import {LibSwap} from "../lib/LibSwap.sol";
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol";
import {SafeCallback} from "@uniswap/v4-periphery/src/base/SafeCallback.sol";
error TychoRouter__WithdrawalFailed();
error TychoRouter__AddressZero();
@@ -28,7 +30,8 @@ contract TychoRouter is
ExecutionDispatcher,
CallbackVerificationDispatcher,
Pausable,
ReentrancyGuard
ReentrancyGuard,
SafeCallback
{
IAllowanceTransfer public immutable permit2;
IWETH private immutable _weth;
@@ -65,7 +68,12 @@ contract TychoRouter is
address private immutable _usv3Factory;
constructor(address _permit2, address weth, address usv3Factory) {
constructor(
IPoolManager _poolManager,
address _permit2,
address weth,
address usv3Factory
) SafeCallback(_poolManager) {
if (
_permit2 == address(0) || weth == address(0)
|| usv3Factory == address(0)
@@ -434,4 +442,22 @@ contract TychoRouter is
return (amountIn, tokenIn);
}
function _unlockCallback(bytes calldata data)
internal
override
returns (bytes memory)
{
require(data.length >= 20, "Invalid data length");
bytes4 selector = bytes4(data[data.length - 24:data.length - 20]);
address executor = address(uint160(bytes20(data[data.length - 20:])));
bytes memory protocolData = data[:data.length - 24];
// slither-disable-next-line controlled-delegatecall,low-level-calls
(bool success,) = executor.delegatecall(
abi.encodeWithSelector(selector, protocolData)
);
require(success, "delegatecall to uniswap v4 callback failed");
return "";
}
}