-
Notifications
You must be signed in to change notification settings - Fork 14
/
BeefyAdapter.sol
240 lines (205 loc) · 7.96 KB
/
BeefyAdapter.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// SPDX-License-Identifier: GPL-3.0
// Docgen-SOLC: 0.8.15
pragma solidity ^0.8.15;
import {AdapterBase, IERC20, IERC20Metadata, SafeERC20, ERC20, Math, IStrategy, IAdapter} from "../abstracts/AdapterBase.sol";
import {WithRewards, IWithRewards} from "../abstracts/WithRewards.sol";
import {IBeefyVault, IBeefyBooster, IBeefyBalanceCheck, IBeefyStrat} from "./IBeefy.sol";
import {IPermissionRegistry} from "../../../interfaces/vault/IPermissionRegistry.sol";
/**
* @title Beefy Adapter
* @author RedVeil
* @notice ERC4626 wrapper for Beefy Vaults.
*
* An ERC4626 compliant Wrapper for https://github.com/beefyfinance/beefy-contracts/blob/master/contracts/BIFI/vaults/BeefyVaultV6.sol.
* Allows wrapping Beefy Vaults with or without an active Booster.
* Allows for additional strategies to use rewardsToken in case of an active Booster.
*/
contract BeefyAdapter is AdapterBase, WithRewards {
using SafeERC20 for IERC20;
using Math for uint256;
string internal _name;
string internal _symbol;
IBeefyVault public beefyVault;
IBeefyBooster public beefyBooster;
IBeefyBalanceCheck public beefyBalanceCheck;
uint256 public constant BPS_DENOMINATOR = 10_000;
error NotEndorsed(address beefyVault);
error InvalidBeefyVault(address beefyVault);
error InvalidBeefyBooster(address beefyBooster);
/**
* @notice Initialize a new Beefy Adapter.
* @param adapterInitData Encoded data for the base adapter initialization.
* @param registry Endorsement Registry to check if the beefy adapter is endorsed.
* @param beefyInitData Encoded data for the beefy adapter initialization.
* @dev `_beefyVault` - The underlying beefy vault.
* @dev `_beefyBooster` - An optional beefy booster.
* @dev This function is called by the factory contract when deploying a new vault.
*/
function initialize(
bytes memory adapterInitData,
address registry,
bytes memory beefyInitData
) external initializer {
(address _beefyVault, address _beefyBooster) = abi.decode(
beefyInitData,
(address, address)
);
__AdapterBase_init(adapterInitData);
if (!IPermissionRegistry(registry).endorsed(_beefyVault))
revert NotEndorsed(_beefyVault);
if (IBeefyVault(_beefyVault).want() != asset())
revert InvalidBeefyVault(_beefyVault);
if (
_beefyBooster != address(0) &&
IBeefyBooster(_beefyBooster).stakedToken() != _beefyVault
) revert InvalidBeefyBooster(_beefyBooster);
_name = string.concat(
"Popcorn Beefy",
IERC20Metadata(asset()).name(),
" Adapter"
);
_symbol = string.concat("popB-", IERC20Metadata(asset()).symbol());
beefyVault = IBeefyVault(_beefyVault);
beefyBooster = IBeefyBooster(_beefyBooster);
beefyBalanceCheck = IBeefyBalanceCheck(
_beefyBooster == address(0) ? _beefyVault : _beefyBooster
);
IERC20(asset()).approve(_beefyVault, type(uint256).max);
if (_beefyBooster != address(0))
IERC20(_beefyVault).approve(_beefyBooster, type(uint256).max);
}
function name()
public
view
override(IERC20Metadata, ERC20)
returns (string memory)
{
return _name;
}
function symbol()
public
view
override(IERC20Metadata, ERC20)
returns (string memory)
{
return _symbol;
}
/*//////////////////////////////////////////////////////////////
ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/
function _totalAssets() internal view override returns (uint256) {
return
underlyingBalance.mulDiv(
beefyVault.balance(),
beefyVault.totalSupply(),
Math.Rounding.Down
);
}
function _underlyingBalance() internal view override returns (uint256) {
return beefyBalanceCheck.balanceOf(address(this));
}
/// @notice The amount of beefy shares to withdraw given an amount of adapter shares
function convertToUnderlyingShares(uint256, uint256 shares)
public
view
override
returns (uint256)
{
uint256 supply = totalSupply();
return
supply == 0
? shares
: shares.mulDiv(underlyingBalance, supply, Math.Rounding.Up);
}
/// @notice The token rewarded if a beefy booster is configured
function rewardTokens() external view override returns (address[] memory) {
address[] memory _rewardTokens = new address[](1);
if (address(beefyBooster) == address(0)) return _rewardTokens;
_rewardTokens[0] = beefyBooster.rewardToken();
return _rewardTokens;
}
/*//////////////////////////////////////////////////////////////
ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/
/// @notice `previewWithdraw` that takes beefy withdrawal fees into account
function previewWithdraw(uint256 assets)
public
view
override
returns (uint256)
{
IBeefyStrat strat = IBeefyStrat(beefyVault.strategy());
uint256 beefyFee = strat.withdrawalFee();
if (beefyFee > 0)
assets = assets.mulDiv(
BPS_DENOMINATOR,
BPS_DENOMINATOR - beefyFee,
Math.Rounding.Up
);
return _convertToShares(assets, Math.Rounding.Up);
}
/// @notice `previewRedeem` that takes beefy withdrawal fees into account
function previewRedeem(uint256 shares)
public
view
override
returns (uint256)
{
uint256 assets = _convertToAssets(shares, Math.Rounding.Down);
IBeefyStrat strat = IBeefyStrat(beefyVault.strategy());
uint256 beefyFee = strat.withdrawalFee();
if (beefyFee > 0)
assets -= assets.mulDiv(
beefyFee,
BPS_DENOMINATOR,
Math.Rounding.Up
);
return assets;
}
/*//////////////////////////////////////////////////////////////
INTERNAL HOOKS LOGIC
//////////////////////////////////////////////////////////////*/
/// @notice Deposit into beefy vault and optionally into the booster given its configured
function _protocolDeposit(uint256 amount, uint256)
internal
virtual
override
{
beefyVault.deposit(amount);
if (address(beefyBooster) != address(0))
beefyBooster.stake(beefyVault.balanceOf(address(this)));
}
/// @notice Withdraw from the beefy vault and optionally from the booster given its configured
function _protocolWithdraw(uint256, uint256 shares)
internal
virtual
override
{
uint256 beefyShares = convertToUnderlyingShares(0, shares);
if (address(beefyBooster) != address(0))
beefyBooster.withdraw(beefyShares);
beefyVault.withdraw(beefyShares);
}
/*//////////////////////////////////////////////////////////////
STRATEGY LOGIC
//////////////////////////////////////////////////////////////*/
error NoBeefyBooster();
/// @notice Claim rewards from the beefy booster given its configured
function claim() public override onlyStrategy {
if (address(beefyBooster) == address(0)) revert NoBeefyBooster();
beefyBooster.getReward();
}
/*//////////////////////////////////////////////////////////////
EIP-165 LOGIC
//////////////////////////////////////////////////////////////*/
function supportsInterface(bytes4 interfaceId)
public
pure
override(WithRewards, AdapterBase)
returns (bool)
{
return
interfaceId == type(IWithRewards).interfaceId ||
interfaceId == type(IAdapter).interfaceId;
}
}