-
Notifications
You must be signed in to change notification settings - Fork 205
Expand file tree
/
Copy pathFinancialContractClient.js
More file actions
959 lines (884 loc) · 41.9 KB
/
FinancialContractClient.js
File metadata and controls
959 lines (884 loc) · 41.9 KB
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
const { getContract, web3 } = require("hardhat");
const { toWei, toBN, utf8ToHex, padRight } = web3.utils;
const { parseFixed } = require("@ethersproject/bignumber");
const winston = require("winston");
const { assert } = require("chai");
const {
interfaceName,
MAX_UINT_VAL,
runTestForVersion,
createConstructorParamsForContractVersion,
TESTED_CONTRACT_VERSIONS,
getContractsNodePackageAliasForVerion,
TEST_DECIMAL_COMBOS,
} = require("@uma/common");
// Script to test
const { FinancialContractClient } = require("../../dist/clients/FinancialContractClient");
const startTime = "15798990420";
const zeroAddress = "0x0000000000000000000000000000000000000000";
const unreachableDeadline = MAX_UINT_VAL;
// Common contract objects.
let collateralToken;
let syntheticToken;
let financialContract;
let mockOracle;
let identifierWhitelist;
let identifier;
let finder;
let store;
let timer;
let collateralWhitelist;
let constructorParams;
let iterationTestVersion;
let multicallContract;
// Js Objects, clients and helpers
let client;
let convertDecimals;
let dummyLogger;
// Perpetual
let configStore;
let optimisticOracle;
let fundingRateIdentifier;
let accounts;
let sponsor1, sponsor2;
// Helper functions
const updateAndVerify = async (client, expectedSponsors, expectedPositions) => {
await client.update();
assert.deepStrictEqual(client.getAllSponsors().sort(), expectedSponsors.sort());
assert.deepStrictEqual(client.getAllPositions().sort(), expectedPositions.sort());
};
// Set the funding rate and advances time by 10k seconds.
const _setFundingRateAndAdvanceTime = async (fundingRate) => {
const currentTime = parseInt(await financialContract.methods.getCurrentTime().call());
await financialContract.methods
.proposeFundingRate({ rawValue: fundingRate }, currentTime)
.send({ from: accounts[0] });
await financialContract.methods.setCurrentTime(currentTime + 10000).send({ from: accounts[0] });
};
// If the current version being executed is part of the `supportedVersions` array then return `it` to run the test.
// Else, do nothing. Can be used exactly in place of a normal `it` to parameterize contract types and versions supported
// for a given test.eg: versionedIt([{ contractType: "Perpetual", contractVersion: "latest" }])("test name", async function () { assert.isTrue(true) })
// Note that a second param can be provided to make the test an `it.only` thereby ONLY running that single test, on
// the provided version. This is very useful for debugging and writing single unit tests without having ro run all tests.
const versionedIt = function (supportedVersions, shouldBeItOnly = false) {
if (shouldBeItOnly)
return runTestForVersion(supportedVersions, TESTED_CONTRACT_VERSIONS, iterationTestVersion) ? it.only : () => {};
return runTestForVersion(supportedVersions, TESTED_CONTRACT_VERSIONS, iterationTestVersion) ? it : () => {};
};
const Convert = (decimals) => (number) => parseFixed(number.toString(), decimals).toString();
describe("FinancialContractClient.js", function () {
before(async function () {
accounts = await web3.eth.getAccounts();
[sponsor1, sponsor2] = accounts;
});
TESTED_CONTRACT_VERSIONS.forEach(function (contractVersion) {
// Store the contractVersion.contractVersion, type and version being tested
iterationTestVersion = contractVersion;
const { getAbi, getBytecode } = require(getContractsNodePackageAliasForVerion(contractVersion.contractVersion));
const createContract = (name) => {
const abi = getAbi(name);
const bytecode = getBytecode(name);
return getContract(name, { abi, bytecode });
};
// Import the tested versions of contracts. note that financialContract is either an ExpiringMultiParty or a
// Perpetual depending on the current iteration version.
const FinancialContract = createContract(contractVersion.contractType);
const Finder = createContract("Finder");
const IdentifierWhitelist = createContract("IdentifierWhitelist");
const AddressWhitelist = createContract("AddressWhitelist");
const MockOracle = createContract("MockOracle");
const Token = createContract("ExpandedERC20");
const SyntheticToken = createContract("SyntheticToken");
const Timer = createContract("Timer");
const Store = createContract("Store");
const ConfigStore = createContract("ConfigStore");
const OptimisticOracle = createContract("OptimisticOracle");
const MulticallMock = createContract("MulticallMock");
for (let testConfig of TEST_DECIMAL_COMBOS) {
describe(`${testConfig.collateralDecimals} collateral, ${testConfig.syntheticDecimals} synthetic & ${testConfig.priceFeedDecimals} pricefeed decimals, for smart contract version ${contractVersion.contractType} @ ${contractVersion.contractVersion}`, function () {
before(async function () {
identifier = `${testConfig.tokenName}TEST`;
fundingRateIdentifier = `${testConfig.tokenName}_FUNDING`;
convertDecimals = Convert(testConfig.collateralDecimals);
collateralToken = await Token.new(
testConfig.tokenSymbol + "Token", // Construct the token name.
testConfig.tokenSymbol,
testConfig.collateralDecimals
).send({ from: sponsor1 });
syntheticToken = await SyntheticToken.new(
"Test Synthetic Token",
"SYNTH",
testConfig.syntheticDecimals
).send({ from: sponsor1 });
await collateralToken.methods.addMember(1, sponsor1).send({ from: sponsor1 });
await collateralToken.methods.mint(sponsor1, convertDecimals("1000000000")).send({ from: sponsor1 });
await collateralToken.methods.mint(sponsor2, convertDecimals("1000000000")).send({ from: sponsor1 });
identifierWhitelist = await IdentifierWhitelist.new().send({ from: accounts[0] });
await identifierWhitelist.methods.addSupportedIdentifier(utf8ToHex(identifier)).send({ from: accounts[0] });
finder = await Finder.new().send({ from: accounts[0] });
timer = await Timer.new().send({ from: accounts[0] });
store = await Store.new({ rawValue: "0" }, { rawValue: "0" }, timer.options.address).send({
from: accounts[0],
});
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.Store), store.options.address)
.send({ from: accounts[0] });
collateralWhitelist = await AddressWhitelist.new().send({ from: accounts[0] });
await finder.methods
.changeImplementationAddress(
web3.utils.utf8ToHex(interfaceName.CollateralWhitelist),
collateralWhitelist.options.address
)
.send({ from: accounts[0] });
await collateralWhitelist.methods.addToWhitelist(collateralToken.options.address).send({ from: accounts[0] });
await finder.methods
.changeImplementationAddress(
utf8ToHex(interfaceName.IdentifierWhitelist),
identifierWhitelist.options.address
)
.send({ from: accounts[0] });
mockOracle = await MockOracle.new(finder.options.address, timer.options.address).send({ from: accounts[0] });
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.Oracle), mockOracle.options.address)
.send({ from: accounts[0] });
multicallContract = await MulticallMock.new().send({ from: accounts[0] });
});
beforeEach(async function () {
await timer.methods.setCurrentTime(startTime - 1).send({ from: accounts[0] });
// If we are testing a perpetual then we need to also deploy a config store, an optimistic oracle and set the funding rate identifier.
if (contractVersion.contractType == "Perpetual") {
configStore = await ConfigStore.new(
{
timelockLiveness: 86400, // 1 day
rewardRatePerSecond: { rawValue: "0" },
proposerBondPercentage: { rawValue: "0" },
maxFundingRate: { rawValue: toWei("0.00001") },
minFundingRate: { rawValue: toWei("-0.00001") },
proposalTimePastLimit: 0,
},
timer.options.address
).send({ from: accounts[0] });
await identifierWhitelist.methods
.addSupportedIdentifier(padRight(utf8ToHex(fundingRateIdentifier)))
.send({ from: accounts[0] });
optimisticOracle = await OptimisticOracle.new(7200, finder.options.address, timer.options.address).send({
from: accounts[0],
});
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.OptimisticOracle), optimisticOracle.options.address)
.send({ from: accounts[0] });
}
constructorParams = await createConstructorParamsForContractVersion(
contractVersion,
{
convertDecimals,
finder,
collateralToken,
syntheticToken,
identifier,
fundingRateIdentifier,
timer,
store,
configStore: configStore || {}, // if the contract type is not a perp this will be null.
},
{ collateralRequirement: { rawValue: toWei("1.5") } } // these tests assume a CR of 1.5, not the 1.2 default.
);
financialContract = await FinancialContract.new(constructorParams).send({ from: accounts[0] });
await syntheticToken.methods.addMinter(financialContract.options.address).send({ from: accounts[0] });
await syntheticToken.methods.addBurner(financialContract.options.address).send({ from: accounts[0] });
await collateralToken.methods
.approve(financialContract.options.address, convertDecimals("1000000"))
.send({ from: sponsor1 });
await collateralToken.methods
.approve(financialContract.options.address, convertDecimals("1000000"))
.send({ from: sponsor2 });
await syntheticToken.methods
.approve(financialContract.options.address, convertDecimals("100000000"))
.send({ from: sponsor1 });
await syntheticToken.methods
.approve(financialContract.options.address, convertDecimals("100000000"))
.send({ from: sponsor2 });
// If we are testing a perpetual then we need to apply the initial funding rate to start the timer.
await financialContract.methods.setCurrentTime(startTime).send({ from: accounts[0] });
// The FinancialContractClient does not emit any info `level` events. Therefore no need to test Winston outputs.
// DummyLogger will not print anything to console as only capture `info` level events.
dummyLogger = winston.createLogger({ level: "info", transports: [new winston.transports.Console()] });
client = new FinancialContractClient(
dummyLogger,
FinancialContract.abi,
web3,
financialContract.options.address,
multicallContract.options.address,
testConfig.collateralDecimals,
testConfig.syntheticDecimals,
testConfig.priceFeedDecimals,
contractVersion.contractType // either ExpiringMultiParty OR Perpetual depending on the test
);
});
versionedIt([{ contractType: "any", contractVersion: "any" }])("Returns all positions", async function () {
// Create a position and check that it is detected correctly from the client.
await financialContract.methods
.create({ rawValue: convertDecimals("10") }, { rawValue: convertDecimals("50") })
.send({ from: sponsor1 });
await updateAndVerify(
client,
[sponsor1], // expected sponsor
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("50"),
numTokens: convertDecimals("50"),
amountCollateral: convertDecimals("10"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
] // expected position
);
// Calling create again from the same sponsor should add additional collateral & debt.
await financialContract.methods
.create({ rawValue: convertDecimals("10") }, { rawValue: convertDecimals("50") })
.send({ from: sponsor1 });
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
// Calling create from a new address will create a new position and this should be added the client.
await financialContract.methods
.create({ rawValue: convertDecimals("100") }, { rawValue: convertDecimals("45") })
.send({ from: sponsor2 });
await updateAndVerify(
client,
[sponsor1, sponsor2],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
{
sponsor: sponsor2,
adjustedTokens: convertDecimals("45"),
numTokens: convertDecimals("45"),
amountCollateral: convertDecimals("100"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
// If a position is liquidated it should be removed from the list of positions and added to the undisputed liquidations.
const { liquidationId } = await financialContract.methods
.createLiquidation(
sponsor2,
{ rawValue: "0" },
{ rawValue: toWei("99999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.call({ from: sponsor1 });
await financialContract.methods
.createLiquidation(
sponsor2,
{ rawValue: "0" },
{ rawValue: toWei("99999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.send({ from: sponsor1 });
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
const expectedLiquidations = [
{
sponsor: sponsor2,
id: liquidationId.toString(),
numTokens: convertDecimals("45"),
liquidatedCollateral: convertDecimals("100"),
lockedCollateral: convertDecimals("100"),
liquidationTime: (await financialContract.methods.getCurrentTime().call()).toString(),
state: "1",
liquidator: sponsor1,
disputer: zeroAddress,
},
];
assert.deepStrictEqual(expectedLiquidations.sort(), client.getUndisputedLiquidations().sort());
// Pending withdrawals state should be correctly identified.
await financialContract.methods
.requestWithdrawal({ rawValue: convertDecimals("10") })
.send({ from: sponsor1 });
await client.update();
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: true,
withdrawalRequestPassTimestamp: toBN(await financialContract.methods.getCurrentTime().call())
.add(toBN(await financialContract.methods.withdrawalLiveness().call()))
.toString(),
withdrawalRequestAmount: convertDecimals("10"),
},
]
);
// Remove the pending withdrawal and ensure it is removed from the client.
await financialContract.methods.cancelWithdrawal().send({ from: sponsor1 });
await client.update();
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
// Correctly returns sponsors who create, redeem.
await financialContract.methods
.create({ rawValue: convertDecimals("100") }, { rawValue: convertDecimals("45") })
.send({ from: sponsor2 });
await financialContract.methods.redeem({ rawValue: convertDecimals("45") }).send({ from: sponsor2 });
// as created and redeemed sponsor should not show up in table as they are no longer an active sponsor.
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
// If sponsor, creates, redeemes and then creates again they should now appear in the table.
await financialContract.methods
.create({ rawValue: convertDecimals("100") }, { rawValue: convertDecimals("45") })
.send({ from: sponsor2 });
await financialContract.methods.redeem({ rawValue: convertDecimals("45") }).send({ from: sponsor2 });
await financialContract.methods
.create({ rawValue: convertDecimals("100") }, { rawValue: convertDecimals("45") })
.send({ from: sponsor2 });
await financialContract.methods.redeem({ rawValue: convertDecimals("45") }).send({ from: sponsor2 });
await financialContract.methods
.create({ rawValue: convertDecimals("100") }, { rawValue: convertDecimals("45") })
.send({ from: sponsor2 });
await updateAndVerify(
client,
[sponsor1, sponsor2],
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("20"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
{
sponsor: sponsor2,
adjustedTokens: convertDecimals("45"),
numTokens: convertDecimals("45"),
amountCollateral: convertDecimals("100"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
});
versionedIt([{ contractType: "any", contractVersion: "any" }])(
"Returns undercollateralized positions",
async function () {
await financialContract.methods
.create({ rawValue: convertDecimals("150") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor1 });
await financialContract.methods
.create({ rawValue: convertDecimals("1500") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor2 });
await client.update();
// At 150% collateralization requirement, the position is just collateralized enough at a token price of 1.
assert.deepStrictEqual([], client.getUnderCollateralizedPositions(toWei("1")));
// Undercollateralized at a price just above 1.
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("150"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
],
client.getUnderCollateralizedPositions(toWei("1.00000001"))
);
// After submitting a withdraw request that brings the position below the CR ratio the client should detect this.
// Withdrawing just 1 wei of collateral will place the position below the CR ratio.
await financialContract.methods
.requestWithdrawal({ rawValue: convertDecimals("1") })
.send({ from: sponsor1 });
await client.update();
// Update client to get withdrawal information.
const currentTime = Number(await financialContract.methods.getCurrentTime().call());
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("150"),
hasPendingWithdrawal: true,
withdrawalRequestPassTimestamp: (currentTime + 1000).toString(),
withdrawalRequestAmount: convertDecimals("1"),
},
],
client.getUnderCollateralizedPositions(toWei("1"))
);
}
);
versionedIt([{ contractType: "any", contractVersion: "any" }])(
"Returns undisputed liquidations",
async function () {
const liquidator = sponsor2;
await financialContract.methods
.create({ rawValue: convertDecimals("150") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor1 });
await syntheticToken.methods.transfer(liquidator, convertDecimals("100")).send({ from: sponsor1 });
// Create a new liquidation for account[0]'s position.
const { liquidationId } = await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.call({ from: liquidator });
await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.send({ from: liquidator });
await client.update();
const liquidations = client.getUndisputedLiquidations();
// Disputable if the disputer believes the price was `1`, and not disputable if they believe the price was just
// above `1`.
assert.isTrue(client.isDisputable(liquidations[0], toWei("1")));
assert.isFalse(client.isDisputable(liquidations[0], toWei("1.00000001")));
// Dispute the liquidation and make sure it no longer shows up in the list.
// We need to advance the Oracle time forward to make `requestPrice` work.
await mockOracle.methods
.setCurrentTime(Number(await financialContract.methods.getCurrentTime().call()) + 1)
.send({ from: accounts[0] });
await financialContract.methods.dispute(liquidationId.toString(), sponsor1).send({ from: sponsor1 });
await client.update();
// The disputed liquidation should no longer show up as undisputed.
assert.deepStrictEqual([], client.getUndisputedLiquidations().sort());
}
);
versionedIt([{ contractType: "any", contractVersion: "any" }])(
"Returns expired liquidations",
async function () {
const liquidator = sponsor2;
await financialContract.methods
.create({ rawValue: convertDecimals("150") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor1 });
await syntheticToken.methods.transfer(liquidator, convertDecimals("100")).send({ from: sponsor1 });
await financialContract.methods
.requestWithdrawal({ rawValue: convertDecimals("10") })
.send({ from: sponsor1 });
// Create a new liquidation for account[0]'s position.
await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.call({ from: liquidator });
await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.send({ from: liquidator });
await client.update();
const liquidations = client.getUndisputedLiquidations();
const liquidationTime = liquidations[0].liquidationTime;
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
id: "0",
state: "1",
liquidationTime: liquidationTime,
numTokens: convertDecimals("100"),
liquidatedCollateral: convertDecimals("140"),
// This should `lockedCollateral` reduced by requested withdrawal amount
lockedCollateral: convertDecimals("150"),
liquidator: liquidator,
disputer: zeroAddress,
},
],
liquidations
);
assert.deepStrictEqual([], client.getExpiredLiquidations().sort());
// Move Financial Contract time to the liquidation's expiry.
const liquidationLiveness = 1000;
await financialContract.methods
.setCurrentTime(Number(liquidationTime) + liquidationLiveness)
.send({ from: accounts[0] });
await client.update();
// The liquidation is registered by the Financial Contract client as expired.
assert.deepStrictEqual([], client.getUndisputedLiquidations().sort());
const expiredLiquidations = client.getExpiredLiquidations();
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
id: "0",
state: "1",
liquidationTime: liquidationTime,
numTokens: convertDecimals("100"),
liquidatedCollateral: convertDecimals("140"),
lockedCollateral: convertDecimals("150"),
liquidator: liquidator,
disputer: zeroAddress,
},
],
expiredLiquidations
);
// Withdraw from the expired liquidation and check that the liquidation is deleted.
await financialContract.methods.withdrawLiquidation("0", sponsor1).send({ from: liquidator });
await client.update();
assert.deepStrictEqual([], client.getExpiredLiquidations().sort());
}
);
versionedIt([{ contractType: "any", contractVersion: "any" }])(
"Returns disputed liquidations",
async function () {
const liquidator = sponsor2;
await financialContract.methods
.create({ rawValue: convertDecimals("150") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor1 });
await syntheticToken.methods.transfer(liquidator, convertDecimals("100")).send({ from: sponsor1 });
// Create a new liquidation for account[0]'s position.
const { liquidationId } = await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.call({ from: liquidator });
await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
{ rawValue: toWei("100") },
unreachableDeadline
)
.send({ from: liquidator });
await client.update();
const liquidations = client.getUndisputedLiquidations();
const liquidationTime = liquidations[0].liquidationTime;
// There should be no disputed liquidations initially.
assert.deepStrictEqual([], client.getDisputedLiquidations().sort());
// Dispute the liquidation and make sure it no longer shows up in the list.
// We need to advance the Oracle time forward to make `requestPrice` work.
await mockOracle.methods
.setCurrentTime(Number(await financialContract.methods.getCurrentTime().call()) + 1)
.send({ from: accounts[0] });
await financialContract.methods.dispute(liquidationId.toString(), sponsor1).send({ from: sponsor1 });
await client.update();
// The disputed liquidation should no longer show up as undisputed.
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
id: "0",
state: "2",
liquidationTime: liquidationTime,
numTokens: convertDecimals("100"),
liquidatedCollateral: convertDecimals("150"),
lockedCollateral: convertDecimals("150"),
liquidator: liquidator,
disputer: sponsor1,
},
],
client.getDisputedLiquidations().sort()
);
assert.deepStrictEqual([], client.getUndisputedLiquidations().sort());
// Force a price such that the dispute fails, and then
// withdraw from the unsuccessfully disputed liquidation and check that the liquidation is deleted.
const disputePrice = toWei("1.6");
await mockOracle.methods
.pushPrice(utf8ToHex(identifier), liquidationTime, disputePrice)
.send({ from: accounts[0] });
await financialContract.methods.withdrawLiquidation("0", sponsor1).send({ from: liquidator });
await client.update();
assert.deepStrictEqual([], client.getDisputedLiquidations().sort());
}
);
versionedIt([{ contractType: "ExpiringMultiParty", contractVersion: "2.0.1" }])(
"Client correctly defaults to ExpiringMultiParty to enable backward compatibility",
async function () {
// The constructor of the Financial Contract client does not contain any type.
// It should therefore default to the Financial Contract which ensures that packages that are yet to update.
client = new FinancialContractClient(
dummyLogger,
FinancialContract.abi,
web3,
financialContract.options.address,
multicallContract.options.address,
testConfig.collateralDecimals,
testConfig.syntheticDecimals,
testConfig.priceFeedDecimals
);
assert.equal(client.getContractType(), "ExpiringMultiParty");
}
);
versionedIt([{ contractType: "ExpiringMultiParty", contractVersion: "2.0.1" }])(
"Correctly rejects invalid contract types",
async function () {
let didThrow = false;
try {
client = new FinancialContractClient(
dummyLogger,
financialContract.abi,
web3,
financialContract.options.address,
multicallContract.options.address,
testConfig.collateralDecimals,
testConfig.syntheticDecimals,
testConfig.priceFeedDecimals,
"ExpiringMultiPartyV2" // some contract name that does not exist
);
} catch (error) {
didThrow = true;
}
assert.isTrue(didThrow);
didThrow = false;
try {
client = new FinancialContractClient(
dummyLogger,
financialContract.abi,
web3,
financialContract.options.address,
multicallContract.options.address,
testConfig.collateralDecimals,
testConfig.syntheticDecimals,
testConfig.priceFeedDecimals,
null // some contract name that does not exist
);
} catch (error) {
didThrow = true;
}
assert.isTrue(didThrow);
}
);
versionedIt([{ contractType: "Perpetual", contractVersion: "2.0.1" }])(
"Fetches funding rate from the perpetual contract and correctly applies it to token debt",
async function () {
// Create a position and check that it is detected correctly from the client.
await financialContract.methods
.create({ rawValue: convertDecimals("10") }, { rawValue: convertDecimals("50") })
.send({ from: sponsor1 });
await updateAndVerify(
client,
[sponsor1], // expected sponsor
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("50"),
numTokens: convertDecimals("50"),
amountCollateral: convertDecimals("10"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
] // expected position
);
// Set a funding rate
await _setFundingRateAndAdvanceTime(toWei("0.000005"));
// funding rate change is not detected until the client is updated.
assert.equal(client.getLatestCumulativeFundingRateMultiplier(), toWei("1"));
// After updating the client the pending funding rate should be detected.
await client.update();
assert.equal(client.getLatestCumulativeFundingRateMultiplier(), toWei("1.05"));
// But, the on-chain state has not changed! This is possible because the client
// uses the Multicall contract to simulate how `applyFundingRate()` would
// affect `fundingRate()`.
const onchainFundingRate = await financialContract.methods.fundingRate().call();
assert.equal(onchainFundingRate.cumulativeMultiplier.rawValue, toWei("1"));
// Correctly scales sponsors token debt by the funding rate
await updateAndVerify(
client,
[sponsor1],
[
{
sponsor: sponsor1,
numTokens: convertDecimals("50"),
adjustedTokens: toBN(convertDecimals("50"))
.mul(toBN(toWei("1.05")))
.div(toBN(toWei("1")))
.toString(), // the funding rate should be applied to the num of tokens
amountCollateral: convertDecimals("10"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
]
);
}
);
versionedIt([{ contractType: "Perpetual", contractVersion: "2.0.1" }])(
"Correctly applies funding rate to token debt. Liquidatable and disputable position are updated accordingly",
async function () {
await financialContract.methods
.create({ rawValue: convertDecimals("150") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor1 });
await financialContract.methods
.create({ rawValue: convertDecimals("175") }, { rawValue: convertDecimals("100") })
.send({ from: sponsor2 });
await client.update();
// At 150% collateralization requirement, the position is just collateralized enough at a token price of 1.
// At any positive funding rate value the first position should become undercollateralied.
assert.deepStrictEqual([], client.getUnderCollateralizedPositions(toWei("1")));
// Undercollateralized at a price just above 1.
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
adjustedTokens: convertDecimals("100"),
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("150"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
],
client.getUnderCollateralizedPositions(toWei("1.00000001"))
);
// Or, undercollateralized at a price of 1 with a small funding rate applied. Funding rate of 0.000001 applied
// over 10k seconds resulting in 0.01 cumulative multiplier. At a price of 1 this works out to a CR of:
// 150 / (100 * 1.01 * 1) = 1.485, which is less than the CR requirement of 1.5
await _setFundingRateAndAdvanceTime(toWei("0.000001"));
// Should not need to call `applyFundingRate()` in order for client to detect publishable
// funding rate proposals.
await client.update();
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
adjustedTokens: toBN(convertDecimals("100"))
.mul(toBN(toWei("1.01")))
.div(toBN(toWei("1")))
.toString(), // the funding rate should be applied to the num of tokens
numTokens: convertDecimals("100"),
amountCollateral: convertDecimals("150"),
hasPendingWithdrawal: false,
withdrawalRequestPassTimestamp: "0",
withdrawalRequestAmount: "0",
},
],
client.getUnderCollateralizedPositions(toWei("1"))
);
// Now liquidate the position, advance some time so the current funding rate changes, and check that the
// client's stored liquidation state has the correct funding-rate adjusted amount of tokens outstanding for
// the liquidation time, not the current time.
await financialContract.methods
.createLiquidation(
sponsor1,
{ rawValue: "0" },
{ rawValue: toWei("9999999") },
// Note: Liquidates the full position of 100 tokens
{ rawValue: toWei("100") },
unreachableDeadline
)
.send({ from: sponsor2 });
const currentTime = parseInt(await financialContract.methods.getCurrentTime().call());
// Note: Advance < liquidationLiveness amount of time so that liquidation still appears under
// undisputedLiquidations struct:
await financialContract.methods.setCurrentTime(currentTime + 999).send({ from: accounts[0] });
await financialContract.methods.applyFundingRate().send({ from: accounts[0] });
const currentFundingRateData = await financialContract.methods.fundingRate().call();
// Here we show that current funding rate multiplier has increased:
assert.isTrue(toBN(currentFundingRateData.cumulativeMultiplier.rawValue).gt(toWei("1.01")));
await client.update();
assert.deepStrictEqual(
[
{
sponsor: sponsor1,
id: "0",
state: "1",
liquidationTime: currentTime.toString(),
// Here the `numTokens` should adjust for the CFRM at the time of liquidation, not the current one!
numTokens: convertDecimals("101"),
liquidatedCollateral: convertDecimals("150"),
lockedCollateral: convertDecimals("150"),
liquidator: sponsor2,
disputer: zeroAddress,
},
],
client.getUndisputedLiquidations()
);
}
);
});
}
});
});