-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathLidoWrapping.sol
104 lines (82 loc) · 3.84 KB
/
LidoWrapping.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: GPL-3.0-or-later
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
pragma solidity ^0.7.0;
pragma experimental ABIEncoderV2;
import "@balancer-labs/v2-interfaces/contracts/standalone-utils/IstETH.sol";
import "@balancer-labs/v2-interfaces/contracts/standalone-utils/IwstETH.sol";
import "@balancer-labs/v2-solidity-utils/contracts/openzeppelin/Address.sol";
import "./IBaseRelayerLibrary.sol";
/**
* @title LidoWrapping
* @notice Allows users to wrap and unwrap stETH
* @dev All functions must be payable so they can be called from a multicall involving ETH
*/
abstract contract LidoWrapping is IBaseRelayerLibrary {
using Address for address payable;
IstETH private immutable _stETH;
IwstETH private immutable _wstETH;
/**
* @dev The zero address may be passed as wstETH to safely disable this module
* @param wstETH - the address of Lido's wrapped stETH contract
*/
constructor(IERC20 wstETH) {
// Safely disable stETH wrapping if no address has been passed for wstETH
_stETH = wstETH != IERC20(0) ? IwstETH(address(wstETH)).stETH() : IstETH(0);
_wstETH = IwstETH(address(wstETH));
}
function wrapStETH(
address sender,
address recipient,
uint256 amount,
uint256 outputReference
) external payable {
amount = _resolveAmountPullTokenAndApproveSpender(_stETH, address(_wstETH), amount, sender);
uint256 result = IwstETH(_wstETH).wrap(amount);
_transferAndSetChainedReference(_wstETH, recipient, result, outputReference);
}
function unwrapWstETH(
address sender,
address recipient,
uint256 amount,
uint256 outputReference
) external payable {
amount = _resolveAmountAndPullToken(_wstETH, amount, sender);
// No approval is needed here, as wstETH is burned directly from the relayer's account
uint256 result = _wstETH.unwrap(amount);
_transferAndSetChainedReference(_stETH, recipient, result, outputReference);
}
function stakeETH(
address recipient,
uint256 amount,
uint256 outputReference
) external payable {
amount = _resolveAmount(amount);
uint256 result = _stETH.submit{ value: amount }(address(this));
_transferAndSetChainedReference(_stETH, recipient, result, outputReference);
}
function stakeETHAndWrap(
address recipient,
uint256 amount,
uint256 outputReference
) external payable {
amount = _resolveAmount(amount);
// We must query this separately, since the wstETH contract doesn't return how much wstETH is minted.
uint256 result = _wstETH.getWstETHByStETH(amount);
// The fallback function on the wstETH contract automatically stakes and wraps any ETH sent to it.
// We can then send the ETH safely, and only have to ensure that the call doesn't revert.
//
// This would be dangerous if `_wstETH` were set to the zero address. However, in this scenario,
// this function would have already reverted during the call to `getWstETHByStETH`, preventing loss of funds.
payable(address(_wstETH)).sendValue(amount);
_transferAndSetChainedReference(_wstETH, recipient, result, outputReference);
}
}