Merge branch 'main' into router/hr/ENG-4171-Implement-Pause

This commit is contained in:
Harsh Vardhan Roy
2025-01-27 20:40:36 +05:30
committed by GitHub
19 changed files with 586 additions and 95 deletions

3
.gitmodules vendored
View File

@@ -4,3 +4,6 @@
[submodule "foundry/lib/permit2"]
path = foundry/lib/permit2
url = https://github.com/Uniswap/permit2
[submodule "foundry/lib/v2-core"]
path = foundry/lib/v2-core
url = https://github.com/uniswap/v2-core

View File

@@ -1,3 +1,42 @@
## [0.16.0](https://github.com/propeller-heads/tycho-execution/compare/0.15.0...0.16.0) (2025-01-27)
### Features
* add balance v2 encoder test ([9cecea8](https://github.com/propeller-heads/tycho-execution/commit/9cecea896833b27ec855f1ea4d981dde64f869ac))
### Bug Fixes
* async ([7c198ff](https://github.com/propeller-heads/tycho-execution/commit/7c198fff92bb6bb8858912008d0bb40364d8bcd6))
## [0.15.0](https://github.com/propeller-heads/tycho-execution/compare/0.14.0...0.15.0) (2025-01-24)
### Features
* UniswapV2 SwapExecutor ([5627a19](https://github.com/propeller-heads/tycho-execution/commit/5627a1902b74ace7eccce9888b4505f77b827d43))
### Bug Fixes
* Add input validation size in Uniswapv2SwapExecutor ([ed44f4e](https://github.com/propeller-heads/tycho-execution/commit/ed44f4e993f3856dbeb14cae04acffec72c25524))
* Remove exactOut logic from Uniswapv2SwapExecutor ([b9f4451](https://github.com/propeller-heads/tycho-execution/commit/b9f445176924e7f52d5e130f96038cfe8c44ea18))
## [0.14.0](https://github.com/propeller-heads/tycho-execution/compare/0.13.0...0.14.0) (2025-01-24)
### Features
* delegatecall to executor in SwapExecutionDispatcher ([e91ee96](https://github.com/propeller-heads/tycho-execution/commit/e91ee9612995eb038fb0f0c837438976cedc9a9a))
* Emit event when removing executor ([1fabff1](https://github.com/propeller-heads/tycho-execution/commit/1fabff19c4427caee0a758e2f89336ea784462cb))
### Bug Fixes
* ISwapExecutor shouldn't be payable ([3df17e8](https://github.com/propeller-heads/tycho-execution/commit/3df17e892491fbb47bf6ed03680b0fb7fbb68140))
* Silence slither warnings ([b616e11](https://github.com/propeller-heads/tycho-execution/commit/b616e11354ee325dcbecff70caf4e7daf4d144d0))
## [0.13.0](https://github.com/propeller-heads/tycho-execution/compare/0.12.0...0.13.0) (2025-01-23)

2
Cargo.lock generated
View File

@@ -4163,7 +4163,7 @@ dependencies = [
[[package]]
name = "tycho-execution"
version = "0.13.0"
version = "0.16.0"
dependencies = [
"alloy",
"alloy-primitives",

View File

@@ -1,6 +1,6 @@
[package]
name = "tycho-execution"
version = "0.13.0"
version = "0.16.0"
edition = "2021"
[dependencies]

View File

@@ -16,7 +16,7 @@ To run locally, simply install Slither in your conda env and run it inside the f
conda create --name tycho-execution python=3.10
conda activate tycho-execution
python3 -m pip install slither-analyzer`
pip install slither-analyzer
cd foundry
slither .
```

View File

@@ -45,12 +45,6 @@ $ forge snapshot
$ anvil
```
### Deploy
```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```
### Cast
```shell

View File

@@ -0,0 +1,31 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
pragma abicoder v2;
interface IExecutor {
/**
* @notice Performs a swap on a liquidity pool.
* @dev This method takes the amount of the input token and returns the amount of
* the output token which has been swapped.
*
* Note Part of the informal interface is that the executor supports sending the received
* tokens to a receiver address. If the underlying smart contract does not provide this
* functionality consider adding an additional transfer in the implementation.
*
* @param givenAmount The amount of the input token to swap.
* @param data Data that holds information necessary to perform the swap.
* @return calculatedAmount The amount of the output token swapped, depending on
* the givenAmount inputted.
*/
function swap(uint256 givenAmount, bytes calldata data)
external
returns (uint256 calculatedAmount);
}
interface IExecutorErrors {
error InvalidParameterLength(uint256);
error UnknownPoolType(uint8);
}

1
foundry/lib/v2-core Submodule

Submodule foundry/lib/v2-core added at 4dd59067c7

View File

@@ -1,3 +1,5 @@
@openzeppelin/=lib/openzeppelin-contracts/
@interfaces/=interfaces/
@permit2/=lib/permit2/
@src/=src/
@uniswap-v2/=lib/v2-core/

View File

@@ -0,0 +1,102 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@interfaces/IExecutor.sol";
error ExecutionDispatcher__UnapprovedExecutor();
error ExecutionDispatcher__NonContractExecutor();
/**
* @title ExecutionDispatcher - Dispatch execution to external contracts
* @author PropellerHeads Devs
* @dev Provides the ability to delegate execution of swaps to external
* contracts. This allows dynamically adding new supported protocols
* without needing to upgrade any contracts. External contracts will
* be called using delegatecall so they can share state with the main
* contract if needed.
*
* Note Executor contracts need to implement the IExecutor interface unless
* an alternate selector is specified.
*/
contract ExecutionDispatcher {
mapping(address => bool) public executors;
event ExecutorSet(address indexed executor);
event ExecutorRemoved(address indexed executor);
/**
* @dev Adds or replaces an approved executor contract address if it is a
* contract.
* @param target address of the executor contract
*/
function _setExecutor(address target) internal {
if (target.code.length == 0) {
revert ExecutionDispatcher__NonContractExecutor();
}
executors[target] = true;
emit ExecutorSet(target);
}
/**
* @dev Removes an approved executor contract address
* @param target address of the executor contract
*/
function _removeExecutor(address target) internal {
delete executors[target];
emit ExecutorRemoved(target);
}
/**
* @dev Calls an executor, assumes swap.protocolData contains
* 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);
if (!executors[executor]) {
revert ExecutionDispatcher__UnapprovedExecutor();
}
bytes4 selector = decodedSelector == bytes4(0)
? IExecutor.swap.selector
: decodedSelector;
// slither-disable-next-line low-level-calls
(bool success, bytes memory result) = executor.delegatecall(
abi.encodeWithSelector(selector, amount, protocolData)
);
if (!success) {
revert(
string(
result.length > 0
? result
: abi.encodePacked("Execution failed")
)
);
}
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,17 +0,0 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
/**
* @title SwapExecutionDispatcher - Dispatch swap execution to external contracts
* @author PropellerHeads Devs
* @dev Provides the ability to delegate execution of swaps to external
* contracts. This allows dynamically adding new supported protocols
* without needing to upgrade any contracts. External contracts will
* be called using delegatecall so they can share state with the main
* contract if needed.
*
* Note Executor contracts need to implement the ISwapExecutor interface
*/
contract SwapExecutionDispatcher {
mapping(address => bool) public swapExecutors;
}

View File

@@ -5,13 +5,12 @@ import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@permit2/src/interfaces/IAllowanceTransfer.sol";
import "./SwapExecutionDispatcher.sol";
import "./ExecutionDispatcher.sol";
import "./CallbackVerificationDispatcher.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
error TychoRouter__WithdrawalFailed();
error TychoRouter__AddressZero();
error TychoRouter__NonContractExecutor();
error TychoRouter__NonContractVerifier();
contract TychoRouter is
@@ -49,7 +48,6 @@ contract TychoRouter is
address indexed oldFeeReceiver, address indexed newFeeReceiver
);
event FeeSet(uint256 indexed oldFee, uint256 indexed newFee);
event ExecutorSet(address indexed executor);
event CallbackVerifierSet(address indexed callbackVerifier);
constructor(address _permit2) {
@@ -112,32 +110,30 @@ contract TychoRouter is
}
/**
* @dev Entrypoint to add or replace an approved swap executor contract address
* @param target address of the swap method contract
* @dev Entrypoint to add or replace an approved executor contract address
* @param target address of the executor contract
*/
function setSwapExecutor(address target)
function setExecutor(address target)
external
onlyRole(EXECUTOR_SETTER_ROLE)
{
if (target.code.length == 0) revert TychoRouter__NonContractExecutor();
swapExecutors[target] = true;
emit ExecutorSet(target);
_setExecutor(target);
}
/**
* @dev Entrypoint to remove an approved swap executor contract address
* @param target address of the swap method contract
* @dev Entrypoint to remove an approved executor contract address
* @param target address of the executor contract
*/
function removeSwapExecutor(address target)
function removeExecutor(address target)
external
onlyRole(EXECUTOR_SETTER_ROLE)
{
delete swapExecutors[target];
_removeExecutor(target);
}
/**
* @dev Entrypoint to add or replace an approved swap executor contract address
* @param target address of the swap method contract
* @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
@@ -149,8 +145,8 @@ contract TychoRouter is
}
/**
* @dev Entrypoint to remove an approved swap executor contract address
* @param target address of the swap method contract
* @dev Entrypoint to remove an approved callback verifier contract address
* @param target address of the callback verifier contract
*/
function removeCallbackVerifier(address target)
external

View File

@@ -0,0 +1,75 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@interfaces/IExecutor.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap-v2/contracts/interfaces/IUniswapV2Pair.sol";
error UniswapV2Executor__InvalidDataLength();
contract UniswapV2Executor is IExecutor {
using SafeERC20 for IERC20;
function swap(uint256 givenAmount, bytes calldata data)
external
returns (uint256 calculatedAmount)
{
address target;
address receiver;
bool zeroForOne;
IERC20 tokenIn;
(tokenIn, target, receiver, zeroForOne) = _decodeData(data);
calculatedAmount = _getAmountOut(target, givenAmount, zeroForOne);
tokenIn.safeTransfer(target, givenAmount);
IUniswapV2Pair pool = IUniswapV2Pair(target);
if (zeroForOne) {
pool.swap(0, calculatedAmount, receiver, "");
} else {
pool.swap(calculatedAmount, 0, receiver, "");
}
}
function _decodeData(bytes calldata data)
internal
pure
returns (
IERC20 inToken,
address target,
address receiver,
bool zeroForOne
)
{
if (data.length != 61) {
revert UniswapV2Executor__InvalidDataLength();
}
inToken = IERC20(address(bytes20(data[0:20])));
target = address(bytes20(data[20:40]));
receiver = address(bytes20(data[40:60]));
zeroForOne = uint8(data[60]) > 0;
}
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();
} else {
// slither-disable-next-line unused-return
(reserveOut, reserveIn,) = pair.getReserves();
}
require(reserveIn > 0 && reserveOut > 0, "L");
uint256 amountInWithFee = amountIn * 997;
uint256 numerator = amountInWithFee * uint256(reserveOut);
uint256 denominator = (uint256(reserveIn) * 1000) + amountInWithFee;
amount = numerator / denominator;
}
}

View File

@@ -8,9 +8,23 @@ contract Constants is Test {
address BOB = makeAddr("bob"); //bob=someone!=us
address FUND_RESCUER = makeAddr("fundRescuer");
address FEE_SETTER = makeAddr("feeSetter");
// dummy contracts
address FEE_RECEIVER = makeAddr("feeReceiver");
// Dummy contracts
address DUMMY = makeAddr("dummy");
address FEE_RECEIVER = makeAddr("feeReceiver");
address PAUSER = makeAddr("pauser");
address UNPAUSER = makeAddr("unpauser");
// Assets
address WETH_ADDR = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address DAI_ADDR = address(0x6B175474E89094C44Da98b954EedeAC495271d0F);
/**
* @dev Deploys a dummy contract with non-empty bytecode
*/
function deployDummyContract() internal {
bytes memory minimalBytecode = hex"01"; // Single-byte bytecode
vm.etch(DUMMY, minimalBytecode); // Deploy minimal bytecode
}
}

View File

@@ -0,0 +1,149 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@src/ExecutionDispatcher.sol";
import "./TychoRouterTestSetup.sol";
contract ExecutionDispatcherExposed is ExecutionDispatcher {
function exposedCallExecutor(uint256 amount, bytes calldata data)
external
returns (uint256 calculatedAmount)
{
return _callExecutor(amount, data);
}
function exposedDecodeExecutorAndSelector(bytes calldata data)
external
pure
returns (address executor, bytes4 selector, bytes memory protocolData)
{
return _decodeExecutorAndSelector(data);
}
function exposedSetExecutor(address target) external {
_setExecutor(target);
}
function exposedRemoveExecutor(address target) external {
_removeExecutor(target);
}
}
contract ExecutionDispatcherTest is Constants {
ExecutionDispatcherExposed dispatcherExposed;
event ExecutorSet(address indexed executor);
event ExecutorRemoved(address indexed executor);
function setUp() public {
uint256 forkBlock = 20673900;
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
dispatcherExposed = new ExecutionDispatcherExposed();
deal(WETH_ADDR, address(dispatcherExposed), 15 ether);
deployDummyContract();
}
function testSetValidExecutor() public {
vm.expectEmit();
// Define the event we expect to be emitted at the next step
emit ExecutorSet(DUMMY);
dispatcherExposed.exposedSetExecutor(DUMMY);
assert(dispatcherExposed.executors(DUMMY) == true);
}
function testRemoveExecutor() public {
dispatcherExposed.exposedSetExecutor(DUMMY);
vm.expectEmit();
// Define the event we expect to be emitted at the next step
emit ExecutorRemoved(DUMMY);
dispatcherExposed.exposedRemoveExecutor(DUMMY);
assert(dispatcherExposed.executors(DUMMY) == false);
}
function testRemoveUnSetExecutor() public {
dispatcherExposed.exposedRemoveExecutor(BOB);
assert(dispatcherExposed.executors(BOB) == false);
}
function testSetExecutorNonContract() public {
vm.expectRevert(
abi.encodeWithSelector(
ExecutionDispatcher__NonContractExecutor.selector
)
);
dispatcherExposed.exposedSetExecutor(BOB);
}
function testCallExecutor() public {
// Test case taken from existing transaction
// 0x755d603962b30f416cf3eefae8d55204d6ffdf746465b2a94aca216faab63804
// For this test, we can use any executor and any calldata that we know works
// for this executor. We don't care about which calldata/executor, since we are
// only testing the functionality of the delegatecall and not the inner
// workings of the executor.
// Thus, we chose a previously-deployed Hashflow executor for simplicity. To
// change this test, we can find any of our transactions that succeeded, and
// obtain the calldata passed to the executor via Tenderly.
dispatcherExposed.exposedSetExecutor(
address(0xe592557AB9F4A75D992283fD6066312FF013ba3d)
);
bytes memory data =
hex"e592557AB9F4A75D992283fD6066312FF013ba3dbd0625ab5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c";
uint256 givenAmount = 15 ether;
uint256 amount =
dispatcherExposed.exposedCallExecutor(givenAmount, data);
assert(amount == 35144641819);
}
function testCallExecutorNoSelector() public {
// Test case taken from existing transaction
// 0x755d603962b30f416cf3eefae8d55204d6ffdf746465b2a94aca216faab63804
// No selector is passed, so the standard swap selector should be used
// For this test, we can use any executor and any calldata that we know works
// for this executor. We don't care about which calldata/executor, since we are
// only testing the functionality of the delegatecall and not the inner
// workings of the executor.
// Thus, we chose a previously-deployed Hashflow executor for simplicity. To
// change this test, we can find any of our transactions that succeeded, and
// obtain the calldata passed to the executor via Tenderly.
dispatcherExposed.exposedSetExecutor(
address(0xe592557AB9F4A75D992283fD6066312FF013ba3d)
);
bytes memory data =
hex"e592557AB9F4A75D992283fD6066312FF013ba3d000000005615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593c81c";
uint256 givenAmount = 15 ether;
uint256 amount =
dispatcherExposed.exposedCallExecutor(givenAmount, data);
assert(amount == 35144641819);
}
function testCallExecutorCallFailed() public {
// Bad data is provided to an approved executor - causing the call to fail
dispatcherExposed.exposedSetExecutor(
address(0xe592557AB9F4A75D992283fD6066312FF013ba3d)
);
bytes memory data =
hex"e592557AB9F4A75D992283fD6066312FF013ba3dbd0625ab5615dEB798BB3E4dFa0139dFa1b3D433Cc23b72fc8c39af7983bf329086de522229a7be5fc4e41cc51c72848c68a965f66fa7a88855f9f7784502a7f2606beffe61000613d6a25b5bfef4cd7652aa94777d4a46b39f2e206411280a12c9344b769ff1066c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000000000000000000000000000d02ab486cedc0000000000000000000000000000000000000000000000000000000000082ec8ad1b0000000000000000000000000000000000000000000000000000000066d7b65800000000000000000000000000000000000000000000000000000191ba9f843c125000064000640000d52de09955f0ffffffffffffff00225c389e595fe9000001fcc910754b349f821e4bb5d8444822a63920be943aba6f1b31ee14ef0fc6840b6d28d604e04a78834b668dba24a6c082ffb901e4fffa9600649e8d991af593";
vm.expectRevert();
dispatcherExposed.exposedCallExecutor(0, data);
}
function testCallExecutorUnapprovedExecutor() public {
bytes memory data =
hex"5d622C9053b8FFB1B3465495C8a42E603632bA70aabbccdd1111111111111111";
vm.expectRevert();
dispatcherExposed.exposedCallExecutor(0, data);
}
function testDecodeExecutorAndSelector() public {
bytes memory data =
hex"6611e616d2db3244244a54c754a16dd3ac7ca7a2aabbccdd1111111111111111";
(address executor, bytes4 selector, bytes memory protocolData) =
dispatcherExposed.exposedDecodeExecutorAndSelector(data);
assert(executor == address(0x6611e616d2db3244244A54c754A16dd3ac7cA7a2));
assert(selector == bytes4(0xaabbccdd));
// Direct bytes comparison not supported - must use keccak
assert(keccak256(protocolData) == keccak256(hex"1111111111111111"));
}
}

View File

@@ -14,56 +14,26 @@ contract TychoRouterTest is TychoRouterTestSetup {
bytes32 public constant FUND_RESCUER_ROLE =
0x912e45d663a6f4cc1d0491d8f046e06c616f40352565ea1cdb86a0e1aaefa41b;
event ExecutorSet(address indexed executor);
event CallbackVerifierSet(address indexed callbackVerifier);
event Withdrawal(
address indexed token, uint256 amount, address indexed receiver
);
function testSetValidExecutor() public {
function testSetExecutorValidRole() public {
vm.startPrank(executorSetter);
vm.expectEmit();
// Define the event we expect to be emitted at the next step
emit ExecutorSet(DUMMY);
tychoRouter.setSwapExecutor(DUMMY);
tychoRouter.setExecutor(DUMMY);
vm.stopPrank();
assert(tychoRouter.swapExecutors(DUMMY) == true);
}
function testRemoveExecutor() public {
vm.startPrank(executorSetter);
tychoRouter.setSwapExecutor(DUMMY);
tychoRouter.removeSwapExecutor(DUMMY);
vm.stopPrank();
assert(tychoRouter.swapExecutors(DUMMY) == false);
}
function testRemoveUnSetExecutor() public {
vm.startPrank(executorSetter);
tychoRouter.removeSwapExecutor(BOB);
vm.stopPrank();
assert(tychoRouter.swapExecutors(BOB) == false);
assert(tychoRouter.executors(DUMMY) == true);
}
function testRemoveExecutorMissingSetterRole() public {
vm.expectRevert();
tychoRouter.removeSwapExecutor(BOB);
tychoRouter.removeExecutor(BOB);
}
function testSetExecutorMissingSetterRole() public {
vm.expectRevert();
tychoRouter.setSwapExecutor(DUMMY);
}
function testSetExecutorNonContract() public {
vm.startPrank(executorSetter);
vm.expectRevert(
abi.encodeWithSelector(TychoRouter__NonContractExecutor.selector)
);
tychoRouter.setSwapExecutor(BOB);
vm.stopPrank();
tychoRouter.setExecutor(DUMMY);
}
function testSetValidVerifier() public {

View File

@@ -30,14 +30,6 @@ contract TychoRouterTestSetup is Test, Constants {
vm.stopPrank();
}
/**
* @dev Deploys a dummy contract with non-empty bytecode
*/
function deployDummyContract() internal {
bytes memory minimalBytecode = hex"01"; // Single-byte bytecode
vm.etch(DUMMY, minimalBytecode); // Deploy minimal bytecode
}
/**
* @dev Mints tokens to the given address
* @param amount The amount of tokens to mint

View File

@@ -0,0 +1,97 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;
import "@src/executors/UniswapV2Executor.sol";
import {Test} from "../../lib/forge-std/src/Test.sol";
import {Constants} from "../Constants.sol";
contract UniswapV2ExecutorExposed is UniswapV2Executor {
function decodeParams(bytes calldata data)
external
pure
returns (
IERC20 inToken,
address target,
address receiver,
bool zeroForOne
)
{
return _decodeData(data);
}
function getAmountOut(address target, uint256 amountIn, bool zeroForOne)
external
view
returns (uint256 amount)
{
return _getAmountOut(target, amountIn, zeroForOne);
}
}
contract UniswapV2ExecutorTest is UniswapV2ExecutorExposed, Test, Constants {
using SafeERC20 for IERC20;
UniswapV2ExecutorExposed uniswapV2Exposed;
IERC20 WETH = IERC20(WETH_ADDR);
IERC20 DAI = IERC20(DAI_ADDR);
address WETH_DAI_POOL = 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11;
function setUp() public {
uint256 forkBlock = 17323404;
vm.createSelectFork(vm.rpcUrl("mainnet"), forkBlock);
uniswapV2Exposed = new UniswapV2ExecutorExposed();
}
function testDecodeParams() public view {
bytes memory params =
abi.encodePacked(WETH_ADDR, address(2), address(3), false);
(IERC20 tokenIn, address target, address receiver, bool zeroForOne) =
uniswapV2Exposed.decodeParams(params);
assertEq(address(tokenIn), WETH_ADDR);
assertEq(target, address(2));
assertEq(receiver, address(3));
assertEq(zeroForOne, false);
}
function testDecodeParamsInvalidDataLength() public {
bytes memory invalidParams =
abi.encodePacked(WETH_ADDR, address(2), address(3));
vm.expectRevert(UniswapV2Executor__InvalidDataLength.selector);
uniswapV2Exposed.decodeParams(invalidParams);
}
function testAmountOut() public view {
uint256 amountOut =
uniswapV2Exposed.getAmountOut(WETH_DAI_POOL, 10 ** 18, false);
uint256 expAmountOut = 1847751195973566072891;
assertEq(amountOut, expAmountOut);
}
// triggers a uint112 overflow on purpose
function testAmountOutInt112Overflow() public view {
address target = 0x0B9f5cEf1EE41f8CCCaA8c3b4c922Ab406c980CC;
uint256 amountIn = 83638098812630667483959471576;
uint256 amountOut =
uniswapV2Exposed.getAmountOut(target, amountIn, true);
assertGe(amountOut, 0);
}
function testSwap() public {
uint256 amountIn = 10 ** 18;
uint256 amountOut = 1847751195973566072891;
bool zeroForOne = false;
bytes memory protocolData =
abi.encodePacked(WETH_ADDR, WETH_DAI_POOL, BOB, zeroForOne);
deal(WETH_ADDR, address(uniswapV2Exposed), amountIn);
uniswapV2Exposed.swap(amountIn, protocolData);
uint256 finalBalance = DAI.balanceOf(BOB);
assertGe(finalBalance, amountOut);
}
}

View File

@@ -106,7 +106,7 @@ impl SwapEncoder for BalancerV2SwapEncoder {
encoding_context.exact_out,
approval_needed,
);
Ok(args.abi_encode())
Ok(args.abi_encode_packed())
}
fn executor_address(&self) -> &str {
@@ -121,8 +121,8 @@ mod tests {
use super::*;
#[tokio::test]
async fn test_encode_uniswap_v2() {
#[test]
fn test_encode_uniswap_v2() {
let usv2_pool = ProtocolComponent {
id: String::from("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
..Default::default()
@@ -159,4 +159,47 @@ mod tests {
))
);
}
#[test]
fn test_encode_balancer_v2() {
let balancer_pool = ProtocolComponent {
id: String::from("0x88e6A0c2dDD26FEEb64F039a2c41296FcB3f5640"),
protocol_system: String::from("vm:balancer_v2"),
..Default::default()
};
let swap = Swap {
component: balancer_pool,
token_in: Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"), // WETH
token_out: Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"), // DAI
split: 0f64,
};
let encoding_context = EncodingContext {
receiver: Bytes::from("0x0000000000000000000000000000000000000001"),
exact_out: false,
router_address: Bytes::zero(20),
};
let encoder = BalancerV2SwapEncoder::new(String::from("0x"));
let encoded_swap = encoder
.encode_swap(swap, encoding_context)
.unwrap();
let hex_swap = encode(&encoded_swap);
assert_eq!(
hex_swap,
String::from(concat!(
// token in
"c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
// token out
"6b175474e89094c44da98b954eedeac495271d0f",
// pool id
"307838386536413063326444443236464545623634463033396132633431323936466342336635363430",
// receiver
"0000000000000000000000000000000000000001",
// exact out
"00",
// approval needed
"01"
))
);
}
}