forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_EtherNomin.py
1142 lines (904 loc) · 51.3 KB
/
test_EtherNomin.py
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
import unittest
from utils.deployutils import W3, UNIT, MASTER, DUMMY, ETHER
from utils.deployutils import compile_contracts, attempt_deploy, mine_tx
from utils.deployutils import take_snapshot, restore_snapshot, fast_forward
from utils.testutils import assertReverts, block_time, send_value, get_eth_balance
from utils.testutils import generate_topic_event_map, get_event_data_from_log
ETHERNOMIN_SOURCE = "tests/contracts/PublicEtherNomin.sol"
FAKECOURT_SOURCE = "tests/contracts/FakeCourt.sol"
def setUpModule():
print("Testing EtherNomin...")
def tearDownModule():
print()
class TestEtherNomin(unittest.TestCase):
def setUp(self):
self.snapshot = take_snapshot()
# Reset the price at the start of tests so that it's never stale.
self.updatePrice(self.oracle(), self.etherPrice())
# Reset the liquidation timestamp so that it's never active.
owner = self.owner()
self.forceLiquidation(owner)
self.terminateLiquidation(owner)
def tearDown(self):
restore_snapshot(self.snapshot)
@classmethod
def setUpClass(cls):
cls.assertReverts = assertReverts
compiled = compile_contracts([ETHERNOMIN_SOURCE, FAKECOURT_SOURCE],
remappings=['""=contracts'])
cls.nomin_abi = compiled['PublicEtherNomin']['abi']
cls.nomin_event_dict = generate_topic_event_map(cls.nomin_abi)
cls.nomin_havven = W3.eth.accounts[1]
cls.nomin_oracle = W3.eth.accounts[2]
cls.nomin_beneficiary = W3.eth.accounts[3]
cls.nomin_owner = W3.eth.accounts[0]
cls.nomin, cls.construction_txr = attempt_deploy(compiled, 'PublicEtherNomin', MASTER,
[cls.nomin_havven, cls.nomin_oracle, cls.nomin_beneficiary,
1000 * UNIT, cls.nomin_owner])
cls.construction_price_time = cls.nomin.functions.lastPriceUpdate().call()
cls.fake_court, _ = attempt_deploy(compiled, 'FakeCourt', MASTER, [])
cls.fake_court.setNomin = lambda sender, new_nomin: mine_tx(cls.fake_court.functions.setNomin(new_nomin).transact({'from': sender}))
cls.fake_court.setConfirming = lambda sender, target, status: mine_tx(cls.fake_court.functions.setConfirming(target, status).transact({'from': sender}))
cls.fake_court.setVotePasses = lambda sender, target, status: mine_tx(cls.fake_court.functions.setVotePasses(target, status).transact({'from': sender}))
cls.fake_court.confiscateBalance = lambda sender, target: mine_tx(cls.fake_court.functions.confiscateBalance(target).transact({'from': sender}))
cls.fake_court.setNomin(W3.eth.accounts[0], cls.nomin.address)
mine_tx(cls.nomin.functions.setCourt(cls.fake_court.address).transact({'from': cls.nomin_owner}))
cls.owner = lambda self: cls.nomin.functions.owner().call()
cls.oracle = lambda self: cls.nomin.functions.oracle().call()
cls.court = lambda self: cls.nomin.functions.court().call()
cls.beneficiary = lambda self: cls.nomin.functions.beneficiary().call()
cls.nominPool = lambda self: cls.nomin.functions.nominPool().call()
cls.poolFeeRate = lambda self: cls.nomin.functions.poolFeeRate().call()
cls.liquidationPeriod = lambda self: cls.nomin.functions.liquidationPeriod().call()
cls.liquidationTimestamp = lambda self: cls.nomin.functions.liquidationTimestamp().call()
cls.etherPrice = lambda self: cls.nomin.functions.etherPrice().call()
cls.isFrozen = lambda self, address: cls.nomin.functions.isFrozen(address).call()
cls.lastPriceUpdate = lambda self: cls.nomin.functions.lastPriceUpdate().call()
cls.stalePeriod = lambda self: cls.nomin.functions.stalePeriod().call()
cls.setOwner = lambda self, sender, address: mine_tx(cls.nomin.functions.setOwner(address).transact({'from': sender}))
cls.setOracle = lambda self, sender, address: mine_tx(cls.nomin.functions.setOracle(address).transact({'from': sender}))
cls.setCourt = lambda self, sender, address: mine_tx(cls.nomin.functions.setCourt(address).transact({'from': sender}))
cls.setBeneficiary = lambda self, sender, address: mine_tx(cls.nomin.functions.setBeneficiary(address).transact({'from': sender}))
cls.setPoolFeeRate = lambda self, sender, rate: mine_tx(cls.nomin.functions.setPoolFeeRate(rate).transact({'from': sender}))
cls.updatePrice = lambda self, sender, price: mine_tx(cls.nomin.functions.updatePrice(price).transact({'from': sender}))
cls.setStalePeriod = lambda self, sender, period: mine_tx(cls.nomin.functions.setStalePeriod(period).transact({'from': sender}))
cls.fiatValue = lambda self, eth: cls.nomin.functions.fiatValue(eth).call()
cls.fiatBalance = lambda self: cls.nomin.functions.fiatBalance().call()
cls.collateralisationRatio = lambda self: cls.nomin.functions.collateralisationRatio().call()
cls.etherValue = lambda self, fiat: cls.nomin.functions.etherValue(fiat).call()
cls.etherValueAllowStale = lambda self, fiat: cls.nomin.functions.publicEtherValueAllowStale(fiat).call()
cls.poolFeeIncurred = lambda self, n: cls.nomin.functions.poolFeeIncurred(n).call()
cls.purchaseCostFiat = lambda self, n: cls.nomin.functions.purchaseCostFiat(n).call()
cls.purchaseCostEther = lambda self, n: cls.nomin.functions.purchaseCostEther(n).call()
cls.saleProceedsFiat = lambda self, n: cls.nomin.functions.saleProceedsFiat(n).call()
cls.saleProceedsEther = lambda self, n: cls.nomin.functions.saleProceedsEther(n).call()
cls.saleProceedsEtherAllowStale = lambda self, n: cls.nomin.functions.publicSaleProceedsEtherAllowStale(n).call()
cls.priceIsStale = lambda self: cls.nomin.functions.priceIsStale().call()
cls.isLiquidating = lambda self: cls.nomin.functions.isLiquidating().call()
cls.canSelfDestruct = lambda self: cls.nomin.functions.canSelfDestruct().call()
cls.transferPlusFee = lambda self, value: cls.nomin.functions.transferPlusFee(value).call()
cls.transfer = lambda self, sender, recipient, value: mine_tx(cls.nomin.functions.transfer(recipient, value).transact({'from': sender}))
cls.transferFrom = lambda self, sender, fromAccount, to, value: mine_tx(cls.nomin.functions.transferFrom(fromAccount, to, value).transact({'from': sender}))
cls.approve = lambda self, sender, spender, value: mine_tx(cls.nomin.functions.approve(spender, value).transact({'from': sender}))
cls.issue = lambda self, sender, n, value: mine_tx(cls.nomin.functions.issue(n).transact({'from': sender, 'value': value}))
cls.burn = lambda self, sender, n: mine_tx(cls.nomin.functions.burn(n).transact({'from': sender}))
cls.buy = lambda self, sender, n, value: mine_tx(cls.nomin.functions.buy(n).transact({'from': sender, 'value': value}))
cls.sell = lambda self, sender, n: mine_tx(cls.nomin.functions.sell(n).transact({'from': sender, 'gasPrice': 10}))
cls.forceLiquidation = lambda self, sender: mine_tx(cls.nomin.functions.forceLiquidation().transact({'from': sender}))
cls.liquidate = lambda self, sender: mine_tx(cls.nomin.functions.liquidate().transact({'from': sender}))
cls.extendLiquidationPeriod = lambda self, sender, extension: mine_tx(cls.nomin.functions.extendLiquidationPeriod(extension).transact({'from': sender}))
cls.terminateLiquidation = lambda self, sender: mine_tx(cls.nomin.functions.terminateLiquidation().transact({'from': sender}))
cls.selfDestruct = lambda self, sender: mine_tx(cls.nomin.functions.selfDestruct().transact({'from': sender}))
cls.confiscateBalance = lambda self, sender, target: mine_tx(cls.nomin.functions.confiscateBalance(target).transact({'from': sender}))
cls.unfreezeAccount = lambda self, sender, target: mine_tx(cls.nomin.functions.unfreezeAccount(target).transact({'from': sender}))
cls.name = lambda self: cls.nomin.functions.name().call()
cls.symbol = lambda self: cls.nomin.functions.symbol().call()
cls.totalSupply = lambda self: cls.nomin.functions.totalSupply().call()
cls.balanceOf = lambda self, account: cls.nomin.functions.balanceOf(account).call()
cls.transferFeeRate = lambda self: cls.nomin.functions.transferFeeRate().call()
cls.feePool = lambda self: cls.nomin.functions.feePool().call()
cls.feeAuthority = lambda self: cls.nomin.functions.feeAuthority().call()
cls.debugWithdrawAllEther = lambda self, sender, recipient: mine_tx(cls.nomin.functions.debugWithdrawAllEther(recipient).transact({'from': sender}))
cls.debugEmptyFeePool = lambda self, sender: mine_tx(cls.nomin.functions.debugEmptyFeePool().transact({'from': sender}))
cls.debugFreezeAccount = lambda self, sender, target: mine_tx(cls.nomin.functions.debugFreezeAccount(target).transact({'from': sender}))
def test_constructor(self):
# Nomin-specific members
self.assertEqual(self.owner(), self.nomin_owner)
self.assertEqual(self.oracle(), self.nomin_oracle)
self.assertEqual(self.beneficiary(), self.nomin_beneficiary)
self.assertEqual(self.etherPrice(), 1000 * UNIT)
self.assertEqual(self.stalePeriod(), 2 * 24 * 60 * 60) # default two days
self.assertEqual(self.liquidationTimestamp(), 2**256 - 1)
self.assertEqual(self.liquidationPeriod(), 90 * 24 * 60 * 60) # default ninety days
self.assertEqual(self.poolFeeRate(), UNIT / 200) # default fifty basis points
self.assertEqual(self.nominPool(), 0)
construct_time = block_time(self.construction_txr.blockNumber)
self.assertEqual(construct_time, self.construction_price_time)
self.assertTrue(self.isFrozen(self.nomin.address))
# ERC20FeeToken members
self.assertEqual(self.name(), "Ether-Backed USD Nomins")
self.assertEqual(self.symbol(), "eUSD")
self.assertEqual(self.totalSupply(), 0)
self.assertEqual(self.balanceOf(MASTER), 0)
self.assertEqual(self.transferFeeRate(), 2 * UNIT // 1000)
self.assertEqual(self.feeAuthority(), self.nomin_havven)
def test_getSetOwner(self):
pre_owner = self.owner()
new_owner = DUMMY
# Only the owner must be able to set the oracle.
self.assertReverts(self.setOwner, new_owner, new_owner)
self.setOwner(pre_owner, new_owner)
self.assertEqual(self.owner(), new_owner)
def test_getSetOracle(self):
pre_oracle = self.oracle()
new_oracle = DUMMY
# Only the owner must be able to set the oracle.
self.assertReverts(self.setOracle, new_oracle, new_oracle)
self.setOracle(self.owner(), new_oracle)
self.assertEqual(self.oracle(), new_oracle)
def test_getSetCourt(self):
new_court = DUMMY
# Only the owner must be able to set the court.
self.assertReverts(self.setOracle, new_court, new_court)
self.setCourt(self.owner(), new_court)
self.assertEqual(self.court(), new_court)
def test_getSetBeneficiary(self):
new_beneficiary = DUMMY
# Only the owner must be able to set the beneficiary.
self.assertReverts(self.setBeneficiary, new_beneficiary, new_beneficiary)
self.setBeneficiary(self.owner(), new_beneficiary)
self.assertEqual(self.beneficiary(), new_beneficiary)
def test_getSetPoolFeeRate(self):
owner = self.owner()
new_rate = UNIT // 10
# Only the owner must be able to set the pool fee rate.
self.assertReverts(self.setPoolFeeRate, DUMMY, new_rate)
# Pool fee rate must be no greater than UNIT.
self.assertReverts(self.setPoolFeeRate, owner, UNIT + 1)
self.assertReverts(self.setPoolFeeRate, owner, 2**256 - 1)
self.setPoolFeeRate(owner, UNIT)
self.assertEqual(self.poolFeeRate(), UNIT)
self.setPoolFeeRate(owner, new_rate)
self.assertEqual(self.poolFeeRate(), new_rate)
def test_getSetStalePeriod(self):
owner = self.owner()
new_period = 52 * 7 * 24 * 60 * 60
# Only the owner should be able to set the pool fee rate.
self.assertReverts(self.setStalePeriod, DUMMY, new_period)
self.setStalePeriod(owner, new_period)
self.assertEqual(self.stalePeriod(), new_period)
def test_updatePrice(self):
owner = self.owner()
pre_price = self.etherPrice()
new_price = 10**8 * UNIT # one hundred million dollar ethers $$$$$$
new_price2 = UNIT // 10**6 # one ten thousandth of a cent ethers :(
pre_oracle = self.oracle()
new_oracle = DUMMY
# Only the oracle must be able to set the current price.
self.assertReverts(self.updatePrice, new_oracle, new_price)
# Check if everything works with nothing in the pool.
tx_receipt = self.updatePrice(pre_oracle, new_price)
tx_time = W3.eth.getBlock(tx_receipt.blockNumber)['timestamp']
self.assertEqual(self.lastPriceUpdate(), tx_time)
self.assertEqual(self.etherPrice(), new_price)
self.setOracle(owner, new_oracle)
self.assertReverts(self.updatePrice, pre_oracle, pre_price)
tx_receipt = self.updatePrice(new_oracle, new_price2)
tx_time = W3.eth.getBlock(tx_receipt.blockNumber)['timestamp']
self.assertEqual(self.lastPriceUpdate(), tx_time)
self.assertEqual(self.etherPrice(), new_price2)
# Check if everything works with something in the pool.
self.updatePrice(new_oracle, UNIT)
backing = self.etherValue(10 * UNIT)
self.issue(owner, UNIT, backing)
tx_receipt = self.updatePrice(new_oracle, pre_price)
tx_time = W3.eth.getBlock(tx_receipt.blockNumber)['timestamp']
self.assertEqual(self.lastPriceUpdate(), tx_time)
self.assertEqual(self.etherPrice(), pre_price)
def test_fiatValue(self):
oracle = self.oracle()
self.updatePrice(oracle, UNIT)
self.assertEqual(self.fiatValue(ETHER), ETHER)
self.assertEqual(self.fiatValue(777 * ETHER), 777 * ETHER)
self.assertEqual(self.fiatValue(ETHER // 777), ETHER // 777)
self.assertEqual(self.fiatValue(10**8 * ETHER), 10**8 * ETHER)
self.assertEqual(self.fiatValue(ETHER // 10**12), ETHER // 10**12)
self.updatePrice(oracle, 10**8 * UNIT)
self.assertEqual(self.fiatValue(ETHER), 10**8 * ETHER)
self.assertEqual(self.fiatValue(317 * ETHER), 317 * 10**8 * ETHER)
self.assertEqual(self.fiatValue(ETHER // 317), 10**8 * (ETHER // 317))
self.assertEqual(self.fiatValue(10**8 * ETHER), 10**16 * ETHER)
self.assertEqual(self.fiatValue(ETHER // 10**12), ETHER // 10**4)
self.updatePrice(oracle, UNIT // 10**12)
self.assertEqual(self.fiatValue(ETHER), ETHER // 10**12)
self.assertEqual(self.fiatValue(10**15 * ETHER), 10**3 * ETHER)
self.assertEqual(self.fiatValue((7 * ETHER) // 3), ((7 * ETHER) // 3) // 10**12)
def test_fiatBalance(self):
owner = self.owner()
oracle = self.oracle()
pre_price = self.etherPrice()
send_value(owner, self.nomin.address, ETHER)
self.assertEqual(self.fiatBalance(), pre_price)
self.updatePrice(oracle, UNIT // 10**12)
self.assertEqual(self.fiatBalance(), UNIT // 10**12)
self.updatePrice(oracle, 300 * UNIT)
self.assertEqual(self.fiatBalance(), 300 * UNIT)
send_value(owner, self.nomin.address, ETHER)
self.assertEqual(self.fiatBalance(), 600 * UNIT)
def test_etherValue(self):
oracle = self.oracle()
self.updatePrice(oracle, UNIT)
self.assertEqual(self.etherValue(UNIT), ETHER)
self.assertEqual(self.etherValue(777 * UNIT), 777 * ETHER)
self.assertEqual(self.etherValue(UNIT // 777), ETHER // 777)
self.assertEqual(self.etherValue(10**8 * UNIT), 10**8 * ETHER)
self.assertEqual(self.etherValue(UNIT // 10**12), ETHER // 10**12)
self.updatePrice(oracle, 10 * UNIT)
self.assertEqual(self.etherValue(UNIT), ETHER // 10)
self.assertEqual(self.etherValue(2 * UNIT), ETHER // 5)
for v in [0.0004, 2.1, 1, 49994, 49.29384, 0.00000028, 1235759872, 2.5 * 10**25]:
vi = int(v * UNIT)
self.assertEqual(self.etherValue(vi), self.etherValueAllowStale(vi))
def test_collateralisationRatio(self):
owner = self.owner()
oracle = self.oracle()
# When there are no nomins in the contract, a zero denominator causes reversion.
self.assertReverts(self.collateralisationRatio)
# Set the ether price to $1, and issue one nomin against 2 ether.
self.updatePrice(oracle, UNIT)
self.issue(owner, UNIT, 2 * ETHER)
self.assertEqual(self.collateralisationRatio(), 2 * UNIT)
# Set the ether price to $2, now the collateralisation ratio should double to 4.
self.updatePrice(oracle, 2 * UNIT)
self.assertEqual(self.collateralisationRatio(), 4 * UNIT)
# Now set the ether price to 50 cents, so that the collateralisation is exactly 1
self.updatePrice(oracle, UNIT // 2)
# (this should not trigger liquidation)
self.assertFalse(self.isLiquidating())
self.assertEqual(self.collateralisationRatio(), UNIT)
# Now double the ether in the contract to 2.
send_value(owner, self.nomin.address, 2 * ETHER)
self.assertEqual(self.collateralisationRatio(), 2 * UNIT)
def test_poolFeeIncurred(self):
poolFeeRate = self.poolFeeRate()
self.assertEqual(self.poolFeeIncurred(0), 0)
self.assertEqual(self.poolFeeIncurred(UNIT), poolFeeRate)
self.assertEqual(self.poolFeeIncurred(10 * UNIT), 10 * poolFeeRate)
self.assertEqual(self.poolFeeIncurred(UNIT // 2), poolFeeRate // 2)
self.setPoolFeeRate(self.owner(), UNIT // 10**7)
self.assertEqual(self.poolFeeIncurred(UNIT), UNIT // 10**7)
self.assertEqual(self.poolFeeIncurred(100 * UNIT), UNIT // 10**5)
self.assertEqual(self.poolFeeIncurred(UNIT // 2), UNIT // (2 * 10**7))
def test_purchaseCostFiat(self):
owner = self.owner()
poolFeeRate = self.poolFeeRate()
self.assertGreater(self.purchaseCostFiat(UNIT), UNIT)
self.assertGreater(self.purchaseCostFiat(UNIT), self.saleProceedsFiat(UNIT))
self.assertEqual(self.purchaseCostFiat(0), 0)
self.assertEqual(self.purchaseCostFiat(UNIT), UNIT + poolFeeRate)
self.assertEqual(self.purchaseCostFiat(10 * UNIT), 10 * (UNIT + poolFeeRate))
self.assertEqual(self.purchaseCostFiat(UNIT // 2), (UNIT + poolFeeRate) // 2)
self.setPoolFeeRate(owner, UNIT // 10**7)
self.assertEqual(self.purchaseCostFiat(UNIT), (UNIT + UNIT // 10**7))
self.assertEqual(self.purchaseCostFiat(100 * UNIT), 100 * (UNIT + UNIT // 10**7))
self.assertEqual(self.purchaseCostFiat(UNIT // 2), (UNIT + UNIT // 10**7) // 2)
self.setPoolFeeRate(owner, poolFeeRate)
def test_purchaseCostEther(self):
owner = self.owner()
oracle = self.oracle()
poolFeeRate = self.poolFeeRate()
self.assertGreater(self.purchaseCostEther(UNIT), self.etherValue(UNIT))
self.assertGreater(self.purchaseCostEther(UNIT), self.saleProceedsEther(UNIT))
self.assertEqual(self.purchaseCostEther(0), 0)
self.updatePrice(oracle, UNIT)
self.assertEqual(self.purchaseCostEther(UNIT), UNIT + poolFeeRate)
self.assertEqual(self.purchaseCostEther(UNIT // 2), (UNIT + poolFeeRate) // 2)
self.setPoolFeeRate(owner, UNIT // 10**7)
self.assertEqual(self.purchaseCostEther(UNIT), (UNIT + UNIT // 10**7))
self.assertEqual(self.purchaseCostEther(100 * UNIT), 100 * (UNIT + UNIT // 10**7))
self.setPoolFeeRate(owner, poolFeeRate)
self.updatePrice(oracle, UNIT // 2)
self.assertEqual(self.purchaseCostEther(UNIT // 2), UNIT + poolFeeRate)
self.assertEqual(self.purchaseCostEther(3 * UNIT), 6 * (UNIT + poolFeeRate))
def test_purchaseCostEtherShoppingSpree(self):
owner = self.owner()
oracle = self.oracle()
self.issue(owner, 800000 * UNIT, 8 * 10**9 * UNIT)
price_multiples = [12398, 1.2384889, 7748.22, 0.238838, 0.00049944, 5.7484, 87.2211111]
qty_multiples = [2.3, 84.4828, 284.10002, 0.4992, 105.289299991, 7.651948, 0.01, 100000]
total_qty = 0
total_cost = 0
pre_balance = get_eth_balance(owner)
for price_mult in price_multiples:
for qty_mult in qty_multiples:
price = int(price_mult * UNIT)
qty = int(qty_mult * UNIT)
total_qty += qty
self.updatePrice(oracle, price)
cost = self.purchaseCostEther(qty)
total_cost += cost
self.buy(owner, qty, cost)
self.assertEqual(self.balanceOf(owner), total_qty)
# We assert only almost equal because we're ignoring gas costs.
self.assertAlmostEqual((pre_balance - get_eth_balance(owner)) / UNIT, total_cost / UNIT)
def test_saleProceedsFiat(self):
owner = self.owner()
poolFeeRate = self.poolFeeRate()
self.assertLess(self.saleProceedsFiat(UNIT), UNIT)
self.assertLess(self.saleProceedsFiat(UNIT), self.purchaseCostFiat(UNIT))
self.assertEqual(self.saleProceedsFiat(0), 0)
self.assertEqual(self.saleProceedsFiat(UNIT), UNIT - poolFeeRate)
self.assertEqual(self.saleProceedsFiat(10 * UNIT), 10 * (UNIT - poolFeeRate))
self.assertEqual(self.saleProceedsFiat(UNIT // 2), (UNIT - poolFeeRate) // 2)
self.setPoolFeeRate(owner, UNIT // 10**7)
self.assertEqual(self.saleProceedsFiat(UNIT), (UNIT - UNIT // 10**7))
self.assertEqual(self.saleProceedsFiat(100 * UNIT), 100 * (UNIT - UNIT // 10**7))
self.assertEqual(self.saleProceedsFiat(UNIT // 2), (UNIT - UNIT // 10**7) // 2)
self.setPoolFeeRate(owner, poolFeeRate)
def test_saleProceedsEther(self):
owner = self.owner()
oracle = self.oracle()
poolFeeRate = self.poolFeeRate()
self.assertLess(self.saleProceedsEther(UNIT), self.etherValue(UNIT))
self.assertLess(self.saleProceedsEther(UNIT), self.purchaseCostEther(UNIT))
self.assertEqual(self.saleProceedsEther(0), 0)
self.updatePrice(oracle, UNIT)
self.assertEqual(self.saleProceedsEther(UNIT), UNIT - poolFeeRate)
self.assertEqual(self.saleProceedsEther(UNIT // 2), (UNIT - poolFeeRate) // 2)
self.setPoolFeeRate(owner, UNIT // 10**7)
self.assertEqual(self.saleProceedsEther(UNIT), (UNIT - UNIT // 10**7))
self.assertEqual(self.saleProceedsEther(100 * UNIT), 100 * (UNIT - UNIT // 10**7))
self.setPoolFeeRate(owner, poolFeeRate)
self.updatePrice(oracle, UNIT // 2)
self.assertEqual(self.saleProceedsEther(UNIT // 2), UNIT - poolFeeRate)
self.assertEqual(self.saleProceedsEther(3 * UNIT), 6 * (UNIT - poolFeeRate))
for v in [0.0004, 2.1, 1, 49994, 49.29384, 0.00000028, 1235759872, 2.5 * 10**25]:
vi = int(v * UNIT)
self.assertEqual(self.saleProceedsEther(vi), self.saleProceedsEtherAllowStale(vi))
def test_saleProceedsEtherBearMarket(self):
owner = self.owner()
oracle = self.oracle()
initial_qty = 800000 * UNIT
self.issue(owner, initial_qty, 8 * 10**9 * UNIT)
self.buy(owner, initial_qty, self.purchaseCostEther(initial_qty))
price_multiples = [12398, 1.2384889, 7748.22, 0.238838, 0.00049944, 5.7484, 87.2211111]
qty_multiples = [2.3, 84.4828, 284.10002, 0.4992, 105.289299991, 7.651948, 0.01, 100000]
total_qty = 0
total_proceeds = 0
pre_balance = get_eth_balance(owner)
for price_mult in price_multiples:
for qty_mult in qty_multiples:
price = int(price_mult * UNIT)
qty = int(qty_mult * UNIT)
total_qty += qty
self.updatePrice(oracle, price)
proceeds = self.saleProceedsEther(qty)
total_proceeds += proceeds
self.sell(owner, qty)
self.assertEqual(initial_qty - self.balanceOf(owner), total_qty)
# We assert only almost equal because we're ignoring gas costs.
self.assertAlmostEqual((get_eth_balance(owner) - pre_balance) / UNIT, total_proceeds / UNIT)
def test_priceIsStale(self):
oracle = self.oracle()
owner = self.owner()
stale_period = self.stalePeriod()
self.updatePrice(oracle, UNIT)
# Price is not stale immediately following an update.
self.assertFalse(self.priceIsStale())
# Price is not stale after part of the period has elapsed.
fast_forward(seconds=stale_period // 2)
self.assertFalse(self.priceIsStale())
# Price is not stale right up to just before the period has elapsed.
fast_forward(seconds=stale_period // 2 - 10)
self.assertFalse(self.priceIsStale())
# Price becomes stale immediately after the period has elapsed.
fast_forward(seconds=11)
self.assertTrue(self.priceIsStale())
# Price stays stale for ages.
fast_forward(seconds=100 * stale_period)
self.assertTrue(self.priceIsStale())
self.updatePrice(oracle, UNIT)
self.assertFalse(self.priceIsStale())
# Lengthening stale periods should not trigger staleness.
self.setStalePeriod(owner, 2 * stale_period)
self.assertFalse(self.priceIsStale())
# Shortening them to longer than the current elapsed period should not trigger staleness.
self.setStalePeriod(owner, stale_period)
self.assertFalse(self.priceIsStale())
# Shortening to shorter than the current elapsed period should trigger staleness.
fast_forward(seconds= 3 * stale_period // 4)
self.setStalePeriod(owner, stale_period // 2)
self.assertTrue(self.priceIsStale())
# Yet if we are able to update the stale period while the price is stale,
# we should be able to turn off staleness by extending the period.
# It's an interesting question of trust as to whether we should be able to do this, say if we
# do not have access to the oracle to send a price update. But as an owner, we could just
# reset the oracle address anyway, so we allow this.
self.setStalePeriod(owner, stale_period)
self.assertFalse(self.priceIsStale())
def test_staleness(self):
owner = self.owner()
oracle = self.oracle()
target = W3.eth.accounts[5]
# Set up target balance to be confiscatable for later testing.
self.assertEqual(self.court(), self.fake_court.address)
self.fake_court.setConfirming(owner, target, True)
self.fake_court.setVotePasses(owner, target, True)
# Create some nomins and set a convenient price.
self.updatePrice(oracle, UNIT)
pce = self.purchaseCostEther(UNIT)
self.issue(owner, 3 * UNIT, 7 * UNIT)
self.buy(owner, UNIT, pce)
# Enter stale period.
fast_forward(seconds=10*self.stalePeriod())
self.assertTrue(self.priceIsStale())
# These calls should work if the price is stale.
oracle = self.oracle()
court = self.court()
beneficiary = self.beneficiary()
poolFeeRate = self.poolFeeRate()
stalePeriod = self.stalePeriod()
self.nominPool()
self.liquidationPeriod()
self.liquidationTimestamp()
self.etherPrice()
self.lastPriceUpdate()
self.isFrozen(self.nomin.address)
self.setOracle(owner, oracle)
self.setCourt(owner, court)
self.setBeneficiary(owner, beneficiary)
self.setPoolFeeRate(owner, poolFeeRate)
self.setStalePeriod(owner, stalePeriod)
self.etherValueAllowStale(UNIT)
self.saleProceedsEtherAllowStale(UNIT)
self.poolFeeIncurred(UNIT)
self.purchaseCostFiat(UNIT)
self.saleProceedsFiat(UNIT)
self.priceIsStale()
self.isLiquidating()
self.assertFalse(self.isFrozen(MASTER))
self.transfer(MASTER, MASTER, 0)
self.transferFrom(MASTER, MASTER, MASTER, 0)
self.fake_court.confiscateBalance(owner, target)
self.unfreezeAccount(owner, target)
self.burn(owner, UNIT)
# These calls should not work when the price is stale.
# That they work when not stale is guaranteed by other tests, hopefully.
self.assertReverts(self.fiatValue, UNIT)
self.assertReverts(self.fiatBalance)
self.assertReverts(self.etherValue, UNIT)
self.assertReverts(self.collateralisationRatio)
self.assertReverts(self.purchaseCostEther, UNIT)
self.assertReverts(self.saleProceedsEther, UNIT)
self.assertGreater(get_eth_balance(owner), 7 * UNIT)
self.assertEqual(self.nominPool(), UNIT)
self.assertEqual(self.balanceOf(owner), UNIT)
self.assertReverts(self.issue, owner, UNIT, 5 * UNIT)
self.assertReverts(self.buy, owner, UNIT, pce)
self.assertReverts(self.sell, owner, UNIT)
# Liquidation things should work while stale...
self.forceLiquidation(owner)
self.assertTrue(self.isLiquidating())
self.extendLiquidationPeriod(owner, 1)
# ...except that we can't terminate liquidation unless the price is fresh.
self.assertReverts(self.terminateLiquidation, owner)
# Confirm that sell works regardless of staleness when in liquidation
self.sell(owner, UNIT)
# We can also burn under these conditions.
self.burn(owner, UNIT)
# Finally that updating the price gets us out of the stale period
self.updatePrice(oracle, UNIT)
self.assertFalse(self.priceIsStale())
self.terminateLiquidation(owner)
# And that self-destruction works during stale period.
self.forceLiquidation(owner)
fast_forward(seconds=self.stalePeriod() + self.liquidationPeriod())
self.assertTrue(self.isLiquidating())
self.assertTrue(self.priceIsStale())
self.selfDestruct(owner)
with self.assertRaises(Exception):
self.etherPrice()
def test_transfer(self):
owner = self.owner()
oracle = self.oracle()
target = W3.eth.accounts[1]
self.updatePrice(oracle, UNIT)
self.issue(owner, 10 * UNIT, 20 * ETHER)
ethercost = self.purchaseCostEther(10 * UNIT)
self.buy(owner, 10 * UNIT, ethercost)
self.assertEqual(self.balanceOf(owner), 10 * UNIT)
self.assertEqual(self.balanceOf(target), 0)
# Should be impossible to transfer to the nomin contract itself.
self.assertReverts(self.transfer, owner, self.nomin.address, UNIT)
self.transfer(owner, target, 5 * UNIT)
remainder = 10 * UNIT - self.transferPlusFee(5 * UNIT)
self.assertEqual(self.balanceOf(owner), remainder)
self.assertEqual(self.balanceOf(target), 5 * UNIT)
self.debugFreezeAccount(owner, target)
self.assertReverts(self.transfer, owner, target, UNIT)
self.assertReverts(self.transfer, target, owner, UNIT)
self.unfreezeAccount(owner, target)
qty = (5 * UNIT * UNIT) // self.transferPlusFee(UNIT) + 1
self.transfer(target, owner, qty)
self.assertEqual(self.balanceOf(owner), remainder + qty)
self.assertEqual(self.balanceOf(target), 0)
def test_transferFrom(self):
owner = self.owner()
oracle = self.oracle()
target = W3.eth.accounts[1]
proxy = W3.eth.accounts[2]
# Unauthorized transfers should not work
self.assertReverts(self.transferFrom, proxy, owner, target, UNIT)
# Neither should transfers that are too large for the allowance.
self.approve(owner, proxy, UNIT)
self.assertReverts(self.transferFrom, proxy, owner, target, 2 * UNIT)
self.approve(owner, proxy, 10000 * UNIT)
self.updatePrice(oracle, UNIT)
self.issue(owner, 10 * UNIT, 20 * ETHER)
ethercost = self.purchaseCostEther(10 * UNIT)
self.buy(owner, 10 * UNIT, ethercost)
self.assertEqual(self.balanceOf(owner), 10 * UNIT)
self.assertEqual(self.balanceOf(target), 0)
# Should be impossible to transfer to the nomin contract itself.
self.assertReverts(self.transferFrom, proxy, owner, self.nomin.address, UNIT)
self.transferFrom(proxy, owner, target, 5 * UNIT)
remainder = 10 * UNIT - self.transferPlusFee(5 * UNIT)
self.assertEqual(self.balanceOf(owner), remainder)
self.assertEqual(self.balanceOf(target), 5 * UNIT)
self.debugFreezeAccount(owner, target)
self.assertReverts(self.transferFrom, proxy, owner, target, UNIT)
self.assertReverts(self.transferFrom, proxy, target, owner, UNIT)
self.unfreezeAccount(owner, target)
qty = (5 * UNIT * UNIT) // self.transferPlusFee(UNIT) + 1
self.transfer(target, owner, qty)
self.assertEqual(self.balanceOf(owner), remainder + qty)
self.assertEqual(self.balanceOf(target), 0)
def test_issue(self):
owner = self.owner()
oracle = self.oracle()
# Only the contract owner should be able to issue new nomins.
self.updatePrice(oracle, UNIT)
self.assertReverts(self.issue, W3.eth.accounts[4], UNIT, 2 * ETHER)
self.assertEqual(self.totalSupply(), 0)
self.assertEqual(self.nominPool(), 0)
# Revert if less than 2x collateral is provided
self.assertReverts(self.issue, owner, UNIT, 2 * ETHER - 1)
# Issue a nomin into the pool
self.issue(owner, UNIT, 2 * ETHER)
self.assertEqual(self.totalSupply(), UNIT)
self.assertEqual(self.nominPool(), UNIT)
self.assertEqual(get_eth_balance(self.nomin.address), 2 * ETHER)
# Issuing more nomins should stack with existing supply
self.issue(owner, UNIT, 2 * ETHER)
self.assertEqual(self.totalSupply(), 2 * UNIT)
self.assertEqual(self.nominPool(), 2 * UNIT)
self.assertEqual(get_eth_balance(self.nomin.address), 4 * ETHER)
# Issue more into the pool for free if price goes up
self.updatePrice(oracle, 2 * UNIT)
self.assertFalse(self.isLiquidating())
self.assertReverts(self.issue, owner, 2 * UNIT + 1, 0)
self.issue(owner, 2 * UNIT, 0)
self.assertEqual(self.totalSupply(), 4 * UNIT)
self.assertEqual(self.nominPool(), 4 * UNIT)
self.assertEqual(get_eth_balance(self.nomin.address), 4 * ETHER)
# provide more than 2x collateral for new issuance if price drops
self.updatePrice(oracle, UNIT)
self.assertFalse(self.isLiquidating())
self.assertReverts(self.issue, owner, UNIT, 2 * ETHER)
self.assertReverts(self.issue, owner, UNIT, 6 * ETHER - 1)
self.issue(owner, UNIT, 6 * ETHER)
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 5 * UNIT)
self.assertEqual(get_eth_balance(self.nomin.address), 10 * ETHER)
def test_burn(self):
owner = self.owner()
oracle = self.oracle()
# issue some nomins to be burned
self.updatePrice(oracle, UNIT)
self.issue(owner, 10 * UNIT, 20 * ETHER)
# Only the contract owner should be able to burn nomins.
self.assertReverts(self.burn, W3.eth.accounts[4], UNIT)
# It should not be possible to burn more nomins than are in the pool.
self.assertReverts(self.burn, owner, 11 * UNIT)
# Burn part of the pool
self.assertEqual(self.totalSupply(), 10 * UNIT)
self.assertEqual(self.nominPool(), 10 * UNIT)
self.burn(owner, UNIT)
self.assertEqual(self.totalSupply(), 9 * UNIT)
self.assertEqual(self.nominPool(), 9 * UNIT)
# Burn the remainder of the pool
self.burn(owner, self.nominPool())
self.assertEqual(self.totalSupply(), 0)
self.assertEqual(self.nominPool(), 0)
def test_buy(self):
self.updatePrice(self.oracle(), UNIT)
buyer = W3.eth.accounts[4]
# Should not be possible to buy when there's no supply
cost = self.purchaseCostEther(UNIT)
self.assertReverts(self.buy, buyer, UNIT, cost)
# issue some nomins to be burned
self.issue(self.owner(), 5 * UNIT, 10 * ETHER)
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 5 * UNIT)
# Should not be able to purchase with the wrong quantity of ether.
self.assertReverts(self.buy, buyer, UNIT, cost + 1)
self.assertReverts(self.buy, buyer, UNIT, cost - 1)
self.assertEqual(self.balanceOf(buyer), 0)
self.buy(buyer, UNIT, cost)
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 4 * UNIT)
self.assertEqual(self.balanceOf(buyer), UNIT)
# It should not be possible to buy fewer nomins than the purchase minimum
purchaseMin = UNIT // 100
self.assertReverts(self.buy, buyer, purchaseMin - 1, self.purchaseCostEther(purchaseMin - 1))
# But it should be possible to buy exactly that quantity
self.buy(buyer, purchaseMin, self.purchaseCostEther(purchaseMin))
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 4 * UNIT - (UNIT // 100))
self.assertEqual(self.balanceOf(buyer), UNIT + UNIT // 100)
# It should not be possible to buy more tokens than are in the pool
total = self.nominPool()
self.assertReverts(self.buy, buyer, total + 1, self.purchaseCostEther(total + 1))
self.buy(buyer, total, self.purchaseCostEther(total))
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 0)
self.assertEqual(self.balanceOf(buyer), 5 * UNIT)
# Should not be possible to buy when there's nothing in the pool
self.assertReverts(self.buy, buyer, UNIT, self.purchaseCostEther(UNIT))
def test_sell(self):
# Prepare a seller who owns some nomins.
self.updatePrice(self.oracle(), UNIT)
seller = W3.eth.accounts[4]
self.issue(self.owner(), 5 * UNIT, 10 * ETHER)
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 5 * UNIT)
self.assertEqual(self.balanceOf(seller), 0)
# It should not be possible to sell nomins if you have none.
self.assertReverts(self.sell, seller, UNIT)
self.buy(seller, 5 * UNIT, self.purchaseCostEther(5 * UNIT))
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 0)
self.assertEqual(self.balanceOf(seller), 5 * UNIT)
# It should not be possible to sell more nomins than you possess.
self.assertReverts(self.sell, seller, 5 * UNIT + 1)
# Selling nomins should yield back the right amount of ether.
pre_balance = get_eth_balance(seller)
self.sell(seller, 2 * UNIT)
# This assertAlmostEqual hack is only because ganache refuses to be sensible about gas prices.
# The receipt refuses to include the gas price and the values appear are inconsistent anyway.
self.assertAlmostEqual(self.saleProceedsEther(2 * UNIT) / UNIT, (get_eth_balance(seller) - pre_balance) / UNIT)
self.assertEqual(self.totalSupply(), 5 * UNIT)
self.assertEqual(self.nominPool(), 2 * UNIT)
self.assertEqual(self.balanceOf(seller), 3 * UNIT)
def test_isLiquidating(self):
self.assertFalse(self.isLiquidating())
self.forceLiquidation(self.owner())
self.assertTrue(self.isLiquidating())
def test_forceLiquidation(self):
owner = self.owner()
# non-owners should not be able to force liquidation.
non_owner = W3.eth.accounts[6]
self.assertNotEqual(owner, non_owner)
self.assertReverts(self.forceLiquidation, non_owner)
self.assertFalse(self.isLiquidating())
tx_receipt = self.forceLiquidation(owner)
self.assertTrue(self.isLiquidating())
self.assertEqual(block_time(tx_receipt.blockNumber), self.liquidationTimestamp())
self.assertEqual(len(tx_receipt.logs), 1)
self.assertEqual(get_event_data_from_log(self.nomin_event_dict, tx_receipt.logs[0])['event'], "Liquidation")
# This call should not work if liquidation has begun.
self.assertReverts(self.forceLiquidation, owner)
def test_autoLiquidation(self):
owner = self.owner()
oracle = self.oracle()
# Do not liquidate if there's nothing in the pool.
self.updatePrice(oracle, UNIT // 10)
self.assertFalse(self.isLiquidating())
# Issue something so that we can liquidate.
self.updatePrice(oracle, UNIT)
self.issue(owner, UNIT, 2 * UNIT)
# Ordinary price updates don't cause liquidation.
self.updatePrice(oracle, UNIT // 2)
self.assertFalse(self.isLiquidating())
# Price updates inducing sub-unity collateralisation ratios cause liquidation.
tx_receipt = self.updatePrice(oracle, UNIT // 2 - 1)
self.assertTrue(self.isLiquidating())
self.assertEqual(len(tx_receipt.logs), 2)
price_update_log = get_event_data_from_log(self.nomin_event_dict, tx_receipt.logs[0])
liquidation_log = get_event_data_from_log(self.nomin_event_dict, tx_receipt.logs[1])
self.assertEqual(price_update_log['event'], 'PriceUpdated')
self.assertEqual(liquidation_log['event'], 'Liquidation')
# The auto liquidation check should do nothing when already under liquidation.
tx_receipt = self.updatePrice(oracle, UNIT // 3 - 1)
self.assertEqual(len(tx_receipt.logs), 1)
price_update_log = get_event_data_from_log(self.nomin_event_dict, tx_receipt.logs[0])
self.assertEqual(price_update_log['event'], 'PriceUpdated')
self.assertTrue(self.isLiquidating())
def test_extendLiquidationPeriod(self):
owner = self.owner()
# Only owner should be able to call this.
non_owner = W3.eth.accounts[6]
self.assertNotEqual(owner, non_owner)
self.assertReverts(self.forceLiquidation, non_owner)
ninetyDays = 90 * 24 * 60 * 60
oneEightyDays = 180 * 24 * 60 * 60
self.forceLiquidation(owner)
self.assertEqual(self.liquidationPeriod(), ninetyDays) # Default 90 days.
self.assertReverts(self.extendLiquidationPeriod, owner, 12309198139871)
self.extendLiquidationPeriod(owner, 1)
self.assertEqual(self.liquidationPeriod(), ninetyDays + 1)
self.extendLiquidationPeriod(owner, 12345)
self.assertEqual(self.liquidationPeriod(), ninetyDays + 12346)
self.extendLiquidationPeriod(owner, ninetyDays - 12346)
self.assertEqual(self.liquidationPeriod(), oneEightyDays)
self.assertReverts(self.extendLiquidationPeriod, owner, 1)
self.assertReverts(self.extendLiquidationPeriod, owner, 12309198139871)
def test_terminateLiquidation(self):
owner = self.owner()
oracle = self.oracle()
# Terminating liquidation should not work when not liquidating.
self.assertReverts(self.terminateLiquidation, owner)
self.forceLiquidation(owner)
# Only the owner should be able to terminate liquidation.
self.assertReverts(self.terminateLiquidation, oracle)
# Should be able to terminate liquidation if there is no supply.
tx_receipt = self.terminateLiquidation(owner)
self.assertEqual(self.liquidationTimestamp(), 2**256 - 1)
self.assertEqual(self.liquidationPeriod(), 90 * 24 * 60 * 60)
self.assertEqual(len(tx_receipt.logs), 1)
self.assertEqual(get_event_data_from_log(self.nomin_event_dict, tx_receipt.logs[0])['event'], "LiquidationTerminated")
# Should not be able to terminate liquidation if the supply is undercollateralised.
self.updatePrice(oracle, 2 * UNIT)
self.issue(owner, UNIT, UNIT)
self.updatePrice(oracle, UNIT - 1)
self.assertTrue(self.isLiquidating()) # Price update triggers liquidation.
self.assertReverts(self.terminateLiquidation, owner)
# But if the price recovers we should be fine to terminate.
self.updatePrice(oracle, UNIT)
self.terminateLiquidation(owner)
self.assertFalse(self.isLiquidating())
# And we should not be able to terminate liquidation if the price is stale.
self.forceLiquidation(owner)
fast_forward(seconds=2 * self.stalePeriod())
self.assertTrue(self.priceIsStale())
self.assertReverts(self.terminateLiquidation, owner)
def test_canSelfDestruct(self):
owner = self.owner()
oracle = self.oracle()
self.updatePrice(oracle, UNIT)
self.issue(owner, 2 * UNIT, 4 * UNIT)
self.buy(owner, UNIT, self.purchaseCostEther(UNIT))
self.assertFalse(self.canSelfDestruct())
self.forceLiquidation(owner)
# Not enough time elapsed.
self.assertFalse(self.canSelfDestruct())
fast_forward(seconds=self.liquidationPeriod() + 10)
self.assertTrue(self.canSelfDestruct())
self.updatePrice(oracle, UNIT)
self.terminateLiquidation(owner)
self.sell(owner, UNIT)
self.forceLiquidation(owner)
self.assertFalse(self.canSelfDestruct())
fast_forward(weeks=3)
self.assertTrue(self.canSelfDestruct())
def test_selfDestruct(self):
owner = self.owner()
oracle = self.oracle()
not_owner = W3.eth.accounts[5]
self.assertNotEqual(not_owner, owner)
# Buy some nomins so that we can't short circuit self-destruction.
self.updatePrice(oracle, UNIT)
self.issue(owner, UNIT, 2 * UNIT)
self.buy(owner, UNIT, self.purchaseCostEther(UNIT))
self.assertFalse(self.isLiquidating())
self.assertFalse(self.canSelfDestruct())