-
Notifications
You must be signed in to change notification settings - Fork 306
/
Liquity.test.ts
1408 lines (1093 loc) · 49.9 KB
/
Liquity.test.ts
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 chai, { expect, assert } from "chai";
import chaiAsPromised from "chai-as-promised";
import chaiSpies from "chai-spies";
import { AddressZero } from "@ethersproject/constants";
import { BigNumber } from "@ethersproject/bignumber";
import { Signer } from "@ethersproject/abstract-signer";
import { ethers, network, deployLiquity } from "hardhat";
import {
Decimal,
Decimalish,
Trove,
StabilityDeposit,
LiquityReceipt,
SuccessfulReceipt,
SentLiquityTransaction,
TroveCreationParams,
Fees,
LUSD_LIQUIDATION_RESERVE,
MAXIMUM_BORROWING_RATE,
MINIMUM_BORROWING_RATE,
LUSD_MINIMUM_DEBT,
LUSD_MINIMUM_NET_DEBT
} from "@liquity/lib-base";
import { HintHelpers } from "../types";
import {
PopulatableEthersLiquity,
PopulatedEthersLiquityTransaction,
_redeemMaxIterations
} from "../src/PopulatableEthersLiquity";
import { EthersTransactionReceipt } from "../src/types";
import { _LiquityDeploymentJSON } from "../src/contracts";
import { _connectToDeployment } from "../src/EthersLiquityConnection";
import { EthersLiquity } from "../src/EthersLiquity";
import { ReadableEthersLiquity } from "../src/ReadableEthersLiquity";
const provider = ethers.provider;
chai.use(chaiAsPromised);
chai.use(chaiSpies);
const STARTING_BALANCE = Decimal.from(100);
// Extra ETH sent to users to be spent on gas
const GAS_BUDGET = Decimal.from(0.1); // ETH
const getGasCost = (tx: EthersTransactionReceipt) => tx.gasUsed.mul(tx.effectiveGasPrice);
const connectToDeployment = async (
deployment: _LiquityDeploymentJSON,
signer: Signer,
frontendTag?: string
) =>
EthersLiquity._from(
_connectToDeployment(deployment, signer, {
userAddress: await signer.getAddress(),
frontendTag
})
);
const increaseTime = async (timeJumpSeconds: number) => {
await provider.send("evm_increaseTime", [timeJumpSeconds]);
};
function assertStrictEqual<T, U extends T>(
actual: T,
expected: U,
message?: string
): asserts actual is U {
assert.strictEqual(actual, expected, message);
}
function assertDefined<T>(actual: T | undefined): asserts actual is T {
assert(actual !== undefined);
}
const waitForSuccess = async <T extends LiquityReceipt>(
tx: Promise<SentLiquityTransaction<unknown, T>>
) => {
const receipt = await (await tx).waitForReceipt();
assertStrictEqual(receipt.status, "succeeded" as const);
return receipt as Extract<T, SuccessfulReceipt>;
};
// TODO make the testcases isolated
describe("EthersLiquity", () => {
let deployer: Signer;
let funder: Signer;
let user: Signer;
let otherUsers: Signer[];
let deployment: _LiquityDeploymentJSON;
let deployerLiquity: EthersLiquity;
let liquity: EthersLiquity;
let otherLiquities: EthersLiquity[];
const connectUsers = (users: Signer[]) =>
Promise.all(users.map(user => connectToDeployment(deployment, user)));
const openTroves = (users: Signer[], params: TroveCreationParams<Decimalish>[]) =>
params
.map((params, i) => () =>
Promise.all([
connectToDeployment(deployment, users[i]),
sendTo(users[i], params.depositCollateral).then(tx => tx.wait())
]).then(async ([liquity]) => {
await liquity.openTrove(params);
})
)
.reduce((a, b) => a.then(b), Promise.resolve());
const sendTo = (user: Signer, value: Decimalish, nonce?: number) =>
funder.sendTransaction({
to: user.getAddress(),
value: Decimal.from(value).add(GAS_BUDGET).hex,
nonce
});
const sendToEach = async (users: Signer[], value: Decimalish) => {
const txCount = await provider.getTransactionCount(funder.getAddress());
const txs = await Promise.all(users.map((user, i) => sendTo(user, value, txCount + i)));
// Wait for the last tx to be mined.
await txs[txs.length - 1].wait();
};
before(async () => {
[deployer, funder, user, ...otherUsers] = await ethers.getSigners();
deployment = await deployLiquity(deployer);
liquity = await connectToDeployment(deployment, user);
expect(liquity).to.be.an.instanceOf(EthersLiquity);
});
// Always setup same initial balance for user
beforeEach(async () => {
const targetBalance = BigNumber.from(STARTING_BALANCE.hex);
const gasLimit = BigNumber.from(21000);
const gasPrice = BigNumber.from(100e9); // 100 Gwei
const balance = await user.getBalance();
const txCost = gasLimit.mul(gasPrice);
if (balance.eq(targetBalance)) {
return;
}
if (balance.gt(targetBalance) && balance.lte(targetBalance.add(txCost))) {
await funder.sendTransaction({
to: user.getAddress(),
value: targetBalance.add(txCost).sub(balance).add(1),
gasLimit,
gasPrice
});
await user.sendTransaction({
to: funder.getAddress(),
value: 1,
gasLimit,
gasPrice
});
} else {
if (balance.lt(targetBalance)) {
await funder.sendTransaction({
to: user.getAddress(),
value: targetBalance.sub(balance),
gasLimit,
gasPrice
});
} else {
await user.sendTransaction({
to: funder.getAddress(),
value: balance.sub(targetBalance).sub(txCost),
gasLimit,
gasPrice
});
}
}
expect(`${await user.getBalance()}`).to.equal(`${targetBalance}`);
});
it("should get the price", async () => {
const price = await liquity.getPrice();
expect(price).to.be.an.instanceOf(Decimal);
});
describe("findHintForCollateralRatio", () => {
it("should pick the closest approx hint", async () => {
type Resolved<T> = T extends Promise<infer U> ? U : never;
type ApproxHint = Resolved<ReturnType<HintHelpers["getApproxHint"]>>;
const fakeHints: ApproxHint[] = [
{ diff: BigNumber.from(3), hintAddress: "alice", latestRandomSeed: BigNumber.from(1111) },
{ diff: BigNumber.from(4), hintAddress: "bob", latestRandomSeed: BigNumber.from(2222) },
{ diff: BigNumber.from(1), hintAddress: "carol", latestRandomSeed: BigNumber.from(3333) },
{ diff: BigNumber.from(2), hintAddress: "dennis", latestRandomSeed: BigNumber.from(4444) }
];
const borrowerOperations = {
estimateGas: {
openTrove: () => Promise.resolve(BigNumber.from(1))
},
populateTransaction: {
openTrove: () => Promise.resolve({})
}
};
const hintHelpers = chai.spy.interface({
getApproxHint: () => Promise.resolve(fakeHints.shift())
});
const sortedTroves = chai.spy.interface({
findInsertPosition: () => Promise.resolve(["fake insert position"])
});
const fakeLiquity = new PopulatableEthersLiquity(({
getNumberOfTroves: () => Promise.resolve(1000000),
getTotal: () => Promise.resolve(new Trove(Decimal.from(10), Decimal.ONE)),
getPrice: () => Promise.resolve(Decimal.ONE),
_getBlockTimestamp: () => Promise.resolve(0),
_getFeesFactory: () =>
Promise.resolve(() => new Fees(0, 0.99, 1, new Date(), new Date(), false)),
connection: {
userAddress: await user.getAddress(),
_contracts: {
borrowerOperations,
hintHelpers,
sortedTroves
}
}
} as unknown) as ReadableEthersLiquity);
const nominalCollateralRatio = Decimal.from(0.05);
const params = Trove.recreate(new Trove(Decimal.from(1), LUSD_MINIMUM_DEBT));
const trove = Trove.create(params);
expect(`${trove._nominalCollateralRatio}`).to.equal(`${nominalCollateralRatio}`);
await fakeLiquity.openTrove(params);
expect(hintHelpers.getApproxHint).to.have.been.called.exactly(4);
expect(hintHelpers.getApproxHint).to.have.been.called.with(nominalCollateralRatio.hex);
// returned latestRandomSeed should be passed back on the next call
expect(hintHelpers.getApproxHint).to.have.been.called.with(BigNumber.from(1111));
expect(hintHelpers.getApproxHint).to.have.been.called.with(BigNumber.from(2222));
expect(hintHelpers.getApproxHint).to.have.been.called.with(BigNumber.from(3333));
expect(sortedTroves.findInsertPosition).to.have.been.called.once;
expect(sortedTroves.findInsertPosition).to.have.been.called.with(
nominalCollateralRatio.hex,
"carol"
);
});
});
describe("Trove", () => {
it("should have no Trove initially", async () => {
const trove = await liquity.getTrove();
expect(trove.isEmpty).to.be.true;
});
it("should fail to create an undercollateralized Trove", async () => {
const price = await liquity.getPrice();
const undercollateralized = new Trove(LUSD_MINIMUM_DEBT.div(price), LUSD_MINIMUM_DEBT);
await expect(liquity.openTrove(Trove.recreate(undercollateralized))).to.eventually.be.rejected;
});
it("should fail to create a Trove with too little debt", async () => {
const withTooLittleDebt = new Trove(Decimal.from(50), LUSD_MINIMUM_DEBT.sub(1));
await expect(liquity.openTrove(Trove.recreate(withTooLittleDebt))).to.eventually.be.rejected;
});
const withSomeBorrowing = { depositCollateral: 50, borrowLUSD: LUSD_MINIMUM_NET_DEBT.add(100) };
it("should create a Trove with some borrowing", async () => {
const { newTrove, fee } = await liquity.openTrove(withSomeBorrowing);
expect(newTrove).to.deep.equal(Trove.create(withSomeBorrowing));
expect(`${fee}`).to.equal(`${MINIMUM_BORROWING_RATE.mul(withSomeBorrowing.borrowLUSD)}`);
});
it("should fail to withdraw all the collateral while the Trove has debt", async () => {
const trove = await liquity.getTrove();
await expect(liquity.withdrawCollateral(trove.collateral)).to.eventually.be.rejected;
});
const repaySomeDebt = { repayLUSD: 10 };
it("should repay some debt", async () => {
const { newTrove, fee } = await liquity.repayLUSD(repaySomeDebt.repayLUSD);
expect(newTrove).to.deep.equal(Trove.create(withSomeBorrowing).adjust(repaySomeDebt));
expect(`${fee}`).to.equal("0");
});
const borrowSomeMore = { borrowLUSD: 20 };
it("should borrow some more", async () => {
const { newTrove, fee } = await liquity.borrowLUSD(borrowSomeMore.borrowLUSD);
expect(newTrove).to.deep.equal(
Trove.create(withSomeBorrowing).adjust(repaySomeDebt).adjust(borrowSomeMore)
);
expect(`${fee}`).to.equal(`${MINIMUM_BORROWING_RATE.mul(borrowSomeMore.borrowLUSD)}`);
});
const depositMoreCollateral = { depositCollateral: 1 };
it("should deposit more collateral", async () => {
const { newTrove } = await liquity.depositCollateral(depositMoreCollateral.depositCollateral);
expect(newTrove).to.deep.equal(
Trove.create(withSomeBorrowing)
.adjust(repaySomeDebt)
.adjust(borrowSomeMore)
.adjust(depositMoreCollateral)
);
});
const repayAndWithdraw = { repayLUSD: 60, withdrawCollateral: 0.5 };
it("should repay some debt and withdraw some collateral at the same time", async () => {
const {
rawReceipt,
details: { newTrove }
} = await waitForSuccess(liquity.send.adjustTrove(repayAndWithdraw));
expect(newTrove).to.deep.equal(
Trove.create(withSomeBorrowing)
.adjust(repaySomeDebt)
.adjust(borrowSomeMore)
.adjust(depositMoreCollateral)
.adjust(repayAndWithdraw)
);
const ethBalance = await user.getBalance();
const expectedBalance = BigNumber.from(STARTING_BALANCE.add(0.5).hex).sub(
getGasCost(rawReceipt)
);
expect(`${ethBalance}`).to.equal(`${expectedBalance}`);
});
const borrowAndDeposit = { borrowLUSD: 60, depositCollateral: 0.5 };
it("should borrow more and deposit some collateral at the same time", async () => {
const {
rawReceipt,
details: { newTrove, fee }
} = await waitForSuccess(liquity.send.adjustTrove(borrowAndDeposit));
expect(newTrove).to.deep.equal(
Trove.create(withSomeBorrowing)
.adjust(repaySomeDebt)
.adjust(borrowSomeMore)
.adjust(depositMoreCollateral)
.adjust(repayAndWithdraw)
.adjust(borrowAndDeposit)
);
expect(`${fee}`).to.equal(`${MINIMUM_BORROWING_RATE.mul(borrowAndDeposit.borrowLUSD)}`);
const ethBalance = await user.getBalance();
const expectedBalance = BigNumber.from(STARTING_BALANCE.sub(0.5).hex).sub(
getGasCost(rawReceipt)
);
expect(`${ethBalance}`).to.equal(`${expectedBalance}`);
});
it("should close the Trove with some LUSD from another user", async () => {
const price = await liquity.getPrice();
const initialTrove = await liquity.getTrove();
const lusdBalance = await liquity.getLQTYBalance();
const lusdShortage = initialTrove.netDebt.sub(lusdBalance);
let funderTrove = Trove.create({ depositCollateral: 1, borrowLUSD: lusdShortage });
funderTrove = funderTrove.setDebt(Decimal.max(funderTrove.debt, LUSD_MINIMUM_DEBT));
funderTrove = funderTrove.setCollateral(funderTrove.debt.mulDiv(1.51, price));
const funderLiquity = await connectToDeployment(deployment, funder);
await funderLiquity.openTrove(Trove.recreate(funderTrove));
await funderLiquity.sendLUSD(await user.getAddress(), lusdShortage);
const { params } = await liquity.closeTrove();
expect(params).to.deep.equal({
withdrawCollateral: initialTrove.collateral,
repayLUSD: initialTrove.netDebt
});
const finalTrove = await liquity.getTrove();
expect(finalTrove.isEmpty).to.be.true;
});
});
describe("SendableEthersLiquity", () => {
it("should parse failed transactions without throwing", async () => {
// By passing a gasLimit, we avoid automatic use of estimateGas which would throw
const tx = await liquity.send.openTrove(
{ depositCollateral: 0.01, borrowLUSD: 0.01 },
undefined,
{ gasLimit: 1e6 }
);
const { status } = await tx.waitForReceipt();
expect(status).to.equal("failed");
});
});
describe("Frontend", () => {
it("should have no frontend initially", async () => {
const frontend = await liquity.getFrontendStatus(await user.getAddress());
assertStrictEqual(frontend.status, "unregistered" as const);
});
it("should register a frontend", async () => {
await liquity.registerFrontend(0.75);
});
it("should have a frontend now", async () => {
const frontend = await liquity.getFrontendStatus(await user.getAddress());
assertStrictEqual(frontend.status, "registered" as const);
expect(`${frontend.kickbackRate}`).to.equal("0.75");
});
it("other user's deposit should be tagged with the frontend's address", async () => {
const frontendTag = await user.getAddress();
await funder.sendTransaction({
to: otherUsers[0].getAddress(),
value: Decimal.from(20.1).hex
});
const otherLiquity = await connectToDeployment(deployment, otherUsers[0], frontendTag);
await otherLiquity.openTrove({ depositCollateral: 20, borrowLUSD: LUSD_MINIMUM_DEBT });
await otherLiquity.depositLUSDInStabilityPool(LUSD_MINIMUM_DEBT);
const deposit = await otherLiquity.getStabilityDeposit();
expect(deposit.frontendTag).to.equal(frontendTag);
});
});
describe("StabilityPool", () => {
before(async () => {
deployment = await deployLiquity(deployer);
[deployerLiquity, liquity, ...otherLiquities] = await connectUsers([
deployer,
user,
...otherUsers.slice(0, 1)
]);
await funder.sendTransaction({
to: otherUsers[0].getAddress(),
value: LUSD_MINIMUM_DEBT.div(170).hex
});
});
const initialTroveOfDepositor = Trove.create({
depositCollateral: LUSD_MINIMUM_DEBT.div(100),
borrowLUSD: LUSD_MINIMUM_NET_DEBT
});
const smallStabilityDeposit = Decimal.from(10);
it("should make a small stability deposit", async () => {
const { newTrove } = await liquity.openTrove(Trove.recreate(initialTroveOfDepositor));
expect(newTrove).to.deep.equal(initialTroveOfDepositor);
const details = await liquity.depositLUSDInStabilityPool(smallStabilityDeposit);
expect(details).to.deep.equal({
lusdLoss: Decimal.from(0),
newLUSDDeposit: smallStabilityDeposit,
collateralGain: Decimal.from(0),
lqtyReward: Decimal.from(0),
change: {
depositLUSD: smallStabilityDeposit
}
});
});
const troveWithVeryLowICR = Trove.create({
depositCollateral: LUSD_MINIMUM_DEBT.div(180),
borrowLUSD: LUSD_MINIMUM_NET_DEBT
});
it("other user should make a Trove with very low ICR", async () => {
const { newTrove } = await otherLiquities[0].openTrove(Trove.recreate(troveWithVeryLowICR));
const price = await liquity.getPrice();
expect(Number(`${newTrove.collateralRatio(price)}`)).to.be.below(1.15);
});
const dippedPrice = Decimal.from(190);
it("the price should take a dip", async () => {
await deployerLiquity.setPrice(dippedPrice);
const price = await liquity.getPrice();
expect(`${price}`).to.equal(`${dippedPrice}`);
});
it("should liquidate other user's Trove", async () => {
const details = await liquity.liquidateUpTo(1);
expect(details).to.deep.equal({
liquidatedAddresses: [await otherUsers[0].getAddress()],
collateralGasCompensation: troveWithVeryLowICR.collateral.mul(0.005), // 0.5%
lusdGasCompensation: LUSD_LIQUIDATION_RESERVE,
totalLiquidated: new Trove(
troveWithVeryLowICR.collateral
.mul(0.995) // -0.5% gas compensation
.add("0.000000000000000001"), // tiny imprecision
troveWithVeryLowICR.debt
)
});
const otherTrove = await otherLiquities[0].getTrove();
expect(otherTrove.isEmpty).to.be.true;
});
it("should have a depleted stability deposit and some collateral gain", async () => {
const stabilityDeposit = await liquity.getStabilityDeposit();
expect(stabilityDeposit).to.deep.equal(
new StabilityDeposit(
smallStabilityDeposit,
Decimal.ZERO,
troveWithVeryLowICR.collateral
.mul(0.995) // -0.5% gas compensation
.mulDiv(smallStabilityDeposit, troveWithVeryLowICR.debt)
.sub("0.000000000000000005"), // tiny imprecision
Decimal.ZERO,
AddressZero
)
);
});
it("the Trove should have received some liquidation shares", async () => {
const trove = await liquity.getTrove();
expect(trove).to.deep.equal({
ownerAddress: await user.getAddress(),
status: "open",
...initialTroveOfDepositor
.addDebt(troveWithVeryLowICR.debt.sub(smallStabilityDeposit))
.addCollateral(
troveWithVeryLowICR.collateral
.mul(0.995) // -0.5% gas compensation
.mulDiv(troveWithVeryLowICR.debt.sub(smallStabilityDeposit), troveWithVeryLowICR.debt)
.add("0.000000000000000001") // tiny imprecision
)
});
});
it("total should equal the Trove", async () => {
const trove = await liquity.getTrove();
const numberOfTroves = await liquity.getNumberOfTroves();
expect(numberOfTroves).to.equal(1);
const total = await liquity.getTotal();
expect(total).to.deep.equal(
trove.addCollateral("0.000000000000000001") // tiny imprecision
);
});
it("should transfer the gains to the Trove", async () => {
const details = await liquity.transferCollateralGainToTrove();
expect(details).to.deep.equal({
lusdLoss: smallStabilityDeposit,
newLUSDDeposit: Decimal.ZERO,
lqtyReward: Decimal.ZERO,
collateralGain: troveWithVeryLowICR.collateral
.mul(0.995) // -0.5% gas compensation
.mulDiv(smallStabilityDeposit, troveWithVeryLowICR.debt)
.sub("0.000000000000000005"), // tiny imprecision
newTrove: initialTroveOfDepositor
.addDebt(troveWithVeryLowICR.debt.sub(smallStabilityDeposit))
.addCollateral(
troveWithVeryLowICR.collateral
.mul(0.995) // -0.5% gas compensation
.sub("0.000000000000000005") // tiny imprecision
)
});
const stabilityDeposit = await liquity.getStabilityDeposit();
expect(stabilityDeposit.isEmpty).to.be.true;
});
describe("when people overstay", () => {
before(async () => {
// Deploy new instances of the contracts, for a clean slate
deployment = await deployLiquity(deployer);
const otherUsersSubset = otherUsers.slice(0, 5);
[deployerLiquity, liquity, ...otherLiquities] = await connectUsers([
deployer,
user,
...otherUsersSubset
]);
await sendToEach(otherUsersSubset, 21.1);
let price = Decimal.from(200);
await deployerLiquity.setPrice(price);
// Use this account to print LUSD
await liquity.openTrove({ depositCollateral: 50, borrowLUSD: 5000 });
// otherLiquities[0-2] will be independent stability depositors
await liquity.sendLUSD(await otherUsers[0].getAddress(), 3000);
await liquity.sendLUSD(await otherUsers[1].getAddress(), 1000);
await liquity.sendLUSD(await otherUsers[2].getAddress(), 1000);
// otherLiquities[3-4] will be Trove owners whose Troves get liquidated
await otherLiquities[3].openTrove({ depositCollateral: 21, borrowLUSD: 2900 });
await otherLiquities[4].openTrove({ depositCollateral: 21, borrowLUSD: 2900 });
await otherLiquities[0].depositLUSDInStabilityPool(3000);
await otherLiquities[1].depositLUSDInStabilityPool(1000);
// otherLiquities[2] doesn't deposit yet
// Tank the price so we can liquidate
price = Decimal.from(150);
await deployerLiquity.setPrice(price);
// Liquidate first victim
await liquity.liquidate(await otherUsers[3].getAddress());
expect((await otherLiquities[3].getTrove()).isEmpty).to.be.true;
// Now otherLiquities[2] makes their deposit too
await otherLiquities[2].depositLUSDInStabilityPool(1000);
// Liquidate second victim
await liquity.liquidate(await otherUsers[4].getAddress());
expect((await otherLiquities[4].getTrove()).isEmpty).to.be.true;
// Stability Pool is now empty
expect(`${await liquity.getLUSDInStabilityPool()}`).to.equal("0");
});
it("should still be able to withdraw remaining deposit", async () => {
for (const l of [otherLiquities[0], otherLiquities[1], otherLiquities[2]]) {
const stabilityDeposit = await l.getStabilityDeposit();
await l.withdrawLUSDFromStabilityPool(stabilityDeposit.currentLUSD);
}
});
});
});
describe("Redemption", () => {
const troveCreations = [
{ depositCollateral: 99, borrowLUSD: 4600 },
{ depositCollateral: 20, borrowLUSD: 2000 }, // net debt: 2010
{ depositCollateral: 20, borrowLUSD: 2100 }, // net debt: 2110.5
{ depositCollateral: 20, borrowLUSD: 2200 } // net debt: 2211
];
before(async function () {
if (network.name !== "hardhat") {
// Redemptions are only allowed after a bootstrap phase of 2 weeks.
// Since fast-forwarding only works on Hardhat EVM, skip these tests elsewhere.
this.skip();
}
// Deploy new instances of the contracts, for a clean slate
deployment = await deployLiquity(deployer);
const otherUsersSubset = otherUsers.slice(0, 3);
[deployerLiquity, liquity, ...otherLiquities] = await connectUsers([
deployer,
user,
...otherUsersSubset
]);
await sendToEach(otherUsersSubset, 20.1);
});
it("should fail to redeem during the bootstrap phase", async () => {
await liquity.openTrove(troveCreations[0]);
await otherLiquities[0].openTrove(troveCreations[1]);
await otherLiquities[1].openTrove(troveCreations[2]);
await otherLiquities[2].openTrove(troveCreations[3]);
await expect(liquity.redeemLUSD(4326.5)).to.eventually.be.rejected;
});
const someLUSD = Decimal.from(4326.5);
it("should redeem some LUSD after the bootstrap phase", async () => {
// Fast-forward 15 days
await increaseTime(60 * 60 * 24 * 15);
expect(`${await otherLiquities[0].getCollateralSurplusBalance()}`).to.equal("0");
expect(`${await otherLiquities[1].getCollateralSurplusBalance()}`).to.equal("0");
expect(`${await otherLiquities[2].getCollateralSurplusBalance()}`).to.equal("0");
const expectedTotal = troveCreations
.map(params => Trove.create(params))
.reduce((a, b) => a.add(b));
const total = await liquity.getTotal();
expect(total).to.deep.equal(expectedTotal);
const expectedDetails = {
attemptedLUSDAmount: someLUSD,
actualLUSDAmount: someLUSD,
collateralTaken: someLUSD.div(200),
fee: new Fees(0, 0.99, 2, new Date(), new Date(), false)
.redemptionRate(someLUSD.div(total.debt))
.mul(someLUSD.div(200))
};
const { rawReceipt, details } = await waitForSuccess(liquity.send.redeemLUSD(someLUSD));
expect(details).to.deep.equal(expectedDetails);
const balance = Decimal.fromBigNumberString(`${await user.getBalance()}`);
const gasCost = Decimal.fromBigNumberString(`${getGasCost(rawReceipt)}`);
expect(`${balance}`).to.equal(
`${STARTING_BALANCE.add(expectedDetails.collateralTaken)
.sub(expectedDetails.fee)
.sub(gasCost)}`
);
expect(`${await liquity.getLUSDBalance()}`).to.equal("273.5");
expect(`${(await otherLiquities[0].getTrove()).debt}`).to.equal(
`${Trove.create(troveCreations[1]).debt.sub(
someLUSD
.sub(Trove.create(troveCreations[2]).netDebt)
.sub(Trove.create(troveCreations[3]).netDebt)
)}`
);
expect((await otherLiquities[1].getTrove()).isEmpty).to.be.true;
expect((await otherLiquities[2].getTrove()).isEmpty).to.be.true;
});
it("should claim the collateral surplus after redemption", async () => {
const balanceBefore1 = await provider.getBalance(otherUsers[1].getAddress());
const balanceBefore2 = await provider.getBalance(otherUsers[2].getAddress());
expect(`${await otherLiquities[0].getCollateralSurplusBalance()}`).to.equal("0");
const surplus1 = await otherLiquities[1].getCollateralSurplusBalance();
const trove1 = Trove.create(troveCreations[2]);
expect(`${surplus1}`).to.equal(`${trove1.collateral.sub(trove1.netDebt.div(200))}`);
const surplus2 = await otherLiquities[2].getCollateralSurplusBalance();
const trove2 = Trove.create(troveCreations[3]);
expect(`${surplus2}`).to.equal(`${trove2.collateral.sub(trove2.netDebt.div(200))}`);
const { rawReceipt: receipt1 } = await waitForSuccess(
otherLiquities[1].send.claimCollateralSurplus()
);
const { rawReceipt: receipt2 } = await waitForSuccess(
otherLiquities[2].send.claimCollateralSurplus()
);
expect(`${await otherLiquities[0].getCollateralSurplusBalance()}`).to.equal("0");
expect(`${await otherLiquities[1].getCollateralSurplusBalance()}`).to.equal("0");
expect(`${await otherLiquities[2].getCollateralSurplusBalance()}`).to.equal("0");
const balanceAfter1 = await otherUsers[1].getBalance();
const balanceAfter2 = await otherUsers[2].getBalance();
expect(`${balanceAfter1}`).to.equal(
`${balanceBefore1.add(surplus1.hex).sub(getGasCost(receipt1))}`
);
expect(`${balanceAfter2}`).to.equal(
`${balanceBefore2.add(surplus2.hex).sub(getGasCost(receipt2))}`
);
});
it("borrowing rate should be maxed out now", async () => {
const borrowLUSD = Decimal.from(10);
const { fee, newTrove } = await liquity.borrowLUSD(borrowLUSD);
expect(`${fee}`).to.equal(`${borrowLUSD.mul(MAXIMUM_BORROWING_RATE)}`);
expect(newTrove).to.deep.equal(
Trove.create(troveCreations[0]).adjust({ borrowLUSD }, MAXIMUM_BORROWING_RATE)
);
});
});
describe("Redemption (truncation)", () => {
const troveCreationParams = { depositCollateral: 20, borrowLUSD: 2000 };
const netDebtPerTrove = Trove.create(troveCreationParams).netDebt;
const amountToAttempt = Decimal.from(3000);
const expectedRedeemable = netDebtPerTrove.mul(2).sub(LUSD_MINIMUM_NET_DEBT);
before(function () {
if (network.name !== "hardhat") {
// Redemptions are only allowed after a bootstrap phase of 2 weeks.
// Since fast-forwarding only works on Hardhat EVM, skip these tests elsewhere.
this.skip();
}
});
beforeEach(async () => {
// Deploy new instances of the contracts, for a clean slate
deployment = await deployLiquity(deployer);
const otherUsersSubset = otherUsers.slice(0, 3);
[deployerLiquity, liquity, ...otherLiquities] = await connectUsers([
deployer,
user,
...otherUsersSubset
]);
await sendToEach(otherUsersSubset, 20.1);
await liquity.openTrove({ depositCollateral: 99, borrowLUSD: 5000 });
await otherLiquities[0].openTrove(troveCreationParams);
await otherLiquities[1].openTrove(troveCreationParams);
await otherLiquities[2].openTrove(troveCreationParams);
await increaseTime(60 * 60 * 24 * 15);
});
it("should truncate the amount if it would put the last Trove below the min debt", async () => {
const redemption = await liquity.populate.redeemLUSD(amountToAttempt);
expect(`${redemption.attemptedLUSDAmount}`).to.equal(`${amountToAttempt}`);
expect(`${redemption.redeemableLUSDAmount}`).to.equal(`${expectedRedeemable}`);
expect(redemption.isTruncated).to.be.true;
const { details } = await waitForSuccess(redemption.send());
expect(`${details.attemptedLUSDAmount}`).to.equal(`${expectedRedeemable}`);
expect(`${details.actualLUSDAmount}`).to.equal(`${expectedRedeemable}`);
});
it("should increase the amount to the next lowest redeemable value", async () => {
const increasedRedeemable = expectedRedeemable.add(LUSD_MINIMUM_NET_DEBT);
const initialRedemption = await liquity.populate.redeemLUSD(amountToAttempt);
const increasedRedemption = await initialRedemption.increaseAmountByMinimumNetDebt();
expect(`${increasedRedemption.attemptedLUSDAmount}`).to.equal(`${increasedRedeemable}`);
expect(`${increasedRedemption.redeemableLUSDAmount}`).to.equal(`${increasedRedeemable}`);
expect(increasedRedemption.isTruncated).to.be.false;
const { details } = await waitForSuccess(increasedRedemption.send());
expect(`${details.attemptedLUSDAmount}`).to.equal(`${increasedRedeemable}`);
expect(`${details.actualLUSDAmount}`).to.equal(`${increasedRedeemable}`);
});
it("should fail to increase the amount if it's not truncated", async () => {
const redemption = await liquity.populate.redeemLUSD(netDebtPerTrove);
expect(redemption.isTruncated).to.be.false;
expect(() => redemption.increaseAmountByMinimumNetDebt()).to.throw(
"can only be called when amount is truncated"
);
});
});
describe("Redemption (gas checks)", function () {
this.timeout("5m");
const massivePrice = Decimal.from(1000000);
const amountToBorrowPerTrove = Decimal.from(2000);
const netDebtPerTrove = MINIMUM_BORROWING_RATE.add(1).mul(amountToBorrowPerTrove);
const collateralPerTrove = netDebtPerTrove
.add(LUSD_LIQUIDATION_RESERVE)
.mulDiv(1.5, massivePrice);
const amountToRedeem = netDebtPerTrove.mul(_redeemMaxIterations);
const amountToDeposit = MINIMUM_BORROWING_RATE.add(1)
.mul(amountToRedeem)
.add(LUSD_LIQUIDATION_RESERVE)
.mulDiv(2, massivePrice);
before(async function () {
if (network.name !== "hardhat") {
// Redemptions are only allowed after a bootstrap phase of 2 weeks.
// Since fast-forwarding only works on Hardhat EVM, skip these tests elsewhere.
this.skip();
}
// Deploy new instances of the contracts, for a clean slate
deployment = await deployLiquity(deployer);
const otherUsersSubset = otherUsers.slice(0, _redeemMaxIterations);
expect(otherUsersSubset).to.have.length(_redeemMaxIterations);
[deployerLiquity, liquity, ...otherLiquities] = await connectUsers([
deployer,
user,
...otherUsersSubset
]);
await deployerLiquity.setPrice(massivePrice);
await sendToEach(otherUsersSubset, collateralPerTrove);
for (const otherLiquity of otherLiquities) {
await otherLiquity.openTrove({
depositCollateral: collateralPerTrove,
borrowLUSD: amountToBorrowPerTrove
});
}
await increaseTime(60 * 60 * 24 * 15);
});
it("should redeem using the maximum iterations and almost all gas", async () => {
await liquity.openTrove({
depositCollateral: amountToDeposit,
borrowLUSD: amountToRedeem
});
const { rawReceipt } = await waitForSuccess(liquity.send.redeemLUSD(amountToRedeem));
const gasUsed = rawReceipt.gasUsed.toNumber();
// gasUsed is ~half the real used amount because of how refunds work, see:
// https://ethereum.stackexchange.com/a/859/9205
expect(gasUsed).to.be.at.least(4900000, "should use close to 10M gas");
});
});
describe("Liquidity mining", () => {
before(async () => {
deployment = await deployLiquity(deployer);
[deployerLiquity, liquity] = await connectUsers([deployer, user]);
});
const someUniTokens = 1000;
it("should obtain some UNI LP tokens", async () => {
await liquity._mintUniToken(someUniTokens);
const uniTokenBalance = await liquity.getUniTokenBalance();
expect(`${uniTokenBalance}`).to.equal(`${someUniTokens}`);
});
it("should fail to stake UNI LP before approving the spend", async () => {
await expect(liquity.stakeUniTokens(someUniTokens)).to.eventually.be.rejected;
});
it("should stake UNI LP after approving the spend", async () => {
const initialAllowance = await liquity.getUniTokenAllowance();
expect(`${initialAllowance}`).to.equal("0");
await liquity.approveUniTokens();
const newAllowance = await liquity.getUniTokenAllowance();
expect(newAllowance.isZero).to.be.false;
await liquity.stakeUniTokens(someUniTokens);
const uniTokenBalance = await liquity.getUniTokenBalance();
expect(`${uniTokenBalance}`).to.equal("0");
const stake = await liquity.getLiquidityMiningStake();
expect(`${stake}`).to.equal(`${someUniTokens}`);
});
it("should have an LQTY reward after some time has passed", async function () {
this.timeout("20s");
// Liquidity mining rewards are seconds-based, so we don't need to wait long.
// By actually waiting in real time, we avoid using increaseTime(), which only works on
// Hardhat EVM.
await new Promise(resolve => setTimeout(resolve, 4000));
// Trigger a new block with a dummy TX.
await liquity._mintUniToken(0);
const lqtyReward = Number(await liquity.getLiquidityMiningLQTYReward());
expect(lqtyReward).to.be.at.least(1); // ~0.2572 per second [(4e6/3) / (60*24*60*60)]
await liquity.withdrawLQTYRewardFromLiquidityMining();
const lqtyBalance = Number(await liquity.getLQTYBalance());
expect(lqtyBalance).to.be.at.least(lqtyReward); // may have increased since checking
});