forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollateralManager.sol
567 lines (437 loc) · 19.2 KB
/
CollateralManager.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
pragma solidity ^0.5.16;
// Inheritance
import "./Owned.sol";
import "./Pausable.sol";
import "./MixinResolver.sol";
import "./interfaces/ICollateralManager.sol";
// Libraries
import "./AddressSetLib.sol";
import "./Bytes32SetLib.sol";
import "./SafeDecimalMath.sol";
// Internal references
import "./CollateralManagerState.sol";
import "./interfaces/IIssuer.sol";
import "./interfaces/IExchangeRates.sol";
import "./interfaces/IERC20.sol";
import "./interfaces/ISynth.sol";
contract CollateralManager is ICollateralManager, Owned, Pausable, MixinResolver {
/* ========== LIBRARIES ========== */
using SafeMath for uint;
using SafeDecimalMath for uint;
using AddressSetLib for AddressSetLib.AddressSet;
using Bytes32SetLib for Bytes32SetLib.Bytes32Set;
/* ========== CONSTANTS ========== */
bytes32 private constant sUSD = "sUSD";
uint private constant SECONDS_IN_A_YEAR = 31556926 * 1e18;
// Flexible storage names
bytes32 public constant CONTRACT_NAME = "CollateralManager";
bytes32 internal constant COLLATERAL_SYNTHS = "collateralSynth";
/* ========== STATE VARIABLES ========== */
// Stores debt balances and borrow rates.
CollateralManagerState public state;
// The set of all collateral contracts.
AddressSetLib.AddressSet internal _collaterals;
// The set of all available currency keys.
Bytes32SetLib.Bytes32Set internal _currencyKeys;
// The set of all synths issuable by the various collateral contracts
Bytes32SetLib.Bytes32Set internal _synths;
// Map from currency key to synth contract name.
mapping(bytes32 => bytes32) public synthsByKey;
// The set of all synths that are shortable.
Bytes32SetLib.Bytes32Set internal _shortableSynths;
mapping(bytes32 => bytes32) public shortableSynthsByKey;
// The factor that will scale the utilisation ratio.
uint public utilisationMultiplier = 1e18;
// The maximum amount of debt in sUSD that can be issued by non snx collateral.
uint public maxDebt;
// The rate that determines the skew limit maximum.
uint public maxSkewRate;
// The base interest rate applied to all borrows.
uint public baseBorrowRate;
// The base interest rate applied to all shorts.
uint public baseShortRate;
/* ---------- Address Resolver Configuration ---------- */
bytes32 private constant CONTRACT_ISSUER = "Issuer";
bytes32 private constant CONTRACT_EXRATES = "ExchangeRates";
bytes32[24] private addressesToCache = [CONTRACT_ISSUER, CONTRACT_EXRATES];
/* ========== CONSTRUCTOR ========== */
constructor(
CollateralManagerState _state,
address _owner,
address _resolver,
uint _maxDebt,
uint _maxSkewRate,
uint _baseBorrowRate,
uint _baseShortRate
) public Owned(_owner) Pausable() MixinResolver(_resolver) {
owner = msg.sender;
state = _state;
setMaxDebt(_maxDebt);
setMaxSkewRate(_maxSkewRate);
setBaseBorrowRate(_baseBorrowRate);
setBaseShortRate(_baseShortRate);
owner = _owner;
}
/* ========== VIEWS ========== */
function resolverAddressesRequired() public view returns (bytes32[] memory addresses) {
bytes32[] memory staticAddresses = new bytes32[](2);
staticAddresses[0] = CONTRACT_ISSUER;
staticAddresses[1] = CONTRACT_EXRATES;
bytes32[] memory shortAddresses;
uint length = _shortableSynths.elements.length;
if (length > 0) {
shortAddresses = new bytes32[](length);
for (uint i = 0; i < length; i++) {
shortAddresses[i] = _shortableSynths.elements[i];
}
}
bytes32[] memory synthAddresses = combineArrays(shortAddresses, _synths.elements);
if (synthAddresses.length > 0) {
addresses = combineArrays(synthAddresses, staticAddresses);
} else {
addresses = staticAddresses;
}
}
// helper function to check whether synth "by key" is a collateral issued by multi-collateral
function isSynthManaged(bytes32 currencyKey) external view returns (bool) {
return synthsByKey[currencyKey] != bytes32(0);
}
/* ---------- Related Contracts ---------- */
function _issuer() internal view returns (IIssuer) {
return IIssuer(requireAndGetAddress(CONTRACT_ISSUER));
}
function _exchangeRates() internal view returns (IExchangeRates) {
return IExchangeRates(requireAndGetAddress(CONTRACT_EXRATES));
}
function _synth(bytes32 synthName) internal view returns (ISynth) {
return ISynth(requireAndGetAddress(synthName));
}
/* ---------- Manager Information ---------- */
function hasCollateral(address collateral) public view returns (bool) {
return _collaterals.contains(collateral);
}
function hasAllCollaterals(address[] memory collaterals) public view returns (bool) {
for (uint i = 0; i < collaterals.length; i++) {
if (!hasCollateral(collaterals[i])) {
return false;
}
}
return true;
}
/* ---------- State Information ---------- */
function long(bytes32 synth) external view returns (uint amount) {
return state.long(synth);
}
function short(bytes32 synth) external view returns (uint amount) {
return state.short(synth);
}
function totalLong() public view returns (uint susdValue, bool anyRateIsInvalid) {
bytes32[] memory synths = _currencyKeys.elements;
if (synths.length > 0) {
for (uint i = 0; i < synths.length; i++) {
bytes32 synth = synths[i];
if (synth == sUSD) {
susdValue = susdValue.add(state.long(synth));
} else {
(uint rate, bool invalid) = _exchangeRates().rateAndInvalid(synth);
uint amount = state.long(synth).multiplyDecimal(rate);
susdValue = susdValue.add(amount);
if (invalid) {
anyRateIsInvalid = true;
}
}
}
}
}
function totalShort() public view returns (uint susdValue, bool anyRateIsInvalid) {
bytes32[] memory synths = _shortableSynths.elements;
if (synths.length > 0) {
for (uint i = 0; i < synths.length; i++) {
bytes32 synth = _synth(synths[i]).currencyKey();
(uint rate, bool invalid) = _exchangeRates().rateAndInvalid(synth);
uint amount = state.short(synth).multiplyDecimal(rate);
susdValue = susdValue.add(amount);
if (invalid) {
anyRateIsInvalid = true;
}
}
}
}
function totalLongAndShort() public view returns (uint susdValue, bool anyRateIsInvalid) {
bytes32[] memory currencyKeys = _currencyKeys.elements;
if (currencyKeys.length > 0) {
(uint[] memory rates, bool invalid) = _exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);
for (uint i = 0; i < rates.length; i++) {
uint longAmount = state.long(currencyKeys[i]).multiplyDecimal(rates[i]);
uint shortAmount = state.short(currencyKeys[i]).multiplyDecimal(rates[i]);
susdValue = susdValue.add(longAmount).add(shortAmount);
if (invalid) {
anyRateIsInvalid = true;
}
}
}
}
function getBorrowRate() public view returns (uint borrowRate, bool anyRateIsInvalid) {
// get the snx backed debt.
uint snxDebt = _issuer().totalIssuedSynths(sUSD, true);
// now get the non snx backed debt.
(uint nonSnxDebt, bool ratesInvalid) = totalLong();
// the total.
uint totalDebt = snxDebt.add(nonSnxDebt);
// now work out the utilisation ratio, and divide through to get a per second value.
uint utilisation = nonSnxDebt.divideDecimal(totalDebt).divideDecimal(SECONDS_IN_A_YEAR);
// scale it by the utilisation multiplier.
uint scaledUtilisation = utilisation.multiplyDecimal(utilisationMultiplier);
// finally, add the base borrow rate.
borrowRate = scaledUtilisation.add(baseBorrowRate);
anyRateIsInvalid = ratesInvalid;
}
function getShortRate(bytes32 synthKey) public view returns (uint shortRate, bool rateIsInvalid) {
rateIsInvalid = _exchangeRates().rateIsInvalid(synthKey);
// Get the long and short supply.
uint longSupply = IERC20(address(_synth(shortableSynthsByKey[synthKey]))).totalSupply();
uint shortSupply = state.short(synthKey);
// In this case, the market is skewed long so its free to short.
if (longSupply > shortSupply) {
return (0, rateIsInvalid);
}
// Otherwise workout the skew towards the short side.
uint skew = shortSupply.sub(longSupply);
// Divide through by the size of the market.
uint proportionalSkew = skew.divideDecimal(longSupply.add(shortSupply)).divideDecimal(SECONDS_IN_A_YEAR);
// Enforce a skew limit maximum.
uint maxSkewLimit = proportionalSkew.multiplyDecimal(maxSkewRate);
// Finally, add the base short rate.
shortRate = maxSkewLimit.add(baseShortRate);
}
function getRatesAndTime(uint index)
public
view
returns (
uint entryRate,
uint lastRate,
uint lastUpdated,
uint newIndex
)
{
(entryRate, lastRate, lastUpdated, newIndex) = state.getRatesAndTime(index);
}
function getShortRatesAndTime(bytes32 currency, uint index)
public
view
returns (
uint entryRate,
uint lastRate,
uint lastUpdated,
uint newIndex
)
{
(entryRate, lastRate, lastUpdated, newIndex) = state.getShortRatesAndTime(currency, index);
}
function exceedsDebtLimit(uint amount, bytes32 currency) external view returns (bool canIssue, bool anyRateIsInvalid) {
uint usdAmount = _exchangeRates().effectiveValue(currency, amount, sUSD);
(uint longAndShortValue, bool invalid) = totalLongAndShort();
return (longAndShortValue.add(usdAmount) <= maxDebt, invalid);
}
/* ========== MUTATIVE FUNCTIONS ========== */
/* ---------- SETTERS ---------- */
function setUtilisationMultiplier(uint _utilisationMultiplier) public onlyOwner {
require(_utilisationMultiplier > 0, "Must be greater than 0");
utilisationMultiplier = _utilisationMultiplier;
emit UtilisationMultiplierUpdated(utilisationMultiplier);
}
function setMaxDebt(uint _maxDebt) public onlyOwner {
require(_maxDebt > 0, "Must be greater than 0");
maxDebt = _maxDebt;
emit MaxDebtUpdated(maxDebt);
}
function setMaxSkewRate(uint _maxSkewRate) public onlyOwner {
maxSkewRate = _maxSkewRate;
emit MaxSkewRateUpdated(maxSkewRate);
}
function setBaseBorrowRate(uint _baseBorrowRate) public onlyOwner {
baseBorrowRate = _baseBorrowRate;
emit BaseBorrowRateUpdated(baseBorrowRate);
}
function setBaseShortRate(uint _baseShortRate) public onlyOwner {
baseShortRate = _baseShortRate;
emit BaseShortRateUpdated(baseShortRate);
}
/* ---------- LOANS ---------- */
function getNewLoanId() external onlyCollateral returns (uint id) {
id = state.incrementTotalLoans();
}
/* ---------- MANAGER ---------- */
function addCollaterals(address[] calldata collaterals) external onlyOwner {
for (uint i = 0; i < collaterals.length; i++) {
if (!_collaterals.contains(collaterals[i])) {
_collaterals.add(collaterals[i]);
emit CollateralAdded(collaterals[i]);
}
}
}
function removeCollaterals(address[] calldata collaterals) external onlyOwner {
for (uint i = 0; i < collaterals.length; i++) {
if (_collaterals.contains(collaterals[i])) {
_collaterals.remove(collaterals[i]);
emit CollateralRemoved(collaterals[i]);
}
}
}
function addSynths(bytes32[] calldata synthNamesInResolver, bytes32[] calldata synthKeys) external onlyOwner {
require(synthNamesInResolver.length == synthKeys.length, "Input array length mismatch");
for (uint i = 0; i < synthNamesInResolver.length; i++) {
if (!_synths.contains(synthNamesInResolver[i])) {
bytes32 synthName = synthNamesInResolver[i];
_synths.add(synthName);
_currencyKeys.add(synthKeys[i]);
synthsByKey[synthKeys[i]] = synthName;
emit SynthAdded(synthName);
}
}
rebuildCache();
}
function areSynthsAndCurrenciesSet(bytes32[] calldata requiredSynthNamesInResolver, bytes32[] calldata synthKeys)
external
view
returns (bool)
{
if (_synths.elements.length != requiredSynthNamesInResolver.length) {
return false;
}
for (uint i = 0; i < requiredSynthNamesInResolver.length; i++) {
if (!_synths.contains(requiredSynthNamesInResolver[i])) {
return false;
}
if (synthsByKey[synthKeys[i]] != requiredSynthNamesInResolver[i]) {
return false;
}
}
return true;
}
function removeSynths(bytes32[] calldata synthNamesInResolver, bytes32[] calldata synthKeys) external onlyOwner {
require(synthNamesInResolver.length == synthKeys.length, "Input array length mismatch");
for (uint i = 0; i < synthNamesInResolver.length; i++) {
if (_synths.contains(synthNamesInResolver[i])) {
// Remove it from the the address set lib.
_synths.remove(synthNamesInResolver[i]);
_currencyKeys.remove(synthKeys[i]);
delete synthsByKey[synthKeys[i]];
emit SynthRemoved(synthNamesInResolver[i]);
}
}
}
function addShortableSynths(bytes32[] calldata requiredSynthNamesInResolver, bytes32[] calldata synthKeys)
external
onlyOwner
{
require(requiredSynthNamesInResolver.length == synthKeys.length, "Input array length mismatch");
for (uint i = 0; i < requiredSynthNamesInResolver.length; i++) {
bytes32 synth = requiredSynthNamesInResolver[i];
if (!_shortableSynths.contains(synth)) {
// Add it to the address set lib.
_shortableSynths.add(synth);
shortableSynthsByKey[synthKeys[i]] = synth;
emit ShortableSynthAdded(synth);
// now the associated synth key to the CollateralManagerState
state.addShortCurrency(synthKeys[i]);
}
}
rebuildCache();
}
function areShortableSynthsSet(bytes32[] calldata requiredSynthNamesInResolver, bytes32[] calldata synthKeys)
external
view
returns (bool)
{
require(requiredSynthNamesInResolver.length == synthKeys.length, "Input array length mismatch");
if (_shortableSynths.elements.length != requiredSynthNamesInResolver.length) {
return false;
}
// now check everything added to external state contract
for (uint i = 0; i < synthKeys.length; i++) {
if (state.getShortRatesLength(synthKeys[i]) == 0) {
return false;
}
}
return true;
}
function removeShortableSynths(bytes32[] calldata synths) external onlyOwner {
for (uint i = 0; i < synths.length; i++) {
if (_shortableSynths.contains(synths[i])) {
// Remove it from the the address set lib.
_shortableSynths.remove(synths[i]);
bytes32 synthKey = _synth(synths[i]).currencyKey();
delete shortableSynthsByKey[synthKey];
state.removeShortCurrency(synthKey);
emit ShortableSynthRemoved(synths[i]);
}
}
}
/* ---------- STATE MUTATIONS ---------- */
function updateBorrowRates(uint rate) internal {
state.updateBorrowRates(rate);
}
function updateShortRates(bytes32 currency, uint rate) internal {
state.updateShortRates(currency, rate);
}
function updateBorrowRatesCollateral(uint rate) external onlyCollateral {
state.updateBorrowRates(rate);
}
function updateShortRatesCollateral(bytes32 currency, uint rate) external onlyCollateral {
state.updateShortRates(currency, rate);
}
function incrementLongs(bytes32 synth, uint amount) external onlyCollateral {
state.incrementLongs(synth, amount);
}
function decrementLongs(bytes32 synth, uint amount) external onlyCollateral {
state.decrementLongs(synth, amount);
}
function incrementShorts(bytes32 synth, uint amount) external onlyCollateral {
state.incrementShorts(synth, amount);
}
function decrementShorts(bytes32 synth, uint amount) external onlyCollateral {
state.decrementShorts(synth, amount);
}
function accrueInterest(
uint interestIndex,
bytes32 currency,
bool isShort
) external onlyCollateral returns (uint difference, uint index) {
// 1. Get the rates we need.
(uint entryRate, uint lastRate, uint lastUpdated, uint newIndex) =
isShort ? getShortRatesAndTime(currency, interestIndex) : getRatesAndTime(interestIndex);
// 2. Get the instantaneous rate.
(uint rate, bool invalid) = isShort ? getShortRate(currency) : getBorrowRate();
require(!invalid, "Invalid rate");
// 3. Get the time since we last updated the rate.
// TODO: consider this in the context of l2 time.
uint timeDelta = block.timestamp.sub(lastUpdated).mul(1e18);
// 4. Get the latest cumulative rate. F_n+1 = F_n + F_last
uint latestCumulative = lastRate.add(rate.multiplyDecimal(timeDelta));
// 5. Return the rate differential and the new interest index.
difference = latestCumulative.sub(entryRate);
index = newIndex;
// 5. Update rates with the lastest cumulative rate. This also updates the time.
isShort ? updateShortRates(currency, latestCumulative) : updateBorrowRates(latestCumulative);
}
/* ========== MODIFIERS ========== */
modifier onlyCollateral {
bool isMultiCollateral = hasCollateral(msg.sender);
require(isMultiCollateral, "Only collateral contracts");
_;
}
// ========== EVENTS ==========
event MaxDebtUpdated(uint maxDebt);
event MaxSkewRateUpdated(uint maxSkewRate);
event LiquidationPenaltyUpdated(uint liquidationPenalty);
event BaseBorrowRateUpdated(uint baseBorrowRate);
event BaseShortRateUpdated(uint baseShortRate);
event UtilisationMultiplierUpdated(uint utilisationMultiplier);
event CollateralAdded(address collateral);
event CollateralRemoved(address collateral);
event SynthAdded(bytes32 synth);
event SynthRemoved(bytes32 synth);
event ShortableSynthAdded(bytes32 synth);
event ShortableSynthRemoved(bytes32 synth);
}