|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 5 | +import {IWNativeToken} from "src/hooks/cypher-eth/IWNativeToken.sol"; |
| 6 | +import {ISwapRouter} from "src/hooks/cypher-eth/ISwapRouter.sol"; |
| 7 | + |
| 8 | +contract BETHToETH { |
| 9 | + IERC20 public immutable bethContract; |
| 10 | + IWNativeToken public immutable wethContract; |
| 11 | + ISwapRouter public immutable swapRouterContract; |
| 12 | + |
| 13 | + constructor(IERC20 _bethContract, IWNativeToken _wethContract, ISwapRouter _swapRouterContract) { |
| 14 | + require(address(_bethContract) != address(0), "Invalid BETH address"); |
| 15 | + require(address(_wethContract) != address(0), "Invalid WETH address"); |
| 16 | + require(address(_swapRouterContract) != address(0), "Invalid WETH address"); |
| 17 | + bethContract = _bethContract; |
| 18 | + wethContract = _wethContract; |
| 19 | + swapRouterContract = _swapRouterContract; |
| 20 | + } |
| 21 | + |
| 22 | + function swapBethWithEth(uint256 _swapAmount, address _recipient) public returns (uint256 amountOut) { |
| 23 | + require(_swapAmount > 0, "Amount must be greater than 0"); |
| 24 | + require(_recipient != address(0), "Invalid recipient"); |
| 25 | + |
| 26 | + require( |
| 27 | + bethContract.transferFrom(msg.sender, address(this), _swapAmount), "error while transferFrom beth to this" |
| 28 | + ); |
| 29 | + |
| 30 | + bethContract.approve(address(swapRouterContract), _swapAmount); |
| 31 | + |
| 32 | + amountOut = swapRouterContract.exactInputSingle( |
| 33 | + ISwapRouter.ExactInputSingleParams({ |
| 34 | + tokenIn: address(bethContract), |
| 35 | + tokenOut: address(wethContract), |
| 36 | + deployer: address(0), |
| 37 | + recipient: address(this), |
| 38 | + deadline: block.timestamp + 15 minutes, |
| 39 | + amountIn: _swapAmount, |
| 40 | + amountOutMinimum: 0, |
| 41 | + limitSqrtPrice: 0 |
| 42 | + }) |
| 43 | + ); |
| 44 | + |
| 45 | + bethContract.approve(address(swapRouterContract), 0); // extra safety |
| 46 | + wethContract.withdraw(amountOut); |
| 47 | + (bool success,) = _recipient.call{value: amountOut}(""); |
| 48 | + require(success, "ETH transfer failed"); |
| 49 | + } |
| 50 | + |
| 51 | + fallback() external payable {} |
| 52 | +} |
0 commit comments