/
BaseVault.sol
473 lines (398 loc) · 14.6 KB
/
BaseVault.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity 0.8.9;
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-IERC20Permit.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../interfaces/IConfigurationManager.sol";
import "../interfaces/IVault.sol";
import "../libs/AuxMath.sol";
import "../libs/DepositQueueLib.sol";
import "../libs/CastUint.sol";
import "../mixins/Capped.sol";
/**
* @title A Vault that tokenize shares of strategy
* @author Pods Finance
*/
abstract contract BaseVault is IVault, ERC20Permit, Capped {
using SafeERC20 for IERC20Metadata;
using AuxMath for uint256;
using CastUint for uint256;
using DepositQueueLib for DepositQueueLib.DepositQueue;
IConfigurationManager public immutable configuration;
IERC20Metadata internal immutable _asset;
uint256 public currentRoundId;
bool public isProcessingDeposits = false;
/*
DENOMINATOR represents the precision for the following system variables:
- MAX_WITHDRAW_FEE
- InvestorRatio
*/
uint256 public constant DENOMINATOR = 10000;
/*
MAX_WITDRAW_FEE is a safe check in case the ConfiguratorManager sets
a fee high enough that can be used as a way to drain funds.
The precision of this number is set by constant DENOMINATOR.
*/
uint256 public constant MAX_WITHDRAW_FEE = 1000;
uint256 public constant EMERGENCY_INTERVAL = 604800;
uint256 public processedDeposits = 0;
uint256 private _lastEndRound;
DepositQueueLib.DepositQueue internal depositQueue;
constructor(IConfigurationManager _configuration, IERC20Metadata _asset_)
ERC20(
string(abi.encodePacked("Pods Yield ", _asset_.symbol())),
string(abi.encodePacked("py", _asset_.symbol()))
)
ERC20Permit(string(abi.encodePacked("Pods Yield ", _asset_.symbol())))
Capped(_configuration)
{
configuration = _configuration;
_asset = _asset_;
// Vault starts in `start` state
emit StartRound(currentRoundId, 0);
_lastEndRound = block.timestamp;
}
modifier onlyController() {
if (msg.sender != controller()) revert IVault__CallerIsNotTheController();
_;
}
modifier onlyRoundStarter() {
bool lastRoundEndedAWeekAgo = block.timestamp >= _lastEndRound + EMERGENCY_INTERVAL;
if (!lastRoundEndedAWeekAgo && msg.sender != controller()) {
revert IVault__CallerIsNotTheController();
}
_;
}
/**
* @inheritdoc ERC20
*/
function decimals() public view override returns (uint8) {
return _asset.decimals();
}
/**
* @inheritdoc IERC4626
*/
function asset() public view returns (address) {
return address(_asset);
}
/**
* @inheritdoc IERC4626
*/
function deposit(uint256 assets, address receiver) external virtual override returns (uint256 shares) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
shares = previewDeposit(assets);
if (shares == 0) revert IVault__ZeroShares();
_deposit(assets, shares, receiver);
}
function depositWithPermit(
uint256 assets,
address receiver,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external returns (uint256 shares) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
shares = previewDeposit(assets);
if (shares == 0) revert IVault__ZeroShares();
IERC20Permit(address(_asset)).permit(msg.sender, address(this), assets, deadline, v, r, s);
_deposit(assets, shares, receiver);
}
/**
* @inheritdoc IERC4626
*/
function mint(uint256 shares, address receiver) external virtual override returns (uint256 assets) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
assets = previewMint(shares);
assets = _deposit(assets, shares, receiver);
}
function mintWithPermit(
uint256 shares,
address receiver,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public returns (uint256 assets) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
assets = previewMint(shares);
IERC20Permit(address(_asset)).permit(msg.sender, address(this), assets, deadline, v, r, s);
assets = _deposit(assets, shares, receiver);
}
/**
* @inheritdoc IERC4626
*/
function redeem(
uint256 shares,
address receiver,
address owner
) public virtual override returns (uint256 assets) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
assets = convertToAssets(shares);
if (assets == 0) revert IVault__ZeroAssets();
(assets, ) = _withdraw(assets, shares, receiver, owner);
}
/**
* @inheritdoc IERC4626
*/
function withdraw(
uint256 assets,
address receiver,
address owner
) external virtual override returns (uint256 shares) {
if (isProcessingDeposits) revert IVault__ForbiddenWhileProcessingDeposits();
shares = convertToShares(assets);
(, shares) = _withdraw(assets, shares, receiver, owner);
}
/**
* @inheritdoc IERC4626
*/
function totalAssets() public view virtual returns (uint256);
/**
* @inheritdoc IERC4626
*/
function previewDeposit(uint256 assets) public view override returns (uint256 shares) {
return convertToShares(assets);
}
/**
* @inheritdoc IERC4626
*/
function previewMint(uint256 shares) public view override returns (uint256 assets) {
uint256 supply = totalSupply();
return supply == 0 ? shares : shares.mulDivUp(totalAssets(), supply);
}
/**
* @inheritdoc IERC4626
*/
function previewWithdraw(uint256 assets) public view override returns (uint256 shares) {
shares = convertToShares(assets);
uint256 invertedFee = DENOMINATOR - withdrawFeeRatio();
return shares.mulDivUp(DENOMINATOR, invertedFee);
}
/**
* @inheritdoc IERC4626
*/
function previewRedeem(uint256 shares) public view override returns (uint256 assets) {
assets = convertToAssets(shares);
return assets - _getFee(assets);
}
/**
* @inheritdoc IERC4626
*/
function convertToShares(uint256 assets) public view override returns (uint256 shares) {
uint256 supply = totalSupply();
return supply == 0 ? assets : assets.mulDivDown(supply, totalAssets());
}
/**
* @inheritdoc IERC4626
*/
function convertToAssets(uint256 shares) public view override returns (uint256 assets) {
uint256 supply = totalSupply();
return supply == 0 ? shares : shares.mulDivDown(totalAssets(), supply);
}
/**
* @inheritdoc IERC4626
*/
function maxDeposit(address) public pure override returns (uint256 assets) {
return type(uint256).max;
}
/**
* @inheritdoc IERC4626
*/
function maxMint(address) public pure override returns (uint256 shares) {
return type(uint256).max;
}
/**
* @inheritdoc IERC4626
*/
function maxWithdraw(address owner) public view override returns (uint256 assets) {
return previewRedeem(balanceOf(owner));
}
/**
* @inheritdoc IERC4626
*/
function maxRedeem(address owner) public view override returns (uint256 shares) {
return balanceOf(owner);
}
/**
* @inheritdoc IVault
*/
function withdrawFeeRatio() public view override returns (uint256) {
uint256 _withdrawFeeRatio = configuration.getParameter(address(this), "WITHDRAW_FEE_RATIO");
// Fee is limited to MAX_WITHDRAW_FEE
return AuxMath.min(_withdrawFeeRatio, MAX_WITHDRAW_FEE);
}
/**
* @inheritdoc IVault
*/
function idleAssetsOf(address owner) public view virtual returns (uint256) {
return depositQueue.balanceOf(owner);
}
/**
* @inheritdoc IVault
*/
function assetsOf(address owner) public view virtual returns (uint256) {
uint256 supply = totalSupply();
uint256 shares = balanceOf(owner);
uint256 committedAssets = supply == 0 ? 0 : shares.mulDivDown(_asset.balanceOf(address(this)), supply);
return convertToAssets(shares) + idleAssetsOf(owner) + committedAssets;
}
/**
* @inheritdoc IVault
*/
function totalIdleAssets() public view virtual returns (uint256) {
return depositQueue.totalDeposited;
}
/**
* @inheritdoc IVault
*/
function depositQueueSize() public view returns (uint256) {
return depositQueue.size();
}
/**
* @inheritdoc IVault
*/
function controller() public view returns (address) {
return configuration.getParameter(address(this), "VAULT_CONTROLLER").toAddress();
}
/**
* @inheritdoc IVault
*/
function startRound() external virtual onlyRoundStarter returns (uint256 roundId) {
if (!isProcessingDeposits) revert IVault__NotProcessingDeposits();
isProcessingDeposits = false;
_afterRoundStart(processedDeposits);
emit StartRound(currentRoundId, processedDeposits);
processedDeposits = 0;
return currentRoundId;
}
/**
* @inheritdoc IVault
*/
function endRound() external virtual onlyController {
if (isProcessingDeposits) revert IVault__AlreadyProcessingDeposits();
isProcessingDeposits = true;
_afterRoundEnd();
_lastEndRound = block.timestamp;
emit EndRound(currentRoundId++);
}
/**
* @inheritdoc IVault
*/
function refund() external returns (uint256 assets) {
assets = depositQueue.balanceOf(msg.sender);
if (assets == 0) revert IVault__ZeroAssets();
for (uint256 i = 0; i < depositQueue.size(); i++) {
DepositQueueLib.DepositEntry memory depositEntry = depositQueue.get(i);
if (depositEntry.owner == msg.sender) {
depositQueue.remove(i, i + 1);
break;
}
}
emit DepositRefunded(msg.sender, currentRoundId, assets);
_asset.safeTransfer(msg.sender, assets);
}
/**
* @inheritdoc IVault
*/
function migrate(IVault newVault) external override {
if (address(_asset) != newVault.asset() || !configuration.isVaultAllowed(address(newVault))) {
revert IVault__MigrationNotAllowed();
}
// Redeem owner assets from this Vault
uint256 shares = balanceOf(msg.sender);
uint256 assets = redeem(shares, address(this), msg.sender);
// Deposit assets to `newVault`
_asset.safeApprove(address(newVault), assets);
newVault.deposit(assets, msg.sender);
emit Migrated(msg.sender, address(this), address(newVault), assets, shares);
}
/**
* @inheritdoc IVault
*/
function processQueuedDeposits(uint256 startIndex, uint256 endIndex) external {
if (!isProcessingDeposits) revert IVault__NotProcessingDeposits();
uint256 _totalAssets = totalAssets();
for (uint256 i = startIndex; i < endIndex; i++) {
uint256 currentAssets = _totalAssets + processedDeposits;
DepositQueueLib.DepositEntry memory depositEntry = depositQueue.get(i);
_processDeposit(depositEntry, currentAssets);
processedDeposits += depositEntry.amount;
}
depositQueue.remove(startIndex, endIndex);
}
/** Internals **/
/**
* @notice Mint new shares, effectively representing user participation in the Vault.
*/
function _processDeposit(DepositQueueLib.DepositEntry memory depositEntry, uint256 currentAssets) internal virtual {
uint256 supply = totalSupply();
uint256 assets = depositEntry.amount;
uint256 shares = currentAssets == 0 || supply == 0 ? assets : assets.mulDivUp(supply, currentAssets);
_mint(depositEntry.owner, shares);
emit DepositProcessed(depositEntry.owner, currentRoundId, assets, shares);
}
/**
* @notice Calculate the fee amount on withdraw.
*/
function _getFee(uint256 assets) internal view returns (uint256) {
return assets.mulDivDown(withdrawFeeRatio(), DENOMINATOR);
}
/**
* @dev Pull assets from the caller and create shares to the receiver
*/
function _deposit(
uint256 assets,
uint256 shares,
address receiver
) internal virtual returns (uint256 depositedAssets) {
_spendCap(shares);
depositQueue.push(DepositQueueLib.DepositEntry(receiver, assets));
emit Deposit(msg.sender, receiver, assets, shares);
_asset.safeTransferFrom(msg.sender, address(this), assets);
return assets;
}
/**
* @dev Burn shares from the caller and release assets to the receiver
*/
function _withdraw(
uint256 assets,
uint256 shares,
address receiver,
address owner
) internal virtual returns (uint256 receiverAssets, uint256 receiverShares) {
if (msg.sender != owner) {
_spendAllowance(owner, msg.sender, shares);
}
_burn(owner, shares);
_restoreCap(shares);
// Apply custom withdraw logic
_beforeWithdraw(shares, assets);
uint256 fee = _getFee(assets);
receiverAssets = assets - fee;
receiverShares = shares;
emit Withdraw(msg.sender, receiver, owner, receiverAssets, shares);
_asset.safeTransfer(receiver, receiverAssets);
if (fee > 0) {
emit FeeCollected(fee);
_asset.safeTransfer(controller(), fee);
}
}
/** Hooks **/
// solhint-disable-next-line no-empty-blocks
/* This hook should be implemented in the contract implementation.
It will trigger after the shares were burned
*/
function _beforeWithdraw(uint256 shares, uint256 assets) internal virtual {}
// solhint-disable-next-line no-empty-blocks
/* This hook should be implemented in the contract implementation.
It will trigger after setting isProcessingDeposits to false
*/
function _afterRoundStart(uint256 assets) internal virtual {}
// solhint-disable-next-line no-empty-blocks
/* This hook should be implemented in the contract implementation.
It will trigger after setting isProcessingDeposits to true
*/
function _afterRoundEnd() internal virtual {}
}