generated from ZeframLou/foundry-template
-
Notifications
You must be signed in to change notification settings - Fork 29
/
StETHERC4626.sol
68 lines (50 loc) · 2.67 KB
/
StETHERC4626.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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.13;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ERC4626} from "solmate/mixins/ERC4626.sol";
import {FixedPointMathLib} from "solmate/utils/FixedPointMathLib.sol";
import {IStETH} from "./external/IStETH.sol";
/// @title StETHERC4626
/// @author zefram.eth
/// @notice ERC4626 wrapper for Lido stETH
/// @dev Uses stETH's internal shares accounting instead of using regular vault accounting
/// since this prevents attackers from atomically increasing the vault's share value
/// and exploiting lending protocols that use this vault as a borrow asset.
contract StETHERC4626 is ERC4626 {
/// -----------------------------------------------------------------------
/// Libraries usage
/// -----------------------------------------------------------------------
using FixedPointMathLib for uint256;
/// -----------------------------------------------------------------------
/// Constructor
/// -----------------------------------------------------------------------
constructor(ERC20 asset_) ERC4626(asset_, "ERC4626-Wrapped Lido stETH", "wlstETH") {}
/// -----------------------------------------------------------------------
/// Getters
/// -----------------------------------------------------------------------
function stETH() public view returns (IStETH) {
return IStETH(address(asset));
}
/// -----------------------------------------------------------------------
/// ERC4626 overrides
/// -----------------------------------------------------------------------
function totalAssets() public view virtual override returns (uint256) {
return stETH().balanceOf(address(this));
}
function convertToShares(uint256 assets) public view virtual override returns (uint256) {
uint256 supply = stETH().totalSupply();
return supply == 0 ? assets : assets.mulDivDown(stETH().getTotalShares(), supply);
}
function convertToAssets(uint256 shares) public view virtual override returns (uint256) {
uint256 totalShares = stETH().getTotalShares();
return totalShares == 0 ? shares : shares.mulDivDown(stETH().totalSupply(), totalShares);
}
function previewMint(uint256 shares) public view virtual override returns (uint256) {
uint256 totalShares = stETH().getTotalShares();
return totalShares == 0 ? shares : shares.mulDivUp(stETH().totalSupply(), totalShares);
}
function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
uint256 supply = stETH().totalSupply();
return supply == 0 ? assets : assets.mulDivUp(stETH().getTotalShares(), supply);
}
}