This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
OlympusTreasury.sol
686 lines (575 loc) · 25.3 KB
/
OlympusTreasury.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
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.15;
import {ERC20} from "solmate/tokens/ERC20.sol";
import {ReentrancyGuard} from "solmate/utils/ReentrancyGuard.sol";
import {TransferHelper} from "libraries/TransferHelper.sol";
import "src/modules/TRSRY/TRSRY.v1.sol";
import "src/Kernel.sol";
/// @title OlympusTreasury
/// @author Oighty
/// @notice Treasury holds all other assets under the control of the protocol.
contract OlympusTreasury is TRSRYv1_1, ReentrancyGuard {
using TransferHelper for ERC20;
//============================================================================================//
// MODULE SETUP //
//============================================================================================//
constructor(Kernel kernel_) Module(kernel_) {
active = true;
// Configure Asset Categories and Groups
// Create category groups
categoryGroups.push(toCategoryGroup("liquidity-preference"));
categoryGroups.push(toCategoryGroup("value-baskets"));
categoryGroups.push(toCategoryGroup("market-sensitivity"));
// Liquidity Preference: Liquid, Illiquid
categoryToGroup[toCategory("liquid")] = toCategoryGroup("liquidity-preference");
groupToCategories[toCategoryGroup("liquidity-preference")].push(toCategory("liquid"));
categoryToGroup[toCategory("illiquid")] = toCategoryGroup("liquidity-preference");
groupToCategories[toCategoryGroup("liquidity-preference")].push(toCategory("illiquid"));
// Value Baskets: Reserves, Strategic, Protocol-Owned Liquidity
categoryToGroup[toCategory("reserves")] = toCategoryGroup("value-baskets");
groupToCategories[toCategoryGroup("value-baskets")].push(toCategory("reserves"));
categoryToGroup[toCategory("strategic")] = toCategoryGroup("value-baskets");
groupToCategories[toCategoryGroup("value-baskets")].push(toCategory("strategic"));
categoryToGroup[toCategory("protocol-owned-liquidity")] = toCategoryGroup("value-baskets");
groupToCategories[toCategoryGroup("value-baskets")].push(
toCategory("protocol-owned-liquidity")
);
// Market Sensitivity: Stable, Volatile
categoryToGroup[toCategory("stable")] = toCategoryGroup("market-sensitivity");
groupToCategories[toCategoryGroup("market-sensitivity")].push(toCategory("stable"));
categoryToGroup[toCategory("volatile")] = toCategoryGroup("market-sensitivity");
groupToCategories[toCategoryGroup("market-sensitivity")].push(toCategory("volatile"));
}
/// @inheritdoc Module
function KEYCODE() public pure override returns (Keycode) {
return toKeycode("TRSRY");
}
/// @inheritdoc Module
function VERSION() external pure override returns (uint8 major, uint8 minor) {
major = 1;
minor = 1;
}
//============================================================================================//
// CORE FUNCTIONS //
//============================================================================================//
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function increaseWithdrawApproval(
address withdrawer_,
ERC20 token_,
uint256 amount_
) external override permissioned {
uint256 approval = withdrawApproval[withdrawer_][token_];
uint256 newAmount = type(uint256).max - approval <= amount_
? type(uint256).max
: approval + amount_;
withdrawApproval[withdrawer_][token_] = newAmount;
emit IncreaseWithdrawApproval(withdrawer_, token_, newAmount);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function decreaseWithdrawApproval(
address withdrawer_,
ERC20 token_,
uint256 amount_
) external override permissioned {
uint256 approval = withdrawApproval[withdrawer_][token_];
uint256 newAmount = approval <= amount_ ? 0 : approval - amount_;
withdrawApproval[withdrawer_][token_] = newAmount;
emit DecreaseWithdrawApproval(withdrawer_, token_, newAmount);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - The module is not active
function withdrawReserves(
address to_,
ERC20 token_,
uint256 amount_
) public override permissioned onlyWhileActive {
withdrawApproval[msg.sender][token_] -= amount_;
token_.safeTransfer(to_, amount_);
emit Withdrawal(msg.sender, to_, token_, amount_);
}
// ========= DEBT FUNCTIONS ========= //
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function increaseDebtorApproval(
address debtor_,
ERC20 token_,
uint256 amount_
) external override permissioned {
uint256 newAmount = debtApproval[debtor_][token_] + amount_;
debtApproval[debtor_][token_] = newAmount;
emit IncreaseDebtorApproval(debtor_, token_, newAmount);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function decreaseDebtorApproval(
address debtor_,
ERC20 token_,
uint256 amount_
) external override permissioned {
uint256 newAmount = debtApproval[debtor_][token_] - amount_;
debtApproval[debtor_][token_] = newAmount;
emit DecreaseDebtorApproval(debtor_, token_, newAmount);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - The module is not active
function incurDebt(
ERC20 token_,
uint256 amount_
) external override permissioned onlyWhileActive {
debtApproval[msg.sender][token_] -= amount_;
// Add debt to caller
reserveDebt[token_][msg.sender] += amount_;
totalDebt[token_] += amount_;
token_.safeTransfer(msg.sender, amount_);
emit DebtIncurred(token_, msg.sender, amount_);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - The debt for `token_` and `debtor_` is 0
function repayDebt(
address debtor_,
ERC20 token_,
uint256 amount_
) external override permissioned nonReentrant {
if (reserveDebt[token_][debtor_] == 0) revert TRSRY_NoDebtOutstanding();
// Deposit from caller first (to handle nonstandard token transfers)
uint256 prevBalance = token_.balanceOf(address(this));
token_.safeTransferFrom(msg.sender, address(this), amount_);
uint256 received = token_.balanceOf(address(this)) - prevBalance;
// Choose minimum between passed-in amount and received amount
if (received > amount_) received = amount_;
// Subtract debt from debtor
reserveDebt[token_][debtor_] -= received;
totalDebt[token_] -= received;
emit DebtRepaid(token_, debtor_, received);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function setDebt(
address debtor_,
ERC20 token_,
uint256 amount_
) external override permissioned {
uint256 oldDebt = reserveDebt[token_][debtor_];
reserveDebt[token_][debtor_] = amount_;
if (oldDebt < amount_) totalDebt[token_] += amount_ - oldDebt;
else totalDebt[token_] -= oldDebt - amount_;
emit DebtSet(token_, debtor_, amount_);
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function deactivate() external override permissioned {
active = false;
}
/// @inheritdoc TRSRYv1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
function activate() external override permissioned {
active = true;
}
//============================================================================================//
// VIEW FUNCTIONS //
//============================================================================================//
/// @inheritdoc TRSRYv1
function getReserveBalance(ERC20 token_) public view override returns (uint256) {
return token_.balanceOf(address(this)) + totalDebt[token_];
}
//============================================================================================//
// DATA FUNCTIONS //
//============================================================================================//
// ========== ASSET INFORMATION ========== //
/// @inheritdoc TRSRYv1_1
function getAssets() external view override returns (address[] memory) {
return assets;
}
/// @inheritdoc TRSRYv1_1
function getAssetData(address asset_) external view override returns (Asset memory) {
return assetData[asset_];
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - `category_` is not a valid category
function getAssetsByCategory(
Category category_
) public view override returns (address[] memory) {
// Get category group
CategoryGroup group = categoryToGroup[category_];
if (fromCategoryGroup(group) == bytes32(0))
revert TRSRY_InvalidParams(0, abi.encode(category_));
// Iterate through assets and count the ones in the category
uint256 len = assets.length;
uint256 count;
for (uint256 i; i < len; i++) {
if (fromCategory(categorization[assets[i]][group]) == fromCategory(category_)) {
unchecked {
++count;
}
}
}
// Create array and iterate through assets again to populate it
address[] memory categoryAssets = new address[](count);
count = 0;
for (uint256 i; i < len; i++) {
if (fromCategory(categorization[assets[i]][group]) == fromCategory(category_)) {
categoryAssets[count] = assets[i];
unchecked {
++count;
}
}
}
return categoryAssets;
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - `asset_` is not approved
/// @dev - `variant_` is invalid
/// @dev - There is an error when determining the balance
function getAssetBalance(
address asset_,
Variant variant_
) public view override returns (uint256, uint48) {
// Check if asset is approved
if (!assetData[asset_].approved) revert TRSRY_AssetNotApproved(asset_);
// Route to correct balance function based on requested variant
if (variant_ == Variant.CURRENT) {
return _getCurrentBalance(asset_);
} else if (variant_ == Variant.LAST) {
return _getLastBalance(asset_);
} else {
revert TRSRY_InvalidParams(1, abi.encode(variant_));
}
}
/// @notice Returns the current balance of `asset_` (including debt) across all locations
///
/// @param asset_ The asset to get the balance of
/// @return The current balance of `asset_`
/// @return The timestamp of the current balance
function _getCurrentBalance(address asset_) internal view returns (uint256, uint48) {
// Cast asset to ERC20
Asset memory asset = assetData[asset_];
ERC20 token = ERC20(asset_);
// Get reserve balance from this contract to begin with
uint256 balance = getReserveBalance(token);
// Get balances from other locations
uint256 len = asset.locations.length;
for (uint256 i; i < len; ) {
balance += token.balanceOf(asset.locations[i]);
unchecked {
++i;
}
}
return (balance, uint48(block.timestamp));
}
/// @notice Returns the last cached balance of `asset_` (including debt) across all locations
///
/// @param asset_ The asset to get the balance of
/// @return The last cached balance of `asset_`
/// @return The timestamp of the last cached balance
function _getLastBalance(address asset_) internal view returns (uint256, uint48) {
// Return last balance and time
return (assetData[asset_].lastBalance, assetData[asset_].updatedAt);
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
function storeBalance(address asset_) external override permissioned returns (uint256) {
Asset storage asset = assetData[asset_];
// Check that asset is approved
if (!asset.approved) revert TRSRY_AssetNotApproved(asset_);
// Get the current balance for the asset
(uint256 balance, uint48 time) = _getCurrentBalance(asset_);
// Store the data
asset.lastBalance = balance;
asset.updatedAt = time;
// Emit event
emit BalanceStored(asset_, balance, time);
return balance;
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - `category_` is invalid
/// @dev - `getAssetsByCategory()` reverts
function getCategoryBalance(
Category category_,
Variant variant_
) external view override returns (uint256, uint48) {
// Get category group and check that it is valid
CategoryGroup group = categoryToGroup[category_];
if (fromCategoryGroup(group) == bytes32(0))
revert TRSRY_InvalidParams(0, abi.encode(category_));
// Get category assets
address[] memory categoryAssets = getAssetsByCategory(category_);
// Get balance for each asset in the category and add to total
uint256 len = categoryAssets.length;
uint256 balance;
uint48 time;
for (uint256 i; i < len; ) {
(uint256 assetBalance, uint48 assetTime) = getAssetBalance(categoryAssets[i], variant_);
balance += assetBalance;
// Get the most outdated time
if (i == 0) {
time = assetTime;
} else if (assetTime < time) {
time = assetTime;
}
unchecked {
++i;
}
}
return (balance, time);
}
// ========== DATA MANAGEMENT ========== //
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is already approved
/// @dev - `asset_` is not a contract
/// @dev - `locations_` contains the zero address
function addAsset(
address asset_,
address[] calldata locations_
) external override permissioned {
Asset storage asset = assetData[asset_];
// Ensure asset is not already added
if (asset.approved) revert TRSRY_AssetAlreadyApproved(asset_);
// Check that asset is a contract
if (asset_.code.length == 0) revert TRSRY_AssetNotContract(asset_);
// Set asset as approved and add to array
asset.approved = true;
assets.push(asset_);
// Validate balance locations and store
uint256 len = locations_.length;
for (uint256 i; i < len; ) {
if (locations_[i] == address(0))
revert TRSRY_InvalidParams(1, abi.encode(locations_[i]));
asset.locations.push(locations_[i]);
unchecked {
++i;
}
}
// Initialize cache with current value
(asset.lastBalance, asset.updatedAt) = _getCurrentBalance(asset_);
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
function removeAsset(address asset_) external override permissioned {
Asset storage asset = assetData[asset_];
// Check that asset is approved
if (!asset.approved) revert TRSRY_AssetNotApproved(asset_);
// Remove asset
uint256 len = assets.length;
for (uint256 i; i < len; ) {
if (assets[i] == asset_) {
assets[i] = assets[len - 1];
assets.pop();
break;
}
unchecked {
++i;
}
}
// Remove locations
len = asset.locations.length;
for (uint256 i; i < len; ) {
asset.locations[i] = asset.locations[len - 1];
asset.locations.pop();
unchecked {
++i;
}
}
// Remove categorization
len = categoryGroups.length;
for (uint256 i; i < len; ) {
categorization[asset_][categoryGroups[i]] = toCategory(bytes32(0));
unchecked {
++i;
}
}
// Remove asset data
delete assetData[asset_];
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
/// @dev - `location_` is the zero address
/// @dev - `location_` is already added to `asset_`
function addAssetLocation(address asset_, address location_) external override permissioned {
Asset storage asset = assetData[asset_];
// Check that asset is approved
if (!asset.approved) revert TRSRY_AssetNotApproved(asset_);
// Check that the location is not the zero address
if (location_ == address(0)) revert TRSRY_InvalidParams(1, abi.encode(location_));
// Check that location is not already added
uint256 len = asset.locations.length;
for (uint256 i; i < len; ) {
if (asset.locations[i] == location_)
revert TRSRY_InvalidParams(1, abi.encode(location_));
unchecked {
++i;
}
}
// Add location to array
asset.locations.push(location_);
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
function removeAssetLocation(address asset_, address location_) external override permissioned {
Asset storage asset = assetData[asset_];
// Check that asset is approved
if (!asset.approved) revert TRSRY_AssetNotApproved(asset_);
// Remove location
// Don't have to check if it's already added because the loop will just not perform any actions is not
uint256 len = asset.locations.length;
for (uint256 i; i < len; ) {
if (asset.locations[i] == location_) {
asset.locations[i] = asset.locations[len - 1];
asset.locations.pop();
break;
}
unchecked {
++i;
}
}
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `group_` exists
/// @dev - `group_` is empty or 0
function addCategoryGroup(CategoryGroup group_) external override permissioned {
// Check if the category group is valid
if (fromCategoryGroup(group_) == bytes32(0))
revert TRSRY_InvalidParams(0, abi.encode(group_));
// Check if the category group exists
if (_categoryGroupExists(group_)) revert TRSRY_CategoryGroupExists(group_);
// Store new category group
categoryGroups.push(group_);
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `group_` does not exist
function removeCategoryGroup(CategoryGroup group_) external override permissioned {
// Check if the category group exists
if (!_categoryGroupExists(group_)) revert TRSRY_CategoryGroupDoesNotExist(group_);
// Remove category group
uint256 len = categoryGroups.length;
for (uint256 i; i < len; ) {
if (fromCategoryGroup(categoryGroups[i]) == fromCategoryGroup(group_)) {
categoryGroups[i] = categoryGroups[len - 1];
categoryGroups.pop();
break;
}
unchecked {
++i;
}
}
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `group_` does not exist
/// @dev - `category_` is empty or 0
/// @dev - `category_` exists
function addCategory(Category category_, CategoryGroup group_) external override permissioned {
// Check if the category group exists
if (!_categoryGroupExists(group_)) revert TRSRY_CategoryGroupDoesNotExist(group_);
// Check if the category is valid
if (fromCategory(category_) == bytes32(0))
revert TRSRY_InvalidParams(0, abi.encode(category_));
// Check if the category exists by seeing if it has a non-zero category group
if (fromCategoryGroup(categoryToGroup[category_]) != bytes32(0))
revert TRSRY_CategoryExists(category_);
// Store category data
categoryToGroup[category_] = group_;
groupToCategories[group_].push(category_);
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `category_` does not exist
function removeCategory(Category category_) external override permissioned {
// Check if the category exists by seeing if it has a non-zero category group
CategoryGroup group = categoryToGroup[category_];
if (fromCategoryGroup(group) == bytes32(0)) revert TRSRY_CategoryDoesNotExist(category_);
// Remove category data
categoryToGroup[category_] = toCategoryGroup(bytes32(0));
// Remove category from group
uint256 len = groupToCategories[group].length;
for (uint256 i; i < len; ) {
if (fromCategory(groupToCategories[group][i]) == fromCategory(category_)) {
groupToCategories[group][i] = groupToCategories[group][len - 1];
groupToCategories[group].pop();
break;
}
unchecked {
++i;
}
}
}
/// @notice Checks if a category group exists
///
/// @param categoryGroup_ The category group to check
/// @return True if the category group exists, otherwise false
function _categoryGroupExists(CategoryGroup categoryGroup_) internal view returns (bool) {
// It's expected that the number of category groups will be fairly small
// so we should be able to iterate through them instead of creating a mapping
uint256 len = categoryGroups.length;
for (uint256 i; i < len; ) {
if (fromCategoryGroup(categoryGroups[i]) == fromCategoryGroup(categoryGroup_))
return true;
unchecked {
++i;
}
}
return false;
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
/// @dev - `category_` does not exist
function categorize(address asset_, Category category_) external override permissioned {
// Check that asset is initialized
if (!assetData[asset_].approved) revert TRSRY_InvalidParams(0, abi.encode(asset_));
// Check if the category exists by seeing if it has a non-zero category group
CategoryGroup group = categoryToGroup[category_];
if (fromCategoryGroup(group) == bytes32(0)) revert TRSRY_CategoryDoesNotExist(category_);
// Store category data for address
categorization[asset_][group] = category_;
}
/// @inheritdoc TRSRYv1_1
/// @dev This function reverts if:
/// @dev - The caller is not permissioned
/// @dev - `asset_` is not approved
/// @dev - `category_` does not contain `asset_`
function uncategorize(address asset_, Category category_) external override permissioned {
// Check that asset is initialized
if (!assetData[asset_].approved) revert TRSRY_InvalidParams(0, abi.encode(asset_));
// Check that the asset is in the category
CategoryGroup group = categoryToGroup[category_];
if (fromCategory(categorization[asset_][group]) != fromCategory(category_))
revert TRSRY_AssetNotInCategory(asset_, category_);
// Remove category data for address
categorization[asset_][group] = toCategory(bytes32(0));
}
}