This repository has been archived by the owner on Apr 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
/
sai.t.sol
2559 lines (2186 loc) · 80.9 KB
/
sai.t.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
pragma solidity ^0.4.18;
import "ds-test/test.sol";
import "ds-math/math.sol";
import 'ds-token/token.sol';
import 'ds-roles/roles.sol';
import 'ds-value/value.sol';
import './weth9.sol';
import './mom.sol';
import './fab.sol';
import './pit.sol';
contract TestWarp is DSNote {
uint256 _era;
function TestWarp() public {
_era = now;
}
function era() public view returns (uint256) {
return _era == 0 ? now : _era;
}
function warp(uint age) public note {
require(_era != 0);
_era = age == 0 ? 0 : _era + age;
}
}
contract DevTub is SaiTub, TestWarp {
function DevTub(
DSToken sai_,
DSToken sin_,
DSToken skr_,
ERC20 gem_,
DSToken gov_,
DSValue pip_,
DSValue pep_,
SaiVox vox_,
address pit_
) public
SaiTub(sai_, sin_, skr_, gem_, gov_, pip_, pep_, vox_, pit_) {}
}
contract DevTop is SaiTop, TestWarp {
function DevTop(SaiTub tub_, SaiTap tap_) public SaiTop(tub_, tap_) {}
}
contract DevVox is SaiVox, TestWarp {
function DevVox(uint par_) SaiVox(par_) public {}
}
contract DevVoxFab {
function newVox() public returns (DevVox vox) {
vox = new DevVox(10 ** 27);
vox.setOwner(msg.sender);
}
}
contract DevTubFab {
function newTub(DSToken sai, DSToken sin, DSToken skr, DSToken gem, DSToken gov, DSValue pip, DSValue pep, SaiVox vox, address pit) public returns (DevTub tub) {
tub = new DevTub(sai, sin, skr, gem, gov, pip, pep, vox, pit);
tub.setOwner(msg.sender);
}
}
contract DevTopFab {
function newTop(DevTub tub, SaiTap tap) public returns (DevTop top) {
top = new DevTop(tub, tap);
top.setOwner(msg.sender);
}
}
contract DevDadFab {
function newDad() public returns (DSGuard dad) {
dad = new DSGuard();
// convenience in tests
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sai(), bytes4(keccak256('mint(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sai(), bytes4(keccak256('burn(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sai(), bytes4(keccak256('mint(address,uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sai(), bytes4(keccak256('burn(address,uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sin(), bytes4(keccak256('mint(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sin(), bytes4(keccak256('burn(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sin(), bytes4(keccak256('mint(address,uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).sin(), bytes4(keccak256('burn(address,uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).skr(), bytes4(keccak256('mint(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).skr(), bytes4(keccak256('burn(uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).skr(), bytes4(keccak256('mint(address,uint256)')));
dad.permit(DaiFab(msg.sender).owner(), DaiFab(msg.sender).skr(), bytes4(keccak256('burn(address,uint256)')));
dad.setOwner(msg.sender);
}
}
contract FakePerson {
SaiTap public tap;
DSToken public sai;
function FakePerson(SaiTap _tap) public {
tap = _tap;
sai = tap.sai();
sai.approve(tap);
}
function cash() public {
tap.cash(sai.balanceOf(this));
}
}
contract SaiTestBase is DSTest, DSMath {
DevVox vox;
DevTub tub;
DevTop top;
SaiTap tap;
SaiMom mom;
WETH9 gem;
DSToken sai;
DSToken sin;
DSToken skr;
DSToken gov;
GemPit pit;
DSValue pip;
DSValue pep;
DSRoles dad;
function ray(uint256 wad) internal pure returns (uint256) {
return wad * 10 ** 9;
}
function wad(uint256 ray_) internal pure returns (uint256) {
return wdiv(ray_, RAY);
}
function mark(uint price) internal {
pip.poke(bytes32(price));
}
function mark(DSToken tkn, uint price) internal {
if (address(tkn) == address(gov)) pep.poke(bytes32(price));
else if (address(tkn) == address(gem)) mark(price);
}
function warp(uint256 age) internal {
vox.warp(age);
tub.warp(age);
top.warp(age);
}
function setUp() public {
GemFab gemFab = new GemFab();
DevVoxFab voxFab = new DevVoxFab();
DevTubFab tubFab = new DevTubFab();
TapFab tapFab = new TapFab();
DevTopFab topFab = new DevTopFab();
MomFab momFab = new MomFab();
DevDadFab dadFab = new DevDadFab();
DaiFab daiFab = new DaiFab(gemFab, VoxFab(voxFab), TubFab(tubFab), tapFab, TopFab(topFab), momFab, DadFab(dadFab));
gem = new WETH9();
gem.deposit.value(100 ether)();
gov = new DSToken('GOV');
pip = new DSValue();
pep = new DSValue();
pit = new GemPit();
daiFab.makeTokens();
daiFab.makeVoxTub(ERC20(gem), gov, pip, pep, pit);
daiFab.makeTapTop();
daiFab.configParams();
daiFab.verifyParams();
DSRoles authority = new DSRoles();
authority.setRootUser(this, true);
daiFab.configAuth(authority);
sai = DSToken(daiFab.sai());
sin = DSToken(daiFab.sin());
skr = DSToken(daiFab.skr());
vox = DevVox(daiFab.vox());
tub = DevTub(daiFab.tub());
tap = SaiTap(daiFab.tap());
top = DevTop(daiFab.top());
mom = SaiMom(daiFab.mom());
dad = DSRoles(daiFab.dad());
sai.approve(tub);
skr.approve(tub);
gem.approve(tub, uint(-1));
gov.approve(tub);
sai.approve(tap);
skr.approve(tap);
mark(1 ether);
mark(gov, 1 ether);
mom.setCap(20 ether);
mom.setAxe(ray(1 ether));
mom.setMat(ray(1 ether));
mom.setTax(ray(1 ether));
mom.setFee(ray(1 ether));
mom.setTubGap(1 ether);
mom.setTapGap(1 ether);
}
}
contract SaiTubTest is SaiTestBase {
function testBasic() public {
assertEq( skr.balanceOf(tub), 0 ether );
assertEq( skr.balanceOf(this), 0 ether );
assertEq( gem.balanceOf(tub), 0 ether );
// edge case
assertEq( uint256(tub.per()), ray(1 ether) );
tub.join(10 ether);
assertEq( uint256(tub.per()), ray(1 ether) );
assertEq( skr.balanceOf(this), 10 ether );
assertEq( gem.balanceOf(tub), 10 ether );
// price formula
tub.join(10 ether);
assertEq( uint256(tub.per()), ray(1 ether) );
assertEq( skr.balanceOf(this), 20 ether );
assertEq( gem.balanceOf(tub), 20 ether );
var cup = tub.open();
assertEq( skr.balanceOf(this), 20 ether );
assertEq( skr.balanceOf(tub), 0 ether );
tub.lock(cup, 10 ether); // lock skr token
assertEq( skr.balanceOf(this), 10 ether );
assertEq( skr.balanceOf(tub), 10 ether );
assertEq( sai.balanceOf(this), 0 ether);
tub.draw(cup, 5 ether);
assertEq( sai.balanceOf(this), 5 ether);
assertEq( sai.balanceOf(this), 5 ether);
tub.wipe(cup, 2 ether);
assertEq( sai.balanceOf(this), 3 ether);
assertEq( sai.balanceOf(this), 3 ether);
assertEq( skr.balanceOf(this), 10 ether );
tub.shut(cup);
assertEq( sai.balanceOf(this), 0 ether);
assertEq( skr.balanceOf(this), 20 ether );
}
function testGive() public {
var cup = tub.open();
assertEq(tub.lad(cup), this);
address ali = 0x456;
tub.give(cup, ali);
assertEq(tub.lad(cup), ali);
}
function testFailGiveNotLad() public {
var cup = tub.open();
address ali = 0x456;
tub.give(cup, ali);
address bob = 0x789;
tub.give(cup, bob);
}
function testMold() public {
var setAxe = bytes4(keccak256('setAxe(uint256)'));
var setCap = bytes4(keccak256('setCap(uint256)'));
var setMat = bytes4(keccak256('setMat(uint256)'));
assertTrue(mom.call(setCap, 0 ether));
assertTrue(mom.call(setCap, 5 ether));
assertTrue(!mom.call(setAxe, ray(2 ether)));
assertTrue( mom.call(setMat, ray(2 ether)));
assertTrue( mom.call(setAxe, ray(2 ether)));
assertTrue(!mom.call(setMat, ray(1 ether)));
}
function testTune() public {
assertEq(vox.how(), 0);
mom.setHow(2 * 10 ** 25);
assertEq(vox.how(), 2 * 10 ** 25);
}
function testPriceFeedSetters() public {
var setPip = bytes4(keccak256('setPip(address)'));
var setPep = bytes4(keccak256('setPep(address)'));
var setVox = bytes4(keccak256('setVox(address)'));
assertTrue(tub.pip() != address(0x1));
assertTrue(tub.pep() != address(0x2));
assertTrue(tub.vox() != address(0x3));
assertTrue(mom.call(setPip, address(0x1)));
assertTrue(mom.call(setPep, address(0x2)));
assertTrue(mom.call(setVox, address(0x3)));
assertTrue(tub.pip() == address(0x1));
assertTrue(tub.pep() == address(0x2));
assertTrue(tub.vox() == address(0x3));
}
function testJoinInitial() public {
assertEq(skr.totalSupply(), 0 ether);
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 100 ether);
tub.join(10 ether);
assertEq(skr.balanceOf(this), 10 ether);
assertEq(gem.balanceOf(this), 90 ether);
assertEq(gem.balanceOf(tub), 10 ether);
}
function testJoinExit() public {
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 100 ether);
tub.join(10 ether);
assertEq(skr.balanceOf(this), 10 ether);
assertEq(gem.balanceOf(this), 90 ether);
assertEq(gem.balanceOf(tub), 10 ether);
tub.exit(5 ether);
assertEq(skr.balanceOf(this), 5 ether);
assertEq(gem.balanceOf(this), 95 ether);
assertEq(gem.balanceOf(tub), 5 ether);
tub.join(2 ether);
assertEq(skr.balanceOf(this), 7 ether);
assertEq(gem.balanceOf(this), 93 ether);
assertEq(gem.balanceOf(tub), 7 ether);
tub.exit(1 ether);
assertEq(skr.balanceOf(this), 6 ether);
assertEq(gem.balanceOf(this), 94 ether);
assertEq(gem.balanceOf(tub), 6 ether);
}
function testFailOverDraw() public {
mom.setMat(ray(1 ether));
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 11 ether);
}
function testFailOverDrawExcess() public {
mom.setMat(ray(1 ether));
tub.join(20 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 11 ether);
}
function testDraw() public {
mom.setMat(ray(1 ether));
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
assertEq(sai.balanceOf(this), 0 ether);
tub.draw(cup, 10 ether);
assertEq(sai.balanceOf(this), 10 ether);
}
function testWipe() public {
mom.setMat(ray(1 ether));
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 10 ether);
assertEq(sai.balanceOf(this), 10 ether);
tub.wipe(cup, 5 ether);
assertEq(sai.balanceOf(this), 5 ether);
}
function testUnsafe() public {
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 9 ether);
assertTrue(tub.safe(cup));
mark(1 ether / 2);
assertTrue(!tub.safe(cup));
}
function testBiteUnderParity() public {
assertEq(uint(tub.axe()), uint(ray(1 ether))); // 100% collateralisation limit
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 5 ether); // 200% collateralisation
mark(1 ether / 4); // 50% collateralisation
assertEq(tap.fog(), uint(0));
tub.bite(cup);
assertEq(tap.fog(), uint(10 ether));
}
function testBiteOverParity() public {
mom.setMat(ray(2 ether)); // require 200% collateralisation
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 4 ether); // 250% collateralisation
assertTrue(tub.safe(cup));
mark(1 ether / 2); // 125% collateralisation
assertTrue(!tub.safe(cup));
assertEq(tub.din(), 4 ether);
assertEq(tub.tab(cup), 4 ether);
assertEq(tap.fog(), 0 ether);
assertEq(tap.woe(), 0 ether);
tub.bite(cup);
assertEq(tub.din(), 0 ether);
assertEq(tub.tab(cup), 0 ether);
assertEq(tap.fog(), 8 ether);
assertEq(tap.woe(), 4 ether);
// cdp should now be safe with 0 sai debt and 2 skr remaining
var skr_before = skr.balanceOf(this);
tub.free(cup, 1 ether);
assertEq(skr.balanceOf(this) - skr_before, 1 ether);
}
function testLock() public {
tub.join(10 ether);
var cup = tub.open();
assertEq(skr.balanceOf(tub), 0 ether);
tub.lock(cup, 10 ether);
assertEq(skr.balanceOf(tub), 10 ether);
}
function testFree() public {
mom.setMat(ray(2 ether)); // require 200% collateralisation
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 4 ether); // 250% collateralisation
var skr_before = skr.balanceOf(this);
tub.free(cup, 2 ether); // 225%
assertEq(skr.balanceOf(this) - skr_before, 2 ether);
}
function testFailFreeToUnderCollat() public {
mom.setMat(ray(2 ether)); // require 200% collateralisation
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 4 ether); // 250% collateralisation
tub.free(cup, 3 ether); // 175% -- fails
}
function testFailDrawOverDebtCeiling() public {
mom.setCap(4 ether);
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 5 ether);
}
function testDebtCeiling() public {
mom.setCap(5 ether);
mom.setMat(ray(2 ether)); // require 200% collat
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 5 ether); // 200% collat, full debt ceiling
mark(1 ether / 2); // 100% collat
assertEq(tub.air(), uint(10 ether));
assertEq(tap.fog(), uint(0 ether));
tub.bite(cup);
assertEq(tub.air(), uint(0 ether));
assertEq(tap.fog(), uint(10 ether));
tub.join(10 ether);
// skr hasn't been diluted yet so still 1:1 skr:gem
assertEq(skr.balanceOf(this), 10 ether);
}
}
contract CageTest is SaiTestBase {
// ensure cage sets the settle prices right
function cageSetup() public returns (bytes32) {
mom.setCap(5 ether); // 5 sai debt ceiling
mark(1 ether); // price 1:1 gem:ref
mom.setMat(ray(2 ether)); // require 200% collat
tub.join(10 ether);
var cup = tub.open();
tub.lock(cup, 10 ether);
tub.draw(cup, 5 ether); // 200% collateralisation
return cup;
}
function testCageSafeOverCollat() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tap.woe(), 0); // no bad debt
assertEq(tub.pie(), 10 ether);
tub.join(20 ether); // give us some more skr
mark(1 ether);
top.cage();
assertEq(tub.din(), 5 ether); // debt remains in tub
assertEq(wad(top.fix()), 1 ether); // sai redeems 1:1 with gem
assertEq(wad(tub.fit()), 1 ether); // skr redeems 1:1 with gem just before pushing gem to tub
assertEq(gem.balanceOf(tap), 5 ether); // saved for sai
assertEq(gem.balanceOf(tub), 25 ether); // saved for skr
}
function testCageUnsafeOverCollat() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tub.per(), ray(1 ether));
tub.join(20 ether); // give us some more skr
var price = wdiv(3 ether, 4 ether);
mark(price);
top.cage(); // 150% collat
assertEq(top.fix(), rdiv(1 ether, price)); // sai redeems 4:3 with gem
assertEq(tub.fit(), ray(price)); // skr redeems 1:1 with gem just before pushing gem to tub
// gem needed for sai is 5 * 4 / 3
var saved = rmul(5 ether, rdiv(WAD, price));
assertEq(gem.balanceOf(tap), saved); // saved for sai
assertEq(gem.balanceOf(tub), 30 ether - saved); // saved for skr
}
function testCageAtCollat() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tub.per(), ray(1 ether));
var price = wdiv(1 ether, 2 ether); // 100% collat
mark(price);
top.cage();
assertEq(top.fix(), ray(2 ether)); // sai redeems 1:2 with gem, 1:1 with ref
assertEq(tub.per(), 0); // skr redeems 1:0 with gem after cage
}
function testCageAtCollatFreeSkr() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tub.per(), ray(1 ether));
tub.join(20 ether); // give us some more skr
var price = wdiv(1 ether, 2 ether); // 100% collat
mark(price);
top.cage();
assertEq(top.fix(), ray(2 ether)); // sai redeems 1:2 with gem, 1:1 with ref
assertEq(tub.fit(), ray(price)); // skr redeems 1:1 with gem just before pushing gem to tub
}
function testCageUnderCollat() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tub.per(), ray(1 ether));
var price = wdiv(1 ether, 4 ether); // 50% collat
mark(price);
top.cage();
assertEq(2 * sai.totalSupply(), gem.balanceOf(tap));
assertEq(top.fix(), ray(2 ether)); // sai redeems 1:2 with gem, 2:1 with ref
assertEq(tub.per(), 0); // skr redeems 1:0 with gem after cage
}
function testCageUnderCollatFreeSkr() public {
cageSetup();
assertEq(top.fix(), 0);
assertEq(tub.fit(), 0);
assertEq(tub.per(), ray(1 ether));
tub.join(20 ether); // give us some more skr
var price = wdiv(1 ether, 4 ether); // 50% collat
mark(price);
top.cage();
assertEq(4 * sai.totalSupply(), gem.balanceOf(tap));
assertEq(top.fix(), ray(4 ether)); // sai redeems 1:4 with gem, 1:1 with ref
}
function testCageNoSai() public {
var cup = cageSetup();
tub.wipe(cup, 5 ether);
assertEq(sai.totalSupply(), 0);
top.cage();
assertEq(top.fix(), ray(1 ether));
}
function testMock() public {
cageSetup();
top.cage();
gem.deposit.value(1000 ether)();
gem.approve(tap, uint(-1));
tap.mock(1000 ether);
assertEq(sai.balanceOf(this), 1005 ether);
assertEq(gem.balanceOf(tap), 1005 ether);
}
function testMockNoSai() public {
var cup = cageSetup();
tub.wipe(cup, 5 ether);
assertEq(sai.totalSupply(), 0);
top.cage();
gem.deposit.value(1000 ether)();
gem.approve(tap, uint(-1));
tap.mock(1000 ether);
assertEq(sai.balanceOf(this), 1000 ether);
assertEq(gem.balanceOf(tap), 1000 ether);
}
// ensure cash returns the expected amount
function testCashSafeOverCollat() public {
var cup = cageSetup();
mark(1 ether);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 90 ether);
assertEq(gem.balanceOf(tub), 5 ether);
assertEq(gem.balanceOf(tap), 5 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 95 ether);
assertEq(gem.balanceOf(tub), 5 ether);
assertEq(tub.ink(cup), 10 ether);
tub.bite(cup);
assertEq(tub.ink(cup), 5 ether);
tub.free(cup, tub.ink(cup));
assertEq(skr.balanceOf(this), 5 ether);
tap.vent();
top.flow();
tub.exit(uint256(skr.balanceOf(this)));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
assertEq(skr.totalSupply(), 0);
}
function testCashSafeOverCollatWithFreeSkr() public {
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
mark(1 ether);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 20 ether);
assertEq(gem.balanceOf(this), 70 ether);
assertEq(gem.balanceOf(tub), 25 ether);
assertEq(gem.balanceOf(tap), 5 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
tap.vent();
top.flow();
assertEq(skr.balanceOf(this), 25 ether);
tap.cash(sai.balanceOf(this));
tub.exit(uint256(skr.balanceOf(this)));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(sai.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tap.vent();
assertEq(sai.totalSupply(), 0);
assertEq(skr.totalSupply(), 0);
}
function testFailCashSafeOverCollatWithFreeSkrExitBeforeBail() public {
// fails because exit is before bail
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
mark(1 ether);
top.cage();
tap.cash(sai.balanceOf(this));
tub.exit(uint256(skr.balanceOf(this)));
assertEq(skr.balanceOf(this), 0 ether);
uint256 gemBySAI = 5 ether; // Adding 5 gem from 5 sai
uint256 gemBySKR = wdiv(wmul(20 ether, 30 ether - gemBySAI), 30 ether);
assertEq(gem.balanceOf(this), 70 ether + gemBySAI + gemBySKR);
assertEq(sai.balanceOf(this), 0);
assertEq(sai.totalSupply(), 0);
assertEq(sin.totalSupply(), 0);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
tap.vent();
top.flow();
assertEq(skr.balanceOf(this), 5 ether); // skr retrieved by bail(cup)
tub.exit(uint256(skr.balanceOf(this)));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(sai.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(tub), 0 ether);
assertEq(sai.totalSupply(), 0);
assertEq(sin.totalSupply(), 0);
assertEq(skr.totalSupply(), 0);
}
function testCashUnsafeOverCollat() public {
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
var price = wdiv(3 ether, 4 ether);
mark(price);
top.cage(); // 150% collat
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 20 ether);
assertEq(gem.balanceOf(this), 70 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
assertEq(skr.balanceOf(this), 20 ether);
uint256 gemBySAI = wdiv(wmul(5 ether, 4 ether), 3 ether);
uint256 gemBySKR = 0;
assertEq(gem.balanceOf(this), 70 ether + gemBySAI + gemBySKR);
assertEq(gem.balanceOf(tub), 30 ether - gemBySAI - gemBySKR);
// how much gem should be returned?
// there were 10 gems initially, of which 5 were 100% collat
// at the cage price, 5 * 4 / 3 are 100% collat,
// leaving 10 - 5 * 4 / 3 as excess = 3.333
// this should all be returned
var ink = tub.ink(cup);
var tab = tub.tab(cup);
var skrToRecover = sub(ink, wdiv(tab, price));
tub.bite(cup);
tub.free(cup, tub.ink(cup));
assertEq(skr.balanceOf(this), 20 ether + skrToRecover);
assertEq(skr.balanceOf(tub), 0 ether);
tap.vent();
top.flow();
tub.exit(uint256(skr.balanceOf(this)));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tap.vent();
assertEq(skr.totalSupply(), 0);
assertEq(sai.totalSupply(), 0);
}
function testCashAtCollat() public {
var cup = cageSetup();
var price = wdiv(1 ether, 2 ether); // 100% collat
mark(price);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 90 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
assertEq(skr.balanceOf(this), 0 ether);
var saved = rmul(5 ether, rdiv(WAD, price));
assertEq(gem.balanceOf(this), 90 ether + saved);
assertEq(gem.balanceOf(tub), 10 ether - saved);
// how much gem should be returned?
// none :D
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tap.vent();
assertEq(skr.totalSupply(), 0);
assertEq(sai.totalSupply(), 0);
}
function testCashAtCollatFreeSkr() public {
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
var price = wdiv(1 ether, 2 ether); // 100% collat
mark(price);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 20 ether);
assertEq(gem.balanceOf(this), 70 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
tap.vent();
top.flow();
tub.exit(uint256(skr.balanceOf(this)));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
assertEq(skr.totalSupply(), 0);
}
function testFailCashAtCollatFreeSkrExitBeforeBail() public {
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
var price = wdiv(1 ether, 2 ether); // 100% collat
mark(price);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 20 ether);
assertEq(gem.balanceOf(this), 70 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
tub.exit(uint256(skr.balanceOf(this)));
assertEq(skr.balanceOf(this), 0 ether);
var gemBySAI = wmul(5 ether, 2 ether);
var gemBySKR = wdiv(wmul(20 ether, 30 ether - gemBySAI), 30 ether);
assertEq(gem.balanceOf(this), 70 ether + gemBySAI + gemBySKR);
assertEq(gem.balanceOf(tub), 30 ether - gemBySAI - gemBySKR);
assertEq(sai.totalSupply(), 0);
assertEq(sin.totalSupply(), 0);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
tap.vent();
tub.exit(uint256(skr.balanceOf(this)));
// Cup did not have skr to free, then the ramaining gem in tub can not be shared as there is not more skr to exit
assertEq(gem.balanceOf(this), 70 ether + gemBySAI + gemBySKR);
assertEq(gem.balanceOf(tub), 30 ether - gemBySAI - gemBySKR);
assertEq(skr.totalSupply(), 0);
}
function testCashUnderCollat() public {
var cup = cageSetup();
var price = wdiv(1 ether, 4 ether); // 50% collat
mark(price);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(skr.balanceOf(this), 0 ether);
assertEq(gem.balanceOf(this), 90 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
assertEq(skr.balanceOf(this), 0 ether);
// get back all 10 gems, which are now only worth 2.5 ref
// so you've lost 50% on you sai
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
// how much gem should be returned?
// none :D
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
tap.vent();
assertEq(skr.totalSupply(), 0);
assertEq(sai.totalSupply(), 0);
}
function testCashUnderCollatFreeSkr() public {
var cup = cageSetup();
tub.join(20 ether); // give us some more skr
var price = wdiv(1 ether, 4 ether); // 50% collat
mark(price);
top.cage();
assertEq(sai.balanceOf(this), 5 ether);
assertEq(gem.balanceOf(this), 70 ether);
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0 ether);
// returns 20 gems, taken from the free skr,
// sai is made whole
assertEq(gem.balanceOf(this), 90 ether);
assertEq(skr.balanceOf(this), 20 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup));
tap.vent();
top.flow();
tub.exit(uint256(skr.balanceOf(this)));
assertEq(skr.balanceOf(this), 0 ether);
// the skr has taken a 50% loss - 10 gems returned from 20 put in
assertEq(gem.balanceOf(this), 100 ether);
assertEq(gem.balanceOf(tub), 0 ether);
assertEq(sai.totalSupply(), 0);
assertEq(skr.totalSupply(), 0);
}
function testCashSafeOverCollatAndMock() public {
testCashSafeOverCollat();
gem.approve(tap, uint(-1));
tap.mock(5 ether);
assertEq(sai.balanceOf(this), 5 ether);
assertEq(gem.balanceOf(this), 95 ether);
assertEq(gem.balanceOf(tap), 5 ether);
}
function testCashSafeOverCollatWithFreeSkrAndMock() public {
testCashSafeOverCollatWithFreeSkr();
gem.approve(tap, uint(-1));
tap.mock(5 ether);
assertEq(sai.balanceOf(this), 5 ether);
assertEq(gem.balanceOf(this), 95 ether);
assertEq(gem.balanceOf(tap), 5 ether);
}
function testFailCashSafeOverCollatWithFreeSkrExitBeforeBailAndMock() public {
testFailCashSafeOverCollatWithFreeSkrExitBeforeBail();
gem.approve(tap, uint(-1));
tap.mock(5 ether);
assertEq(sai.balanceOf(this), 5 ether);
assertEq(gem.balanceOf(this), 95 ether);
assertEq(gem.balanceOf(tap), 5 ether);
}
function testThreeCupsOverCollat() public {
var cup = cageSetup();
tub.join(90 ether); // give us some more skr
var cup2 = tub.open(); // open a new cup
tub.lock(cup2, 20 ether); // lock collateral but not draw DAI
var cup3 = tub.open(); // open a new cup
tub.lock(cup3, 20 ether); // lock collateral but not draw DAI
assertEq(gem.balanceOf(tap), 0);
assertEq(gem.balanceOf(tub), 100 ether);
assertEq(gem.balanceOf(this), 0);
assertEq(skr.balanceOf(this), 50 ether); // free skr
assertEq(skr.balanceOf(tub), 50 ether); // locked skr
uint256 price = 1 ether;
mark(price);
top.cage();
assertEq(gem.balanceOf(tap), 5 ether); // Needed to payout 5 sai
assertEq(gem.balanceOf(tub), 95 ether);
tub.bite(cup);
tub.free(cup, tub.ink(cup)); // 5 skr recovered, and 5 skr burnt
assertEq(skr.balanceOf(this), 55 ether); // free skr
assertEq(skr.balanceOf(tub), 40 ether); // locked skr
tub.bite(cup2);
tub.free(cup2, tub.ink(cup2)); // 20 skr recovered
assertEq(skr.balanceOf(this), 75 ether); // free skr
assertEq(skr.balanceOf(tub), 20 ether); // locked skr
tub.bite(cup3);
tub.free(cup3, tub.ink(cup3)); // 20 skr recovered
assertEq(skr.balanceOf(this), 95 ether); // free skr
assertEq(skr.balanceOf(tub), 0); // locked skr
tap.cash(sai.balanceOf(this));
assertEq(sai.balanceOf(this), 0);
assertEq(gem.balanceOf(this), 5 ether);
tap.vent();
top.flow();
tub.exit(uint256(skr.balanceOf(this))); // exit 95 skr at price 95/95
assertEq(gem.balanceOf(tub), 0);
assertEq(gem.balanceOf(tap), 0);
assertEq(gem.balanceOf(this), 100 ether);
assertEq(skr.totalSupply(), 0);
}
function testThreeCupsAtCollat() public {
var cup = cageSetup();
tub.join(90 ether); // give us some more skr
var cup2 = tub.open(); // open a new cup
tub.lock(cup2, 20 ether); // lock collateral but not draw DAI
var cup3 = tub.open(); // open a new cup
tub.lock(cup3, 20 ether); // lock collateral but not draw DAI
assertEq(gem.balanceOf(tap), 0);
assertEq(gem.balanceOf(tub), 100 ether);
assertEq(gem.balanceOf(this), 0);
assertEq(skr.balanceOf(this), 50 ether); // free skr
assertEq(skr.balanceOf(tub), 50 ether); // locked skr