This repository has been archived by the owner on Oct 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
DssExecLib.sol
1120 lines (1031 loc) · 50.7 KB
/
DssExecLib.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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// DssExecLib.sol -- MakerDAO Executive Spellcrafting Library
//
// Copyright (C) 2020-2022 Dai Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.16;
import { CollateralOpts } from "./CollateralOpts.sol";
interface Initializable {
function init(bytes32) external;
}
interface Authorizable {
function rely(address) external;
function deny(address) external;
function setAuthority(address) external;
}
interface Fileable {
function file(bytes32, address) external;
function file(bytes32, uint256) external;
function file(bytes32, bytes32, uint256) external;
function file(bytes32, bytes32, address) external;
}
interface Drippable {
function drip() external returns (uint256);
function drip(bytes32) external returns (uint256);
}
interface Pricing {
function poke(bytes32) external;
}
interface ERC20 {
function decimals() external returns (uint8);
}
interface DssVat {
function hope(address) external;
function nope(address) external;
function ilks(bytes32) external returns (uint256 Art, uint256 rate, uint256 spot, uint256 line, uint256 dust);
function Line() external view returns (uint256);
function suck(address, address, uint256) external;
}
interface ClipLike {
function vat() external returns (address);
function dog() external returns (address);
function spotter() external view returns (address);
function calc() external view returns (address);
function ilk() external returns (bytes32);
}
interface DogLike {
function ilks(bytes32) external returns (address clip, uint256 chop, uint256 hole, uint256 dirt);
}
interface JoinLike {
function vat() external returns (address);
function ilk() external returns (bytes32);
function gem() external returns (address);
function dec() external returns (uint256);
function join(address, uint256) external;
function exit(address, uint256) external;
}
// Includes Median and OSM functions
interface OracleLike {
function src() external view returns (address);
function lift(address[] calldata) external;
function drop(address[] calldata) external;
function setBar(uint256) external;
function kiss(address) external;
function diss(address) external;
function kiss(address[] calldata) external;
function diss(address[] calldata) external;
function orb0() external view returns (address);
function orb1() external view returns (address);
}
interface MomLike {
function setOsm(bytes32, address) external;
function setPriceTolerance(address, uint256) external;
}
interface RegistryLike {
function add(address) external;
function xlip(bytes32) external view returns (address);
}
// https://github.com/makerdao/dss-chain-log
interface ChainlogLike {
function setVersion(string calldata) external;
function setIPFS(string calldata) external;
function setSha256sum(string calldata) external;
function getAddress(bytes32) external view returns (address);
function setAddress(bytes32, address) external;
function removeAddress(bytes32) external;
}
interface IAMLike {
function ilks(bytes32) external view returns (uint256,uint256,uint48,uint48,uint48);
function setIlk(bytes32,uint256,uint256,uint256) external;
function remIlk(bytes32) external;
function exec(bytes32) external returns (uint256);
}
interface LerpFactoryLike {
function newLerp(bytes32 name_, address target_, bytes32 what_, uint256 startTime_, uint256 start_, uint256 end_, uint256 duration_) external returns (address);
function newIlkLerp(bytes32 name_, address target_, bytes32 ilk_, bytes32 what_, uint256 startTime_, uint256 start_, uint256 end_, uint256 duration_) external returns (address);
}
interface LerpLike {
function tick() external returns (uint256);
}
interface RwaOracleLike {
function bump(bytes32 ilk, uint256 val) external;
}
library DssExecLib {
/*****************/
/*** Constants ***/
/*****************/
address constant public LOG = 0xdA0Ab1e0017DEbCd72Be8599041a2aa3bA7e740F;
uint256 constant internal WAD = 10 ** 18;
uint256 constant internal RAY = 10 ** 27;
uint256 constant internal RAD = 10 ** 45;
uint256 constant internal THOUSAND = 10 ** 3;
uint256 constant internal MILLION = 10 ** 6;
uint256 constant internal BPS_ONE_PCT = 100;
uint256 constant internal BPS_ONE_HUNDRED_PCT = 100 * BPS_ONE_PCT;
uint256 constant internal RATES_ONE_HUNDRED_PCT = 1000000021979553151239153027;
/**********************/
/*** Math Functions ***/
/**********************/
function wdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = (x * WAD + y / 2) / y;
}
function rdiv(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = (x * RAY + y / 2) / y;
}
/****************************/
/*** Core Address Helpers ***/
/****************************/
function dai() public view returns (address) { return getChangelogAddress("MCD_DAI"); }
function mkr() public view returns (address) { return getChangelogAddress("MCD_GOV"); }
function vat() public view returns (address) { return getChangelogAddress("MCD_VAT"); }
function cat() public view returns (address) { return getChangelogAddress("MCD_CAT"); }
function dog() public view returns (address) { return getChangelogAddress("MCD_DOG"); }
function jug() public view returns (address) { return getChangelogAddress("MCD_JUG"); }
function pot() public view returns (address) { return getChangelogAddress("MCD_POT"); }
function vow() public view returns (address) { return getChangelogAddress("MCD_VOW"); }
function end() public view returns (address) { return getChangelogAddress("MCD_END"); }
function esm() public view returns (address) { return getChangelogAddress("MCD_ESM"); }
function reg() public view returns (address) { return getChangelogAddress("ILK_REGISTRY"); }
function spotter() public view returns (address) { return getChangelogAddress("MCD_SPOT"); }
function flap() public view returns (address) { return getChangelogAddress("MCD_FLAP"); }
function flop() public view returns (address) { return getChangelogAddress("MCD_FLOP"); }
function osmMom() public view returns (address) { return getChangelogAddress("OSM_MOM"); }
function govGuard() public view returns (address) { return getChangelogAddress("GOV_GUARD"); }
function flipperMom() public view returns (address) { return getChangelogAddress("FLIPPER_MOM"); }
function clipperMom() public view returns (address) { return getChangelogAddress("CLIPPER_MOM"); }
function pauseProxy() public view returns (address) { return getChangelogAddress("MCD_PAUSE_PROXY"); }
function autoLine() public view returns (address) { return getChangelogAddress("MCD_IAM_AUTO_LINE"); }
function daiJoin() public view returns (address) { return getChangelogAddress("MCD_JOIN_DAI"); }
function lerpFab() public view returns (address) { return getChangelogAddress("LERP_FAB"); }
function clip(bytes32 _ilk) public view returns (address _clip) {
_clip = RegistryLike(reg()).xlip(_ilk);
}
function flip(bytes32 _ilk) public view returns (address _flip) {
_flip = RegistryLike(reg()).xlip(_ilk);
}
function calc(bytes32 _ilk) public view returns (address _calc) {
_calc = ClipLike(clip(_ilk)).calc();
}
function getChangelogAddress(bytes32 _key) public view returns (address) {
return ChainlogLike(LOG).getAddress(_key);
}
/****************************/
/*** Changelog Management ***/
/****************************/
/**
@dev Set an address in the MCD on-chain changelog.
@param _key Access key for the address (e.g. "MCD_VAT")
@param _val The address associated with the _key
*/
function setChangelogAddress(bytes32 _key, address _val) public {
ChainlogLike(LOG).setAddress(_key, _val);
}
/**
@dev Set version in the MCD on-chain changelog.
@param _version Changelog version (e.g. "1.1.2")
*/
function setChangelogVersion(string memory _version) public {
ChainlogLike(LOG).setVersion(_version);
}
/**
@dev Set IPFS hash of IPFS changelog in MCD on-chain changelog.
@param _ipfsHash IPFS hash (e.g. "QmefQMseb3AiTapiAKKexdKHig8wroKuZbmLtPLv4u2YwW")
*/
function setChangelogIPFS(string memory _ipfsHash) public {
ChainlogLike(LOG).setIPFS(_ipfsHash);
}
/**
@dev Set SHA256 hash in MCD on-chain changelog.
@param _SHA256Sum SHA256 hash (e.g. "e42dc9d043a57705f3f097099e6b2de4230bca9a020c797508da079f9079e35b")
*/
function setChangelogSHA256(string memory _SHA256Sum) public {
ChainlogLike(LOG).setSha256sum(_SHA256Sum);
}
/**********************/
/*** Authorizations ***/
/**********************/
/**
@dev Give an address authorization to perform auth actions on the contract.
@param _base The address of the contract where the authorization will be set
@param _ward Address to be authorized
*/
function authorize(address _base, address _ward) public {
Authorizable(_base).rely(_ward);
}
/**
@dev Revoke contract authorization from an address.
@param _base The address of the contract where the authorization will be revoked
@param _ward Address to be deauthorized
*/
function deauthorize(address _base, address _ward) public {
Authorizable(_base).deny(_ward);
}
/**
@dev Give an address authorization to perform auth actions on the contract.
@param _base The address of the contract with a `setAuthority` pattern
@param _authority Address to be authorized
*/
function setAuthority(address _base, address _authority) public {
Authorizable(_base).setAuthority(_authority);
}
/**
@dev Delegate vat authority to the specified address.
@param _usr Address to be authorized
*/
function delegateVat(address _usr) public {
DssVat(vat()).hope(_usr);
}
/**
@dev Revoke vat authority to the specified address.
@param _usr Address to be deauthorized
*/
function undelegateVat(address _usr) public {
DssVat(vat()).nope(_usr);
}
/******************************/
/*** OfficeHours Management ***/
/******************************/
/**
@dev Returns true if a time is within office hours range
@param _ts The timestamp to check, usually block.timestamp
@param _officeHours true if office hours is enabled.
@return true if time is in castable range
*/
function canCast(uint40 _ts, bool _officeHours) public pure returns (bool) {
if (_officeHours) {
uint256 day = (_ts / 1 days + 3) % 7;
if (day >= 5) { return false; } // Can only be cast on a weekday
uint256 hour = _ts / 1 hours % 24;
if (hour < 14 || hour >= 21) { return false; } // Outside office hours
}
return true;
}
/**
@dev Calculate the next available cast time in epoch seconds
@param _eta The scheduled time of the spell plus the pause delay
@param _ts The current timestamp, usually block.timestamp
@param _officeHours true if office hours is enabled.
@return castTime The next available cast timestamp
*/
function nextCastTime(uint40 _eta, uint40 _ts, bool _officeHours) public pure returns (uint256 castTime) {
require(_eta != 0); // "DssExecLib/invalid eta"
require(_ts != 0); // "DssExecLib/invalid ts"
castTime = _ts > _eta ? _ts : _eta; // Any day at XX:YY
if (_officeHours) {
uint256 day = (castTime / 1 days + 3) % 7;
uint256 hour = castTime / 1 hours % 24;
uint256 minute = castTime / 1 minutes % 60;
uint256 second = castTime % 60;
if (day >= 5) {
castTime += (6 - day) * 1 days; // Go to Sunday XX:YY
castTime += (24 - hour + 14) * 1 hours; // Go to 14:YY UTC Monday
castTime -= minute * 1 minutes + second; // Go to 14:00 UTC
} else {
if (hour >= 21) {
if (day == 4) castTime += 2 days; // If Friday, fast forward to Sunday XX:YY
castTime += (24 - hour + 14) * 1 hours; // Go to 14:YY UTC next day
castTime -= minute * 1 minutes + second; // Go to 14:00 UTC
} else if (hour < 14) {
castTime += (14 - hour) * 1 hours; // Go to 14:YY UTC same day
castTime -= minute * 1 minutes + second; // Go to 14:00 UTC
}
}
}
}
/**************************/
/*** Accumulating Rates ***/
/**************************/
/**
@dev Update rate accumulation for the Dai Savings Rate (DSR).
*/
function accumulateDSR() public {
Drippable(pot()).drip();
}
/**
@dev Update rate accumulation for the stability fees of a given collateral type.
@param _ilk Collateral type
*/
function accumulateCollateralStabilityFees(bytes32 _ilk) public {
Drippable(jug()).drip(_ilk);
}
/*********************/
/*** Price Updates ***/
/*********************/
/**
@dev Update price of a given collateral type.
@param _ilk Collateral type
*/
function updateCollateralPrice(bytes32 _ilk) public {
Pricing(spotter()).poke(_ilk);
}
/****************************/
/*** System Configuration ***/
/****************************/
/**
@dev Set a contract in another contract, defining the relationship (ex. set a new Calc contract in Clip)
@param _base The address of the contract where the new contract address will be filed
@param _what Name of contract to file
@param _addr Address of contract to file
*/
function setContract(address _base, bytes32 _what, address _addr) public {
Fileable(_base).file(_what, _addr);
}
/**
@dev Set a contract in another contract, defining the relationship (ex. set a new Calc contract in a Clip)
@param _base The address of the contract where the new contract address will be filed
@param _ilk Collateral type
@param _what Name of contract to file
@param _addr Address of contract to file
*/
function setContract(address _base, bytes32 _ilk, bytes32 _what, address _addr) public {
Fileable(_base).file(_ilk, _what, _addr);
}
/**
@dev Set a value in a contract, via a governance authorized File pattern.
@param _base The address of the contract where the new contract address will be filed
@param _what Name of tag for the value (e.x. "Line")
@param _amt The value to set or update
*/
function setValue(address _base, bytes32 _what, uint256 _amt) public {
Fileable(_base).file(_what, _amt);
}
/**
@dev Set an ilk-specific value in a contract, via a governance authorized File pattern.
@param _base The address of the contract where the new value will be filed
@param _ilk Collateral type
@param _what Name of tag for the value (e.x. "Line")
@param _amt The value to set or update
*/
function setValue(address _base, bytes32 _ilk, bytes32 _what, uint256 _amt) public {
Fileable(_base).file(_ilk, _what, _amt);
}
/******************************/
/*** System Risk Parameters ***/
/******************************/
// function setGlobalDebtCeiling(uint256 _amount) public { setGlobalDebtCeiling(vat(), _amount); }
/**
@dev Set the global debt ceiling. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setGlobalDebtCeiling(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-global-Line-precision"
setValue(vat(), "Line", _amount * RAD);
}
/**
@dev Increase the global debt ceiling by a specific amount. Amount will be converted to the correct internal precision.
@param _amount The amount to add in DAI (ex. 10m DAI amount == 10000000)
*/
function increaseGlobalDebtCeiling(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-Line-increase-precision"
address _vat = vat();
setValue(_vat, "Line", DssVat(_vat).Line() + _amount * RAD);
}
/**
@dev Decrease the global debt ceiling by a specific amount. Amount will be converted to the correct internal precision.
@param _amount The amount to reduce in DAI (ex. 10m DAI amount == 10000000)
*/
function decreaseGlobalDebtCeiling(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-Line-decrease-precision"
address _vat = vat();
setValue(_vat, "Line", DssVat(_vat).Line() - _amount * RAD);
}
/**
@dev Set the Dai Savings Rate. See: docs/rates.txt
@param _rate The accumulated rate (ex. 4% => 1000000001243680656318820312)
@param _doDrip `true` to accumulate interest owed
*/
function setDSR(uint256 _rate, bool _doDrip) public {
require((_rate >= RAY) && (_rate <= RATES_ONE_HUNDRED_PCT)); // "LibDssExec/dsr-out-of-bounds"
if (_doDrip) Drippable(pot()).drip();
setValue(pot(), "dsr", _rate);
}
/**
@dev Set the DAI amount for system surplus auctions. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setSurplusAuctionAmount(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-vow-bump-precision"
setValue(vow(), "bump", _amount * RAD);
}
/**
@dev Set the DAI amount for system surplus buffer, must be exceeded before surplus auctions start. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setSurplusBuffer(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-vow-hump-precision"
setValue(vow(), "hump", _amount * RAD);
}
/**
@dev Set minimum bid increase for surplus auctions. Amount will be converted to the correct internal precision.
@dev Equation used for conversion is (1 + pct / 10,000) * WAD
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 5% = 5 * 100 = 500)
*/
function setMinSurplusAuctionBidIncrease(uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-flap-beg-precision"
setValue(flap(), "beg", WAD + wdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set bid duration for surplus auctions.
@param _duration Amount of time for bids. (in seconds)
*/
function setSurplusAuctionBidDuration(uint256 _duration) public {
setValue(flap(), "ttl", _duration);
}
/**
@dev Set total auction duration for surplus auctions.
@param _duration Amount of time for auctions. (in seconds)
*/
function setSurplusAuctionDuration(uint256 _duration) public {
setValue(flap(), "tau", _duration);
}
/**
@dev Set the number of seconds that pass before system debt is auctioned for MKR tokens.
@param _duration Duration in seconds
*/
function setDebtAuctionDelay(uint256 _duration) public {
setValue(vow(), "wait", _duration);
}
/**
@dev Set the DAI amount for system debt to be covered by each debt auction. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setDebtAuctionDAIAmount(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-vow-sump-precision"
setValue(vow(), "sump", _amount * RAD);
}
/**
@dev Set the starting MKR amount to be auctioned off to cover system debt in debt auctions. Amount will be converted to the correct internal precision.
@param _amount The amount to set in MKR (ex. 250 MKR amount == 250)
*/
function setDebtAuctionMKRAmount(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-vow-dump-precision"
setValue(vow(), "dump", _amount * WAD);
}
/**
@dev Set minimum bid increase for debt auctions. Amount will be converted to the correct internal precision.
@dev Equation used for conversion is (1 + pct / 10,000) * WAD
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 5% = 5 * 100 = 500)
*/
function setMinDebtAuctionBidIncrease(uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-flop-beg-precision"
setValue(flop(), "beg", WAD + wdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set bid duration for debt auctions.
@param _duration Amount of time for bids. (seconds)
*/
function setDebtAuctionBidDuration(uint256 _duration) public {
require(_duration < type(uint48).max); // "LibDssExec/incorrect-flop-ttl-precision"
setValue(flop(), "ttl", _duration);
}
/**
@dev Set total auction duration for debt auctions.
@param _duration Amount of time for auctions. (seconds)
*/
function setDebtAuctionDuration(uint256 _duration) public {
require(_duration < type(uint48).max); // "LibDssExec/incorrect-flop-tau-precision"
setValue(flop(), "tau", _duration);
}
/**
@dev Set the rate of increasing amount of MKR out for auction during debt auctions. Amount will be converted to the correct internal precision.
@dev MKR amount is increased by this rate every "tick" (if auction duration has passed and no one has bid on the MKR)
@dev Equation used for conversion is (1 + pct / 10,000) * WAD
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 5% = 5 * 100 = 500)
*/
function setDebtAuctionMKRIncreaseRate(uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-flop-pad-precision"
setValue(flop(), "pad", WAD + wdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set the maximum total DAI amount that can be out for liquidation in the system at any point. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 250,000 DAI amount == 250000)
*/
function setMaxTotalDAILiquidationAmount(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-dog-Hole-precision"
setValue(dog(), "Hole", _amount * RAD);
}
/**
@dev (LIQ 1.2) Set the maximum total DAI amount that can be out for liquidation in the system at any point. Amount will be converted to the correct internal precision.
@param _amount The amount to set in DAI (ex. 250,000 DAI amount == 250000)
*/
function setMaxTotalDAILiquidationAmountLEGACY(uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-cat-box-amount"
setValue(cat(), "box", _amount * RAD);
}
/**
@dev Set the duration of time that has to pass during emergency shutdown before collateral can start being claimed by DAI holders.
@param _duration Time in seconds to set for ES processing time
*/
function setEmergencyShutdownProcessingTime(uint256 _duration) public {
setValue(end(), "wait", _duration);
}
/**
@dev Set the global stability fee (is not typically used, currently is 0).
Many of the settings that change weekly rely on the rate accumulator
described at https://docs.makerdao.com/smart-contract-modules/rates-module
To check this yourself, use the following rate calculation (example 8%):
$ bc -l <<< 'scale=27; e( l(1.08)/(60 * 60 * 24 * 365) )'
A table of rates can also be found at:
https://ipfs.io/ipfs/QmefQMseb3AiTapiAKKexdKHig8wroKuZbmLtPLv4u2YwW
@param _rate The accumulated rate (ex. 4% => 1000000001243680656318820312)
*/
function setGlobalStabilityFee(uint256 _rate) public {
require((_rate >= RAY) && (_rate <= RATES_ONE_HUNDRED_PCT)); // "LibDssExec/global-stability-fee-out-of-bounds"
setValue(jug(), "base", _rate);
}
/**
@dev Set the value of DAI in the reference asset (e.g. $1 per DAI). Value will be converted to the correct internal precision.
@dev Equation used for conversion is value * RAY / 1000
@param _value The value to set as integer (x1000) (ex. $1.025 == 1025)
*/
function setDAIReferenceValue(uint256 _value) public {
require(_value < WAD); // "LibDssExec/incorrect-par-precision"
setValue(spotter(), "par", rdiv(_value, 1000));
}
/*****************************/
/*** Collateral Management ***/
/*****************************/
/**
@dev Set a collateral debt ceiling. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setIlkDebtCeiling(bytes32 _ilk, uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-ilk-line-precision"
setValue(vat(), _ilk, "line", _amount * RAD);
}
/**
@dev Increase a collateral debt ceiling. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to increase in DAI (ex. 10m DAI amount == 10000000)
@param _global If true, increases the global debt ceiling by _amount
*/
function increaseIlkDebtCeiling(bytes32 _ilk, uint256 _amount, bool _global) public {
require(_amount < WAD); // "LibDssExec/incorrect-ilk-line-precision"
address _vat = vat();
(,,,uint256 line_,) = DssVat(_vat).ilks(_ilk);
setValue(_vat, _ilk, "line", line_ + _amount * RAD);
if (_global) { increaseGlobalDebtCeiling(_amount); }
}
/**
@dev Decrease a collateral debt ceiling. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to decrease in DAI (ex. 10m DAI amount == 10000000)
@param _global If true, decreases the global debt ceiling by _amount
*/
function decreaseIlkDebtCeiling(bytes32 _ilk, uint256 _amount, bool _global) public {
require(_amount < WAD); // "LibDssExec/incorrect-ilk-line-precision"
address _vat = vat();
(,,,uint256 line_,) = DssVat(_vat).ilks(_ilk);
setValue(_vat, _ilk, "line", line_ - _amount * RAD);
if (_global) { decreaseGlobalDebtCeiling(_amount); }
}
/**
@dev Set a RWA collateral debt ceiling by specifying its new oracle price.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _ceiling The new debt ceiling in natural units (e.g. set 10m DAI as 10_000_000)
@param _price The new oracle price in natural units
@dev note: _price should enable DAI to be drawn over the loan period while taking into
account the configured ink amount, interest rate and liquidation ratio
@dev note: _price * WAD should be greater than or equal to the current oracle price
*/
function setRWAIlkDebtCeiling(bytes32 _ilk, uint256 _ceiling, uint256 _price) public {
require(_price < WAD);
setIlkDebtCeiling(_ilk, _ceiling);
RwaOracleLike(getChangelogAddress("MIP21_LIQUIDATION_ORACLE")).bump(_ilk, _price * WAD);
updateCollateralPrice(_ilk);
}
/**
@dev Set the parameters for an ilk in the "MCD_IAM_AUTO_LINE" auto-line
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The Maximum value (ex. 100m DAI amount == 100000000)
@param _gap The amount of Dai per step (ex. 5m Dai == 5000000)
@param _ttl The amount of time (in seconds)
*/
function setIlkAutoLineParameters(bytes32 _ilk, uint256 _amount, uint256 _gap, uint256 _ttl) public {
require(_amount < WAD); // "LibDssExec/incorrect-auto-line-amount-precision"
require(_gap < WAD); // "LibDssExec/incorrect-auto-line-gap-precision"
IAMLike(autoLine()).setIlk(_ilk, _amount * RAD, _gap * RAD, _ttl);
}
/**
@dev Set the debt ceiling for an ilk in the "MCD_IAM_AUTO_LINE" auto-line without updating the time values
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The Maximum value (ex. 100m DAI amount == 100000000)
*/
function setIlkAutoLineDebtCeiling(bytes32 _ilk, uint256 _amount) public {
address _autoLine = autoLine();
(, uint256 gap, uint48 ttl,,) = IAMLike(_autoLine).ilks(_ilk);
require(gap != 0 && ttl != 0); // "LibDssExec/auto-line-not-configured"
IAMLike(_autoLine).setIlk(_ilk, _amount * RAD, uint256(gap), uint256(ttl));
}
/**
@dev Remove an ilk in the "MCD_IAM_AUTO_LINE" auto-line
@param _ilk The ilk to remove (ex. bytes32("ETH-A"))
*/
function removeIlkFromAutoLine(bytes32 _ilk) public {
IAMLike(autoLine()).remIlk(_ilk);
}
/**
@dev Set a collateral minimum vault amount. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setIlkMinVaultAmount(bytes32 _ilk, uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-ilk-dust-precision"
(,, uint256 _hole,) = DogLike(dog()).ilks(_ilk);
require(_amount <= _hole / RAD); // Ensure ilk.hole >= dust
setValue(vat(), _ilk, "dust", _amount * RAD);
(bool ok,) = clip(_ilk).call(abi.encodeWithSignature("upchost()")); ok;
}
/**
@dev Set a collateral liquidation penalty. Amount will be converted to the correct internal precision.
@dev Equation used for conversion is (1 + pct / 10,000) * WAD
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 10.25% = 10.25 * 100 = 1025)
*/
function setIlkLiquidationPenalty(bytes32 _ilk, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-ilk-chop-precision"
setValue(dog(), _ilk, "chop", WAD + wdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
(bool ok,) = clip(_ilk).call(abi.encodeWithSignature("upchost()")); ok;
}
/**
@dev Set max DAI amount for liquidation per vault for collateral. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to set in DAI (ex. 10m DAI amount == 10000000)
*/
function setIlkMaxLiquidationAmount(bytes32 _ilk, uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-ilk-hole-precision"
setValue(dog(), _ilk, "hole", _amount * RAD);
}
/**
@dev Set a collateral liquidation ratio. Amount will be converted to the correct internal precision.
@dev Equation used for conversion is pct * RAY / 10,000
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 150% = 150 * 100 = 15000)
*/
function setIlkLiquidationRatio(bytes32 _ilk, uint256 _pct_bps) public {
require(_pct_bps < 10 * BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-ilk-mat-precision" // Fails if pct >= 1000%
require(_pct_bps >= BPS_ONE_HUNDRED_PCT); // the liquidation ratio has to be bigger or equal to 100%
setValue(spotter(), _ilk, "mat", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set an auction starting multiplier. Amount will be converted to the correct internal precision.
@dev Equation used for conversion is pct * RAY / 10,000
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 1.3x starting multiplier = 130% = 13000)
*/
function setStartingPriceMultiplicativeFactor(bytes32 _ilk, uint256 _pct_bps) public {
require(_pct_bps < 10 * BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-ilk-mat-precision" // Fails if gt 10x
require(_pct_bps >= BPS_ONE_HUNDRED_PCT); // fail if start price is less than OSM price
setValue(clip(_ilk), "buf", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set the amout of time before an auction resets.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _duration Amount of time before auction resets (in seconds).
*/
function setAuctionTimeBeforeReset(bytes32 _ilk, uint256 _duration) public {
setValue(clip(_ilk), "tail", _duration);
}
/**
@dev Percentage drop permitted before auction reset
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _pct_bps The pct, in basis points, of drop to permit (x100).
*/
function setAuctionPermittedDrop(bytes32 _ilk, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-clip-cusp-value"
setValue(clip(_ilk), "cusp", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Percentage of tab to suck from vow to incentivize keepers. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _pct_bps The pct, in basis points, of the tab to suck. (0.01% == 1)
*/
function setKeeperIncentivePercent(bytes32 _ilk, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-clip-chip-precision"
setValue(clip(_ilk), "chip", wdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set max DAI amount for flat rate keeper incentive. Amount will be converted to the correct internal precision.
@param _ilk The ilk to update (ex. bytes32("ETH-A"))
@param _amount The amount to set in DAI (ex. 1000 DAI amount == 1000)
*/
function setKeeperIncentiveFlatRate(bytes32 _ilk, uint256 _amount) public {
require(_amount < WAD); // "LibDssExec/incorrect-clip-tip-precision"
setValue(clip(_ilk), "tip", _amount * RAD);
}
/**
@dev Sets the circuit breaker price tolerance in the clipper mom.
This is somewhat counter-intuitive,
to accept a 25% price drop, use a value of 75%
@param _clip The clipper to set the tolerance for
@param _pct_bps The pct, in basis points, to set in integer form (x100). (ex. 5% = 5 * 100 = 500)
*/
function setLiquidationBreakerPriceTolerance(address _clip, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // "LibDssExec/incorrect-clippermom-price-tolerance"
MomLike(clipperMom()).setPriceTolerance(_clip, rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/**
@dev Set the stability fee for a given ilk.
Many of the settings that change weekly rely on the rate accumulator
described at https://docs.makerdao.com/smart-contract-modules/rates-module
To check this yourself, use the following rate calculation (example 8%):
$ bc -l <<< 'scale=27; e( l(1.08)/(60 * 60 * 24 * 365) )'
A table of rates can also be found at:
https://ipfs.io/ipfs/QmefQMseb3AiTapiAKKexdKHig8wroKuZbmLtPLv4u2YwW
@param _ilk The ilk to update (ex. bytes32("ETH-A") )
@param _rate The accumulated rate (ex. 4% => 1000000001243680656318820312)
@param _doDrip `true` to accumulate stability fees for the collateral
*/
function setIlkStabilityFee(bytes32 _ilk, uint256 _rate, bool _doDrip) public {
require((_rate >= RAY) && (_rate <= RATES_ONE_HUNDRED_PCT)); // "LibDssExec/ilk-stability-fee-out-of-bounds"
address _jug = jug();
if (_doDrip) Drippable(_jug).drip(_ilk);
setValue(_jug, _ilk, "duty", _rate);
}
/*************************/
/*** Abacus Management ***/
/*************************/
/**
@dev Set the number of seconds from the start when the auction reaches zero price.
@dev Abacus:LinearDecrease only.
@param _calc The address of the LinearDecrease pricing contract
@param _duration Amount of time for auctions.
*/
function setLinearDecrease(address _calc, uint256 _duration) public {
setValue(_calc, "tau", _duration);
}
/**
@dev Set the number of seconds for each price step.
@dev Abacus:StairstepExponentialDecrease only.
@param _calc The address of the StairstepExponentialDecrease pricing contract
@param _duration Length of time between price drops [seconds]
@param _pct_bps Per-step multiplicative factor in basis points. (ex. 99% == 9900)
*/
function setStairstepExponentialDecrease(address _calc, uint256 _duration, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // DssExecLib/cut-too-high
setValue(_calc, "cut", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
setValue(_calc, "step", _duration);
}
/**
@dev Set the number of seconds for each price step. (99% cut = 1% price drop per step)
Amounts will be converted to the correct internal precision.
@dev Abacus:ExponentialDecrease only
@param _calc The address of the ExponentialDecrease pricing contract
@param _pct_bps Per-step multiplicative factor in basis points. (ex. 99% == 9900)
*/
function setExponentialDecrease(address _calc, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // DssExecLib/cut-too-high
setValue(_calc, "cut", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/*************************/
/*** Oracle Management ***/
/*************************/
/**
@dev Allows an oracle to read prices from its source feeds
@param _oracle An OSM or LP oracle contract
*/
function whitelistOracleMedians(address _oracle) public {
(bool ok, bytes memory data) = _oracle.call(abi.encodeWithSignature("orb0()"));
if (ok) {
// Token is an LP oracle
address median0 = abi.decode(data, (address));
addReaderToWhitelistCall(median0, _oracle);
addReaderToWhitelistCall(OracleLike(_oracle).orb1(), _oracle);
} else {
// Standard OSM
addReaderToWhitelistCall(OracleLike(_oracle).src(), _oracle);
}
}
/**
@dev Adds an address to the OSM or Median's reader whitelist, allowing the address to read prices.
@param _oracle Oracle Security Module (OSM) or Median core contract address
@param _reader Address to add to whitelist
*/
function addReaderToWhitelist(address _oracle, address _reader) public {
OracleLike(_oracle).kiss(_reader);
}
/**
@dev Removes an address to the OSM or Median's reader whitelist, disallowing the address to read prices.
@param _oracle Oracle Security Module (OSM) or Median core contract address
@param _reader Address to remove from whitelist
*/
function removeReaderFromWhitelist(address _oracle, address _reader) public {
OracleLike(_oracle).diss(_reader);
}
/**
@dev Adds an address to the OSM or Median's reader whitelist, allowing the address to read prices.
@param _oracle OSM or Median core contract address
@param _reader Address to add to whitelist
*/
function addReaderToWhitelistCall(address _oracle, address _reader) public {
(bool ok,) = _oracle.call(abi.encodeWithSignature("kiss(address)", _reader)); ok;
}
/**
@dev Removes an address to the OSM or Median's reader whitelist, disallowing the address to read prices.
@param _oracle Oracle Security Module (OSM) or Median core contract address
@param _reader Address to remove from whitelist
*/
function removeReaderFromWhitelistCall(address _oracle, address _reader) public {
(bool ok,) = _oracle.call(abi.encodeWithSignature("diss(address)", _reader)); ok;
}
/**
@dev Sets the minimum number of valid messages from whitelisted oracle feeds needed to update median price.
@param _median Median core contract address
@param _minQuorum Minimum number of valid messages from whitelisted oracle feeds needed to update median price (NOTE: MUST BE ODD NUMBER)
*/
function setMedianWritersQuorum(address _median, uint256 _minQuorum) public {
OracleLike(_median).setBar(_minQuorum);
}
/**
@dev Add OSM address to OSM mom, allowing it to be frozen by governance.
@param _osm Oracle Security Module (OSM) core contract address
@param _ilk Collateral type using OSM
*/
function allowOSMFreeze(address _osm, bytes32 _ilk) public {
MomLike(osmMom()).setOsm(_ilk, _osm);
}
/*****************************/
/*** Direct Deposit Module ***/
/*****************************/
/**
@dev Sets the target rate threshold for a dai direct deposit module (d3m)
@dev Aave: Targets the variable borrow rate
@param _d3m The address of the D3M contract
@param _pct_bps Target rate in basis points. (ex. 4% == 400)
*/
function setD3MTargetInterestRate(address _d3m, uint256 _pct_bps) public {
require(_pct_bps < BPS_ONE_HUNDRED_PCT); // DssExecLib/bar-too-high
setValue(_d3m, "bar", rdiv(_pct_bps, BPS_ONE_HUNDRED_PCT));
}
/*****************************/
/*** Collateral Onboarding ***/
/*****************************/
/**
@dev Performs basic functions and sanity checks to add a new collateral type to the MCD system
@param _ilk Collateral type key code [Ex. "ETH-A"]
@param _gem Address of token contract
@param _join Address of join adapter
@param _clip Address of liquidation agent
@param _calc Address of the pricing function
@param _pip Address of price feed
*/
function addCollateralBase(
bytes32 _ilk,
address _gem,
address _join,
address _clip,
address _calc,
address _pip
) public {
// Sanity checks
address _vat = vat();
address _dog = dog();
address _spotter = spotter();
require(JoinLike(_join).vat() == _vat); // "join-vat-not-match"
require(JoinLike(_join).ilk() == _ilk); // "join-ilk-not-match"
require(JoinLike(_join).gem() == _gem); // "join-gem-not-match"
require(JoinLike(_join).dec() ==
ERC20(_gem).decimals()); // "join-dec-not-match"
require(ClipLike(_clip).vat() == _vat); // "clip-vat-not-match"
require(ClipLike(_clip).dog() == _dog); // "clip-dog-not-match"
require(ClipLike(_clip).ilk() == _ilk); // "clip-ilk-not-match"
require(ClipLike(_clip).spotter() == _spotter); // "clip-ilk-not-match"
// Set the token PIP in the Spotter
setContract(spotter(), _ilk, "pip", _pip);
// Set the ilk Clipper in the Dog
setContract(_dog, _ilk, "clip", _clip);
// Set vow in the clip
setContract(_clip, "vow", vow());
// Set the pricing function for the Clipper
setContract(_clip, "calc", _calc);
// Init ilk in Vat & Jug
Initializable(_vat).init(_ilk); // Vat
Initializable(jug()).init(_ilk); // Jug
// Allow ilk Join to modify Vat registry
authorize(_vat, _join);
// Allow ilk Join to suck dai for keepers
authorize(_vat, _clip);
// Allow the ilk Clipper to reduce the Dog hole on deal()
authorize(_dog, _clip);
// Allow Dog to kick auctions in ilk Clipper
authorize(_clip, _dog);
// Allow End to yank auctions in ilk Clipper
authorize(_clip, end());
// Authorize the ESM to execute in the clipper
authorize(_clip, esm());
// Add new ilk to the IlkRegistry
RegistryLike(reg()).add(_join);
}
// Complete collateral onboarding logic.
function addNewCollateral(CollateralOpts memory co) public {
// Add the collateral to the system.
addCollateralBase(co.ilk, co.gem, co.join, co.clip, co.calc, co.pip);
address clipperMom_ = clipperMom();