-
Notifications
You must be signed in to change notification settings - Fork 122
/
feature_loan_payback_with_collateral.py
executable file
·851 lines (687 loc) · 28.3 KB
/
feature_loan_payback_with_collateral.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
#!/usr/bin/env python3
# Copyright (c) 2014-2019 The Bitcoin Core developers
# Copyright (c) DeFi Blockchain Developers
# Distributed under the MIT software license, see the accompanying
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
"""Test Loan - Payback with collateral."""
from test_framework.test_framework import DefiTestFramework
from test_framework.util import assert_equal, assert_raises_rpc_error
from decimal import ROUND_UP, ROUND_DOWN, Decimal
import calendar
import time
BLOCKS_PER_YEAR = Decimal(1051200)
class LoanPaybackWithCollateralTest(DefiTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.setup_clean_chain = True
self.extra_args = [
[
"-txnotokens=0",
"-amkheight=1",
"-bayfrontheight=1",
"-eunosheight=1",
"-fortcanningheight=1",
"-fortcanningmuseumheight=1",
"-fortcanninghillheight=1",
"-fortcanningroadheight=1",
"-fortcanningspringheight=1",
"-fortcanningcrunchheight=1",
"-fortcanninggreatworldheight=1",
"-fortcanningepilogueheight=1",
"-jellyfish_regtest=1",
"-simulatemainnet=1",
]
]
def createOracles(self):
self.oracle_address1 = self.nodes[0].getnewaddress("", "legacy")
price_feeds = [
{"currency": "USD", "token": "DFI"},
{"currency": "USD", "token": "DUSD"},
{"currency": "USD", "token": "TSLA"},
]
self.oracle_id1 = self.nodes[0].appointoracle(
self.oracle_address1, price_feeds, 10
)
self.nodes[0].generate(1)
# feed oracle
oracle_prices = [
{"currency": "USD", "tokenAmount": "1@TSLA"},
{"currency": "USD", "tokenAmount": "1@DUSD"},
{"currency": "USD", "tokenAmount": "10@DFI"},
]
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(self.oracle_id1, timestamp, oracle_prices)
self.oracle_address2 = self.nodes[0].getnewaddress("", "legacy")
self.oracle_id2 = self.nodes[0].appointoracle(
self.oracle_address2, price_feeds, 10
)
self.nodes[0].generate(1)
# feed oracle
timestamp = calendar.timegm(time.gmtime())
self.nodes[0].setoracledata(self.oracle_id2, timestamp, oracle_prices)
self.nodes[0].generate(120)
def setup(self):
self.nodes[0].generate(120)
self.createOracles()
self.mn_address = self.nodes[0].get_genesis_keys().ownerAuthAddress
self.account0 = self.nodes[0].getnewaddress()
self.collateralAmount = 2000
self.loanAmount = 1000
self.symbolDFI = "DFI"
self.symbolTSLA = "TSLA"
self.symbolDUSD = "DUSD"
self.nodes[0].setloantoken(
{
"symbol": "DUSD",
"name": "DUSD",
"fixedIntervalPriceId": "DUSD/USD",
"mintable": True,
"interest": 0,
}
)
self.nodes[0].generate(120)
self.idDUSD = list(self.nodes[0].gettoken(self.symbolDUSD).keys())[0]
# Mint DUSD
self.nodes[0].minttokens("100000@DUSD")
self.nodes[0].generate(1)
# Create DFI tokens
self.nodes[0].utxostoaccount({self.mn_address: "100000@" + self.symbolDFI})
self.nodes[0].generate(1)
# Create pool pair
self.nodes[0].createpoolpair(
{
"tokenA": self.symbolDFI,
"tokenB": self.symbolDUSD,
"commission": 0,
"status": True,
"ownerAddress": self.mn_address,
}
)
self.nodes[0].generate(1)
# Add pool liquidity
self.nodes[0].addpoolliquidity(
{self.mn_address: ["10000@" + self.symbolDFI, "8000@" + self.symbolDUSD]},
self.mn_address,
)
self.nodes[0].generate(1)
self.nodes[0].setloantoken(
{
"symbol": "TSLA",
"name": "TSLA",
"fixedIntervalPriceId": "TSLA/USD",
"mintable": True,
"interest": 0,
}
)
self.nodes[0].generate(1)
# Create loan scheme
self.nodes[0].createloanscheme(150, 1, "LOAN001")
self.nodes[0].generate(1)
# Set collateral tokens
self.nodes[0].setcollateraltoken(
{"token": self.symbolDFI, "factor": 1, "fixedIntervalPriceId": "DFI/USD"}
)
self.nodes[0].setcollateraltoken(
{"token": self.symbolDUSD, "factor": 1, "fixedIntervalPriceId": "DUSD/USD"}
)
self.nodes[0].generate(120)
self.nodes[0].accounttoaccount(
self.mn_address,
{self.account0: str(self.collateralAmount) + "@" + self.symbolDUSD},
)
self.nodes[0].accounttoaccount(
self.mn_address,
{self.account0: str(self.collateralAmount) + "@" + self.symbolDFI},
)
self.nodes[0].generate(1)
self.nodes[0].setgov(
{
"ATTRIBUTES": {
"v0/token/" + self.idDUSD + "/loan_payback_collateral": "true"
}
}
)
self.nodes[0].generate(1)
vault_address = self.nodes[0].getnewaddress()
self.vaultId = self.nodes[0].createvault(vault_address, "LOAN001")
self.nodes[0].generate(1)
def test_guards(self):
height = self.nodes[0].getblockcount()
self.nodes[0].setgov(
{
"ATTRIBUTES": {
"v0/token/" + self.idDUSD + "/loan_payback_collateral": "false"
}
}
)
self.nodes[0].generate(1)
assert_raises_rpc_error(
-32600,
"Payback of DUSD loan with collateral is not currently active",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
self.nodes[0].setgov(
{
"ATTRIBUTES": {
"v0/token/" + self.idDUSD + "/loan_payback_collateral": "true"
}
}
)
self.nodes[0].generate(1)
assert_raises_rpc_error(
-32600,
"Vault has no collaterals",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
# Deposit DUSD and DFI to vault
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
assert_raises_rpc_error(
-32600,
"Vault does not have any DUSD collaterals",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
assert_raises_rpc_error(
-32600,
"Vault has no loans",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
# take TSLA loan
self.nodes[0].takeloan(
{"vaultId": self.vaultId, "amounts": "1@" + self.symbolTSLA}
)
self.nodes[0].generate(1)
assert_raises_rpc_error(
-32600,
"Vault does not have any DUSD loans",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
self.rollback_to(height)
def test_guard_against_liquidation(self):
height = self.nodes[0].getblockcount()
self.nodes[0].setgov(
{"ATTRIBUTES": {f"v0/token/{self.idDUSD}/loan_collateral_factor": "1.49"}}
)
self.nodes[0].generate(1)
# Check results
attributes = self.nodes[0].getgov("ATTRIBUTES")["ATTRIBUTES"]
assert_equal(attributes["v0/token/1/loan_collateral_factor"], "1.49")
# Deposit DUSD and DFI to vault
self.nodes[0].deposittovault(
self.vaultId, self.account0, "151@" + self.symbolDFI
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId, self.account0, "1000@" + self.symbolDUSD
)
self.nodes[0].generate(1)
self.nodes[0].takeloan(
{"vaultId": self.vaultId, "amounts": "1900@" + self.symbolTSLA}
)
self.nodes[0].generate(1)
self.nodes[0].takeloan(
{"vaultId": self.vaultId, "amounts": "100@" + self.symbolDUSD}
)
self.nodes[0].generate(1)
self.nodes[0].setgov(
{
"ATTRIBUTES": {
f"v0/token/{self.idDUSD}/loan_minting_interest": "50000000"
}
}
)
self.nodes[0].generate(
2
) # accrue enough interest to drop below collateralization ratio
assert_raises_rpc_error(
-32600,
"Vault does not have enough collateralization ratio defined by loan scheme - 143 < 150",
self.nodes[0].paybackwithcollateral,
self.vaultId,
)
self.rollback_to(height)
def test_collaterals_greater_than_loans(self, payback_with_collateral):
height = self.nodes[0].getblockcount()
collateralDUSDAmount = 2000
loanDUSDAmount = 1000
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
# Take DUSD loan
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
vaultBefore = self.nodes[0].getvault(self.vaultId)
[collateralAmountBefore, _] = vaultBefore["collateralAmounts"][1].split("@")
[loanAmountBefore, _] = vaultBefore["loanAmounts"][0].split("@")
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
[collateralAmount, _] = vault["collateralAmounts"][1].split("@")
assert not any(
"DUSD" in loan for loan in vault["loanAmounts"]
) # Payback all DUSD loans
assert not any(
"DUSD" in interest for interest in vault["interestAmounts"]
) # Payback all DUSD interests
assert_equal(
Decimal(collateralAmount),
Decimal(collateralAmountBefore) - Decimal(loanAmountBefore),
)
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(Decimal(storedInterest["interestPerBlock"]), Decimal(0))
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(mintedAmountBefore, mintedAmount + Decimal(loanAmountBefore))
self.rollback_to(height)
def test_loans_greater_than_collaterals(self, payback_with_collateral):
height = self.nodes[0].getblockcount()
collateralDUSDAmount = 1000
loanDUSDAmount = 1100
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
# Take DUSD loan
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000010464231354642313546")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
vaultBefore = self.nodes[0].getvault(self.vaultId)
[loanAmountBefore, _] = vaultBefore["loanAmounts"][0].split("@")
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
[interestAmount, _] = vault["interestAmounts"][0].split("@")
[loanAmount, _] = vault["loanAmounts"][0].split("@")
assert not any(
"DUSD" in collateral for collateral in vault["collateralAmounts"]
) # Used all DUSD collateral
assert_equal(
Decimal(loanAmount),
Decimal(loanAmountBefore)
- Decimal(collateralDUSDAmount)
+ Decimal(interestAmount),
)
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000000951293859113394216")
assert_equal(
Decimal(interestAmount),
Decimal(storedInterest["interestPerBlock"]).quantize(
Decimal("1E-8"), rounding=ROUND_UP
),
)
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(mintedAmountBefore, mintedAmount + collateralDUSDAmount)
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"][0], vaultBefore["interestAmounts"][0])
self.rollback_to(height)
def test_loans_equal_to_collaterals(self, payback_with_collateral):
height = self.nodes[0].getblockcount()
expected_IPB = 0.00000952
loanDUSDAmount = 1000
collateralDUSDAmount = loanDUSDAmount + expected_IPB
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
) # Deposit enough to match amount of loans + interest after one block
self.nodes[0].generate(1)
# Take DUSD loan
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000009512937595129375951")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
vaultBefore = self.nodes[0].getvault(self.vaultId)
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
assert not any(
"DUSD" in loan for loan in vault["loanAmounts"]
) # Payback all DUSD loans
assert not any(
"DUSD" in interest for interest in vault["interestAmounts"]
) # Payback all DUSD interests
assert not any(
"DUSD" in collateral for collateral in vault["collateralAmounts"]
) # Used all DUSD collateral
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(Decimal(storedInterest["interestPerBlock"]), Decimal(0))
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(
mintedAmountBefore,
Decimal(mintedAmount + Decimal(collateralDUSDAmount)).quantize(
Decimal("1E-8"), rounding=ROUND_DOWN
),
)
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"][0], vaultBefore["interestAmounts"][0])
self.rollback_to(height)
def test_interest_greater_than_collaterals(self, payback_with_collateral):
height = self.nodes[0].getblockcount()
collateralDUSDAmount = 0.000001
loanDUSDAmount = 1000
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000009512937595129375951")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
vaultBefore = self.nodes[0].getvault(self.vaultId)
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
[interestAmount, _] = vault["interestAmounts"][0].split("@")
assert not any(
"DUSD" in collateral for collateral in vault["collateralAmounts"]
) # Used all DUSD collateral
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000009512937595129375951")
assert_equal(
Decimal(storedInterest["interestToHeight"]),
Decimal("0.000008512937595129375951"),
)
assert_equal(
Decimal(interestAmount),
Decimal(
Decimal(storedInterest["interestPerBlock"]) * 2
- Decimal(collateralDUSDAmount)
).quantize(Decimal("1E-8"), rounding=ROUND_UP),
)
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(mintedAmountBefore, mintedAmount + Decimal(collateralDUSDAmount))
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"][0], vaultBefore["collateralAmounts"][1])
self.rollback_to(height)
def test_interest_equal_to_collaterals(self, payback_with_collateral):
height = self.nodes[0].getblockcount()
expected_IPB = 0.00000952
loanDUSDAmount = 1000
collateralDUSDAmount = expected_IPB
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
) # Deposit enough to match amount of interest after one block
self.nodes[0].generate(1)
# Take DUSD loan
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000009512937595129375951")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
vaultBefore = self.nodes[0].getvault(self.vaultId)
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
assert not any(
"DUSD" in collateral for collateral in vault["collateralAmounts"]
) # Used all DUSD collateral
assert_equal(vault["interestAmounts"], vaultBefore["interestAmounts"])
assert_equal(vault["loanAmounts"], vaultBefore["loanAmounts"])
assert_equal(
vault["collateralValue"],
float(vaultBefore["collateralValue"]) - expected_IPB,
)
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "0.000009512937595129375951")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(
mintedAmountBefore,
Decimal(mintedAmount + Decimal(collateralDUSDAmount)).quantize(
Decimal("1E-8"), rounding=ROUND_DOWN
),
)
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"][0], vaultBefore["interestAmounts"][0])
self.rollback_to(height)
def test_negative_interest_collaterals_greater_than_loans(
self, payback_with_collateral
):
height = self.nodes[0].getblockcount()
collateralDUSDAmount = 1000
loanDUSDAmount = 1000
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
self.nodes[0].setgov(
{"ATTRIBUTES": {f"v0/token/{self.idDUSD}/loan_minting_interest": "-500"}}
)
self.nodes[0].generate(1)
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
# accrue negative interest
self.nodes[0].generate(5)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
vault = self.nodes[0].getvault(self.vaultId)
[DUSDInterestAmount, _] = vault["interestAmounts"][0].split("@")
[DUSDloanAmount, _] = vault["loanAmounts"][0].split("@")
assert Decimal(DUSDInterestAmount) < 0
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "-0.004746955859969558599695")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
# collateral amount should be equal to the opposite of DUSDInterestAmount
[DUSDCollateralAmount, _] = vault["collateralAmounts"][1].split("@")
assert_equal(Decimal(DUSDCollateralAmount), Decimal(DUSDInterestAmount) * -1)
assert not any(
"DUSD" in loan for loan in vault["loanAmounts"]
) # Paid back all DUSD loans
assert not any(
"DUSD" in interest for interest in vault["interestAmounts"]
) # Paid back all DUSD interests
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(Decimal(storedInterest["interestPerBlock"]), Decimal("0"))
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal("0"))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(mintedAmountBefore, mintedAmount + Decimal(DUSDloanAmount))
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"], []) # no burn on negative interest
self.rollback_to(height)
def test_negative_interest_loans_greater_than_collaterals(
self, payback_with_collateral
):
height = self.nodes[0].getblockcount()
collateralDUSDAmount = 1000
loanDUSDAmount = 1100
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(self.collateralAmount) + "@" + self.symbolDFI,
)
self.nodes[0].generate(1)
self.nodes[0].deposittovault(
self.vaultId,
self.account0,
str(collateralDUSDAmount) + "@" + self.symbolDUSD,
)
self.nodes[0].generate(1)
self.nodes[0].setgov(
{"ATTRIBUTES": {f"v0/token/{self.idDUSD}/loan_minting_interest": "-500"}}
)
self.nodes[0].generate(1)
self.nodes[0].takeloan(
{
"vaultId": self.vaultId,
"amounts": str(loanDUSDAmount) + "@" + self.symbolDUSD,
}
)
self.nodes[0].generate(1)
# accrue negative interest
self.nodes[0].generate(5)
mintedAmountBefore = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD][
"minted"
]
vaultBefore = self.nodes[0].getvault(self.vaultId)
[DUSDLoanAmountBefore, _] = vaultBefore["loanAmounts"][0].split("@")
[DUSDInterestAmount, _] = vaultBefore["interestAmounts"][0].split("@")
assert Decimal(DUSDInterestAmount) < 0
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(storedInterest["interestPerBlock"], "-0.005221651445966514459665")
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal(0))
payback_with_collateral()
self.nodes[0].generate(1)
vault = self.nodes[0].getvault(self.vaultId)
[DUSDLoanAmount, _] = vault["loanAmounts"][0].split("@")
[DUSDInterestAmount, _] = vault["interestAmounts"][0].split("@")
assert_equal(
Decimal(DUSDLoanAmount),
Decimal(DUSDLoanAmountBefore)
- Decimal(collateralDUSDAmount)
+ Decimal(DUSDInterestAmount),
)
assert not any(
"DUSD" in collateral for collateral in vault["collateralAmounts"]
) # Used all DUSD collateral
storedInterest = self.nodes[0].getstoredinterest(self.vaultId, self.idDUSD)
assert_equal(
Decimal(storedInterest["interestPerBlock"]),
Decimal("-0.000474546864344558599695"),
)
assert_equal(Decimal(storedInterest["interestToHeight"]), Decimal("0"))
mintedAmount = self.nodes[0].gettoken(self.symbolDUSD)[self.idDUSD]["minted"]
assert_equal(mintedAmountBefore, mintedAmount + collateralDUSDAmount)
burnInfo = self.nodes[0].getburninfo()
assert_equal(burnInfo["paybackburn"], []) # no burn on negative interest
self.rollback_to(height)
def run_test(self):
self.setup()
self.test_guards()
self.test_guard_against_liquidation()
payback_fns = [
lambda: self.nodes[0].paybackwithcollateral(self.vaultId),
lambda: self.nodes[0].paybackloan(
{
"vaultId": self.vaultId,
"from": self.account0,
"amounts": "9999999999.99999999@" + self.symbolDUSD,
}
),
]
for payback_fn in payback_fns:
self.test_collaterals_greater_than_loans(payback_fn)
self.test_loans_greater_than_collaterals(payback_fn)
self.test_loans_equal_to_collaterals(payback_fn)
self.test_interest_greater_than_collaterals(payback_fn)
self.test_interest_equal_to_collaterals(payback_fn)
self.test_negative_interest_collaterals_greater_than_loans(payback_fn)
self.test_negative_interest_loans_greater_than_collaterals(payback_fn)
if __name__ == "__main__":
LoanPaybackWithCollateralTest().main()