diff --git a/foundry/interfaces/ICallback.sol b/foundry/interfaces/ICallback.sol new file mode 100644 index 0000000..38decfa --- /dev/null +++ b/foundry/interfaces/ICallback.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.26; + +interface ICallback { + error UnauthorizedCaller(string exchange, address sender); + + function handleCallback( + bytes calldata data + ) external returns (bytes memory result); +} diff --git a/foundry/interfaces/ICallbackVerifier.sol b/foundry/interfaces/ICallbackVerifier.sol deleted file mode 100644 index 612279c..0000000 --- a/foundry/interfaces/ICallbackVerifier.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.26; - -interface ICallbackVerifier { - error UnauthorizedCaller(string exchange, address sender); - - /** - * @dev This method should revert if the sender is not a verified sender of the exchange. - */ - function verifyCallback( - address sender, - bytes calldata data - ) external returns (uint256 amountOwed, address tokenOwed); -} diff --git a/foundry/interfaces/IExecutor.sol b/foundry/interfaces/IExecutor.sol index 5e595a3..0a60022 100644 --- a/foundry/interfaces/IExecutor.sol +++ b/foundry/interfaces/IExecutor.sol @@ -24,10 +24,6 @@ interface IExecutor { uint256 givenAmount, bytes calldata data ) external payable returns (uint256 calculatedAmount); - - function handleCallback( - bytes calldata callbackData - ) external returns (bytes memory result); } interface IExecutorErrors { diff --git a/foundry/src/CallbackVerificationDispatcher.sol b/foundry/src/CallbackVerificationDispatcher.sol deleted file mode 100644 index 8df5848..0000000 --- a/foundry/src/CallbackVerificationDispatcher.sol +++ /dev/null @@ -1,99 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.26; - -import "@interfaces/ICallbackVerifier.sol"; - -error CallbackVerificationDispatcher__UnapprovedVerifier(); -error CallbackVerificationDispatcher__NonContractVerifier(); - -/** - * @title Dispatch callback verification to external contracts - * @author PropellerHeads Devs - * @dev Provides the ability call external contracts to perform callback - * verification. This allows dynamically adding new supported protocols - * without needing to upgrade any contracts. - * - * Note: Verifier contracts need to implement the ICallbackVerifier interface - */ -contract CallbackVerificationDispatcher { - mapping(address => bool) public callbackVerifiers; - - event CallbackVerifierSet(address indexed callbackVerifier); - event CallbackVerifierRemoved(address indexed callbackVerifier); - - /** - * @dev Adds or replaces an approved callback verifier contract address if it is a - * contract. - * @param target address of the callback verifier contract - */ - function _setCallbackVerifier(address target) internal { - if (target.code.length == 0) { - revert CallbackVerificationDispatcher__NonContractVerifier(); - } - callbackVerifiers[target] = true; - emit CallbackVerifierSet(target); - } - - /** - * @dev Removes an approved callback verifier contract address - * @param target address of the callback verifier contract - */ - function _removeCallbackVerifier(address target) internal { - delete callbackVerifiers[target]; - emit CallbackVerifierRemoved(target); - } - - /** - * @dev Calls a callback verifier. This should revert if the callback verification fails. - */ - // slither-disable-next-line dead-code - function _callVerifyCallback(bytes calldata data) - internal - view - returns (uint256 amountOwed, address tokenOwed) - { - address verifier; - bytes4 decodedSelector; - bytes memory verifierData; - - (verifier, decodedSelector, verifierData) = - _decodeVerifierAndSelector(data); - - if (!callbackVerifiers[verifier]) { - revert CallbackVerificationDispatcher__UnapprovedVerifier(); - } - - bytes4 selector = decodedSelector == bytes4(0) - ? ICallbackVerifier.verifyCallback.selector - : decodedSelector; - - address sender = msg.sender; - - // slither-disable-next-line low-level-calls - (bool success, bytes memory result) = verifier.staticcall( - abi.encodeWithSelector(selector, sender, verifierData) - ); - - if (!success) { - if (result.length > 0) { - revert(string(result)); - } else { - revert("Callback verification failed"); - } - } - - (amountOwed, tokenOwed) = abi.decode(result, (uint256, address)); - } - - // slither-disable-next-line dead-code - function _decodeVerifierAndSelector(bytes calldata data) - internal - pure - returns (address verifier, bytes4 selector, bytes memory verifierData) - { - require(data.length >= 20, "Invalid data length"); - verifier = address(uint160(bytes20(data[:20]))); - selector = bytes4(data[20:24]); - verifierData = data[24:]; - } -} diff --git a/foundry/src/ExecutionDispatcher.sol b/foundry/src/ExecutionDispatcher.sol index 1283b41..c48caca 100644 --- a/foundry/src/ExecutionDispatcher.sol +++ b/foundry/src/ExecutionDispatcher.sol @@ -2,6 +2,7 @@ pragma solidity ^0.8.26; import "@interfaces/IExecutor.sol"; +import "@interfaces/ICallback.sol"; import "forge-std/console.sol"; error ExecutionDispatcher__UnapprovedExecutor(); @@ -95,11 +96,13 @@ contract ExecutionDispatcher { revert ExecutionDispatcher__UnapprovedExecutor(); } - selector = - selector == bytes4(0) ? IExecutor.handleCallback.selector : selector; + selector = selector == bytes4(0) + ? ICallback.handleCallback.selector + : selector; // slither-disable-next-line controlled-delegatecall,low-level-calls - (bool success, bytes memory result) = - executor.delegatecall(abi.encodeWithSelector(selector, data)); + (bool success, bytes memory result) = executor.delegatecall( + abi.encodeWithSelector(selector, data) + ); if (!success) { revert( diff --git a/foundry/src/TychoRouter.sol b/foundry/src/TychoRouter.sol index 8084afc..41e8ac3 100644 --- a/foundry/src/TychoRouter.sol +++ b/foundry/src/TychoRouter.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.26; import "../lib/IWETH.sol"; import "../lib/bytes/LibPrefixLengthEncodedByteArray.sol"; -import "./CallbackVerificationDispatcher.sol"; + import "@openzeppelin/contracts/access/AccessControl.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; @@ -27,7 +27,6 @@ error TychoRouter__MessageValueMismatch(uint256 value, uint256 amount); contract TychoRouter is AccessControl, ExecutionDispatcher, - CallbackVerificationDispatcher, Pausable, ReentrancyGuard, SafeCallback @@ -58,16 +57,21 @@ contract TychoRouter is uint256 public fee; event Withdrawal( - address indexed token, uint256 amount, address indexed receiver + address indexed token, + uint256 amount, + address indexed receiver ); event FeeReceiverSet( - address indexed oldFeeReceiver, address indexed newFeeReceiver + address indexed oldFeeReceiver, + address indexed newFeeReceiver ); event FeeSet(uint256 indexed oldFee, uint256 indexed newFee); - constructor(IPoolManager _poolManager, address _permit2, address weth) - SafeCallback(_poolManager) - { + constructor( + IPoolManager _poolManager, + address _permit2, + address weth + ) SafeCallback(_poolManager) { if (_permit2 == address(0) || weth == address(0)) { revert TychoRouter__AddressZero(); } @@ -181,10 +185,11 @@ contract TychoRouter is * * @return The total amount of the buy token obtained after all swaps have been executed. */ - function _swap(uint256 amountIn, uint256 nTokens, bytes calldata swaps_) - internal - returns (uint256) - { + function _swap( + uint256 amountIn, + uint256 nTokens, + bytes calldata swaps_ + ) internal returns (uint256) { if (swaps_.length == 0) { revert TychoRouter__EmptySwaps(); } @@ -251,10 +256,10 @@ contract TychoRouter is /** * @dev Allows granting roles to multiple accounts in a single call. */ - function batchGrantRole(bytes32 role, address[] memory accounts) - external - onlyRole(DEFAULT_ADMIN_ROLE) - { + function batchGrantRole( + bytes32 role, + address[] memory accounts + ) external onlyRole(DEFAULT_ADMIN_ROLE) { for (uint256 i = 0; i < accounts.length; i++) { _grantRole(role, accounts[i]); } @@ -264,10 +269,9 @@ contract TychoRouter is * @dev Entrypoint to add or replace an approved executor contract address * @param targets address of the executor contract */ - function setExecutors(address[] memory targets) - external - onlyRole(EXECUTOR_SETTER_ROLE) - { + function setExecutors( + address[] memory targets + ) external onlyRole(EXECUTOR_SETTER_ROLE) { for (uint256 i = 0; i < targets.length; i++) { _setExecutor(targets[i]); } @@ -277,42 +281,18 @@ contract TychoRouter is * @dev Entrypoint to remove an approved executor contract address * @param target address of the executor contract */ - function removeExecutor(address target) - external - onlyRole(EXECUTOR_SETTER_ROLE) - { + function removeExecutor( + address target + ) external onlyRole(EXECUTOR_SETTER_ROLE) { _removeExecutor(target); } - /** - * @dev Entrypoint to add or replace an approved callback verifier contract address - * @param target address of the callback verifier contract - */ - function setCallbackVerifier(address target) - external - onlyRole(EXECUTOR_SETTER_ROLE) - { - _setCallbackVerifier(target); - } - - /** - * @dev Entrypoint to remove an approved callback verifier contract address - * @param target address of the callback verifier contract - */ - function removeCallbackVerifier(address target) - external - onlyRole(EXECUTOR_SETTER_ROLE) - { - _removeCallbackVerifier(target); - } - /** * @dev Allows setting the fee receiver. */ - function setFeeReceiver(address newfeeReceiver) - external - onlyRole(FEE_SETTER_ROLE) - { + function setFeeReceiver( + address newfeeReceiver + ) external onlyRole(FEE_SETTER_ROLE) { if (newfeeReceiver == address(0)) revert TychoRouter__AddressZero(); emit FeeReceiverSet(feeReceiver, newfeeReceiver); feeReceiver = newfeeReceiver; @@ -329,10 +309,10 @@ contract TychoRouter is /** * @dev Allows withdrawing any ERC20 funds if funds get stuck in case of a bug. */ - function withdraw(IERC20[] memory tokens, address receiver) - external - onlyRole(FUND_RESCUER_ROLE) - { + function withdraw( + IERC20[] memory tokens, + address receiver + ) external onlyRole(FUND_RESCUER_ROLE) { if (receiver == address(0)) revert TychoRouter__AddressZero(); for (uint256 i = 0; i < tokens.length; i++) { @@ -349,10 +329,9 @@ contract TychoRouter is * @dev Allows withdrawing any NATIVE funds if funds get stuck in case of a bug. * The contract should never hold any NATIVE tokens for security reasons. */ - function withdrawNative(address receiver) - external - onlyRole(FUND_RESCUER_ROLE) - { + function withdrawNative( + address receiver + ) external onlyRole(FUND_RESCUER_ROLE) { if (receiver == address(0)) revert TychoRouter__AddressZero(); uint256 amount = address(this).balance; @@ -380,8 +359,9 @@ contract TychoRouter is * @param amount of WETH to unwrap. */ function _unwrapETH(uint256 amount) internal { - uint256 unwrapAmount = - amount == 0 ? _weth.balanceOf(address(this)) : amount; + uint256 unwrapAmount = amount == 0 + ? _weth.balanceOf(address(this)) + : amount; _weth.withdraw(unwrapAmount); } @@ -400,15 +380,14 @@ contract TychoRouter is bytes calldata msgData ) external { _handleCallback( - bytes4(0), abi.encodePacked(amount0Delta, amount1Delta, msgData) + bytes4(0), + abi.encodePacked(amount0Delta, amount1Delta, msgData) ); } - function _unlockCallback(bytes calldata data) - internal - override - returns (bytes memory) - { + 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:]))); @@ -419,7 +398,7 @@ contract TychoRouter is } // slither-disable-next-line controlled-delegatecall,low-level-calls - (bool success,) = executor.delegatecall( + (bool success, ) = executor.delegatecall( abi.encodeWithSelector(selector, protocolData) ); require(success, "delegatecall to uniswap v4 callback failed"); diff --git a/foundry/src/executors/BalancerV2Executor.sol b/foundry/src/executors/BalancerV2Executor.sol index a328715..d738e92 100644 --- a/foundry/src/executors/BalancerV2Executor.sol +++ b/foundry/src/executors/BalancerV2Executor.sol @@ -2,10 +2,7 @@ pragma solidity ^0.8.26; import "@interfaces/IExecutor.sol"; -import { - IERC20, - SafeERC20 -} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; // slither-disable-next-line solc-version import {IAsset} from "@balancer-labs/v2-interfaces/contracts/vault/IAsset.sol"; // slither-disable-next-line solc-version @@ -19,11 +16,10 @@ contract BalancerV2Executor is IExecutor { address private constant VAULT = 0xBA12222222228d8Ba445958a75a0704d566BF2C8; // slither-disable-next-line locked-ether - function swap(uint256 givenAmount, bytes calldata data) - external - payable - returns (uint256 calculatedAmount) - { + function swap( + uint256 givenAmount, + bytes calldata data + ) external payable returns (uint256 calculatedAmount) { ( IERC20 tokenIn, IERC20 tokenOut, @@ -55,16 +51,17 @@ contract BalancerV2Executor is IExecutor { uint256 limit = 0; - calculatedAmount = - IVault(VAULT).swap(singleSwap, funds, limit, block.timestamp); + calculatedAmount = IVault(VAULT).swap( + singleSwap, + funds, + limit, + block.timestamp + ); } - function handleCallback(bytes calldata data) - external - returns (bytes memory result) - {} - - function _decodeData(bytes calldata data) + function _decodeData( + bytes calldata data + ) internal pure returns ( diff --git a/foundry/src/executors/UniswapV2Executor.sol b/foundry/src/executors/UniswapV2Executor.sol index 870ef74..15522b1 100644 --- a/foundry/src/executors/UniswapV2Executor.sol +++ b/foundry/src/executors/UniswapV2Executor.sol @@ -11,11 +11,10 @@ contract UniswapV2Executor is IExecutor { using SafeERC20 for IERC20; // slither-disable-next-line locked-ether - function swap(uint256 givenAmount, bytes calldata data) - external - payable - returns (uint256 calculatedAmount) - { + function swap( + uint256 givenAmount, + bytes calldata data + ) external payable returns (uint256 calculatedAmount) { address target; address receiver; bool zeroForOne; @@ -33,12 +32,9 @@ contract UniswapV2Executor is IExecutor { } } - function handleCallback(bytes calldata data) - external - returns (bytes memory result) - {} - - function _decodeData(bytes calldata data) + function _decodeData( + bytes calldata data + ) internal pure returns ( @@ -57,20 +53,20 @@ contract UniswapV2Executor is IExecutor { zeroForOne = uint8(data[60]) > 0; } - function _getAmountOut(address target, uint256 amountIn, bool zeroForOne) - internal - view - returns (uint256 amount) - { + function _getAmountOut( + address target, + uint256 amountIn, + bool zeroForOne + ) internal view returns (uint256 amount) { IUniswapV2Pair pair = IUniswapV2Pair(target); uint112 reserveIn; uint112 reserveOut; if (zeroForOne) { // slither-disable-next-line unused-return - (reserveIn, reserveOut,) = pair.getReserves(); + (reserveIn, reserveOut, ) = pair.getReserves(); } else { // slither-disable-next-line unused-return - (reserveOut, reserveIn,) = pair.getReserves(); + (reserveOut, reserveIn, ) = pair.getReserves(); } require(reserveIn > 0 && reserveOut > 0, "L"); diff --git a/foundry/src/executors/UniswapV4Executor.sol b/foundry/src/executors/UniswapV4Executor.sol index 2a2e40b..9077b1a 100644 --- a/foundry/src/executors/UniswapV4Executor.sol +++ b/foundry/src/executors/UniswapV4Executor.sol @@ -2,14 +2,9 @@ pragma solidity ^0.8.26; import "@interfaces/IExecutor.sol"; -import { - IERC20, - SafeERC20 -} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; +import {IERC20, SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; -import { - Currency, CurrencyLibrary -} from "@uniswap/v4-core/src/types/Currency.sol"; +import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; import {IHooks} from "@uniswap/v4-core/src/interfaces/IHooks.sol"; import {V4Router} from "@uniswap/v4-periphery/src/V4Router.sol"; @@ -23,13 +18,16 @@ contract UniswapV4Executor is IExecutor, V4Router { constructor(IPoolManager _poolManager) V4Router(_poolManager) {} - function swap(uint256, bytes calldata data) - external - payable - returns (uint256 calculatedAmount) - { - (address tokenIn, address tokenOut, bool isExactInput, uint256 amount) = - _decodeData(data); + function swap( + uint256, + bytes calldata data + ) external payable returns (uint256 calculatedAmount) { + ( + address tokenIn, + address tokenOut, + bool isExactInput, + uint256 amount + ) = _decodeData(data); uint256 tokenOutBalanceBefore; uint256 tokenInBalanceBefore; @@ -64,7 +62,9 @@ contract UniswapV4Executor is IExecutor, V4Router { return calculatedAmount; } - function _decodeData(bytes calldata data) + function _decodeData( + bytes calldata data + ) internal pure returns ( @@ -74,15 +74,19 @@ contract UniswapV4Executor is IExecutor, V4Router { uint256 amount ) { - (bytes memory actions, bytes[] memory params) = - abi.decode(data, (bytes, bytes[])); + (bytes memory actions, bytes[] memory params) = abi.decode( + data, + (bytes, bytes[]) + ); // First byte of actions determines the swap type uint8 action = uint8(bytes1(actions[0])); if (action == uint8(Actions.SWAP_EXACT_IN_SINGLE)) { - IV4Router.ExactInputSingleParams memory swapParams = - abi.decode(params[0], (IV4Router.ExactInputSingleParams)); + IV4Router.ExactInputSingleParams memory swapParams = abi.decode( + params[0], + (IV4Router.ExactInputSingleParams) + ); tokenIn = swapParams.zeroForOne ? address(uint160(swapParams.poolKey.currency0.toId())) @@ -93,8 +97,10 @@ contract UniswapV4Executor is IExecutor, V4Router { isExactInput = true; amount = swapParams.amountIn; } else if (action == uint8(Actions.SWAP_EXACT_OUT_SINGLE)) { - IV4Router.ExactOutputSingleParams memory swapParams = - abi.decode(params[0], (IV4Router.ExactOutputSingleParams)); + IV4Router.ExactOutputSingleParams memory swapParams = abi.decode( + params[0], + (IV4Router.ExactOutputSingleParams) + ); tokenIn = swapParams.zeroForOne ? address(uint160(swapParams.poolKey.currency0.toId())) @@ -105,18 +111,23 @@ contract UniswapV4Executor is IExecutor, V4Router { isExactInput = false; amount = swapParams.amountOut; } else if (action == uint8(Actions.SWAP_EXACT_IN)) { - IV4Router.ExactInputParams memory swapParams = - abi.decode(params[0], (IV4Router.ExactInputParams)); + IV4Router.ExactInputParams memory swapParams = abi.decode( + params[0], + (IV4Router.ExactInputParams) + ); tokenIn = address(uint160(swapParams.currencyIn.toId())); - PathKey memory lastPath = - swapParams.path[swapParams.path.length - 1]; + PathKey memory lastPath = swapParams.path[ + swapParams.path.length - 1 + ]; tokenOut = address(uint160(lastPath.intermediateCurrency.toId())); isExactInput = true; amount = swapParams.amountIn; } else if (action == uint8(Actions.SWAP_EXACT_OUT)) { - IV4Router.ExactOutputParams memory swapParams = - abi.decode(params[0], (IV4Router.ExactOutputParams)); + IV4Router.ExactOutputParams memory swapParams = abi.decode( + params[0], + (IV4Router.ExactOutputParams) + ); PathKey memory firstPath = swapParams.path[0]; tokenIn = address(uint160(firstPath.intermediateCurrency.toId())); @@ -126,21 +137,18 @@ contract UniswapV4Executor is IExecutor, V4Router { } } - function _pay(Currency token, address payer, uint256 amount) - internal - override - { + function _pay( + Currency token, + address payer, + uint256 amount + ) internal override { IERC20(Currency.unwrap(token)).safeTransfer( - address(poolManager), amount + address(poolManager), + amount ); } function msgSender() public view override returns (address) { return address(this); } - - function handleCallback(bytes calldata data) - external - returns (bytes memory result) - {} } diff --git a/foundry/test/CallbackVerificationDispatcher.t.sol b/foundry/test/CallbackVerificationDispatcher.t.sol deleted file mode 100644 index fd2577e..0000000 --- a/foundry/test/CallbackVerificationDispatcher.t.sol +++ /dev/null @@ -1,180 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.26; - -import "@src/CallbackVerificationDispatcher.sol"; -import "./TychoRouterTestSetup.sol"; - -contract CallbackVerificationDispatcherExposed is - CallbackVerificationDispatcher -{ - function exposedCallVerifier(bytes calldata data) - external - view - returns (uint256 amountOwed, address tokenOwed) - { - return _callVerifyCallback(data); - } - - function exposedDecodeVerifierAndSelector(bytes calldata data) - external - pure - returns (address executor, bytes4 selector, bytes memory protocolData) - { - return _decodeVerifierAndSelector(data); - } - - function exposedSetCallbackVerifier(address target) external { - _setCallbackVerifier(target); - } - - function exposedRemoveCallbackVerifier(address target) external { - _removeCallbackVerifier(target); - } -} - -contract CallbackVerificationDispatcherTest is Constants { - CallbackVerificationDispatcherExposed dispatcherExposed; - - event CallbackVerifierSet(address indexed callbackVerifier); - event CallbackVerifierRemoved(address indexed callbackVerifier); - - function setUp() public { - uint256 forkBlock = 20673900; - vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock); - dispatcherExposed = new CallbackVerificationDispatcherExposed(); - deal(WETH_ADDR, address(dispatcherExposed), 15 ether); - deployDummyContract(); - } - - function testSetValidVerifier() public { - vm.expectEmit(); - // Define the event we expect to be emitted at the next step - emit CallbackVerifierSet(DUMMY); - dispatcherExposed.exposedSetCallbackVerifier(DUMMY); - assert(dispatcherExposed.callbackVerifiers(DUMMY) == true); - } - - function testRemoveVerifier() public { - dispatcherExposed.exposedSetCallbackVerifier(DUMMY); - vm.expectEmit(); - // Define the event we expect to be emitted at the next step - emit CallbackVerifierRemoved(DUMMY); - dispatcherExposed.exposedRemoveCallbackVerifier(DUMMY); - assert(dispatcherExposed.callbackVerifiers(DUMMY) == false); - } - - function testRemoveUnSetVerifier() public { - dispatcherExposed.exposedRemoveCallbackVerifier(BOB); - assert(dispatcherExposed.callbackVerifiers(BOB) == false); - } - - function testSetVerifierNonContract() public { - vm.expectRevert( - abi.encodeWithSelector( - CallbackVerificationDispatcher__NonContractVerifier.selector - ) - ); - dispatcherExposed.exposedSetCallbackVerifier(BOB); - } - - function testCallVerifierSuccess() public { - // For this test, we can use any callback verifier and any calldata that we - // know works for this verifier. We don't care about which calldata/executor, - // since we are only testing the functionality of the staticcall and not - // the inner verifier. - // Thus, this test case designed from scratch using previously-deployed - // Maverick callback verifier. Looking at the code, we can easily design - // passing calldata. - dispatcherExposed.exposedSetCallbackVerifier( - address(0x2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8) - ); - bytes memory data = - hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e876b20f8a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; - vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360)); - (uint256 amountOwed, address tokenOwed) = - dispatcherExposed.exposedCallVerifier(data); - vm.stopPrank(); - - // The specific values returned are not important for this test. - // The goal is to ensure correct calling of the Maverick verifier's. - // Since the verifier's output format has changed, the asserted values may not be meaningful. - // Full validation of the functionality will be covered in the integration tests. - assert(amountOwed == 1); - assert(tokenOwed == address(0x0000000000000000000000000000000000000001)); - } - - function testCallVerifierNoSelector() public { - // This test is exactly the same as testCallVerifierSuccess, except that the - // fn selector is not explicitly passed. The test should still pass using the - // default selector. - dispatcherExposed.exposedSetCallbackVerifier( - address(0x2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8) - ); - - // Pass all-zero selector. This should default to the verifyCallback selector - bytes memory data = - hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; - vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360)); - (uint256 amountOwed, address tokenOwed) = - dispatcherExposed.exposedCallVerifier(data); - vm.stopPrank(); - - // The specific values returned are not important for this test. - // The goal is to ensure correct calling of the Maverick verifier's. - // Since the verifier's output format has changed, the asserted values may not be meaningful. - // Full validation of the functionality will be covered in the integration tests. - assert(amountOwed == 1); - assert(tokenOwed == address(0x0000000000000000000000000000000000000001)); - } - - function testCallVerifierBadSelector() public { - // A bad selector is provided to an approved executor - causing the call - // itself to fail. Make sure this actually reverts. - dispatcherExposed.exposedSetCallbackVerifier( - address(0x2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8) - ); - vm.startPrank(address(0xD0b2F5018B5D22759724af6d4281AC0B13266360)); - bytes memory data = - hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8aa0000000000"; - vm.expectRevert(bytes("Callback verification failed")); - dispatcherExposed.exposedCallVerifier(data); - vm.stopPrank(); - } - - function testCallVerifierParseRevertMessage() public { - // Verification should fail because caller is not a Maverick pool - // Check that we correctly parse the revert message - dispatcherExposed.exposedSetCallbackVerifier( - address(0x2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8) - ); - bytes memory data = - hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; - vm.expectRevert( - abi.encodeWithSignature( - "Error(string)", "Must call from a Maverick Factory Pool" - ) - ); - dispatcherExposed.exposedCallVerifier(data); - } - - function testCallVerifierUnapprovedVerifier() public { - bytes memory data = - hex"5d622C9053b8FFB1B3465495C8a42E603632bA70aabbccdd1111111111111111"; - vm.expectRevert(); - dispatcherExposed.exposedCallVerifier(data); - } - - function testDecodeVerifierAndSelector() public view { - bytes memory data = - hex"2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e876b20f8aA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; - (address executor, bytes4 selector, bytes memory verifierData) = - dispatcherExposed.exposedDecodeVerifierAndSelector(data); - assert(executor == address(0x2C960bD1CFE09A26105ad3C351bEa0a3fAD0F8e8)); - assert(selector == bytes4(0x76b20f8a)); - // Direct bytes comparison not supported - must use keccak - assert( - keccak256(verifierData) - == keccak256(hex"A0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") - ); - } -} diff --git a/foundry/test/TychoRouter.t.sol b/foundry/test/TychoRouter.t.sol index bfbca0b..ef3b69b 100644 --- a/foundry/test/TychoRouter.t.sol +++ b/foundry/test/TychoRouter.t.sol @@ -18,7 +18,9 @@ contract TychoRouterTest is TychoRouterTestSetup { event CallbackVerifierSet(address indexed callbackVerifier); event Withdrawal( - address indexed token, uint256 amount, address indexed receiver + address indexed token, + uint256 amount, + address indexed receiver ); function testSetExecutorsValidRole() public { @@ -63,31 +65,6 @@ contract TychoRouterTest is TychoRouterTestSetup { tychoRouter.setExecutors(executors); } - function testSetVerifierValidRole() public { - vm.startPrank(EXECUTOR_SETTER); - tychoRouter.setCallbackVerifier(DUMMY); - vm.stopPrank(); - assert(tychoRouter.callbackVerifiers(DUMMY) == true); - } - - function testRemoveVerifierValidRole() public { - vm.startPrank(EXECUTOR_SETTER); - tychoRouter.setCallbackVerifier(DUMMY); - tychoRouter.removeCallbackVerifier(DUMMY); - vm.stopPrank(); - assert(tychoRouter.callbackVerifiers(DUMMY) == false); - } - - function testRemoveVerifierMissingSetterRole() public { - vm.expectRevert(); - tychoRouter.removeCallbackVerifier(BOB); - } - - function testSetVerifierMissingSetterRole() public { - vm.expectRevert(); - tychoRouter.setCallbackVerifier(DUMMY); - } - function testWithdrawNative() public { vm.startPrank(FUND_RESCUER); // Send 100 ether to tychoRouter @@ -239,7 +216,10 @@ contract TychoRouterTest is TychoRouterTestSetup { deal(WETH_ADDR, tychoRouterAddr, amountIn); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -276,7 +256,10 @@ contract TychoRouterTest is TychoRouterTestSetup { address(usv2Executor), bytes4(0), encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ) ); @@ -315,7 +298,10 @@ contract TychoRouterTest is TychoRouterTestSetup { address(usv2Executor), bytes4(0), encodeUniswapV2Swap( - WETH_ADDR, WETH_WBTC_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_WBTC_POOL, + tychoRouterAddr, + false ) ); // WBTC -> USDC @@ -326,7 +312,10 @@ contract TychoRouterTest is TychoRouterTestSetup { address(usv2Executor), bytes4(0), encodeUniswapV2Swap( - WBTC_ADDR, USDC_WBTC_POOL, tychoRouterAddr, true + WBTC_ADDR, + USDC_WBTC_POOL, + tychoRouterAddr, + true ) ); // WETH -> DAI @@ -337,7 +326,10 @@ contract TychoRouterTest is TychoRouterTestSetup { address(usv2Executor), bytes4(0), encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ) ); @@ -373,7 +365,10 @@ contract TychoRouterTest is TychoRouterTestSetup { ) = handlePermit2Approval(WETH_ADDR, amountIn); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -426,7 +421,10 @@ contract TychoRouterTest is TychoRouterTestSetup { ) = handlePermit2Approval(WETH_ADDR, amountIn); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -485,7 +483,10 @@ contract TychoRouterTest is TychoRouterTestSetup { ) = handlePermit2Approval(WETH_ADDR, amountIn); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -517,7 +518,10 @@ contract TychoRouterTest is TychoRouterTestSetup { assertEq(amountOut, expectedAmount); uint256 daiBalance = IERC20(DAI_ADDR).balanceOf(ALICE); assertEq(daiBalance, expectedAmount); - assertEq(IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER), 26598819248184436997); + assertEq( + IERC20(DAI_ADDR).balanceOf(FEE_RECEIVER), + 26598819248184436997 + ); vm.stopPrank(); } @@ -530,19 +534,22 @@ contract TychoRouterTest is TychoRouterTestSetup { vm.startPrank(ALICE); - IAllowanceTransfer.PermitSingle memory emptyPermitSingle = - IAllowanceTransfer.PermitSingle({ - details: IAllowanceTransfer.PermitDetails({ - token: address(0), - amount: 0, - expiration: 0, - nonce: 0 - }), - spender: address(0), - sigDeadline: 0 - }); + IAllowanceTransfer.PermitSingle + memory emptyPermitSingle = IAllowanceTransfer.PermitSingle({ + details: IAllowanceTransfer.PermitDetails({ + token: address(0), + amount: 0, + expiration: 0, + nonce: 0 + }), + spender: address(0), + sigDeadline: 0 + }); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -591,8 +598,12 @@ contract TychoRouterTest is TychoRouterTestSetup { bytes memory signature ) = handlePermit2Approval(DAI_ADDR, amountIn); - bytes memory protocolData = - encodeUniswapV2Swap(DAI_ADDR, WETH_DAI_POOL, tychoRouterAddr, true); + bytes memory protocolData = encodeUniswapV2Swap( + DAI_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + true + ); bytes memory swap = encodeSwap( uint8(0), @@ -636,7 +647,11 @@ contract TychoRouterTest is TychoRouterTestSetup { uint256 expAmountOut = 1205_128428842122129186; //Swap 1 WETH for 1205.12 DAI bool zeroForOne = false; bytes memory protocolData = encodeUniswapV3Swap( - WETH_ADDR, DAI_ADDR, tychoRouterAddr, DAI_WETH_USV3, zeroForOne + WETH_ADDR, + DAI_ADDR, + tychoRouterAddr, + DAI_WETH_USV3, + zeroForOne ); bytes memory swap = encodeSwap( uint8(0), @@ -678,7 +693,7 @@ contract TychoRouterTest is TychoRouterTestSetup { // but manually replacing the executor address // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` - (bool success,) = tychoRouterAddr.call( + (bool success, ) = tychoRouterAddr.call( hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d481bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfbc3000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041f2740fde9662d8bc1f8fe8e8fc29447c1832d625f06f4a56ee5103ad555c12323af5d50eb840f73d17873383ae3b7573956d5df7b2bf76bddba768c2837894a51b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" ); @@ -707,7 +722,7 @@ contract TychoRouterTest is TychoRouterTestSetup { // but manually replacing the executor address // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` - (bool success,) = tychoRouterAddr.call{value: 1 ether}( + (bool success, ) = tychoRouterAddr.call{value: 1 ether}( hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a764000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4806b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfa73000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041c36406a750c499ac7f79f7666650f0d4f20fc27bb49ab68121c0be6554cb5cab6caf90dc3aab2e21083a8fa46976521a1e9df41ce74be59abf03e0d3691541e91c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00020000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950000000000" ); @@ -736,7 +751,7 @@ contract TychoRouterTest is TychoRouterTestSetup { // but manually replacing the executor address // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` - (bool success,) = tychoRouterAddr.call( + (bool success, ) = tychoRouterAddr.call( hex"4860f9ed0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000006b175474e89094c44da98b954eedeac495271d0f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000003000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc20000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000a2a15d09519be000000000000000000000000000000000000000000000000000000000000067d4809800000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfaa000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000004146411c70ec7fee0d5d260803cb220f5365792426c5d94f7a0a4d37abb05205752c5418b1fadd059570a71f0911814e546728e1f21876f2a1c6d38d34bd235fd61c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005c005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fa478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d013950100000000" ); @@ -767,7 +782,7 @@ contract TychoRouterTest is TychoRouterTestSetup { // but manually replacing the executor address // `5c2f5a71f67c01775180adc06909288b4c329308` with the one in this test // `5615deb798bb3e4dfa0139dfa1b3d433cc23b72f` - (bool success,) = tychoRouterAddr.call( + (bool success, ) = tychoRouterAddr.call( hex"4860f9ed0000000000000000000000000000000000000000000000000de0b6b3a7640000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000cd09f75e2bf2a4d11f3ab23f1389fcc1621c0cc2000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000000000067d4810d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000003ede3eca2a72b3aecc820e955b36f38437d013950000000000000000000000000000000000000000000000000000000067acfb15000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000041ecaab75f0791c9683b001ea2f0e01a0a6aaf03e6e49c83e9c8a8e588a38e3be9230d962926628ffbf6a5370cda559ff0e7876a63ed38eebe33dbef5b5e2e46ef1b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000170005a00028000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a478c2975ab1ea89e8196811f51a7b7ade33eb113ede3eca2a72b3aecc820e955b36f38437d0139500005a00010000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625abc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2bb2b8038a1640196fbe3e38816f3e67cba72d9403ede3eca2a72b3aecc820e955b36f38437d0139500005a02030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab6b175474e89094c44da98b954eedeac495271d0fae461ca67b15dc8dc81ce7615e0320da1a9ab8d53ede3eca2a72b3aecc820e955b36f38437d0139501005a01030000005615deb798bb3e4dfa0139dfa1b3d433cc23b72fbd0625ab2260fac5e5542a773aa44fbcfedf7c193bc2c599004375dff511095cc5a197a54140a24efef3a4163ede3eca2a72b3aecc820e955b36f38437d013950100000000000000000000000000000000" ); @@ -797,7 +812,10 @@ contract TychoRouterTest is TychoRouterTestSetup { ) = handlePermit2Approval(WETH_ADDR, amountIn); bytes memory protocolData = encodeUniswapV2Swap( - WETH_ADDR, WETH_DAI_POOL, tychoRouterAddr, false + WETH_ADDR, + WETH_DAI_POOL, + tychoRouterAddr, + false ); bytes memory swap = encodeSwap( @@ -814,7 +832,8 @@ contract TychoRouterTest is TychoRouterTestSetup { vm.expectRevert( abi.encodeWithSelector( - TychoRouter__AmountInNotFullySpent.selector, 400000000000000000 + TychoRouter__AmountInNotFullySpent.selector, + 400000000000000000 ) ); @@ -840,7 +859,12 @@ contract TychoRouterTest is TychoRouterTestSetup { deal(USDE_ADDR, tychoRouterAddr, amountIn); bytes memory protocolData = UniswapV4Utils.encodeExactInputSingle( - USDE_ADDR, USDT_ADDR, 100, true, 1, uint128(amountIn) + USDE_ADDR, + USDT_ADDR, + 100, + true, + 1, + uint128(amountIn) ); // add executor and selector for callback