-
Notifications
You must be signed in to change notification settings - Fork 79
/
VaultCore.sol
836 lines (751 loc) · 29 KB
/
VaultCore.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title OToken VaultCore contract
* @notice The Vault contract stores assets. On a deposit, OTokens will be minted
and sent to the depositor. On a withdrawal, OTokens will be burned and
assets will be sent to the withdrawer. The Vault accepts deposits of
interest from yield bearing strategies which will modify the supply
of OTokens.
* @author Origin Protocol Inc
*/
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { StableMath } from "../utils/StableMath.sol";
import { IOracle } from "../interfaces/IOracle.sol";
import { IGetExchangeRateToken } from "../interfaces/IGetExchangeRateToken.sol";
import { IDripper } from "../interfaces/IDripper.sol";
import "./VaultInitializer.sol";
contract VaultCore is VaultInitializer {
using SafeERC20 for IERC20;
using StableMath for uint256;
// max signed int
uint256 internal constant MAX_INT = 2**255 - 1;
// max un-signed int
uint256 internal constant MAX_UINT =
0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;
/**
* @dev Verifies that the rebasing is not paused.
*/
modifier whenNotRebasePaused() {
require(!rebasePaused, "Rebasing paused");
_;
}
/**
* @dev Verifies that the deposits are not paused.
*/
modifier whenNotCapitalPaused() {
require(!capitalPaused, "Capital paused");
_;
}
modifier onlyOusdMetaStrategy() {
require(
msg.sender == ousdMetaStrategy,
"Caller is not the OUSD meta strategy"
);
_;
}
/**
* @notice Deposit a supported asset and mint OTokens.
* @param _asset Address of the asset being deposited
* @param _amount Amount of the asset being deposited
* @param _minimumOusdAmount Minimum OTokens to mint
*/
function mint(
address _asset,
uint256 _amount,
uint256 _minimumOusdAmount
) external whenNotCapitalPaused nonReentrant {
_mint(_asset, _amount, _minimumOusdAmount);
}
function _mint(
address _asset,
uint256 _amount,
uint256 _minimumOusdAmount
) internal virtual {
require(assets[_asset].isSupported, "Asset is not supported");
require(_amount > 0, "Amount must be greater than 0");
uint256 units = _toUnits(_amount, _asset);
uint256 unitPrice = _toUnitPrice(_asset, true);
uint256 priceAdjustedDeposit = (units * unitPrice) / 1e18;
if (_minimumOusdAmount > 0) {
require(
priceAdjustedDeposit >= _minimumOusdAmount,
"Mint amount lower than minimum"
);
}
emit Mint(msg.sender, priceAdjustedDeposit);
// Rebase must happen before any transfers occur.
if (priceAdjustedDeposit >= rebaseThreshold && !rebasePaused) {
if (dripper != address(0)) {
// Stream any harvested rewards that are available
IDripper(dripper).collect();
}
_rebase();
}
// Mint matching amount of OTokens
oUSD.mint(msg.sender, priceAdjustedDeposit);
// Transfer the deposited coins to the vault
IERC20 asset = IERC20(_asset);
asset.safeTransferFrom(msg.sender, address(this), _amount);
if (priceAdjustedDeposit >= autoAllocateThreshold) {
_allocate();
}
}
/**
* @notice Mint OTokens for a Metapool Strategy
* @param _amount Amount of the asset being deposited
*
* Notice: can't use `nonReentrant` modifier since the `mint` function can
* call `allocate`, and that can trigger `ConvexOUSDMetaStrategy` to call this function
* while the execution of the `mint` has not yet completed -> causing a `nonReentrant` collision.
*
* Also important to understand is that this is a limitation imposed by the test suite.
* Production / mainnet contracts should never be configured in a way where mint/redeem functions
* that are moving funds between the Vault and end user wallets can influence strategies
* utilizing this function.
*/
function mintForStrategy(uint256 _amount)
external
virtual
whenNotCapitalPaused
onlyOusdMetaStrategy
{
require(_amount < MAX_INT, "Amount too high");
emit Mint(msg.sender, _amount);
// safe to cast because of the require check at the beginning of the function
netOusdMintedForStrategy += int256(_amount);
require(
abs(netOusdMintedForStrategy) < netOusdMintForStrategyThreshold,
"Minted ousd surpassed netOusdMintForStrategyThreshold."
);
// Mint matching amount of OTokens
oUSD.mint(msg.sender, _amount);
}
// In memoriam
/**
* @notice Withdraw a supported asset and burn OTokens.
* @param _amount Amount of OTokens to burn
* @param _minimumUnitAmount Minimum stablecoin units to receive in return
*/
function redeem(uint256 _amount, uint256 _minimumUnitAmount)
external
whenNotCapitalPaused
nonReentrant
{
_redeem(_amount, _minimumUnitAmount);
}
/**
* @notice Withdraw a supported asset and burn OTokens.
* @param _amount Amount of OTokens to burn
* @param _minimumUnitAmount Minimum stablecoin units to receive in return
*/
function _redeem(uint256 _amount, uint256 _minimumUnitAmount)
internal
virtual
{
// Calculate redemption outputs
uint256[] memory outputs = _calculateRedeemOutputs(_amount);
emit Redeem(msg.sender, _amount);
// Send outputs
uint256 assetCount = allAssets.length;
for (uint256 i = 0; i < assetCount; ++i) {
if (outputs[i] == 0) continue;
address assetAddr = allAssets[i];
if (IERC20(assetAddr).balanceOf(address(this)) >= outputs[i]) {
// Use Vault funds first if sufficient
IERC20(assetAddr).safeTransfer(msg.sender, outputs[i]);
} else {
address strategyAddr = assetDefaultStrategies[assetAddr];
if (strategyAddr != address(0)) {
// Nothing in Vault, but something in Strategy, send from there
IStrategy strategy = IStrategy(strategyAddr);
strategy.withdraw(msg.sender, assetAddr, outputs[i]);
} else {
// Cant find funds anywhere
revert("Liquidity error");
}
}
}
if (_minimumUnitAmount > 0) {
uint256 unitTotal = 0;
for (uint256 i = 0; i < outputs.length; ++i) {
unitTotal += _toUnits(outputs[i], allAssets[i]);
}
require(
unitTotal >= _minimumUnitAmount,
"Redeem amount lower than minimum"
);
}
oUSD.burn(msg.sender, _amount);
_postRedeem(_amount);
}
function _postRedeem(uint256 _amount) internal {
// Until we can prove that we won't affect the prices of our assets
// by withdrawing them, this should be here.
// It's possible that a strategy was off on its asset total, perhaps
// a reward token sold for more or for less than anticipated.
uint256 totalUnits = 0;
if (_amount >= rebaseThreshold && !rebasePaused) {
totalUnits = _rebase();
} else {
totalUnits = _totalValue();
}
// Check that the OTokens are backed by enough assets
if (maxSupplyDiff > 0) {
// If there are more outstanding withdrawal requests than assets in the vault and strategies
// then the available assets will be negative and totalUnits will be rounded up to zero.
// As we don't know the exact shortfall amount, we will reject all redeem and withdrawals
require(totalUnits > 0, "Too many outstanding requests");
// Allow a max difference of maxSupplyDiff% between
// backing assets value and OUSD total supply
uint256 diff = oUSD.totalSupply().divPrecisely(totalUnits);
require(
(diff > 1e18 ? diff - 1e18 : 1e18 - diff) <= maxSupplyDiff,
"Backing supply liquidity error"
);
}
}
/**
* @notice Burn OTokens for Metapool Strategy
* @param _amount Amount of OUSD to burn
*
* @dev Notice: can't use `nonReentrant` modifier since the `redeem` function could
* require withdrawal on `ConvexOUSDMetaStrategy` and that one can call `burnForStrategy`
* while the execution of the `redeem` has not yet completed -> causing a `nonReentrant` collision.
*
* Also important to understand is that this is a limitation imposed by the test suite.
* Production / mainnet contracts should never be configured in a way where mint/redeem functions
* that are moving funds between the Vault and end user wallets can influence strategies
* utilizing this function.
*/
function burnForStrategy(uint256 _amount)
external
virtual
whenNotCapitalPaused
onlyOusdMetaStrategy
{
require(_amount < MAX_INT, "Amount too high");
emit Redeem(msg.sender, _amount);
// safe to cast because of the require check at the beginning of the function
netOusdMintedForStrategy -= int256(_amount);
require(
abs(netOusdMintedForStrategy) < netOusdMintForStrategyThreshold,
"Attempting to burn too much OUSD."
);
// Burn OTokens
oUSD.burn(msg.sender, _amount);
}
/**
* @notice Withdraw a supported asset and burn all OTokens.
* @param _minimumUnitAmount Minimum stablecoin units to receive in return
*/
function redeemAll(uint256 _minimumUnitAmount)
external
whenNotCapitalPaused
nonReentrant
{
_redeem(oUSD.balanceOf(msg.sender), _minimumUnitAmount);
}
/**
* @notice Allocate unallocated funds on Vault to strategies.
**/
function allocate() external virtual whenNotCapitalPaused nonReentrant {
_allocate();
}
/**
* @dev Allocate unallocated funds on Vault to strategies.
**/
function _allocate() internal virtual {
uint256 vaultValue = _totalValueInVault();
// Nothing in vault to allocate
if (vaultValue == 0) return;
uint256 strategiesValue = _totalValueInStrategies();
// We have a method that does the same as this, gas optimisation
uint256 calculatedTotalValue = vaultValue + strategiesValue;
// We want to maintain a buffer on the Vault so calculate a percentage
// modifier to multiply each amount being allocated by to enforce the
// vault buffer
uint256 vaultBufferModifier;
if (strategiesValue == 0) {
// Nothing in Strategies, allocate 100% minus the vault buffer to
// strategies
vaultBufferModifier = uint256(1e18) - vaultBuffer;
} else {
vaultBufferModifier =
(vaultBuffer * calculatedTotalValue) /
vaultValue;
if (1e18 > vaultBufferModifier) {
// E.g. 1e18 - (1e17 * 10e18)/5e18 = 8e17
// (5e18 * 8e17) / 1e18 = 4e18 allocated from Vault
vaultBufferModifier = uint256(1e18) - vaultBufferModifier;
} else {
// We need to let the buffer fill
return;
}
}
if (vaultBufferModifier == 0) return;
// Iterate over all assets in the Vault and allocate to the appropriate
// strategy
uint256 assetCount = allAssets.length;
for (uint256 i = 0; i < assetCount; ++i) {
IERC20 asset = IERC20(allAssets[i]);
uint256 assetBalance = asset.balanceOf(address(this));
// No balance, nothing to do here
if (assetBalance == 0) continue;
// Multiply the balance by the vault buffer modifier and truncate
// to the scale of the asset decimals
uint256 allocateAmount = assetBalance.mulTruncate(
vaultBufferModifier
);
address depositStrategyAddr = assetDefaultStrategies[
address(asset)
];
if (depositStrategyAddr != address(0) && allocateAmount > 0) {
IStrategy strategy = IStrategy(depositStrategyAddr);
// Transfer asset to Strategy and call deposit method to
// mint or take required action
asset.safeTransfer(address(strategy), allocateAmount);
strategy.deposit(address(asset), allocateAmount);
emit AssetAllocated(
address(asset),
depositStrategyAddr,
allocateAmount
);
}
}
}
/**
* @notice Calculate the total value of assets held by the Vault and all
* strategies and update the supply of OTokens.
*/
function rebase() external virtual nonReentrant {
_rebase();
}
/**
* @dev Calculate the total value of assets held by the Vault and all
* strategies and update the supply of OTokens, optionally sending a
* portion of the yield to the trustee.
* @return totalUnits Total balance of Vault in units
*/
function _rebase() internal whenNotRebasePaused returns (uint256) {
uint256 ousdSupply = oUSD.totalSupply();
uint256 vaultValue = _totalValue();
if (ousdSupply == 0) {
return vaultValue;
}
// Yield fee collection
address _trusteeAddress = trusteeAddress; // gas savings
if (_trusteeAddress != address(0) && (vaultValue > ousdSupply)) {
uint256 yield = vaultValue - ousdSupply;
uint256 fee = yield.mulTruncateScale(trusteeFeeBps, 1e4);
require(yield > fee, "Fee must not be greater than yield");
if (fee > 0) {
oUSD.mint(_trusteeAddress, fee);
}
emit YieldDistribution(_trusteeAddress, yield, fee);
}
// Only rachet OToken supply upwards
ousdSupply = oUSD.totalSupply(); // Final check should use latest value
if (vaultValue > ousdSupply) {
oUSD.changeSupply(vaultValue);
}
return vaultValue;
}
/**
* @notice Determine the total value of assets held by the vault and its
* strategies.
* @return value Total value in USD/ETH (1e18)
*/
function totalValue() external view virtual returns (uint256 value) {
value = _totalValue();
}
/**
* @dev Internal Calculate the total value of the assets held by the
* vault and its strategies.
* @return value Total value in USD/ETH (1e18)
*/
function _totalValue() internal view virtual returns (uint256 value) {
return _totalValueInVault() + _totalValueInStrategies();
}
/**
* @dev Internal to calculate total value of all assets held in Vault.
* @return value Total value in USD/ETH (1e18)
*/
function _totalValueInVault()
internal
view
virtual
returns (uint256 value)
{
uint256 assetCount = allAssets.length;
for (uint256 y = 0; y < assetCount; ++y) {
address assetAddr = allAssets[y];
uint256 balance = IERC20(assetAddr).balanceOf(address(this));
if (balance > 0) {
value += _toUnits(balance, assetAddr);
}
}
}
/**
* @dev Internal to calculate total value of all assets held in Strategies.
* @return value Total value in USD/ETH (1e18)
*/
function _totalValueInStrategies() internal view returns (uint256 value) {
uint256 stratCount = allStrategies.length;
for (uint256 i = 0; i < stratCount; ++i) {
value = value + _totalValueInStrategy(allStrategies[i]);
}
}
/**
* @dev Internal to calculate total value of all assets held by strategy.
* @param _strategyAddr Address of the strategy
* @return value Total value in USD/ETH (1e18)
*/
function _totalValueInStrategy(address _strategyAddr)
internal
view
returns (uint256 value)
{
IStrategy strategy = IStrategy(_strategyAddr);
uint256 assetCount = allAssets.length;
for (uint256 y = 0; y < assetCount; ++y) {
address assetAddr = allAssets[y];
if (strategy.supportsAsset(assetAddr)) {
uint256 balance = strategy.checkBalance(assetAddr);
if (balance > 0) {
value += _toUnits(balance, assetAddr);
}
}
}
}
/**
* @notice Get the balance of an asset held in Vault and all strategies.
* @param _asset Address of asset
* @return uint256 Balance of asset in decimals of asset
*/
function checkBalance(address _asset) external view returns (uint256) {
return _checkBalance(_asset);
}
/**
* @notice Get the balance of an asset held in Vault and all strategies.
* @param _asset Address of asset
* @return balance Balance of asset in decimals of asset
*/
function _checkBalance(address _asset)
internal
view
virtual
returns (uint256 balance)
{
IERC20 asset = IERC20(_asset);
balance = asset.balanceOf(address(this));
uint256 stratCount = allStrategies.length;
for (uint256 i = 0; i < stratCount; ++i) {
IStrategy strategy = IStrategy(allStrategies[i]);
if (strategy.supportsAsset(_asset)) {
balance = balance + strategy.checkBalance(_asset);
}
}
}
/**
* @notice Calculate the outputs for a redeem function, i.e. the mix of
* coins that will be returned
*/
function calculateRedeemOutputs(uint256 _amount)
external
view
returns (uint256[] memory)
{
return _calculateRedeemOutputs(_amount);
}
/**
* @dev Calculate the outputs for a redeem function, i.e. the mix of
* coins that will be returned.
* @return outputs Array of amounts respective to the supported assets
*/
function _calculateRedeemOutputs(uint256 _amount)
internal
view
virtual
returns (uint256[] memory outputs)
{
// We always give out coins in proportion to how many we have,
// Now if all coins were the same value, this math would easy,
// just take the percentage of each coin, and multiply by the
// value to be given out. But if coins are worth more than $1,
// then we would end up handing out too many coins. We need to
// adjust by the total value of coins.
//
// To do this, we total up the value of our coins, by their
// percentages. Then divide what we would otherwise give out by
// this number.
//
// Let say we have 100 DAI at $1.06 and 200 USDT at $1.00.
// So for every 1 DAI we give out, we'll be handing out 2 USDT
// Our total output ratio is: 33% * 1.06 + 66% * 1.00 = 1.02
//
// So when calculating the output, we take the percentage of
// each coin, times the desired output value, divided by the
// totalOutputRatio.
//
// For example, withdrawing: 30 OUSD:
// DAI 33% * 30 / 1.02 = 9.80 DAI
// USDT = 66 % * 30 / 1.02 = 19.60 USDT
//
// Checking these numbers:
// 9.80 DAI * 1.06 = $10.40
// 19.60 USDT * 1.00 = $19.60
//
// And so the user gets $10.40 + $19.60 = $30 worth of value.
uint256 assetCount = allAssets.length;
uint256[] memory assetUnits = new uint256[](assetCount);
uint256[] memory assetBalances = new uint256[](assetCount);
outputs = new uint256[](assetCount);
// Calculate redeem fee
if (redeemFeeBps > 0) {
uint256 redeemFee = _amount.mulTruncateScale(redeemFeeBps, 1e4);
_amount = _amount - redeemFee;
}
// Calculate assets balances and decimals once,
// for a large gas savings.
uint256 totalUnits = 0;
for (uint256 i = 0; i < assetCount; ++i) {
address assetAddr = allAssets[i];
uint256 balance = _checkBalance(assetAddr);
assetBalances[i] = balance;
assetUnits[i] = _toUnits(balance, assetAddr);
totalUnits = totalUnits + assetUnits[i];
}
// Calculate totalOutputRatio
uint256 totalOutputRatio = 0;
for (uint256 i = 0; i < assetCount; ++i) {
uint256 unitPrice = _toUnitPrice(allAssets[i], false);
uint256 ratio = (assetUnits[i] * unitPrice) / totalUnits;
totalOutputRatio = totalOutputRatio + ratio;
}
// Calculate final outputs
uint256 factor = _amount.divPrecisely(totalOutputRatio);
for (uint256 i = 0; i < assetCount; ++i) {
outputs[i] = (assetBalances[i] * factor) / totalUnits;
}
}
/***************************************
Pricing
****************************************/
/**
* @notice Returns the total price in 18 digit units for a given asset.
* Never goes above 1, since that is how we price mints.
* @param asset address of the asset
* @return price uint256: unit (USD / ETH) price for 1 unit of the asset, in 18 decimal fixed
*/
function priceUnitMint(address asset)
external
view
returns (uint256 price)
{
/* need to supply 1 asset unit in asset's decimals and can not just hard-code
* to 1e18 and ignore calling `_toUnits` since we need to consider assets
* with the exchange rate
*/
uint256 units = _toUnits(
uint256(1e18).scaleBy(_getDecimals(asset), 18),
asset
);
price = (_toUnitPrice(asset, true) * units) / 1e18;
}
/**
* @notice Returns the total price in 18 digit unit for a given asset.
* Never goes below 1, since that is how we price redeems
* @param asset Address of the asset
* @return price uint256: unit (USD / ETH) price for 1 unit of the asset, in 18 decimal fixed
*/
function priceUnitRedeem(address asset)
external
view
returns (uint256 price)
{
/* need to supply 1 asset unit in asset's decimals and can not just hard-code
* to 1e18 and ignore calling `_toUnits` since we need to consider assets
* with the exchange rate
*/
uint256 units = _toUnits(
uint256(1e18).scaleBy(_getDecimals(asset), 18),
asset
);
price = (_toUnitPrice(asset, false) * units) / 1e18;
}
/***************************************
Utils
****************************************/
/**
* @dev Convert a quantity of a token into 1e18 fixed decimal "units"
* in the underlying base (USD/ETH) used by the vault.
* Price is not taken into account, only quantity.
*
* Examples of this conversion:
*
* - 1e18 DAI becomes 1e18 units (same decimals)
* - 1e6 USDC becomes 1e18 units (decimal conversion)
* - 1e18 rETH becomes 1.2e18 units (exchange rate conversion)
*
* @param _raw Quantity of asset
* @param _asset Core Asset address
* @return value 1e18 normalized quantity of units
*/
function _toUnits(uint256 _raw, address _asset)
internal
view
returns (uint256)
{
UnitConversion conversion = assets[_asset].unitConversion;
if (conversion == UnitConversion.DECIMALS) {
return _raw.scaleBy(18, _getDecimals(_asset));
} else if (conversion == UnitConversion.GETEXCHANGERATE) {
uint256 exchangeRate = IGetExchangeRateToken(_asset)
.getExchangeRate();
return (_raw * exchangeRate) / 1e18;
} else {
revert("Unsupported conversion type");
}
}
/**
* @dev Returns asset's unit price accounting for different asset types
* and takes into account the context in which that price exists -
* - mint or redeem.
*
* Note: since we are returning the price of the unit and not the one of the
* asset (see comment above how 1 rETH exchanges for 1.2 units) we need
* to make the Oracle price adjustment as well since we are pricing the
* units and not the assets.
*
* The price also snaps to a "full unit price" in case a mint or redeem
* action would be unfavourable to the protocol.
*
*/
function _toUnitPrice(address _asset, bool isMint)
internal
view
returns (uint256 price)
{
UnitConversion conversion = assets[_asset].unitConversion;
price = IOracle(priceProvider).price(_asset);
if (conversion == UnitConversion.GETEXCHANGERATE) {
uint256 exchangeRate = IGetExchangeRateToken(_asset)
.getExchangeRate();
price = (price * 1e18) / exchangeRate;
} else if (conversion != UnitConversion.DECIMALS) {
revert("Unsupported conversion type");
}
/* At this stage the price is already adjusted to the unit
* so the price checks are agnostic to underlying asset being
* pegged to a USD or to an ETH or having a custom exchange rate.
*/
require(price <= MAX_UNIT_PRICE_DRIFT, "Vault: Price exceeds max");
require(price >= MIN_UNIT_PRICE_DRIFT, "Vault: Price under min");
if (isMint) {
/* Never price a normalized unit price for more than one
* unit of OETH/OUSD when minting.
*/
if (price > 1e18) {
price = 1e18;
}
require(price >= MINT_MINIMUM_UNIT_PRICE, "Asset price below peg");
} else {
/* Never give out more than 1 normalized unit amount of assets
* for one unit of OETH/OUSD when redeeming.
*/
if (price < 1e18) {
price = 1e18;
}
}
}
function _getDecimals(address _asset)
internal
view
returns (uint256 decimals)
{
decimals = assets[_asset].decimals;
require(decimals > 0, "Decimals not cached");
}
/**
* @notice Return the number of assets supported by the Vault.
*/
function getAssetCount() public view returns (uint256) {
return allAssets.length;
}
/**
* @notice Gets the vault configuration of a supported asset.
*/
function getAssetConfig(address _asset)
public
view
returns (Asset memory config)
{
config = assets[_asset];
}
/**
* @notice Return all vault asset addresses in order
*/
function getAllAssets() external view returns (address[] memory) {
return allAssets;
}
/**
* @notice Return the number of strategies active on the Vault.
*/
function getStrategyCount() external view returns (uint256) {
return allStrategies.length;
}
/**
* @notice Return the array of all strategies
*/
function getAllStrategies() external view returns (address[] memory) {
return allStrategies;
}
/**
* @notice Returns whether the vault supports the asset
* @param _asset address of the asset
* @return true if supported
*/
function isSupportedAsset(address _asset) external view returns (bool) {
return assets[_asset].isSupported;
}
/**
* @dev Falldown to the admin implementation
* @notice This is a catch all for all functions not declared in core
*/
// solhint-disable-next-line no-complex-fallback
fallback() external {
bytes32 slot = adminImplPosition;
// solhint-disable-next-line no-inline-assembly
assembly {
// Copy msg.data. We take full control of memory in this inline assembly
// block because it will not return to Solidity code. We overwrite the
// Solidity scratch pad at memory position 0.
calldatacopy(0, 0, calldatasize())
// Call the implementation.
// out and outsize are 0 because we don't know the size yet.
let result := delegatecall(
gas(),
sload(slot),
0,
calldatasize(),
0,
0
)
// Copy the returned data.
returndatacopy(0, 0, returndatasize())
switch result
// delegatecall returns 0 on error.
case 0 {
revert(0, returndatasize())
}
default {
return(0, returndatasize())
}
}
}
function abs(int256 x) private pure returns (uint256) {
require(x < int256(MAX_INT), "Amount too high");
return x >= 0 ? uint256(x) : uint256(-x);
}
}