chore: rename _computePairAddress to _verifyPairAddress, add fake v2 pool in v2 test file
This commit is contained in:
@@ -4,7 +4,6 @@ pragma solidity ^0.8.26;
|
||||
import "@src/executors/UniswapV2Executor.sol";
|
||||
import {Test} from "../../lib/forge-std/src/Test.sol";
|
||||
import {Constants} from "../Constants.sol";
|
||||
import {MockUniswapV2Pool} from "../mock/MockUniswapV2Pool.sol";
|
||||
|
||||
contract UniswapV2ExecutorExposed is UniswapV2Executor {
|
||||
function decodeParams(bytes calldata data)
|
||||
@@ -28,12 +27,18 @@ contract UniswapV2ExecutorExposed is UniswapV2Executor {
|
||||
return _getAmountOut(target, amountIn, zeroForOne);
|
||||
}
|
||||
|
||||
function computePairAddress(address target)
|
||||
external
|
||||
view
|
||||
returns (address pair)
|
||||
{
|
||||
return _computePairAddress(target);
|
||||
function verifyPairAddress(address target) external view {
|
||||
_verifyPairAddress(target);
|
||||
}
|
||||
}
|
||||
|
||||
contract FakeUniswapV2Pool {
|
||||
address public token0;
|
||||
address public token1;
|
||||
|
||||
constructor(address _tokenA, address _tokenB) {
|
||||
token0 = _tokenA < _tokenB ? _tokenA : _tokenB;
|
||||
token1 = _tokenA < _tokenB ? _tokenB : _tokenA;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,19 +76,14 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants {
|
||||
uniswapV2Exposed.decodeParams(invalidParams);
|
||||
}
|
||||
|
||||
function testComputePairAddress() public view {
|
||||
address computedPair =
|
||||
uniswapV2Exposed.computePairAddress(WETH_DAI_POOL);
|
||||
assertEq(computedPair, WETH_DAI_POOL);
|
||||
function testVerifyPairAddress() public view {
|
||||
uniswapV2Exposed.verifyPairAddress(WETH_DAI_POOL);
|
||||
}
|
||||
|
||||
function testComputePairAddressInvalid() public {
|
||||
address tokenA = WETH_ADDR;
|
||||
address tokenB = DAI_ADDR;
|
||||
address maliciousPool = address(new MockUniswapV2Pool(tokenA, tokenB));
|
||||
address computedPair =
|
||||
uniswapV2Exposed.computePairAddress(maliciousPool);
|
||||
assertNotEq(computedPair, maliciousPool);
|
||||
function test_RevertIf_InvalidTarget() public {
|
||||
address fakePool = address(new FakeUniswapV2Pool(WETH_ADDR, DAI_ADDR));
|
||||
vm.expectRevert(UniswapV2Executor__InvalidTarget.selector);
|
||||
uniswapV2Exposed.verifyPairAddress(fakePool);
|
||||
}
|
||||
|
||||
function testAmountOut() public view {
|
||||
@@ -145,13 +145,12 @@ contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants {
|
||||
assertGe(finalBalance, amountOut);
|
||||
}
|
||||
|
||||
function test_RevertIf_InvalidTarget() public {
|
||||
function test_RevertIf_Swap_InvalidTarget() public {
|
||||
uint256 amountIn = 10 ** 18;
|
||||
bool zeroForOne = false;
|
||||
address maliciousPool =
|
||||
address(new MockUniswapV2Pool(WETH_ADDR, DAI_ADDR));
|
||||
address fakePool = address(new FakeUniswapV2Pool(WETH_ADDR, DAI_ADDR));
|
||||
bytes memory protocolData =
|
||||
abi.encodePacked(WETH_ADDR, maliciousPool, BOB, zeroForOne);
|
||||
abi.encodePacked(WETH_ADDR, fakePool, BOB, zeroForOne);
|
||||
|
||||
deal(WETH_ADDR, address(uniswapV2Exposed), amountIn);
|
||||
vm.expectRevert(UniswapV2Executor__InvalidTarget.selector);
|
||||
|
||||
@@ -23,12 +23,13 @@ contract UniswapV3ExecutorExposed is UniswapV3Executor {
|
||||
return _decodeData(data);
|
||||
}
|
||||
|
||||
function computePairAddress(address tokenA, address tokenB, uint24 fee)
|
||||
external
|
||||
view
|
||||
returns (address)
|
||||
{
|
||||
return _computePairAddress(tokenA, tokenB, fee);
|
||||
function verifyPairAddress(
|
||||
address tokenA,
|
||||
address tokenB,
|
||||
uint24 fee,
|
||||
address target
|
||||
) external view {
|
||||
_verifyPairAddress(tokenA, tokenB, fee, target);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,10 +78,10 @@ contract UniswapV3ExecutorTest is Test, Constants {
|
||||
uniswapV3Exposed.decodeData(invalidParams);
|
||||
}
|
||||
|
||||
function testComputePairAddress() public view {
|
||||
address computedPair =
|
||||
uniswapV3Exposed.computePairAddress(WETH_ADDR, DAI_ADDR, 3000);
|
||||
assertEq(computedPair, DAI_WETH_USV3);
|
||||
function testVerifyPairAddress() public view {
|
||||
uniswapV3Exposed.verifyPairAddress(
|
||||
WETH_ADDR, DAI_ADDR, 3000, DAI_WETH_USV3
|
||||
);
|
||||
}
|
||||
|
||||
function testUSV3Callback() public {
|
||||
@@ -127,18 +128,18 @@ contract UniswapV3ExecutorTest is Test, Constants {
|
||||
assertGe(IERC20(DAI_ADDR).balanceOf(address(this)), expAmountOut);
|
||||
}
|
||||
|
||||
function test_RevertIf_InvalidTargetV3() public {
|
||||
function test_RevertIf_InvalidTarget() public {
|
||||
uint256 amountIn = 10 ** 18;
|
||||
deal(WETH_ADDR, address(uniswapV3Exposed), amountIn);
|
||||
bool zeroForOne = false;
|
||||
address maliciousPool = DUMMY;
|
||||
address fakePool = DUMMY; // Contract with minimal code
|
||||
|
||||
bytes memory protocolData = abi.encodePacked(
|
||||
WETH_ADDR,
|
||||
DAI_ADDR,
|
||||
uint24(3000),
|
||||
address(this),
|
||||
maliciousPool,
|
||||
fakePool,
|
||||
zeroForOne
|
||||
);
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// SPDX-License-Identifier: Unlicense
|
||||
pragma solidity ^0.8.26;
|
||||
|
||||
// Mock for the UniswapV2Pool contract, it is expected to have malicious behavior
|
||||
contract MockUniswapV2Pool {
|
||||
address public token0;
|
||||
address public token1;
|
||||
|
||||
constructor(address _tokenA, address _tokenB) {
|
||||
token0 = _tokenA < _tokenB ? _tokenA : _tokenB;
|
||||
token1 = _tokenA < _tokenB ? _tokenB : _tokenA;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user