-
Notifications
You must be signed in to change notification settings - Fork 20
/
ERC4626PartnerManager.sol
336 lines (282 loc) · 12.8 KB
/
ERC4626PartnerManager.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol";
import {Ownable} from "solady/auth/Ownable.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ERC4626} from "@ERC4626/ERC4626.sol";
import {bHermes} from "@hermes/bHermes.sol";
import {bHermesVotes as ERC20MultiVotes} from "@hermes/tokens/bHermesVotes.sol";
import {PartnerManagerFactory} from "../factories/PartnerManagerFactory.sol";
import {IBaseVault} from "../interfaces/IBaseVault.sol";
import {PartnerUtilityManager} from "../PartnerUtilityManager.sol";
import {IERC4626PartnerManager} from "../interfaces/IERC4626PartnerManager.sol";
/// @title Yield bearing, boosting, voting, and gauge enabled Partner Token
abstract contract ERC4626PartnerManager is PartnerUtilityManager, Ownable, ERC4626, IERC4626PartnerManager {
using SafeTransferLib for address;
using FixedPointMathLib for uint256;
/*//////////////////////////////////////////////////////////////
PARTNER MANAGER STATE
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626PartnerManager
PartnerManagerFactory public immutable override factory;
/// @inheritdoc IERC4626PartnerManager
bHermes public immutable override bHermesToken;
/// @inheritdoc IERC4626PartnerManager
uint256 public override bHermesRate;
/**
* @notice Initializes the ERC4626PartnerManager token.
* @param _factory The partner manager factory.
* @param _bHermesRate The rate at which bHermes underlying's can be claimed.
* @param _partnerAsset The asset that will be used to deposit to get partner tokens.
* @param _name The name of the token.
* @param _symbol The symbol of the token.
* @param _bhermes The address of the bHermes token.
* @param _partnerVault The address of the partner vault.
* @param _owner The owner of this contract.
*/
constructor(
PartnerManagerFactory _factory,
uint256 _bHermesRate,
ERC20 _partnerAsset,
string memory _name,
string memory _symbol,
address _bhermes,
address _partnerVault,
address _owner
)
PartnerUtilityManager(
address(bHermes(_bhermes).gaugeWeight()),
address(bHermes(_bhermes).gaugeBoost()),
address(bHermes(_bhermes).governance()),
address(new ERC20MultiVotes(_owner)),
partnerVault
)
ERC4626(
_partnerAsset,
string.concat(_name, " - Burned Hermes: Aggregated Gov + Yield + Boost"),
string.concat(_symbol, "-bHermes")
)
{
_initializeOwner(_owner);
partnerVault = _partnerVault;
factory = _factory;
bHermesRate = _bHermesRate;
bHermesToken = bHermes(_bhermes);
}
/*///////////////////////////////////////////////////////////////
UTILITY MANAGER LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626PartnerManager
function updateUnderlyingBalance() public virtual {
bHermesToken.claimOutstanding();
}
/// @inheritdoc IERC4626PartnerManager
function claimOutstanding() public virtual {
uint256 balance = balanceOf[msg.sender] * bHermesRate;
/// @dev Never overflows since balandeOf >= userClaimed.
claimWeight(balance - userClaimedWeight[msg.sender]);
claimBoost(balance - userClaimedBoost[msg.sender]);
claimGovernance(balance - userClaimedGovernance[msg.sender]);
claimPartnerGovernance(balance - userClaimedPartnerGovernance[msg.sender]);
}
/*//////////////////////////////////////////////////////////////
ERC4626 ACCOUNTING LOGIC
//////////////////////////////////////////////////////////////*/
/// @notice Compute the amount of tokens available in contract.
/// @dev 1:1 with underlying asset.
function totalAssets() public view override returns (uint256) {
return totalSupply;
}
/**
* @notice Computes and returns the amount of shares from a given amount of assets.
* @param assets amount of assets to convert to shares
*/
function convertToShares(uint256 assets) public view virtual override returns (uint256) {
return assets;
}
/**
* @notice Computes and returns the amount of assets from a given amount of shares.
* @param shares amount of shares to convert to assets
*/
function convertToAssets(uint256 shares) public view virtual override returns (uint256) {
return shares;
}
/**
* @notice Simulates the amount of shares that the assets deposited are worth.
* @param assets amount of assets to simulate the deposit.
*/
function previewDeposit(uint256 assets) public view virtual override returns (uint256) {
return assets;
}
/**
* @notice Calculates the amount of shares that the assets deposited are worth.
*/
function previewMint(uint256 shares) public view virtual override returns (uint256) {
return shares;
}
/**
* @notice Previews the amount of assets to be withdrawn from a given amount of shares.
*/
function previewWithdraw(uint256 assets) public view virtual override returns (uint256) {
return assets;
}
/**
* @notice Previews the amount of assets to be redeemed from a given amount of shares.
* @param shares amount of shares to convert to assets.
*/
function previewRedeem(uint256 shares) public view virtual override returns (uint256) {
return shares;
}
/*//////////////////////////////////////////////////////////////
ER4626 DEPOSIT/WITHDRAWAL LIMIT LOGIC
//////////////////////////////////////////////////////////////*/
/// @notice Returns the maximum amount of assets that can be deposited by a user.
/// @dev Returns the remaining balance of the bHermes divided by the bHermesRate.
function maxDeposit(address) public view virtual override returns (uint256) {
return (address(bHermesToken).balanceOf(address(this))) / bHermesRate - totalSupply;
}
/// @notice Returns the maximum amount of assets that can be deposited by a user.
/// @dev Returns the remaining balance of the bHermes divided by the bHermesRate.
function maxMint(address) public view virtual override returns (uint256) {
return (address(bHermesToken).balanceOf(address(this))) / bHermesRate - totalSupply;
}
/// @notice Returns the maximum amount of assets that can be withdrawn by a user.
/// @dev Assumes that the user has already forfeited all utility tokens.
function maxWithdraw(address user) public view virtual override returns (uint256) {
return balanceOf[user];
}
/// @notice Returns the maximum amount of assets that can be redeemed by a user.
/// @dev Assumes that the user has already forfeited all utility tokens.
function maxRedeem(address user) public view virtual override returns (uint256) {
return balanceOf[user];
}
/*///////////////////////////////////////////////////////////////
MIGRATION LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626PartnerManager
function migratePartnerVault(address newPartnerVault) external onlyOwner {
if (factory.vaultIds(IBaseVault(newPartnerVault)) == 0) revert UnrecognizedVault();
address oldPartnerVault = partnerVault;
if (oldPartnerVault != address(0)) IBaseVault(oldPartnerVault).clearAll();
bHermesToken.claimOutstanding();
address(gaugeWeight).safeApprove(oldPartnerVault, 0);
address(gaugeBoost).safeApprove(oldPartnerVault, 0);
address(governance).safeApprove(oldPartnerVault, 0);
address(partnerGovernance).safeApprove(oldPartnerVault, 0);
address(gaugeWeight).safeApprove(newPartnerVault, type(uint256).max);
address(gaugeBoost).safeApprove(newPartnerVault, type(uint256).max);
address(governance).safeApprove(newPartnerVault, type(uint256).max);
address(partnerGovernance).safeApprove(newPartnerVault, type(uint256).max);
partnerVault = newPartnerVault;
if (newPartnerVault != address(0)) IBaseVault(newPartnerVault).applyAll();
emit MigratePartnerVault(address(this), newPartnerVault);
}
/*//////////////////////////////////////////////////////////////
ADMIN LOGIC
//////////////////////////////////////////////////////////////*/
/// @inheritdoc IERC4626PartnerManager
function increaseConversionRate(uint256 newRate) external onlyOwner {
if (newRate < bHermesRate) revert InvalidRate();
if (newRate > (address(bHermesToken).balanceOf(address(this)) / totalSupply)) {
revert InsufficientBacking();
}
bHermesRate = newRate;
partnerGovernance.mint(
address(this), totalSupply * newRate - address(partnerGovernance).balanceOf(address(this))
);
bHermesToken.claimOutstanding();
}
/*///////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/
/**
* @notice Mints new partner bhermes tokens to a specific address.
* @param to address to mints tokens to.
* @param amount amount of tokens to mint.
*/
function _mint(address to, uint256 amount) internal virtual override {
if (amount > maxMint(to)) revert ExceedsMaxDeposit();
bHermesToken.claimOutstanding();
ERC20MultiVotes(partnerGovernance).mint(address(this), amount * bHermesRate);
super._mint(to, amount);
}
/**
* @notice Burns (or unstakes) the vMaia token in exchange for the underlying
* Partner tokens, performing changes around bHermes tokens.
* @param from account to burn the partner manager from
* @param amount amounts of vMaia to burn
*/
function _burn(address from, uint256 amount) internal virtual override checkTransfer(from, amount) {
super._burn(from, amount);
}
/**
* @notice Transfer partner manager to a specific address.
* @param to address to transfer the tokens to.
* @param amount amounts of tokens to transfer.
*/
function transfer(address to, uint256 amount)
public
virtual
override
checkTransfer(msg.sender, amount)
returns (bool)
{
return super.transfer(to, amount);
}
/**
* @notice Transfer tokens from a given address.
* @param from address to transfer the tokens from.
* @param to address to transfer the tokens to.
* @param amount amounts of tokens to transfer.
*/
function transferFrom(address from, address to, uint256 amount)
public
virtual
override
checkTransfer(from, amount)
returns (bool)
{
return super.transferFrom(from, to, amount);
}
/*///////////////////////////////////////////////////////////////
MODIFIERS
//////////////////////////////////////////////////////////////*/
/// @dev Checks available weight allows for call.
modifier checkWeight(uint256 amount) virtual override {
if (balanceOf[msg.sender] * bHermesRate < amount + userClaimedWeight[msg.sender]) {
revert InsufficientShares();
}
_;
}
/// @dev Checks available boost allows for call.
modifier checkBoost(uint256 amount) virtual override {
if (balanceOf[msg.sender] * bHermesRate < amount + userClaimedBoost[msg.sender]) {
revert InsufficientShares();
}
_;
}
/// @dev Checks available governance allows for call.
modifier checkGovernance(uint256 amount) virtual override {
if (balanceOf[msg.sender] * bHermesRate < amount + userClaimedGovernance[msg.sender]) {
revert InsufficientShares();
}
_;
}
/// @dev Checks available partner governance allows for call.
modifier checkPartnerGovernance(uint256 amount) virtual override {
if (balanceOf[msg.sender] * bHermesRate < amount + userClaimedPartnerGovernance[msg.sender]) {
revert InsufficientShares();
}
_;
}
modifier checkTransfer(address from, uint256 amount) virtual {
uint256 userBalance = balanceOf[from] * bHermesRate;
if (
userBalance - userClaimedWeight[from] < amount || userBalance - userClaimedBoost[from] < amount
|| userBalance - userClaimedGovernance[from] < amount
|| userBalance - userClaimedPartnerGovernance[from] < amount
) revert InsufficientUnderlying();
_;
}
}