From 0de87f5e7e7e2c37b7d7a2956c8391a855554e38 Mon Sep 17 00:00:00 2001 From: Eric Zhong Date: Mon, 29 Jul 2024 13:28:34 -0400 Subject: [PATCH] Update swapRouter02Executor --- .../SwapRouter02ExecutorExecute.snap | 2 +- ...outer02ExecutorExecuteAlreadyApproved.snap | 2 +- src/sample-executors/SwapRouter02Executor.sol | 33 ++++++++++++++++--- test/executors/SwapRouter02Executor.t.sol | 8 +++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.forge-snapshots/SwapRouter02ExecutorExecute.snap b/.forge-snapshots/SwapRouter02ExecutorExecute.snap index c31b0cf2..3c2d8631 100644 --- a/.forge-snapshots/SwapRouter02ExecutorExecute.snap +++ b/.forge-snapshots/SwapRouter02ExecutorExecute.snap @@ -1 +1 @@ -262691 \ No newline at end of file +267163 \ No newline at end of file diff --git a/.forge-snapshots/SwapRouter02ExecutorExecuteAlreadyApproved.snap b/.forge-snapshots/SwapRouter02ExecutorExecuteAlreadyApproved.snap index cb0f5e97..74b78b17 100644 --- a/.forge-snapshots/SwapRouter02ExecutorExecuteAlreadyApproved.snap +++ b/.forge-snapshots/SwapRouter02ExecutorExecuteAlreadyApproved.snap @@ -1 +1 @@ -117810 \ No newline at end of file +118161 \ No newline at end of file diff --git a/src/sample-executors/SwapRouter02Executor.sol b/src/sample-executors/SwapRouter02Executor.sol index d70c0bdd..a6194607 100644 --- a/src/sample-executors/SwapRouter02Executor.sol +++ b/src/sample-executors/SwapRouter02Executor.sol @@ -16,15 +16,19 @@ contract SwapRouter02Executor is IReactorCallback, Owned { using SafeTransferLib for ERC20; using CurrencyLibrary for address; + event WhitelistedCallerChanged(address newWhitelistedCaller, address oldWhitelistedCaller); + event ReactorChanged(address newReactor, address oldReactor); + /// @notice thrown if reactorCallback is called with a non-whitelisted filler error CallerNotWhitelisted(); /// @notice thrown if reactorCallback is called by an address other than the reactor error MsgSenderNotReactor(); - ISwapRouter02 private immutable swapRouter02; - address private immutable whitelistedCaller; - IReactor private immutable reactor; - WETH private immutable weth; + ISwapRouter02 public immutable swapRouter02; + WETH public immutable weth; + + address private whitelistedCaller; + IReactor public reactor; modifier onlyWhitelistedCaller() { if (msg.sender != whitelistedCaller) { @@ -115,6 +119,27 @@ contract SwapRouter02Executor is IReactorCallback, Owned { SafeTransferLib.safeTransferETH(recipient, address(this).balance); } + /// @notice Transfer the entire balance of an ERC20 token in this contract to a recipient. Can only be called by owner. + /// @param token The ERC20 token to withdraw + /// @param to The recipient of the tokens + function withdrawERC20(ERC20 token, address to) external onlyOwner { + token.safeTransfer(to, token.balanceOf(address(this))); + } + + /// @notice Update the reactor contract address. Can only be called by owner. + /// @param _reactor The new reactor contract address + function updateReactor(IReactor _reactor) external onlyOwner { + emit ReactorChanged(address(_reactor), address(reactor)); + reactor = _reactor; + } + + /// @notice Update the whitelisted caller address. Can only be called by owner. + /// @param _whitelistedCaller The new whitelisted caller address + function updateWhitelistedCaller(address _whitelistedCaller) external onlyOwner { + emit WhitelistedCallerChanged(_whitelistedCaller, whitelistedCaller); + whitelistedCaller = _whitelistedCaller; + } + /// @notice Necessary for this contract to receive ETH when calling unwrapWETH() receive() external payable {} } diff --git a/test/executors/SwapRouter02Executor.t.sol b/test/executors/SwapRouter02Executor.t.sol index 56e9ff6a..111aae70 100644 --- a/test/executors/SwapRouter02Executor.t.sol +++ b/test/executors/SwapRouter02Executor.t.sol @@ -393,6 +393,14 @@ contract SwapRouter02ExecutorTest is Test, PermitSignature, GasSnapshot, DeployP assertEq(balanceAfter - balanceBefore, 1 ether); } + function testWithdrawERC20() public { + deal(address(weth), address(swapRouter02Executor), 1 ether); + uint256 balanceBefore = weth.balanceOf(address(this)); + swapRouter02Executor.withdrawERC20(weth, address(this)); + uint256 balanceAfter = weth.balanceOf(address(this)); + assertEq(balanceAfter - balanceBefore, 1 ether); + } + function testWithdrawETHNotOwner() public { vm.expectRevert("UNAUTHORIZED"); vm.prank(address(0xbeef));