-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathDssSpell.t.base.sol
1992 lines (1713 loc) · 82.5 KB
/
DssSpell.t.base.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-FileCopyrightText: © 2020 Dai Foundation <www.daifoundation.org>
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 "dss-interfaces/Interfaces.sol";
import {DssTest, GodMode} from "dss-test/DssTest.sol";
import "./test/rates.sol";
import "./test/addresses_mainnet.sol";
import "./test/addresses_deployers.sol";
import "./test/addresses_wallets.sol";
import "./test/config.sol";
import {DssSpell} from "./DssSpell.sol";
struct TeleportGUID {
bytes32 sourceDomain;
bytes32 targetDomain;
bytes32 receiver;
bytes32 operator;
uint128 amount;
uint80 nonce;
uint48 timestamp;
}
interface DirectDepositLike is GemJoinAbstract {
function file(bytes32, uint256) external;
function exec() external;
function tau() external view returns (uint256);
function tic() external view returns (uint256);
function bar() external view returns (uint256);
function king() external view returns (address);
}
interface AaveDirectDepositLike is DirectDepositLike {
function adai() external view returns (address);
}
interface CropperLike {
function getOrCreateProxy(address usr) external returns (address urp);
function join(address crop, address usr, uint256 val) external;
function exit(address crop, address usr, uint256 val) external;
function frob(bytes32 ilk, address u, address v, address w, int256 dink, int256 dart) external;
}
interface CropJoinLike {
function wards(address) external view returns (uint256);
function gem() external view returns (address);
function bonus() external view returns (address);
}
interface CurveLPOsmLike is LPOsmAbstract {
function orbs(uint256) external view returns (address);
}
interface TeleportJoinLike {
function wards(address) external view returns (uint256);
function fees(bytes32) external view returns (address);
function line(bytes32) external view returns (uint256);
function debt(bytes32) external view returns (int256);
function vow() external view returns (address);
function vat() external view returns (address);
function daiJoin() external view returns (address);
function ilk() external view returns (bytes32);
function domain() external view returns (bytes32);
}
interface TeleportFeeLike {
function fee() external view returns (uint256);
function ttl() external view returns (uint256);
}
interface TeleportOracleAuthLike {
function wards(address) external view returns (uint256);
function signers(address) external view returns (uint256);
function teleportJoin() external view returns (address);
function threshold() external view returns (uint256);
function addSigners(address[] calldata) external;
function getSignHash(TeleportGUID calldata) external pure returns (bytes32);
function requestMint(
TeleportGUID calldata,
bytes calldata,
uint256,
uint256
) external returns (uint256, uint256);
}
interface TeleportRouterLike {
function wards(address) external view returns (uint256);
function file(bytes32, bytes32, address) external;
function gateways(bytes32) external view returns (address);
function domains(address) external view returns (bytes32);
function numDomains() external view returns (uint256);
function dai() external view returns (address);
function requestMint(
TeleportGUID calldata,
uint256,
uint256
) external returns (uint256, uint256);
function settle(bytes32, uint256) external;
}
interface TeleportBridgeLike {
function l1Escrow() external view returns (address);
function l1TeleportRouter() external view returns (address);
function l1Token() external view returns (address);
}
interface OptimismTeleportBridgeLike is TeleportBridgeLike {
function l2TeleportGateway() external view returns (address);
function messenger() external view returns (address);
}
interface ArbitrumTeleportBridgeLike is TeleportBridgeLike {
function l2TeleportGateway() external view returns (address);
function inbox() external view returns (address);
}
interface StarknetTeleportBridgeLike {
function l2TeleportGateway() external view returns (uint256);
function starkNet() external view returns (address);
}
interface RwaLiquidationLike {
function ilks(bytes32) external view returns (string memory, address, uint48, uint48);
}
interface AuthorityLike {
function authority() external view returns (address);
}
// TODO: add full interfaces to dss-interfaces and remove from here
interface FlapUniV2Abstract {
function gem() external view returns (address);
function hop() external view returns (uint256);
function pair() external view returns (address);
function pip() external view returns (address);
function want() external view returns (uint256);
}
interface FlapperMomAbstract {
function stop() external;
}
contract DssSpellTestBase is Config, DssTest {
Rates rates = new Rates();
Addresses addr = new Addresses();
Deployers deployers = new Deployers();
Wallets wallets = new Wallets();
// ADDRESSES
ChainlogAbstract chainLog = ChainlogAbstract( addr.addr("CHANGELOG"));
DSPauseAbstract pause = DSPauseAbstract( addr.addr("MCD_PAUSE"));
address pauseProxy = addr.addr("MCD_PAUSE_PROXY");
DSChiefAbstract chief = DSChiefAbstract( addr.addr("MCD_ADM"));
VatAbstract vat = VatAbstract( addr.addr("MCD_VAT"));
VowAbstract vow = VowAbstract( addr.addr("MCD_VOW"));
DogAbstract dog = DogAbstract( addr.addr("MCD_DOG"));
PotAbstract pot = PotAbstract( addr.addr("MCD_POT"));
JugAbstract jug = JugAbstract( addr.addr("MCD_JUG"));
SpotAbstract spotter = SpotAbstract( addr.addr("MCD_SPOT"));
DaiAbstract dai = DaiAbstract( addr.addr("MCD_DAI"));
DaiJoinAbstract daiJoin = DaiJoinAbstract( addr.addr("MCD_JOIN_DAI"));
DSTokenAbstract gov = DSTokenAbstract( addr.addr("MCD_GOV"));
EndAbstract end = EndAbstract( addr.addr("MCD_END"));
ESMAbstract esm = ESMAbstract( addr.addr("MCD_ESM"));
CureAbstract cure = CureAbstract( addr.addr("MCD_CURE"));
IlkRegistryAbstract reg = IlkRegistryAbstract(addr.addr("ILK_REGISTRY"));
FlapUniV2Abstract flap = FlapUniV2Abstract( addr.addr("MCD_FLAP"));
CropperLike cropper = CropperLike( addr.addr("MCD_CROPPER"));
OsmMomAbstract osmMom = OsmMomAbstract( addr.addr("OSM_MOM"));
ClipperMomAbstract clipMom = ClipperMomAbstract( addr.addr("CLIPPER_MOM"));
FlapperMomAbstract flapMom = FlapperMomAbstract( addr.addr("FLAPPER_MOM"));
AuthorityLike d3mMom = AuthorityLike( addr.addr("DIRECT_MOM"));
DssAutoLineAbstract autoLine = DssAutoLineAbstract(addr.addr("MCD_IAM_AUTO_LINE"));
LerpFactoryAbstract lerpFactory = LerpFactoryAbstract(addr.addr("LERP_FAB"));
VestAbstract vestDai = VestAbstract( addr.addr("MCD_VEST_DAI"));
VestAbstract vestMkr = VestAbstract( addr.addr("MCD_VEST_MKR_TREASURY"));
RwaLiquidationLike liquidationOracle = RwaLiquidationLike( addr.addr("MIP21_LIQUIDATION_ORACLE"));
DssSpell spell;
event Debug(uint256 index, uint256 val);
event Debug(uint256 index, address addr);
event Debug(uint256 index, bytes32 what);
function _rmul(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = (x * y + RAY / 2) / RAY;
}
// not provided in DSMath
function _rpow(uint256 x, uint256 n, uint256 b) internal pure returns (uint256 z) {
assembly {
switch x case 0 {switch n case 0 {z := b} default {z := 0}}
default {
switch mod(n, 2) case 0 { z := b } default { z := x }
let half := div(b, 2) // for rounding.
for { n := div(n, 2) } n { n := div(n,2) } {
let xx := mul(x, x)
if iszero(eq(div(xx, x), x)) { revert(0,0) }
let xxRound := add(xx, half)
if lt(xxRound, xx) { revert(0,0) }
x := div(xxRound, b)
if mod(n,2) {
let zx := mul(z, x)
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) }
let zxRound := add(zx, half)
if lt(zxRound, zx) { revert(0,0) }
z := div(zxRound, b)
}
}
}
}
}
function _divup(uint256 x, uint256 y) internal pure returns (uint256 z) {
unchecked {
z = x != 0 ? ((x - 1) / y) + 1 : 0;
}
}
// not provided in DSTest
function _assertEqApprox(uint256 _a, uint256 _b, uint256 _tolerance) internal {
uint256 a = _a;
uint256 b = _b;
if (a < b) {
uint256 tmp = a;
a = b;
b = tmp;
}
if (a - b > _tolerance) {
emit log_bytes32("Error: Wrong `uint' value");
emit log_named_uint(" Expected", _b);
emit log_named_uint(" Actual", _a);
fail();
}
}
function _cmpStr(string memory a, string memory b) internal pure returns (bool) {
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))));
}
function _concat(string memory a, string memory b) internal pure returns (string memory) {
return string.concat(a, b);
}
function _concat(string memory a, bytes32 b) internal pure returns (string memory) {
return string.concat(a, _bytes32ToStr(b));
}
function _bytes32ToStr(bytes32 _bytes32) internal pure returns (string memory) {
bytes memory bytesArray = new bytes(32);
for (uint256 i; i < 32; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}
// 10^-5 (tenth of a basis point) as a RAY
uint256 TOLERANCE = 10 ** 22;
function _yearlyYield(uint256 duty) internal pure returns (uint256) {
return _rpow(duty, (365 * 24 * 60 * 60), RAY);
}
function _expectedRate(uint256 percentValue) internal pure returns (uint256) {
return (10000 + percentValue) * (10 ** 23);
}
function _diffCalc(
uint256 expectedRate_,
uint256 yearlyYield_
) internal pure returns (uint256) {
return (expectedRate_ > yearlyYield_) ?
expectedRate_ - yearlyYield_ : yearlyYield_ - expectedRate_;
}
function _castPreviousSpell() internal {
address[] memory prevSpells = spellValues.previous_spells;
// warp and cast previous spells so values are up-to-date to test against
for (uint256 i; i < prevSpells.length; i++) {
DssSpell prevSpell = DssSpell(prevSpells[i]);
if (prevSpell != DssSpell(address(0)) && !prevSpell.done()) {
if (prevSpell.eta() == 0) {
_vote(address(prevSpell));
_scheduleWaitAndCast(address(prevSpell));
}
else {
// jump to nextCastTime to be a little more forgiving on the spell execution time
vm.warp(prevSpell.nextCastTime());
prevSpell.cast();
}
}
}
}
function setUp() public {
setValues(address(chief));
_castPreviousSpell();
spellValues.deployed_spell_created = spellValues.deployed_spell != address(0)
? spellValues.deployed_spell_created
: block.timestamp;
spell = spellValues.deployed_spell != address(0)
? DssSpell(spellValues.deployed_spell)
: new DssSpell();
if (spellValues.deployed_spell_block != 0 && spell.eta() != 0) {
// if we have a deployed spell in the config
// we want to roll our fork to the block where it was deployed
// this means the test suite will continue to accurately pass/fail
// even if mainnet has already scheduled/cast the spell
vm.makePersistent(address(rates));
vm.makePersistent(address(addr));
vm.makePersistent(address(deployers));
vm.rollFork(spellValues.deployed_spell_block);
// Reset `eta` to `0`, otherwise the tests will fail with "This spell has already been scheduled".
// This is a workaround for the issue described here:
// @see { https://github.com/foundry-rs/foundry/issues/5739 }
vm.store(
address(spell),
bytes32(0),
bytes32(0)
);
}
}
function _vote(address spell_) internal {
if (chief.hat() != spell_) {
_giveTokens(address(gov), 999999999999 ether);
gov.approve(address(chief), type(uint256).max);
chief.lock(999999999999 ether);
address[] memory slate = new address[](1);
assertTrue(!DssSpell(spell_).done());
slate[0] = spell_;
chief.vote(slate);
chief.lift(spell_);
}
assertEq(chief.hat(), spell_);
}
function _scheduleWaitAndCast(address spell_) internal {
DssSpell(spell_).schedule();
vm.warp(DssSpell(spell_).nextCastTime());
DssSpell(spell_).cast();
}
function _stringToBytes32(string memory source) internal pure returns (bytes32 result) {
assembly {
result := mload(add(source, 32))
}
}
function _checkSystemValues(SystemValues storage values) internal {
// dsr
uint256 expectedDSRRate = rates.rates(values.pot_dsr);
// make sure dsr is less than 100% APR
// bc -l <<< 'scale=27; e( l(2.00)/(60 * 60 * 24 * 365) )'
// 1000000021979553151239153027
assertEq(pot.dsr(), expectedDSRRate, "TestError/pot-dsr-expected-value");
assertTrue(
pot.dsr() >= RAY && pot.dsr() < 1000000021979553151239153027,
"TestError/pot-dsr-range"
);
assertTrue(
_diffCalc(_expectedRate(values.pot_dsr), _yearlyYield(expectedDSRRate)) <= TOLERANCE,
"TestError/pot-dsr-rates-table"
);
{
// Line values in RAD
assertTrue(
(vat.Line() >= RAD && vat.Line() < 100 * BILLION * RAD) ||
vat.Line() == 0,
"TestError/vat-Line-range"
);
}
// Pause delay
assertEq(pause.delay(), values.pause_delay, "TestError/pause-delay");
// wait
assertEq(vow.wait(), values.vow_wait, "TestError/vow-wait");
{
// dump values in WAD
uint256 normalizedDump = values.vow_dump * WAD;
assertEq(vow.dump(), normalizedDump, "TestError/vow-dump");
assertTrue(
(vow.dump() >= WAD && vow.dump() < 2 * THOUSAND * WAD) ||
vow.dump() == 0,
"TestError/vow-dump-range"
);
}
{
// sump values in RAD
uint256 normalizedSump = values.vow_sump * RAD;
assertEq(vow.sump(), normalizedSump, "TestError/vow-sump");
assertTrue(
(vow.sump() >= RAD && vow.sump() < 500 * THOUSAND * RAD) ||
vow.sump() == 0,
"TestError/vow-sump-range"
);
}
{
// bump values in RAD
uint256 normalizedBump = values.vow_bump * RAD;
assertEq(vow.bump(), normalizedBump, "TestError/vow-bump");
assertTrue(
(vow.bump() >= RAD && vow.bump() < 100 * THOUSAND * RAD) ||
vow.bump() == 0,
"TestError/vow-bump-range"
);
}
{
// hump values in RAD
uint256 normalizedHumpMin = values.vow_hump_min * RAD;
uint256 normalizedHumpMax = values.vow_hump_max * RAD;
assertTrue(vow.hump() >= normalizedHumpMin && vow.hump() <= normalizedHumpMax, "TestError/vow-hump-min-max");
assertTrue(
(vow.hump() >= RAD && vow.hump() < 1 * BILLION * RAD) ||
vow.hump() == 0,
"TestError/vow-hump-range"
);
}
// Hole value in RAD
{
uint256 normalizedHole = values.dog_Hole * RAD;
assertEq(dog.Hole(), normalizedHole, "TestError/dog-Hole");
assertTrue(dog.Hole() >= MILLION * RAD && dog.Hole() <= 200 * MILLION * RAD, "TestError/dog-Hole-range");
}
// ESM min in WAD
{
uint256 normalizedMin = values.esm_min * WAD;
assertEq(esm.min(), normalizedMin, "TestError/esm-min");
assertTrue(esm.min() > WAD && esm.min() < 200 * THOUSAND * WAD, "TestError/esm-min-range");
}
// check Pause authority
assertEq(pause.authority(), values.pause_authority, "TestError/pause-authority");
// check OsmMom authority
assertEq(osmMom.authority(), values.osm_mom_authority, "TestError/osmMom-authority");
// check ClipperMom authority
assertEq(clipMom.authority(), values.clipper_mom_authority, "TestError/clipperMom-authority");
// check D3MMom authority
assertEq(d3mMom.authority(), values.d3m_mom_authority, "TestError/d3mMom-authority");
// check number of ilks
assertEq(reg.count(), values.ilk_count, "TestError/ilks-count");
// flap
// Check flap hop and sanity checks
assertEq(flap.hop(), values.flap_hop, "TestError/flap-hop");
assertTrue(flap.hop() > 0 && flap.hop() < 86400, "TestError/flap-hop-range"); // gt 0 && lt 1 day
// check want value
uint256 normalizedTestWant = values.flap_want * 10**14;
assertEq(flap.want(), normalizedTestWant, "TestError/flap-want");
assertTrue(flap.want() >= 90 * WAD / 100 && flap.want() <= 110 * WAD / 100, "TestError/flap-want-range"); // gte 90% and lte 110%
assertEq(vat.wards(pauseProxy), uint256(1), "TestError/pause-proxy-deauthed-on-vat");
// transferrable vest
// check mkr allowance
_checkTransferrableVestMkrAllowance();
}
function _checkCollateralValues(SystemValues storage values) internal {
// Using an array to work around stack depth limitations.
// sums[0] : sum of all lines
// sums[1] : sum over ilks of (line - Art * rate)--i.e. debt that could be drawn at any time
uint256[] memory sums = new uint256[](2);
bytes32[] memory ilks = reg.list();
for(uint256 i = 0; i < ilks.length; i++) {
bytes32 ilk = ilks[i];
(uint256 duty,) = jug.ilks(ilk);
assertEq(duty, rates.rates(values.collaterals[ilk].pct), _concat("TestError/jug-duty-", ilk));
// make sure duty is less than 1000% APR
// bc -l <<< 'scale=27; e( l(10.00)/(60 * 60 * 24 * 365) )'
// 1000000073014496989316680335
assertTrue(duty >= RAY && duty < 1000000073014496989316680335, _concat("TestError/jug-duty-range-", ilk)); // gt 0 and lt 1000%
assertTrue(
_diffCalc(_expectedRate(values.collaterals[ilk].pct), _yearlyYield(rates.rates(values.collaterals[ilk].pct))) <= TOLERANCE,
_concat("TestError/rates-", ilk)
);
assertTrue(values.collaterals[ilk].pct < THOUSAND * THOUSAND, _concat("TestError/pct-max-", ilk)); // check value lt 1000%
{
uint256 line;
uint256 dust;
{
uint256 Art;
uint256 rate;
(Art, rate,, line, dust) = vat.ilks(ilk);
if (Art * rate < line) {
sums[1] += line - Art * rate;
}
}
// Convert whole Dai units to expected RAD
uint256 normalizedTestLine = values.collaterals[ilk].line * RAD;
sums[0] += line;
(uint256 aL_line, uint256 aL_gap, uint256 aL_ttl,,) = autoLine.ilks(ilk);
if (!values.collaterals[ilk].aL_enabled) {
assertTrue(aL_line == 0, _concat("TestError/al-Line-not-zero-", ilk));
assertEq(line, normalizedTestLine, _concat("TestError/vat-line-", ilk));
assertTrue((line >= RAD && line < 10 * BILLION * RAD) || line == 0, _concat("TestError/vat-line-range-", ilk)); // eq 0 or gt eq 1 RAD and lt 10B
} else {
assertTrue(aL_line > 0, _concat("TestError/al-Line-is-zero-", ilk));
assertEq(aL_line, values.collaterals[ilk].aL_line * RAD, _concat("TestError/al-line-", ilk));
assertEq(aL_gap, values.collaterals[ilk].aL_gap * RAD, _concat("TestError/al-gap-", ilk));
assertEq(aL_ttl, values.collaterals[ilk].aL_ttl, _concat("TestError/al-ttl-", ilk));
assertTrue((aL_line >= RAD && aL_line < 20 * BILLION * RAD) || aL_line == 0, _concat("TestError/al-line-range-", ilk)); // eq 0 or gt eq 1 RAD and lt 10B
}
uint256 normalizedTestDust = values.collaterals[ilk].dust * RAD;
assertEq(dust, normalizedTestDust, _concat("TestError/vat-dust-", ilk));
assertTrue((dust >= RAD && dust <= 100 * THOUSAND * RAD) || dust == 0, _concat("TestError/vat-dust-range-", ilk)); // eq 0 or gt eq 1 and lte 100k
}
{
(address pip, uint256 mat) = spotter.ilks(ilk);
if (pip != address(0)) {
// Convert BP to system expected value
uint256 normalizedTestMat = (values.collaterals[ilk].mat * 10**23);
if (values.collaterals[ilk].offboarding) {
assertTrue(mat <= normalizedTestMat, _concat("TestError/vat-lerping-mat-", ilk));
assertTrue(mat >= RAY && mat <= 300 * RAY, _concat("TestError/vat-mat-range-", ilk)); // cr gt 100% and lt 30000%
} else {
assertEq(mat, normalizedTestMat, _concat("TestError/vat-mat-", ilk));
assertTrue(mat >= RAY && mat < 10 * RAY, _concat("TestError/vat-mat-range-", ilk)); // cr gt 100% and lt 1000%
}
}
}
if (values.collaterals[ilk].liqType == "flip") {
// NOTE: MCD_CAT has been scuttled in the spell on 2023-09-13
revert("TestError/flip-deprecated");
}
if (values.collaterals[ilk].liqType == "clip") {
{
assertEq(reg.class(ilk), 1, _concat("TestError/reg-class-", ilk));
(bool ok, bytes memory val) = reg.xlip(ilk).call(abi.encodeWithSignature("dog()"));
assertTrue(ok, _concat("TestError/reg-xlip-dog-", ilk));
assertEq(abi.decode(val, (address)), address(dog), _concat("TestError/reg-xlip-dog-", ilk));
}
{
(, uint256 chop, uint256 hole,) = dog.ilks(ilk);
// Convert BP to system expected value
uint256 normalizedTestChop = (values.collaterals[ilk].chop * 10**14) + WAD;
assertEq(chop, normalizedTestChop, _concat("TestError/dog-chop-", ilk));
// make sure chop is less than 100%
assertTrue(chop >= WAD && chop < 2 * WAD, _concat("TestError/dog-chop-range-", ilk)); // penalty gt eq 0% and lt 100%
// Convert whole Dai units to expected RAD
uint256 normalizedTesthole = values.collaterals[ilk].dog_hole * RAD;
assertEq(hole, normalizedTesthole, _concat("TestError/dog-hole-", ilk));
assertTrue(hole == 0 || hole >= RAD && hole <= 100 * MILLION * RAD, _concat("TestError/dog-hole-range-", ilk));
}
(address clipper,,,) = dog.ilks(ilk);
assertTrue(clipper != address(0), _concat("TestError/invalid-clip-address-", ilk));
ClipAbstract clip = ClipAbstract(clipper);
{
// Convert BP to system expected value
uint256 normalizedTestBuf = values.collaterals[ilk].clip_buf * 10**23;
assertEq(uint256(clip.buf()), normalizedTestBuf, _concat("TestError/clip-buf-", ilk));
assertTrue(clip.buf() >= RAY && clip.buf() <= 2 * RAY, _concat("TestError/clip-buf-range-", ilk)); // gte 0% and lte 100%
assertEq(uint256(clip.tail()), values.collaterals[ilk].clip_tail, _concat("TestError/clip-tail-", ilk));
if (ilk == "TUSD-A") { // long tail liquidation
assertTrue(clip.tail() >= 1200 && clip.tail() <= 30 days, _concat("TestError/TUSD-clip-tail-range-", ilk)); // gt eq 20 minutes and lt eq 30 days
} else {
assertTrue(clip.tail() >= 1200 && clip.tail() <= 12 hours, _concat("TestError/clip-tail-range-", ilk)); // gt eq 20 minutes and lt eq 12 hours
}
uint256 normalizedTestCusp = (values.collaterals[ilk].clip_cusp) * 10**23;
assertEq(uint256(clip.cusp()), normalizedTestCusp, _concat("TestError/clip-cusp-", ilk));
assertTrue(clip.cusp() >= RAY / 10 && clip.cusp() < RAY, _concat("TestError/clip-cusp-range-", ilk)); // gte 10% and lt 100%
assertTrue(_rmul(clip.buf(), clip.cusp()) <= RAY, _concat("TestError/clip-buf-cusp-limit-", ilk));
uint256 normalizedTestChip = (values.collaterals[ilk].clip_chip) * 10**14;
assertEq(uint256(clip.chip()), normalizedTestChip, _concat("TestError/clip-chip-", ilk));
assertTrue(clip.chip() < 1 * WAD / 100, _concat("TestError/clip-chip-range-", ilk)); // lt 1%
uint256 normalizedTestTip = values.collaterals[ilk].clip_tip * RAD;
assertEq(uint256(clip.tip()), normalizedTestTip, _concat("TestError/clip-tip-", ilk));
assertTrue(clip.tip() == 0 || clip.tip() >= RAD && clip.tip() <= 500 * RAD, _concat("TestError/clip-tip-range-", ilk));
assertEq(clip.wards(address(clipMom)), values.collaterals[ilk].clipper_mom, _concat("TestError/clip-clipperMom-auth-", ilk));
assertEq(clipMom.tolerance(address(clip)), values.collaterals[ilk].cm_tolerance * RAY / 10000, _concat("TestError/clipperMom-tolerance-", ilk));
if (values.collaterals[ilk].liqOn) {
assertEq(clip.stopped(), 0, _concat("TestError/clip-liqOn-", ilk));
} else {
assertTrue(clip.stopped() > 0, _concat("TestError/clip-liqOn-", ilk));
}
assertEq(clip.wards(address(end)), 1, _concat("TestError/clip-end-auth-", ilk));
assertEq(clip.wards(address(pauseProxy)), 1, _concat("TestError/clip-pause-proxy-auth-", ilk)); // Check pause_proxy ward
}
{
(bool exists, bytes memory value) = clip.calc().call(abi.encodeWithSignature("tau()"));
assertEq(exists ? abi.decode(value, (uint256)) : 0, values.collaterals[ilk].calc_tau, _concat("TestError/calc-tau-", ilk));
(exists, value) = clip.calc().call(abi.encodeWithSignature("step()"));
assertEq(exists ? abi.decode(value, (uint256)) : 0, values.collaterals[ilk].calc_step, _concat("TestError/calc-step-", ilk));
if (exists) {
assertTrue(abi.decode(value, (uint256)) > 0, _concat("TestError/calc-step-is-zero-", ilk));
}
(exists, value) = clip.calc().call(abi.encodeWithSignature("cut()"));
uint256 normalizedTestCut = values.collaterals[ilk].calc_cut * 10**23;
assertEq(exists ? abi.decode(value, (uint256)) : 0, normalizedTestCut, _concat("TestError/calc-cut-", ilk));
if (exists) {
assertTrue(abi.decode(value, (uint256)) > 0 && abi.decode(value, (uint256)) < RAY, _concat("TestError/calc-cut-range-", ilk));
}
}
{
uint256 normalizedTestChop = (values.collaterals[ilk].chop * 10**14) + WAD;
uint256 _chost = (values.collaterals[ilk].dust * RAD) * normalizedTestChop / WAD;
assertEq(clip.chost(), _chost, _concat("TestError/calc-chost-incorrect-", ilk)); // Ensure clip.upchost() is called when dust changes
}
}
if (reg.class(ilk) < 3) {
{
GemJoinAbstract join = GemJoinAbstract(reg.join(ilk));
assertEq(join.wards(address(pauseProxy)), 1, _concat("TestError/join-pause-proxy-auth-", ilk)); // Check pause_proxy ward
}
}
}
// Require that debt + (debt that could be drawn) does not exceed Line.
// TODO: consider a buffer for fee accrual
assertTrue(vat.debt() + sums[1] <= vat.Line(), "TestError/vat-Line-1");
// Enforce the global Line also falls between (sum of lines) + offset and (sum of lines) + 2*offset.
assertTrue(sums[0] + values.line_offset * RAD <= vat.Line(), "TestError/vat-Line-2");
assertTrue(sums[0] + 2 * values.line_offset * RAD >= vat.Line(), "TestError/vat-Line-3");
// TODO: have a discussion about how we want to manage the global Line going forward.
}
function _getOSMPrice(address pip) internal returns (uint256) {
// vm.load is to pull the price from the LP Oracle storage bypassing the whitelist
uint256 price = uint256(vm.load(
pip,
bytes32(uint256(3))
)) & type(uint128).max; // Price is in the second half of the 32-byte storage slot
// Price is bounded in the spot by around 10^23
// Give a 10^9 buffer for price appreciation over time
// Note: This currently can't be hit due to the uint112, but we want to backstop
// once the PIP uint256 size is increased
assertTrue(price <= (10 ** 14) * WAD);
return price;
}
function _getUNIV2LPPrice(address pip) internal returns (uint256) {
// vm.load is to pull the price from the LP Oracle storage bypassing the whitelist
uint256 price = uint256(vm.load(
pip,
bytes32(uint256(3))
)) & type(uint128).max; // Price is in the second half of the 32-byte storage slot
// Price is bounded in the spot by around 10^23
// Give a 10^9 buffer for price appreciation over time
// Note: This currently can't be hit due to the uint112, but we want to backstop
// once the PIP uint256 size is increased
assertTrue(price <= (10 ** 14) * WAD);
return price;
}
function _giveTokens(address token, uint256 amount) internal {
if (token == addr.addr("GUSD")) {
_giveTokensGUSD(token, amount);
return;
}
GodMode.setBalance(token, address(this), amount);
}
function _giveTokensGUSD(address _token, uint256 amount) internal {
DSTokenAbstract token = DSTokenAbstract(_token);
if (token.balanceOf(address(this)) == amount) return;
// Special exception GUSD has its storage in a separate contract
address STORE = 0xc42B14e49744538e3C239f8ae48A1Eaaf35e68a0;
// Edge case - balance is already set for some reason
if (token.balanceOf(address(this)) == amount) return;
for (uint256 i = 0; i < 200; i++) {
// Scan the storage for the balance storage slot
bytes32 prevValue = vm.load(
STORE,
keccak256(abi.encode(address(this), uint256(i)))
);
vm.store(
STORE,
keccak256(abi.encode(address(this), uint256(i))),
bytes32(amount)
);
if (token.balanceOf(address(this)) == amount) {
// Found it
return;
} else {
// Keep going after restoring the original value
vm.store(
STORE,
keccak256(abi.encode(address(this), uint256(i))),
prevValue
);
}
}
// We have failed if we reach here
assertTrue(false, "TestError/GiveTokens-slot-not-found");
}
function _checkIlkIntegration(
bytes32 _ilk,
GemJoinAbstract join,
ClipAbstract clip,
address pip,
bool _isOSM,
bool _checkLiquidations,
bool _transferFee
) internal {
GemAbstract token = GemAbstract(join.gem());
if (_isOSM) OsmAbstract(pip).poke();
vm.warp(block.timestamp + 3601);
if (_isOSM) OsmAbstract(pip).poke();
spotter.poke(_ilk);
// Authorization
assertEq(join.wards(pauseProxy), 1, _concat("TestError/checkIlkIntegration-pauseProxy-not-auth-on-join-", _ilk));
assertEq(vat.wards(address(join)), 1, _concat("TestError/checkIlkIntegration-join-not-auth-on-vat-", _ilk));
assertEq(vat.wards(address(clip)), 1, _concat("TestError/checkIlkIntegration-clip-not-auth-on-vat-", _ilk));
assertEq(dog.wards(address(clip)), 1, _concat("TestError/checkIlkIntegration-clip-not-auth-on-dog-", _ilk));
assertEq(clip.wards(address(dog)), 1, _concat("TestError/checkIlkIntegration-dog-not-auth-on-clip-", _ilk));
assertEq(clip.wards(address(end)), 1, _concat("TestError/checkIlkIntegration-end-not-auth-on-clip-", _ilk));
assertEq(clip.wards(address(clipMom)), 1, _concat("TestError/checkIlkIntegration-clipMom-not-auth-on-clip-", _ilk));
assertEq(clip.wards(address(esm)), 1, _concat("TestError/checkIlkIntegration-esm-not-auth-on-clip-", _ilk));
if (_isOSM) {
assertEq(OsmAbstract(pip).wards(address(osmMom)), 1, _concat("TestError/checkIlkIntegration-osmMom-not-auth-on-pip-", _ilk));
assertEq(OsmAbstract(pip).bud(address(spotter)), 1, _concat("TestError/checkIlkIntegration-spot-not-bud-on-pip-", _ilk));
assertEq(OsmAbstract(pip).bud(address(clip)), 1, _concat("TestError/checkIlkIntegration-spot-not-bud-on-pip-", _ilk));
assertEq(OsmAbstract(pip).bud(address(clipMom)), 1, _concat("TestError/checkIlkIntegration-spot-not-bud-on-pip-", _ilk));
assertEq(OsmAbstract(pip).bud(address(end)), 1, _concat("TestError/checkIlkIntegration-spot-not-bud-on-pip-", _ilk));
assertEq(MedianAbstract(OsmAbstract(pip).src()).bud(pip), 1, _concat("TestError/checkIlkIntegration-pip-not-bud-on-osm-", _ilk));
assertEq(OsmMomAbstract(osmMom).osms(_ilk), pip, _concat("TestError/checkIlkIntegration-pip-not-bud-on-osmMom-", _ilk));
}
(,,,, uint256 dust) = vat.ilks(_ilk);
dust /= RAY;
uint256 amount = 4 * dust * 10 ** uint256(token.decimals()) / (_isOSM ? _getOSMPrice(pip) : uint256(DSValueAbstract(pip).read()));
uint256 amount18 = token.decimals() == 18 ? amount : amount * 10**(18 - uint256(token.decimals()));
_giveTokens(address(token), amount);
assertEq(token.balanceOf(address(this)), amount);
assertEq(vat.gem(_ilk, address(this)), 0);
token.approve(address(join), amount);
join.join(address(this), amount);
assertEq(token.balanceOf(address(this)), 0);
if (_transferFee) {
amount = vat.gem(_ilk, address(this));
assertTrue(amount > 0);
}
assertEq(vat.gem(_ilk, address(this)), amount18);
// Tick the fees forward so that art != dai in wad units
vm.warp(block.timestamp + 1);
jug.drip(_ilk);
// Deposit collateral, generate DAI
(,uint256 rate,,uint256 line,) = vat.ilks(_ilk);
assertEq(vat.dai(address(this)), 0);
// Set max line to ensure we can create a new position
_setIlkLine(_ilk, type(uint256).max);
vat.frob(_ilk, address(this), address(this), address(this), int256(amount18), int256(_divup(RAY * dust, rate)));
// Revert ilk line to proceed with testing
_setIlkLine(_ilk, line);
assertEq(vat.gem(_ilk, address(this)), 0);
assertTrue(vat.dai(address(this)) >= dust * RAY);
assertTrue(vat.dai(address(this)) <= (dust + 1) * RAY);
// Payback DAI, withdraw collateral
vat.frob(_ilk, address(this), address(this), address(this), -int256(amount18), -int256(_divup(RAY * dust, rate)));
assertEq(vat.gem(_ilk, address(this)), amount18);
assertEq(vat.dai(address(this)), 0);
// Withdraw from adapter
join.exit(address(this), amount);
if (_transferFee) {
amount = token.balanceOf(address(this));
}
assertEq(token.balanceOf(address(this)), amount);
assertEq(vat.gem(_ilk, address(this)), 0);
// Generate new DAI to force a liquidation
token.approve(address(join), amount);
join.join(address(this), amount);
if (_transferFee) {
amount = vat.gem(_ilk, address(this));
}
// dart max amount of DAI
(,,uint256 spot,,) = vat.ilks(_ilk);
// Set max line to ensure we can draw dai
_setIlkLine(_ilk, type(uint256).max);
vat.frob(_ilk, address(this), address(this), address(this), int256(amount18), int256(amount18 * spot / rate));
// Revert ilk line to proceed with testing
_setIlkLine(_ilk, line);
vm.warp(block.timestamp + 1);
jug.drip(_ilk);
assertEq(clip.kicks(), 0);
if (_checkLiquidations) {
if (_getIlkDuty(_ilk) == rates.rates(0)) {
// Rates wont accrue if 0, raise the mat to make the vault unsafe
_setIlkMat(_ilk, 100000 * RAY);
vm.warp(block.timestamp + 10 days);
spotter.poke(_ilk);
}
dog.bark(_ilk, address(this), address(this));
assertEq(clip.kicks(), 1);
}
// Dump all dai for next run
vat.move(address(this), address(0x0), vat.dai(address(this)));
}
function _checkIlkClipper(
bytes32 ilk,
GemJoinAbstract join,
ClipAbstract clipper,
address calc,
OsmAbstract pip,
uint256 ilkAmt
) internal {
// Contracts set
assertEq(dog.vat(), address(vat));
assertEq(dog.vow(), address(vow));
{
(address clip,,,) = dog.ilks(ilk);
assertEq(clip, address(clipper));
}
assertEq(clipper.ilk(), ilk);
assertEq(clipper.vat(), address(vat));
assertEq(clipper.vow(), address(vow));
assertEq(clipper.dog(), address(dog));
assertEq(clipper.spotter(), address(spotter));
assertEq(clipper.calc(), calc);
// Authorization
assertEq(vat.wards(address(clipper)) , 1);
assertEq(dog.wards(address(clipper)) , 1);
assertEq(clipper.wards(address(dog)) , 1);
assertEq(clipper.wards(address(end)) , 1);
assertEq(clipper.wards(address(clipMom)), 1);
assertEq(clipper.wards(address(esm)), 1);
try pip.bud(address(spotter)) returns (uint256 bud) {
assertEq(bud, 1);
} catch {}
try pip.bud(address(clipper)) returns (uint256 bud) {
assertEq(bud, 1);
} catch {}
try pip.bud(address(clipMom)) returns (uint256 bud) {
assertEq(bud, 1);
} catch {}
try pip.bud(address(end)) returns (uint256 bud) {
assertEq(bud, 1);
} catch {}
// Force max Hole
vm.store(
address(dog),
bytes32(uint256(4)),
bytes32(type(uint256).max)
);
// Initially this test assume that's we are using freshly deployed Cliiper contract without any past auctions
if (clipper.kicks() > 0) {
// Cleanup clipper auction counter
vm.store(
address(clipper),
bytes32(uint256(10)),
bytes32(uint256(0))
);
assertEq(clipper.kicks(), 0);
}
// ----------------------- Check Clipper works and bids can be made -----------------------
{
GemAbstract token = GemAbstract(join.gem());
uint256 tknAmt = ilkAmt / 10 ** (18 - join.dec());
_giveTokens(address(token), tknAmt);
assertEq(token.balanceOf(address(this)), tknAmt);
// Join to adapter
assertEq(vat.gem(ilk, address(this)), 0);
assertEq(token.allowance(address(this), address(join)), 0);
token.approve(address(join), tknAmt);
join.join(address(this), tknAmt);
assertEq(token.balanceOf(address(this)), 0);
assertEq(vat.gem(ilk, address(this)), ilkAmt);
}
{
// Generate new DAI to force a liquidation
uint256 rate;
int256 art;
uint256 spot;
uint256 line;
(,rate, spot, line,) = vat.ilks(ilk);
art = int256(ilkAmt * spot / rate);
// dart max amount of DAI
_setIlkLine(ilk, type(uint256).max);
vat.frob(ilk, address(this), address(this), address(this), int256(ilkAmt), art);
_setIlkLine(ilk, line);
_setIlkMat(ilk, 100000 * RAY);
vm.warp(block.timestamp + 10 days);
spotter.poke(ilk);
assertEq(clipper.kicks(), 0);
dog.bark(ilk, address(this), address(this));
assertEq(clipper.kicks(), 1);
(, rate,,,) = vat.ilks(ilk);
uint256 debt = rate * uint256(art) * dog.chop(ilk) / WAD;
vm.store(
address(vat),
keccak256(abi.encode(address(this), uint256(5))),
bytes32(debt)
);
assertEq(vat.dai(address(this)), debt);
assertEq(vat.gem(ilk, address(this)), 0);
vm.warp(block.timestamp + 20 minutes);
(, uint256 tab, uint256 lot, address usr,, uint256 top) = clipper.sales(1);
assertEq(usr, address(this));
assertEq(tab, debt);
assertEq(lot, ilkAmt);
assertTrue(lot * top > tab); // There is enough collateral to cover the debt at current price
vat.hope(address(clipper));
clipper.take(1, lot, top, address(this), bytes(""));
}
{
(, uint256 tab, uint256 lot, address usr,,) = clipper.sales(1);
assertEq(usr, address(0));
assertEq(tab, 0);
assertEq(lot, 0);
assertEq(vat.dai(address(this)), 0);
assertEq(vat.gem(ilk, address(this)), ilkAmt); // What was purchased + returned back as it is the owner of the vault
}
}
function _checkUNILPIntegration(
bytes32 _ilk,
GemJoinAbstract join,
ClipAbstract clip,
LPOsmAbstract pip,
address _medianizer1,
address _medianizer2,
bool _isMedian1,
bool _isMedian2,
bool _checkLiquidations
) internal {
GemAbstract token = GemAbstract(join.gem());