forked from timeless-fi/yield-daddy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EulerERC4626.sol
94 lines (73 loc) · 3.58 KB
/
EulerERC4626.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
// 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 {SafeTransferLib} from "solmate/utils/SafeTransferLib.sol";
import {IEulerEToken} from "./external/IEulerEToken.sol";
/// @title EulerERC4626
/// @author zefram.eth
/// @notice ERC4626 wrapper for Euler Finance
contract EulerERC4626 is ERC4626 {
/// -----------------------------------------------------------------------
/// Libraries usage
/// -----------------------------------------------------------------------
using SafeTransferLib for ERC20;
/// -----------------------------------------------------------------------
/// Immutable params
/// -----------------------------------------------------------------------
/// @notice The Euler main contract address
/// @dev Target of ERC20 approval when depositing
address public immutable euler;
/// @notice The Euler eToken contract
IEulerEToken public immutable eToken;
/// -----------------------------------------------------------------------
/// Constructor
/// -----------------------------------------------------------------------
constructor(ERC20 asset_, address euler_, IEulerEToken eToken_)
ERC4626(asset_, _vaultName(asset_), _vaultSymbol(asset_))
{
euler = euler_;
eToken = eToken_;
}
/// -----------------------------------------------------------------------
/// ERC4626 overrides
/// -----------------------------------------------------------------------
function totalAssets() public view virtual override returns (uint256) {
return eToken.balanceOfUnderlying(address(this));
}
function beforeWithdraw(uint256 assets, uint256 /*shares*/ ) internal virtual override {
/// -----------------------------------------------------------------------
/// Withdraw assets from Euler
/// -----------------------------------------------------------------------
eToken.withdraw(0, assets);
}
function afterDeposit(uint256 assets, uint256 /*shares*/ ) internal virtual override {
/// -----------------------------------------------------------------------
/// Deposit assets into Euler
/// -----------------------------------------------------------------------
// approve to euler
asset.safeApprove(address(euler), assets);
// deposit into eToken
eToken.deposit(0, assets);
}
function maxWithdraw(address owner) public view override returns (uint256) {
uint256 cash = asset.balanceOf(euler);
uint256 assetsBalance = convertToAssets(balanceOf[owner]);
return cash < assetsBalance ? cash : assetsBalance;
}
function maxRedeem(address owner) public view override returns (uint256) {
uint256 cash = asset.balanceOf(euler);
uint256 cashInShares = convertToShares(cash);
uint256 shareBalance = balanceOf[owner];
return cashInShares < shareBalance ? cashInShares : shareBalance;
}
/// -----------------------------------------------------------------------
/// ERC20 metadata generation
/// -----------------------------------------------------------------------
function _vaultName(ERC20 asset_) internal view virtual returns (string memory vaultName) {
vaultName = string.concat("ERC4626-Wrapped Euler ", asset_.symbol());
}
function _vaultSymbol(ERC20 asset_) internal view virtual returns (string memory vaultSymbol) {
vaultSymbol = string.concat("we", asset_.symbol());
}
}