Skip to content

Commit 56ba844

Browse files
committed
ref(BETHToETH): SwapRouter set move to constructor
1 parent 7161cfb commit 56ba844

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

script/DeployBETHToETH.s.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ contract DeployBETHToETH is Script {
1010
// mainnet addresses
1111
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
1212
address constant BETH = 0x5624344235607940d4d4EE76Bf8817d403EB9Cf8;
13+
address constant swapRouter = 0x20C5893f69F635f55b0367C519F3f95e59c0b0Ab;
1314

1415
function run() external {
1516
vm.startBroadcast();
1617

17-
BETHToETH bethToEth = new BETHToETH(IERC20(BETH), IWNativeToken(WETH));
18+
BETHToETH bethToEth = new BETHToETH(IERC20(BETH), IWNativeToken(WETH), ISwapRouter(swapRouter));
1819

1920
console.log("BETHToETH deployed to:", address(bethToEth));
2021
console.log("BETH address:", BETH);

src/hooks/cypher-eth/BETHToETH.sol

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,28 @@ import {ISwapRouter} from "src/hooks/cypher-eth/ISwapRouter.sol";
88
contract BETHToETH {
99
IERC20 public immutable bethContract;
1010
IWNativeToken public immutable wethContract;
11+
ISwapRouter public immutable swapRouterContract;
1112

12-
constructor(IERC20 _bethContract, IWNativeToken _wethContract) {
13+
constructor(IERC20 _bethContract, IWNativeToken _wethContract, ISwapRouter _swapRouterContract) {
1314
require(address(_bethContract) != address(0), "Invalid BETH address");
1415
require(address(_wethContract) != address(0), "Invalid WETH address");
16+
require(address(_swapRouterContract) != address(0), "Invalid WETH address");
1517
bethContract = _bethContract;
1618
wethContract = _wethContract;
19+
swapRouterContract = _swapRouterContract;
1720
}
1821

19-
function swapBethWithEth(uint256 _swapAmount, address _recipient, ISwapRouter _swapRouter)
20-
public
21-
returns (uint256 amountOut)
22-
{
22+
function swapBethWithEth(uint256 _swapAmount, address _recipient) public returns (uint256 amountOut) {
2323
require(_swapAmount > 0, "Amount must be greater than 0");
2424
require(_recipient != address(0), "Invalid recipient");
2525

2626
require(
2727
bethContract.transferFrom(msg.sender, address(this), _swapAmount), "error while transferFrom beth to this"
2828
);
2929

30-
bethContract.approve(address(_swapRouter), _swapAmount);
30+
bethContract.approve(address(swapRouterContract), _swapAmount);
3131

32-
amountOut = _swapRouter.exactInputSingle(
32+
amountOut = swapRouterContract.exactInputSingle(
3333
ISwapRouter.ExactInputSingleParams({
3434
tokenIn: address(bethContract),
3535
tokenOut: address(wethContract),
@@ -42,7 +42,7 @@ contract BETHToETH {
4242
})
4343
);
4444

45-
bethContract.approve(address(_swapRouter), 0); // extra safety
45+
bethContract.approve(address(swapRouterContract), 0); // extra safety
4646
wethContract.withdraw(amountOut);
4747
(bool success,) = _recipient.call{value: amountOut}("");
4848
require(success, "ETH transfer failed");

0 commit comments

Comments
 (0)