From c361e1515df25a746ac4f2821e9e243926ff0c8e Mon Sep 17 00:00:00 2001 From: TAMARA LIPOWSKI Date: Tue, 25 Mar 2025 12:43:00 +0100 Subject: [PATCH] refactor: remove duplicate code The callback verification was already implemented in _verifyPairAddress. --- .../lib/v3-updated/CallbackValidationV2.sol | 44 -------------- foundry/lib/v3-updated/PoolAddressV2.sol | 57 ------------------- foundry/src/executors/UniswapV3Executor.sol | 6 +- 3 files changed, 1 insertion(+), 106 deletions(-) delete mode 100644 foundry/lib/v3-updated/CallbackValidationV2.sol delete mode 100644 foundry/lib/v3-updated/PoolAddressV2.sol diff --git a/foundry/lib/v3-updated/CallbackValidationV2.sol b/foundry/lib/v3-updated/CallbackValidationV2.sol deleted file mode 100644 index 0a24242..0000000 --- a/foundry/lib/v3-updated/CallbackValidationV2.sol +++ /dev/null @@ -1,44 +0,0 @@ -// Updated v3 lib to solidity >=0.7.6 - -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity >=0.7.6; - -import "./PoolAddressV2.sol"; -import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; - -/// @notice Provides validation for callbacks from Uniswap V3 Pools -library CallbackValidationV2 { - /// @notice Returns the address of a valid Uniswap V3 Pool - /// @param factory The contract address of the Uniswap V3 factory - /// @param tokenA The contract address of either token0 or token1 - /// @param tokenB The contract address of the other token - /// @param fee The fee collected upon every swap in the pool, denominated in hundredths of a bip - /// @return pool The V3 pool contract address - function verifyCallback( - address factory, - address tokenA, - address tokenB, - uint24 fee, - bytes32 initCode - ) internal view returns (IUniswapV3Pool pool) { - return - verifyCallback( - factory, - PoolAddressV2.getPoolKey(tokenA, tokenB, fee), - initCode - ); - } - - /// @notice Returns the address of a valid Uniswap V3 Pool - /// @param factory The contract address of the Uniswap V3 factory - /// @param poolKey The identifying key of the V3 pool - /// @return pool The V3 pool contract address - function verifyCallback( - address factory, - PoolAddressV2.PoolKey memory poolKey, - bytes32 initCode - ) internal view returns (IUniswapV3Pool pool) { - pool = IUniswapV3Pool(PoolAddressV2.computeAddress(factory, poolKey, initCode)); - require(msg.sender == address(pool), "CV"); - } -} diff --git a/foundry/lib/v3-updated/PoolAddressV2.sol b/foundry/lib/v3-updated/PoolAddressV2.sol deleted file mode 100644 index d30eedb..0000000 --- a/foundry/lib/v3-updated/PoolAddressV2.sol +++ /dev/null @@ -1,57 +0,0 @@ -// Updated v3 lib to solidity >=0.7.6 - -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity >=0.5.0; - -/// @title Provides functions for deriving a pool address from the factory, tokens, and the fee -library PoolAddressV2 { - - /// @notice The identifying key of the pool - struct PoolKey { - address token0; - address token1; - uint24 fee; - } - - /// @notice Returns PoolKey: the ordered tokens with the matched fee levels - /// @param tokenA The first token of a pool, unsorted - /// @param tokenB The second token of a pool, unsorted - /// @param fee The fee level of the pool - /// @return Poolkey The pool details with ordered token0 and token1 assignments - function getPoolKey(address tokenA, address tokenB, uint24 fee) - internal - pure - returns (PoolKey memory) - { - if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA); - return PoolKey({token0: tokenA, token1: tokenB, fee: fee}); - } - - /// @notice Deterministically computes the pool address given the factory and PoolKey - /// @param factory The Uniswap V3 factory contract address - /// @param key The PoolKey - /// @return pool The contract address of the V3 pool - function computeAddress(address factory, PoolKey memory key, bytes32 initCode) - internal - pure - returns (address pool) - { - require(key.token0 < key.token1); - pool = address( - uint160( - uint256( - keccak256( - abi.encodePacked( - hex"ff", - factory, - keccak256( - abi.encode(key.token0, key.token1, key.fee) - ), - initCode - ) - ) - ) - ) - ); - } -} diff --git a/foundry/src/executors/UniswapV3Executor.sol b/foundry/src/executors/UniswapV3Executor.sol index 050bb83..1d46c08 100644 --- a/foundry/src/executors/UniswapV3Executor.sol +++ b/foundry/src/executors/UniswapV3Executor.sol @@ -4,7 +4,6 @@ pragma solidity ^0.8.26; import "@interfaces/IExecutor.sol"; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol"; -import "@uniswap/v3-updated/CallbackValidationV2.sol"; import "@interfaces/ICallback.sol"; error UniswapV3Executor__InvalidDataLength(); @@ -106,10 +105,7 @@ contract UniswapV3Executor is IExecutor, ICallback { 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, initCode - ); + _verifyPairAddress(tokenIn, tokenOut, poolFee, msg.sender); } function uniswapV3SwapCallback(