Skip to content

Commit 643f060

Browse files
authored
Merge pull request #23 from worm-privacy/feat/swap-hook
BETH to ETH implementation
2 parents 7981f6e + 56ba844 commit 643f060

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

script/DeployBETHToETH.s.sol

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "forge-std/Script.sol";
5+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
6+
import "src/hooks/cypher-eth/BETHToETH.sol";
7+
import {IWNativeToken} from "src/hooks/cypher-eth/IWNativeToken.sol";
8+
9+
contract DeployBETHToETH is Script {
10+
// mainnet addresses
11+
address constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
12+
address constant BETH = 0x5624344235607940d4d4EE76Bf8817d403EB9Cf8;
13+
address constant swapRouter = 0x20C5893f69F635f55b0367C519F3f95e59c0b0Ab;
14+
15+
function run() external {
16+
vm.startBroadcast();
17+
18+
BETHToETH bethToEth = new BETHToETH(IERC20(BETH), IWNativeToken(WETH), ISwapRouter(swapRouter));
19+
20+
console.log("BETHToETH deployed to:", address(bethToEth));
21+
console.log("BETH address:", BETH);
22+
console.log("WETH address:", WETH);
23+
24+
vm.stopBroadcast();
25+
}
26+
}

src/hooks/cypher-eth/BETHToETH.sol

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity ^0.8.13;
3+
4+
interface ISwapRouter {
5+
struct ExactInputSingleParams {
6+
address tokenIn;
7+
address tokenOut;
8+
address deployer;
9+
address recipient;
10+
uint256 deadline;
11+
uint256 amountIn;
12+
uint256 amountOutMinimum;
13+
uint160 limitSqrtPrice;
14+
}
15+
16+
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
5+
6+
/// @title Interface for WNativeToken
7+
interface IWNativeToken is IERC20 {
8+
/// @notice Deposit ether to get wrapped ether
9+
function deposit() external payable;
10+
11+
/// @notice Withdraw wrapped ether to get ether
12+
function withdraw(uint256) external;
13+
}

0 commit comments

Comments
 (0)