/
TestReferenceCosigner.js
1964 lines (1678 loc) · 64.8 KB
/
TestReferenceCosigner.js
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
const ReferenceCosigner = artifacts.require('./examples/ReferenceCosigner.sol');
const DebtEngine = artifacts.require('./diaspore/DebtEngine.sol');
const LoanManager = artifacts.require('./diaspore/LoanManager.sol');
const TestModel = artifacts.require('./diaspore/utils/test/TestModel.sol');
const TestToken = artifacts.require('./utils/test/TestToken.sol');
const TestReferenceCosigner = artifacts.require('./utils/test/TestReferenceCosigner.sol');
const TestLoanManagerForReferenceCosigner = artifacts.require('./utils/test/TestLoanManagerForReferenceCosigner.sol');
const TestRateOracle = artifacts.require('./utils/test/TestRateOracle.sol');
const Helper = require('./Helper.js');
const Web3Utils = require('web3-utils');
const BigNumber = web3.BigNumber;
require('chai')
.use(require('chai-bignumber')(BigNumber))
.should();
function bn (number) {
if (typeof number != 'string') {
number = number.toString();
}
return new BigNumber(number);
}
function maxUint (base) {
return bn('2').pow(bn(base)).sub(bn('1'));
}
contract('Test ReferenceCosigner Diaspore', function (accounts) {
const owner = accounts[9];
let rcn;
let debtEngine;
let loanManager;
let model;
let cosigner;
let testCosigner;
let testLoanManager;
let oracle;
let cosignerEvents;
function getAllEvents (contract) {
function toEvent (crudeEvent) {
return {
name: crudeEvent.name,
inputs: crudeEvent.inputs,
topic: Web3Utils.soliditySha3(crudeEvent.name + '(' + crudeEvent.inputs.map(x => x.type) + ')'),
};
}
const crudeEvents = contract.abi.filter(x => x.type === 'event');
return crudeEvents.map(x => toEvent(x));
}
async function toDataRequestCosign (cosignerContract, loanManagerContract, id, cost, coverage, requiredArrears, expiration, signer) {
const hashDataSignature = await cosignerContract.hashDataSignature(
loanManagerContract.address,
id,
cost,
coverage,
requiredArrears,
expiration
);
const signature = await web3.eth.sign(signer, hashDataSignature).slice(2);
const r = signature.slice(0, 64);
const s = signature.slice(64, 128);
const v = Web3Utils.toHex(web3.toDecimal(signature.slice(128, 130)) + 27).slice(2);
return (await cosignerContract.encodeData(cost, coverage, requiredArrears, expiration)) + v + r + s;
};
before('Create contracts', async function () {
rcn = await TestToken.new({ from: owner });
debtEngine = await DebtEngine.new(rcn.address, { from: owner });
loanManager = await LoanManager.new(debtEngine.address, { from: owner });
testLoanManager = await TestLoanManagerForReferenceCosigner.new({ from: owner });
model = await TestModel.new({ from: owner });
await model.setEngine(debtEngine.address, { from: owner });
oracle = await TestRateOracle.new({ from: owner });
testCosigner = await TestReferenceCosigner.new(rcn.address, { from: owner });
cosigner = await ReferenceCosigner.new(rcn.address, { from: owner });
cosignerEvents = getAllEvents(cosigner);
assert.equal(await cosigner.rcn(), rcn.address);
});
beforeEach('Clean', async function () {
async function resetBal (spender) {
if ((await rcn.balanceOf(spender)).toString() !== '0') {
await rcn.setBalance(spender, bn('0'));
}
}
async function resetApprove (spender, sender) {
if ((await rcn.allowance(sender, spender)).toString() !== '0') {
await rcn.approve(spender, bn('0'), { from: sender });
}
}
for (let i = 0; i < accounts.length; i++) {
await cosigner.removeDelegate(accounts[i], { from: owner });
await testCosigner.removeDelegate(accounts[i], { from: owner });
await resetBal(accounts[i]);
await resetApprove(loanManager.address, accounts[i]);
await resetApprove(testLoanManager.address, accounts[i]);
await resetApprove(debtEngine.address, accounts[i]);
await resetApprove(cosigner.address, accounts[i]);
}
await resetBal(cosigner.address);
await resetBal(loanManager.address);
await resetBal(debtEngine.address);
(await rcn.totalSupply()).should.be.bignumber.equal(bn('0'));
});
describe('Helper contract', async () => {
const signer = accounts[7];
it('Should encode and decode data and signature', async () => {
const cost = bn('1000');
const coverage = bn('6000');
const requiredArrears = bn('666');
const expiration = bn(await Helper.getBlockTime());
const id = bn('51651');
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
cost,
coverage,
requiredArrears,
expiration,
signer
);
const decodedData = await testCosigner.decodeCosignerData(data);
(decodedData[0]).should.be.bignumber.equal(cost);
(decodedData[1]).should.be.bignumber.equal(coverage);
(decodedData[2]).should.be.bignumber.equal(requiredArrears);
(decodedData[3]).should.be.bignumber.equal(expiration);
const msg = await cosigner.hashDataSignature(loanManager.address, id, cost, coverage, requiredArrears, expiration);
const signature = await web3.eth.sign(signer, msg).slice(2);
const r = signature.slice(0, 64);
const s = signature.slice(64, 128);
const v = web3.toDecimal(signature.slice(128, 130)) + 27;
assert.equal(data.slice(-130, -128), Web3Utils.toHex(v).slice(-2));
assert.equal(data.slice(-128, -64), r);
assert.equal(data.slice(-64), s);
});
it('Try decode a cosign data with invalid length', async () => {
const cost = bn('1000');
const coverage = bn('6000');
const requiredArrears = bn('5652');
const expiration = bn(await Helper.getBlockTime());
const id = bn('51651');
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
cost,
coverage,
requiredArrears,
expiration,
signer
);
await Helper.tryCatchRevert(
() => testCosigner.decodeCosignerData(
data + '00'
),
'Invalid data length'
);
await Helper.tryCatchRevert(
() => testCosigner.decodeCosignerData(
data.slice(0, 4)
),
'Invalid data length'
);
});
it('Should encode data', async () => {
const maxData = await cosigner.encodeData(
maxUint('128'),
maxUint('16'),
maxUint('64'),
maxUint('64')
);
assert.equal(maxData.slice(2, 34), 'ffffffffffffffffffffffffffffffff');
assert.equal(maxData.slice(34, 38), 'ffff');
assert.equal(maxData.slice(38, 54), 'ffffffffffffffff');
assert.equal(maxData.slice(54, 70), 'ffffffffffffffff');
const cost = bn('265987451657894324156789798132164544191');
const coverage = bn('59842');
const requiredArrears = bn('226547874984564984');
const expiration = bn('12559879816516548948');
const data = await cosigner.encodeData(
cost,
coverage,
requiredArrears,
expiration
);
assert.equal(data.slice(2, 34), 'c81b51de46c3dd16602ebe13a15f46bf');
assert.equal(data.slice(34, 38), 'e9c2');
assert.equal(data.slice(38, 54), '0324dc178d1318f8');
assert.equal(data.slice(54, 70), 'ae4da825e5113954');
});
});
describe('function url and setUrl', async () => {
it('Should set url', async () => {
const url = 'https://rcn.cosigner/';
assert.equal(await cosigner.url(), '');
const SetUrl = await Helper.toEvent(
cosigner.setUrl(url, { from: owner }),
'SetUrl'
);
assert.equal(SetUrl._url, url);
assert.equal(await cosigner.url(), url);
});
it('Try set url without ownership', async () => {
await Helper.tryCatchRevert(
() => cosigner.setUrl('TEST', { from: accounts[8] }),
'The owner should be the sender'
);
});
});
describe('function cost', async () => {
const signer = accounts[7];
it('Consult cost of data', async () => {
const cost = bn('1222');
const coverage = bn('6000');
const requiredArrears = bn('512');
const expiration = bn(await Helper.getBlockTime());
const id = bn('898');
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
cost,
coverage,
requiredArrears,
expiration,
signer
);
const costData = await cosigner.cost(
Helper.address0x,
bn('0'),
data,
Helper.address0x
);
costData.should.be.bignumber.equal(cost);
});
});
describe('function requestCosign', async () => {
const signer = accounts[7];
it('Request a cosign on a loan', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('1031230');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6000');
const requiredArrears = ('524');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
const tx = await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
const eventTopic = cosignerEvents.find(x => x.name === 'Cosign').topic;
const event = tx.receipt.logs.find(x => x.topics.some(y => y === eventTopic));
assert.equal(event.topics[1], id);
assert.equal(
event.data.slice(2, 130),
Helper.toBytes32(loanManager.address).slice(2) +
Helper.toBytes32(signer).slice(2)
);
const liability = await cosigner.liabilities(loanManager.address, id);
liability[0].should.be.bignumber.equal(coverage);
liability[1].should.be.bignumber.equal(requiredArrears);
});
it('Try requestCosign with different _loanManager parameter and msg.sender', async () => {
const borrower = accounts[1];
const amount = bn('66666');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6000');
const requiredArrears = bn('500');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await Helper.tryCatchRevert(
() => cosigner.requestCosign(
accounts[5],
id,
data,
'',
{ from: accounts[4] }
),
'The msg.sender should be the loanManager'
);
});
it('Try cosign two times the same loan', async () => {
const id = bn('98489498');
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
testLoanManager,
id,
bn('0'),
bn('6000'),
await Helper.getBlockTime() + 500,
await Helper.getBlockTime() + 1000,
signer
);
await testLoanManager.requestCosign(
id,
cosigner.address,
data
);
await Helper.tryCatchRevert(
() => testLoanManager.requestCosign(
id,
cosigner.address,
data
),
'The liability exist'
);
});
it('Try cosign an expired data', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('66451');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6000');
const requiredArrears = bn('662');
const expirationCosign = bn('0');
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await Helper.tryCatchRevert(
() => loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
),
'The data of requestCosign its expired'
);
});
it('Try cosign a loan with coverage equal 0', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('66451');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('662'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('0');
const requiredArrears = bn('666');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await Helper.tryCatchRevert(
() => loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
),
'The coverage should not be 0'
);
});
it('Try cosign a loan without being a delegate', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('698452');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6552');
const requiredArrears = bn('6632');
const expirationCosign = await Helper.getBlockTime() + 1000;
const noSigner = accounts[2];
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
noSigner
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await Helper.tryCatchRevert(
() => loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
),
'The signer its not a delegate'
);
});
it('Try request cosign and the cosign function return false', async () => {
const id = bn('382137');
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
testLoanManager,
id,
bn('1'),
bn('5561'),
await Helper.getBlockTime() + 500,
await Helper.getBlockTime() + 1000,
signer
);
await Helper.tryCatchRevert(
() => testLoanManager.requestCosign(
id,
cosigner.address,
data
),
'Fail loanManager cosign'
);
});
});
describe('function isDefaulted', async () => {
const signer = accounts[7];
it('The liability should not be defaulted', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('98875');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6562');
const requiredArrears = bn('500');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
assert.equal(await cosigner.isDefaulted(loanManager.address, id), false);
});
it('The liability should not be defaulted (liability non-expired and status paid)', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('98875');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('223'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6562');
const requiredArrears = bn('500');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
await rcn.setBalance(borrower, amount.plus(amount));
await rcn.approve(debtEngine.address, amount.plus(amount), { from: borrower });
await debtEngine.pay(
id,
amount.plus(amount),
borrower,
[],
{ from: borrower }
);
(await model.getStatus(id)).should.be.bignumber.equal(bn('2'));
assert.equal(await cosigner.isDefaulted(loanManager.address, id), false);
});
it('The liability should not be defaulted (liability expired and status paid)', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('98875');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6562');
const requiredArrears = bn('33');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
await rcn.setBalance(borrower, amount.plus(amount));
await rcn.approve(debtEngine.address, amount.plus(amount), { from: borrower });
await debtEngine.pay(
id,
amount.plus(amount),
borrower,
[],
{ from: borrower }
);
(await model.getStatus(id)).should.be.bignumber.equal(bn('2'));
await Helper.increaseTime(3000);
assert.equal(await cosigner.isDefaulted(loanManager.address, id), false);
});
it('The liability should be defaulted (liability expired and status non-paid)', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('98875');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6562');
const requiredArrears = bn('500');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
await Helper.increaseTime(3000);
assert.equal(await cosigner.isDefaulted(loanManager.address, id), true);
});
});
describe('function _currencyToToken', async () => {
it('Use RCN Token as currency', async () => {
const amount = bn('516516');
const tokens = await testCosigner.currencyToToken(
Helper.address0x,
amount,
[]
);
tokens.should.be.bignumber.equal(amount);
});
it('Use ARS as currency', async () => {
const ars = bn('5516512');
const amount = bn('4');
const equivalent = bn('8');
const oracleData = await oracle.encodeRate(amount, equivalent);
const constTokens = ars.mul(amount).div(equivalent);
const rcnTokens = await testCosigner.currencyToToken(
oracle.address,
ars,
oracleData
);
rcnTokens.should.be.bignumber.equal(constTokens);
});
});
describe('function claim', async () => {
const signer = accounts[7];
it('Should claim a liability', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('626232');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'
))._id;
const costCosign = bn('1222');
const coverage = bn('6562');
const requiredArrears = bn('500');
const expirationCosign = await Helper.getBlockTime() + 1000;
await cosigner.addDelegate(signer, { from: owner });
const data = await toDataRequestCosign(
cosigner,
loanManager,
id,
costCosign,
coverage,
requiredArrears,
expirationCosign,
signer
);
await rcn.setBalance(lender, amount.plus(costCosign));
await rcn.approve(loanManager.address, amount.plus(costCosign), { from: lender });
await loanManager.lend(
id,
[],
cosigner.address,
costCosign,
data,
{ from: lender }
);
await Helper.increaseTime(3000);
await rcn.setBalance(cosigner.address, amount);
await debtEngine.approve(cosigner.address, id, { from: lender });
const prevCosignerBal = await rcn.balanceOf(cosigner.address);
const prevLenderBal = await rcn.balanceOf(lender);
const events = await Helper.toEvent(
cosigner.claim(
loanManager.address,
id,
'',
{ from: lender }
),
'Claim',
'Received'
);
const calcClaimAmount = coverage.mul(await loanManager.getClosingObligation(id)).dividedToIntegerBy(bn('10000'));
const Claim = events[0];
assert.equal(Claim._loanManager, loanManager.address);
assert.equal(Claim._index, id);
assert.equal(Claim._sender, lender);
Claim._claimAmount.should.be.bignumber.equal(calcClaimAmount);
assert.equal(Claim._oracleData, '0x');
const Received = events[1];
assert.equal(Received._operator, cosigner.address);
assert.equal(Received._from, lender);
Received._id.should.be.bignumber.equal(id);
assert.equal(Received._data, '0x');
const liability = await cosigner.liabilities(loanManager.address, id);
liability[0].should.be.bignumber.equal(bn('0'));
liability[1].should.be.bignumber.equal(requiredArrears);
assert.equal(await debtEngine.ownerOf(id), cosigner.address);
(await rcn.balanceOf(cosigner.address)).should.be.bignumber.equal(prevCosignerBal.sub(calcClaimAmount));
(await rcn.balanceOf(lender)).should.be.bignumber.equal(prevLenderBal.plus(calcClaimAmount));
});
it('Try claim a non-existent liability', async () => {
await Helper.tryCatchRevert(
() => cosigner.claim(
loanManager.address,
bn('25131651'),
'',
{ from: accounts[2] }
),
'The liability not exists'
);
});
it('Try claim a liability with borrower as msg.sender', async () => {
const borrower = accounts[1];
const lender = accounts[2];
const amount = bn('626232');
const expirationLoan = await Helper.getBlockTime() + 1000;
const id = (await Helper.toEvent(
loanManager.requestLoan(
amount,
model.address,
Helper.address0x,
borrower,
bn('1'),
expirationLoan,
await model.encodeData(amount, expirationLoan),
{ from: borrower }
),
'Requested'