-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGovernance.ts
More file actions
1642 lines (1497 loc) · 100 KB
/
Copy pathGovernance.ts
File metadata and controls
1642 lines (1497 loc) · 100 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
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 {BigNumberish} from "ethers";
import {expect} from "chai";
import {ethers, upgrades} from "hardhat";
import {loadFixture, time} from "@nomicfoundation/hardhat-network-helpers";
import {ExecLoggingProposal, Governance, ProposalTemplates, UpgradableCounterContract} from "../typechain-types"
import type {HardhatEthersSigner} from "@nomicfoundation/hardhat-ethers/src/signers";
import {CallType, DelegateCallType, initConsts, NonExecutableType} from "./utils";
import {min} from "hardhat/internal/util/bigint";
const scales = [0, 2, 3, 4, 5];
const ProposalStatus = {
INITIAL: 0n,
RESOLVED: 1n,
FAILED: 2n,
CANCELED: 3n,
EXECUTION_EXPIRED: 4n,
}
const governanceFixture = async function () {
const [defaultAcc, otherAcc, firstVoterAcc, secondVoterAcc, delegatorAcc] = await ethers.getSigners();
const sfc = await ethers.deployContract("UnitTestSFC");
await sfc.addValidator(1, 0, defaultAcc)
await sfc.addValidator(2, 0, firstVoterAcc)
const govable = await ethers.deployContract("SFCGovernableAdapter", [await sfc.getAddress()]);
const verifier = await ethers.deployContract("ProposalTemplates")
const verifierAddress = await verifier.getAddress();
const votebook = await ethers.deployContract("VotesBookKeeper");
const gov = await ethers.deployContract("Governance");
await verifier.initialize(defaultAcc.getAddress());
await votebook.initialize(defaultAcc.getAddress(), gov.getAddress(), 1000);
await gov.initialize(govable.getAddress(), verifierAddress, votebook.getAddress());
const proposalFee = await gov.proposalFee();
return {
sfc,
govable,
verifier,
verifierAddress,
votebook,
gov,
defaultAcc,
otherAcc,
firstVoterAcc,
secondVoterAcc,
delegatorAcc,
proposalFee,
}
}
describe("Governance test", function () {
beforeEach(async function (){
Object.assign(this, await loadFixture(governanceFixture));
});
it("checking deployment of a plaintext proposal contract", async function () {
await this.verifier.addTemplate(1, "plaintext", ethers.ZeroAddress, NonExecutableType, ethers.parseEther("0.4"), ethers.parseEther("0.6"), [0, 1, 2, 3, 4], 120, 1200, 0, 60);
const option = ethers.encodeBytes32String("opt");
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("0.6"), 0, 120, 1201, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MaxDurationIsTooLong"
)
.withArgs(1201, 1200);
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("0.6"), 0, 119, 1201, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinDurationIsTooShort"
)
.withArgs(119, 120);
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("0.6"), 61, 119, 1201, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinDurationIsTooShort"
)
.withArgs(119, 120);
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("0.6"), 0, 501, 500, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinEndIsAfterMaxEnd"
)
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.399"), ethers.parseEther("0.6"), 0, 501, 500, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinVotesTooSmall"
)
.withArgs(ethers.parseEther("0.399"),ethers.parseEther("0.4"));
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("1.01"), ethers.parseEther("0.6"), 0, 501, 500, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinVotesTooLarge"
)
.withArgs(ethers.parseEther("1.01"), ethers.parseEther("1"));
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("0.599"), 60, 120, 1200, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinAgreementTooSmall"
)
.withArgs(ethers.parseEther("0.599"),ethers.parseEther("0.6"));
await expect(ethers.deployContract("PlainTextProposal", ["plaintext", "plaintext-descr", [option], ethers.parseEther("0.4"), ethers.parseEther("1.01"), 60, 120, 1200, this.verifierAddress]))
.to.be.revertedWithCustomError(
this.verifier,
"MinAgreementTooLarge"
)
.withArgs(ethers.parseEther("1.01"), ethers.parseEther("1"));
});
it("checking creation of a plaintext proposal", async function () {
const pType = 1n;
const now = await time.latest();
await this.verifier.addTemplate(pType, "plaintext", ethers.ZeroAddress, NonExecutableType, ethers.parseEther("0.4"), ethers.parseEther("0.6"), [0, 1, 2, 3, 4], 120, 1200, 0, 60);
const option = ethers.encodeBytes32String("opt");
const emptyOptions = await ethers.deployContract("PlainTextProposal", ["plaintext","plaintext-descr", [], ethers.parseEther("0.5"), ethers.parseEther("0.6"), 30, 121, 1199, this.verifierAddress]);
const tooManyOptions = await ethers.deployContract("PlainTextProposal", ["plaintext","plaintext-descr", [option, option, option, option, option, option, option, option, option, option, option], ethers.parseEther("0.5"), ethers.parseEther("0.6"), 30, 121, 1199, this.verifierAddress]);
const wrongVotes = await ethers.deployContract("PlainTextProposal", ["plaintext","plaintext-descr", [option], ethers.parseEther("0.3"), ethers.parseEther("0.6"), 30, 121, 1199, ethers.ZeroAddress]);
const manyOptions = await ethers.deployContract("PlainTextProposal", ["plaintext","plaintext-descr", [option, option, option, option, option, option, option, option, option, option], ethers.parseEther("0.5"), ethers.parseEther("0.6"), 30, 121, 1199, this.verifierAddress]);
const oneOption = await ethers.deployContract("PlainTextProposal", ["plaintext","plaintext-descr", [option], ethers.parseEther("0.51"), ethers.parseEther("0.6"), 30, 122, 1198, this.verifierAddress]);
await expect(this.gov.createProposal(emptyOptions.getAddress(), {value: this.proposalFee})).to.be.revertedWith("proposal options are empty");
await expect(this.gov.createProposal(tooManyOptions.getAddress(), {value: this.proposalFee})).to.be.revertedWith("too many options");
await expect(this.gov.createProposal(wrongVotes.getAddress(), {value: this.proposalFee}))
.to.be.revertedWithCustomError(
this.verifier,
"MinVotesTooSmall"
)
.withArgs(ethers.parseEther("0.3"), ethers.parseEther("0.4"));
await expect(this.gov.createProposal(manyOptions.getAddress())).to.be.revertedWith("paid proposal fee is wrong");
await expect(this.gov.createProposal(manyOptions.getAddress(), {value: this.proposalFee+1n})).to.be.revertedWith("paid proposal fee is wrong");
await this.gov.createProposal(manyOptions.getAddress(), {value: this.proposalFee});
await this.gov.createProposal(oneOption.getAddress(), {value: this.proposalFee});
const infoManyOptions = await this.gov.proposalParams(1);
expect(infoManyOptions.pType).to.be.equal(pType);
expect(infoManyOptions.executable).to.be.equal(NonExecutableType);
expect(infoManyOptions.minVotes).to.be.equal(ethers.parseEther("0.5"));
expect(infoManyOptions.proposalContract).to.equal(await manyOptions.getAddress());
expect(infoManyOptions.options.length).to.equal(10);
expect(infoManyOptions.options[0]).to.equal("0x6f70740000000000000000000000000000000000000000000000000000000000");
expect(infoManyOptions.votingStartTime).to.be.least(now);
expect(infoManyOptions.votingMinEndTime).to.be.equal(infoManyOptions.votingStartTime+121n);
expect(infoManyOptions.votingMaxEndTime).to.be.equal(infoManyOptions.votingStartTime+1199n);
const infoOneOption = await this.gov.proposalParams(2);
expect(infoOneOption.pType).to.be.equal(pType);
expect(infoOneOption.executable).to.be.equal(NonExecutableType);
expect(infoOneOption.minVotes).to.be.equal(ethers.parseEther("0.51"));
expect(infoOneOption.proposalContract).to.equal(await oneOption.getAddress());
expect(infoOneOption.options.length).to.equal(1);
expect(infoOneOption.votingStartTime).to.be.least(now);
expect(infoOneOption.votingMinEndTime).to.be.equal(infoOneOption.votingStartTime+122n);
expect(infoOneOption.votingMaxEndTime).to.be.equal(infoOneOption.votingStartTime+1198n);
});
it("checking proposal verification with explicit timestamps and opinions", async function () {
// check code which can be checked only by explicitly setting timestamps and opinions
const pType = 999; // non-standard proposal type
await this.verifier.addTemplate(pType, "custom", ethers.ZeroAddress, DelegateCallType, ethers.parseEther("0.4"), ethers.parseEther("0.6"), scales, 1000, 10000, 400, 2000);
const now = await time.latest();
const start = now + 500;
const minEnd = start + 1000;
const maxEnd = minEnd + 1000;
const proposal = await ethers.deployContract("ExplicitProposal");
await proposal.setType(pType);
await proposal.setMinVotes(ethers.parseEther("0.4"));
await proposal.setMinAgreement(ethers.parseEther("0.6"));
await proposal.setOpinionScales(scales);
await proposal.setVotingStartTime(start);
await proposal.setVotingMinEndTime(minEnd);
await proposal.setVotingMaxEndTime(maxEnd);
await proposal.setExecutable(DelegateCallType);
await proposal.verifyProposalParams(this.verifierAddress);
await proposal.setVotingStartTime(now-10); // starts in past
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"StartIsInThePast"
);
await proposal.setVotingStartTime(start);
await proposal.setVotingMinEndTime(start-1); // may end before the start
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"StartIsAfterMinEnd"
)
await proposal.setVotingMinEndTime(minEnd);
await proposal.setVotingMaxEndTime(start-1); // must end before the start
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"MinEndIsAfterMaxEnd"
)
await proposal.setVotingMaxEndTime(maxEnd);
await proposal.setVotingMaxEndTime(minEnd-1); // min > max
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"MinEndIsAfterMaxEnd"
)
await proposal.setVotingMaxEndTime(maxEnd);
await proposal.setType(pType - 1); // wrong type
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"UnknownTemplate"
)
.withArgs(pType - 1)
await proposal.setType(pType);
await proposal.setOpinionScales([]); // wrong scales
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"OpinionScalesLengthMismatch"
)
.withArgs(0, 5)
await proposal.setOpinionScales([1]); // wrong scales
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"OpinionScalesLengthMismatch"
)
.withArgs(1, 5)
await proposal.setOpinionScales([1, 2, 3, 4, 5]); // wrong scales
await expect(proposal.verifyProposalParams(this.verifierAddress))
.to.be.revertedWithCustomError(
this.verifier,
"OpinionScalesMismatch"
)
.withArgs(1,0,0)
await proposal.setOpinionScales(scales);
await proposal.verifyProposalParams(this.verifierAddress)
});
it("checking creation and execution of network parameter proposals via proposal factory", async function () {
const optionsNum = ethers.parseEther("1"); // use maximum number of options to test gas usage
const choices = [4n];
const voteEnd = 120;
const consts = await initConsts(this.defaultAcc);
expect((await consts.minSelfStake()).toString()).to.equals("317500000000000000");
if (await this.verifier.exists(15) === false) {
await this.verifier.addTemplate(6003, "NetworkParameterProposal", ethers.ZeroAddress, DelegateCallType, ethers.parseEther("0.0"), ethers.parseEther("0.0"), [0, 1, 2, 3, 4], 0, 100000000, 0, 100000000);
}
const updateMinSelfStake = await ethers.deployContract(
"NetworkParameterProposal",
[
"Network",
1,
[optionsNum],
await consts.getAddress(),
ethers.parseEther("0.5"),
ethers.parseEther("0.6"),
0,
1,
voteEnd,
this.verifierAddress,
]
)
// make new vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.createProposal(updateMinSelfStake.getAddress(), {value: this.proposalFee});
const proposalIdOne = await this.gov.lastProposalID();
await this.gov.vote(this.defaultAcc, proposalIdOne, choices);
// Advance time by voteEnd + 1 to make sure the voting has ended
await time.increase(voteEnd+1);
// Execute the proposal
await this.gov.handleTasks(0, 1);
expect((await consts.minSelfStake()).toString()).to.equals(optionsNum);
});
it("checking self-vote creation", async function () {
const optionsNum = 3;
const choices = [0n, 3n, 4n];
await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 60);
const proposalID = await this.gov.lastProposalID();
// Voting has not begun yet
await expect(this.gov.vote(this.defaultAcc, proposalID, choices)).to.be.revertedWith("proposal voting hasn't begun");
// Forward time to stat the voting
await time.increase(60);
// Voting with no stake
await expect(this.gov.vote(this.defaultAcc, proposalID, choices)).to.be.revertedWith("zero weight");
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
// Non-existent proposal
await expect(this.gov.vote(this.defaultAcc, proposalID+1n, choices)).to.be.revertedWith("given proposalID doesn't exist");
// Incorrect choices
await expect(this.gov.vote(this.defaultAcc, proposalID, [3n, 4n])).to.be.revertedWith("wrong number of choices");
// Non-existent opinion
await expect(this.gov.vote(this.defaultAcc, proposalID, [5n, 3n, 4n])).to.be.revertedWith("wrong opinion ID");
await this.gov.vote(this.defaultAcc, proposalID, choices);
// Same address vote to same proposal
await expect(this.gov.vote(this.defaultAcc, proposalID, [1n, 3n, 4n])).to.be.revertedWith("vote already exists");
});
describe("checking votes for a self-voter", async function () {
const optionsNum = 3;
const choices = [0n, 3n, 4n];
beforeEach("create vote", async function () {
// Create proposal
await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 60);
this.proposalID = await this.gov.lastProposalID();
// // Make new vote
await time.increase(60);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, this.proposalID, choices);
});
it("checking voting state", async function () {
await this.sfc.stake(this.defaultAcc, ethers.parseEther("5.0"));
// check
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option1 = await this.gov.proposalOptionState(this.proposalID, 1);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("10.0"));
expect(option1.votes).to.be.equal(ethers.parseEther("10.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("10.0"));
expect(option0.agreement).to.be.equal(ethers.parseEther("0.0"));
expect(option1.agreement).to.be.equal(ethers.parseEther("8.0"));
expect(option2.agreement).to.be.equal(ethers.parseEther("10.0"));
expect(option0.agreementRatio).to.be.equal(ethers.parseEther("0.0"));
expect(option1.agreementRatio).to.be.equal(ethers.parseEther("0.8"));
expect(option2.agreementRatio).to.be.equal(ethers.parseEther("1.0"));
const votingInfo = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfo.proposalResolved).to.equal(true);
expect(votingInfo.winnerID).to.be.equal(2n); // option with a best opinion
expect(votingInfo.votes).to.be.equal(ethers.parseEther("10.0"));
// clean up
await this.gov.cancelVote(this.defaultAcc, this.proposalID);
});
it("cancel vote", async function () {
await expect(this.gov.cancelVote(this.defaultAcc, this.proposalID+1n)).to.be.revertedWith("doesn't exist");
await expect(this.gov.cancelVote(this.otherAcc, this.proposalID)).to.be.revertedWith("doesn't exist");
await this.gov.cancelVote(this.defaultAcc, this.proposalID);
// vote should be erased, checked by afterEach
});
it("recount vote", async function () {
await this.sfc.stake(this.defaultAcc, ethers.parseEther("5.0"));
await expect(this.gov.recountVote(this.otherAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc})).to.be.revertedWith("doesn't exist");
await expect(this.gov.recountVote(this.defaultAcc, this.otherAcc, this.proposalID, {from: this.defaultAcc})).to.be.revertedWith("doesn't exist");
await this.gov.recountVote(this.defaultAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc}); // anyone can send
await expect(this.gov.recountVote(this.defaultAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc})).to.be.revertedWith("nothing changed");
// check
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("15.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option1 = await this.gov.proposalOptionState(this.proposalID, 1);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("15.0"));
expect(option1.votes).to.be.equal(ethers.parseEther("15.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("15.0"));
expect(option0.agreement).to.be.equal(ethers.parseEther("0.0"));
expect(option1.agreement).to.be.equal(ethers.parseEther("12.0"));
expect(option2.agreement).to.be.equal(ethers.parseEther("15.0"));
expect(option0.agreementRatio).to.be.equal(ethers.parseEther("0.0"));
expect(option1.agreementRatio).to.be.equal(ethers.parseEther("0.8"));
expect(option2.agreementRatio).to.be.equal(ethers.parseEther("1.0"));
const votingInfo = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfo.proposalResolved).to.equal(true);
expect(votingInfo.winnerID).to.be.equal(2n); // option with a best opinion
expect(votingInfo.votes).to.be.equal(ethers.parseEther("15.0"));
// clean up
await this.gov.cancelVote(this.defaultAcc, this.proposalID);
});
it("cancel vote via recounting", async function () {
this.sfc.unstake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.recountVote(this.defaultAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc});
await expect(this.gov.recountVote(this.defaultAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc})).to.be.revertedWith("doesn't exist");
// vote should be erased, checked by afterEach
});
it("cancel vote via recounting from VotesBookKeeper", async function () {
this.sfc.unstake(this.defaultAcc, ethers.parseEther("10.0"));
await this.votebook.recountVotes(this.defaultAcc, this.defaultAcc, {from: this.defaultAcc});
expect(await this.votebook.getProposalIDs(this.defaultAcc, this.defaultAcc)).to.be.empty
await expect(this.gov.recountVote(this.defaultAcc, this.defaultAcc, this.proposalID, {from: this.defaultAcc})).to.be.revertedWith("doesn't exist");
// vote should be erased, checked by afterEach
});
afterEach("checking state is empty", async function () {
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("0.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const voteInfo = await this.gov.getVote(this.defaultAcc, this.defaultAcc, this.proposalID);
expect(voteInfo.weight).to.be.equal(ethers.parseEther("0.0"));
expect(voteInfo.choices.length).to.equal(0);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("0.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("0.0"));
expect(option0.agreement).to.be.equal(ethers.parseEther("0.0"));
expect(option2.agreement).to.be.equal(ethers.parseEther("0.0"));
expect(option0.agreementRatio).to.be.equal(ethers.parseEther("0.0"));
expect(option2.agreementRatio).to.be.equal(ethers.parseEther("0.0"));
const votingInfo = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfo.proposalResolved).to.equal(false);
expect(votingInfo.winnerID).to.be.equal(optionsNum);
expect(votingInfo.votes).to.be.equal(ethers.parseEther("0.0"));
await expect(this.gov.handleTasks(0, 1)).to.be.revertedWith("no tasks handled");
await expect(this.gov.tasksCleanup(1)).to.be.revertedWith("no tasks erased");
});
});
it("checking voting tally for a self-voter", async function () {
const optionsNum = 10; // use maximum number of options to test gas usage
const choices = [2n, 2n, 3n, 2n, 2n, 2n, 2n, 2n, 2n, 2n];
const proposalContract = await createProposal(this.gov, this.verifier, DelegateCallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 60, 120);
const proposalID = await this.gov.lastProposalID();
// make new vote
await time.increase(60);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// check proposal isn't executed
expect(await proposalContract.executedCounter()).to.be.equal(0n);
// check voting is ready to be finalized
const votingInfo = await this.gov.calculateVotingTally(proposalID);
expect(votingInfo.proposalResolved).to.equal(true);
expect(votingInfo.winnerID).to.be.equal(2n); // option with a best opinion
expect(votingInfo.votes).to.be.equal(ethers.parseEther("10.0"));
// finalize voting by handling its task
const task = await this.gov.getTask(0);
expect(await this.gov.tasksCount()).to.be.equal(1n);
expect(task.active).to.equal(true);
expect(task.assignment).to.be.equal(1n);
expect(task.proposalID).to.be.equal(proposalID);
await expect(this.gov.handleTasks(0, 1)).to.be.revertedWith("no tasks handled");
await time.increase(120); // wait until min voting end time
await expect(this.gov.handleTasks(1, 1)).to.be.revertedWith("no tasks handled");
await this.gov.handleTasks(0, 1);
await expect(this.gov.handleTasks(0, 1)).to.be.revertedWith("no tasks handled");
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(2n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
// check proposal execution via delegatecall
expect(await proposalContract.executedCounter()).to.be.equal(1n);
expect(await proposalContract.executedMsgSender()).to.equal(this.defaultAcc);
expect(await proposalContract.executedAs()).to.equal(await this.gov.getAddress());
expect(await proposalContract.executedOption()).to.be.equal(2n);
// try to cancel vote
await expect(this.gov.cancelVote(this.defaultAcc, proposalID)).to.be.revertedWith("proposal isn't active");
// try to recount vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("5.0"));
await expect(this.gov.recountVote(this.defaultAcc, this.defaultAcc, proposalID, {from: this.defaultAcc})).to.be.revertedWith("proposal isn't active");
// cleanup task
const taskDeactivated = await this.gov.getTask(0);
expect(await this.gov.tasksCount()).to.be.equal(1n);
expect(taskDeactivated.active).to.equal(false);
expect(taskDeactivated.assignment).to.be.equal(1n);
expect(taskDeactivated.proposalID).to.be.equal(proposalID);
await expect(this.gov.tasksCleanup(0)).to.be.revertedWith("no tasks erased");
await this.gov.tasksCleanup(10);
expect(await this.gov.tasksCount()).to.be.equal(0n);
});
it("checking proposal execution via call", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [4n];
const proposalContract = await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 0, 120);
const proposalID = await this.gov.lastProposalID();
// make new vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await time.increase(120); // wait until min voting end time
await this.gov.handleTasks(0, 1);
// check proposal execution via call
expect(await proposalContract.executedCounter()).to.be.equal(1n);
expect(await proposalContract.executedMsgSender()).to.equal(await this.gov.getAddress());
expect(await proposalContract.executedAs()).to.equal(await proposalContract.getAddress());
expect(await proposalContract.executedOption()).to.be.equal(0n);
});
it("checking proposal execution via delegatecall", async function () {
const optionsNum = 1;
const choices = [4n];
const proposalContract = await createProposal(this.gov, this.verifier, DelegateCallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 0, 120);
const proposalID = await this.gov.lastProposalID();
// make new vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await time.increase(120); // wait until min voting end time
await this.gov.handleTasks(0, 1);
// check proposal execution via delegatecall
expect(await proposalContract.executedCounter()).to.be.equal(1n);
expect(await proposalContract.executedMsgSender()).to.equal(this.defaultAcc);
expect(await proposalContract.executedAs()).to.equal(await this.gov.getAddress());
expect(await proposalContract.executedOption()).to.be.equal(0n);
});
it("checking non-executable proposal resolving", async function () {
const optionsNum = 2;
const choices = [0n, 4n];
const proposalContract = await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 0, 120);
const proposalID = await this.gov.lastProposalID();
// make new vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await time.increase(120); // wait until min voting end time
await this.gov.handleTasks(0, 1);
// check proposal execution via delegatecall
expect(await proposalContract.executedCounter()).to.be.equal(0n);
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(1n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
});
it("checking proposal rejecting before max voting end is reached", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [0n];
const proposalContract = await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), 0, 120, 240);
const proposalID = await this.gov.lastProposalID();
// make new vote
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await time.increase(120); // wait until min voting end time
await this.gov.handleTasks(0, 1);
// check proposal is rejected
expect(await proposalContract.executedCounter()).to.be.equal(0n);
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.FAILED);
});
it("checking voting tally with low turnout", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [2n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
// Advance time to be between min and max end
await time.increase(start + minEnd + 10);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.1")); // turnout is less than 50% now, and maxEnd has occurred
await expect(this.gov.handleTasks(0, 1)).to.be.revertedWith("no tasks handled");
await this.sfc.unstake(this.defaultAcc, ethers.parseEther("0.1")); // turnout is exactly 50% now
// finalize voting by handling its task
await this.gov.handleTasks(0, 10);
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
});
it("checking execution expiration", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [2n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
const proposalContract = await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
const maxExecutionPeriod = await this.gov.maxExecutionPeriod();
// Advance time to be over maxExecutionPeriod
await time.increase(maxExecutionPeriod + BigInt(start) + BigInt(maxEnd) + 10n);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await this.gov.handleTasks(0, 10);
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.EXECUTION_EXPIRED);
// check proposal isn't executed
expect(await proposalContract.executedCounter()).to.be.equal(0n);
});
it("checking proposal is rejected if low agreement after max voting end", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [1n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
const proposalContract = await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
const maxExecutionPeriod = await this.gov.maxExecutionPeriod();
// Advance time to be over maxExecutionPeriod
await time.increase(maxExecutionPeriod + BigInt(start) + BigInt(maxEnd) + 10n);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await this.gov.handleTasks(0, 10);
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.FAILED);
// check proposal isn't executed
expect(await proposalContract.executedCounter()).to.be.equal(0n);
});
it("checking execution doesn't expire earlier than needed", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [2n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
const proposalContract = await createProposal(this.gov, this.verifier, DelegateCallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
const maxExecutionPeriod = await this.gov.maxExecutionPeriod();
// Advance time not to exceed maxExecutionPeriod
await time.increase(maxExecutionPeriod + BigInt(start) + BigInt(maxEnd) - 10n);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await this.gov.handleTasks(0, 10);
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
// check proposal is executed
expect(await proposalContract.executedCounter()).to.be.equal(1n);
});
it("checking proposal cancellation", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [2n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
const proposalContract = await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
const maxExecutionPeriod = await this.gov.maxExecutionPeriod();
// Advance time not to exceed maxExecutionPeriod
await time.increase(maxExecutionPeriod + BigInt(start) + BigInt(maxEnd) - 10n);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// try to cancel proposal
await expect(this.gov.cancelProposal(proposalID+1n)).to.be.revertedWith("given proposalID doesn't exist");
await expect(this.gov.cancelProposal(proposalID)).to.be.revertedWith("voting has already begun");
await this.gov.cancelVote(this.defaultAcc, proposalID);
await expect(this.gov.cancelProposal(proposalID)).to.be.revertedWith("sender not the proposal address");
await proposalContract.cancel(proposalID, this.gov.getAddress());
await expect(this.gov.cancelProposal(proposalID)).to.be.revertedWith("proposal isn't active");
await expect(this.gov.vote(this.defaultAcc, proposalID, choices)).to.be.revertedWith("proposal isn't active");
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("0.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.CANCELED);
// handle task
await this.gov.handleTasks(0, 1);
await this.gov.tasksCleanup(1);
// check proposal status hasn't changed after the task
const proposalStateInfoAfterTask = await this.gov.proposalState(proposalID);
expect(proposalStateInfoAfterTask.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfoAfterTask.votes).to.be.equal(ethers.parseEther("0.0"));
expect(proposalStateInfoAfterTask.status).to.be.equal(3n);
// check proposal isn't executed
expect(await proposalContract.executedCounter()).to.be.equal(0n);
});
it("checking handling multiple tasks", async function () {
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0"));
const optionsNum = 1; // use maximum number of options to test gas usage
const start = 0;
const minEnd = 500;
const maxEnd = 1000;
// make 5 proposals which are ready for a finalization
for (const i of [0, 1, 2, 3, 4]) {
const choices = [2n];
const proposalContract = await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
// make a vote
await this.gov.vote(this.defaultAcc, proposalID, choices);
}
// Advance time over earliest possible voting end
await time.increase(minEnd + 10);
expect(await this.gov.tasksCount()).to.be.equal(5n);
await this.gov.handleTasks(1, 3);
for (const i of [1, 2, 3]) {
const proposalID = i + 1;
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
// check task status
const task = await this.gov.getTask(i);
expect(task.active).to.equal(false);
}
for (const i of [0, 4]) {
const proposalID = i + 1;
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
// check task status
const task = await this.gov.getTask(i);
expect(task.active).to.equal(true);
}
await expect(this.gov.tasksCleanup(1)).to.be.revertedWith("no tasks erased"); // last task is still active
await this.gov.handleTasks(4, 1); // handle last task
await expect(this.gov.tasksCleanup(0)).to.be.revertedWith("no tasks erased");
await this.gov.tasksCleanup(1);
expect(await this.gov.tasksCount()).to.be.equal(4n);
await this.gov.tasksCleanup(4);
expect(await this.gov.tasksCount()).to.be.equal(1n); // first task is still active
await expect(this.gov.tasksCleanup(1)).to.be.revertedWith("no tasks erased");
await this.gov.handleTasks(0, 1); // handle first task
await this.gov.tasksCleanup(1);
expect(await this.gov.tasksCount()).to.be.equal(0n);
for (const i of [0, 1, 2, 3, 4]) {
const proposalID = i + 1;
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.RESOLVED);
}
});
it("checking proposal is rejected if low turnout after max voting end", async function () {
const optionsNum = 1; // use maximum number of options to test gas usage
const choices = [4n];
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
const proposalContract = await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
const maxExecutionPeriod = await this.gov.maxExecutionPeriod();
// Advance time to be over maxExecutionPeriod
await time.increase(maxExecutionPeriod + BigInt(start) + BigInt(maxEnd) + 10n);
await this.sfc.stake(this.defaultAcc, ethers.parseEther("10.0")); // defaultAcc has less than 50% of weight
await this.sfc.stake(this.firstVoterAcc, ethers.parseEther("11.0"));
await this.gov.vote(this.defaultAcc, proposalID, choices);
// finalize voting by handling its task
await this.gov.handleTasks(0, 10);
// check proposal status
const proposalStateInfo = await this.gov.proposalState(proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("10.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.FAILED);
// check proposal isn't executed
expect(await proposalContract.executedCounter()).to.be.equal(0n);
});
it("checking delegation vote creation", async function () {
const optionsNum = 3;
const choices0 = [0n, 3n, 4n];
const choices1 = [1n, 2n, 3n];
const start = 60;
const minEnd = 120;
const maxEnd = 1200;
const proposalContract = await createProposal(this.gov, this.verifier, NonExecutableType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
const proposalID = await this.gov.lastProposalID();
// Advance time to start voting
await time.increase(start);
await this.sfc.connect(this.firstVoterAcc).stake(this.firstVoterAcc, ethers.parseEther("10.0"));
await this.gov.connect(this.firstVoterAcc).vote(this.firstVoterAcc, proposalID, choices0);
await expect(this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, proposalID, choices1)).to.be.revertedWith("zero weight");
await this.sfc.connect(this.delegatorAcc).stake(this.firstVoterAcc, ethers.parseEther("10.0"));
await expect(this.gov.connect(this.delegatorAcc).vote(this.delegatorAcc, proposalID, choices1)).to.be.revertedWith("zero weight");
await expect(this.gov.connect(this.delegatorAcc).vote(this.otherAcc, proposalID, choices1)).to.be.revertedWith("zero weight");
await expect(this.gov.connect(this.firstVoterAcc).vote(this.delegatorAcc, proposalID, choices1)).to.be.revertedWith("zero weight");
await expect(this.gov.vote(this.firstVoterAcc, proposalID + 1n, choices1), "given proposalID doesn't exist");
await expect(this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, proposalID, [3n, 4n]), "wrong number of choices");
await expect(this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, proposalID, [3n, 4n, 5n]), "wrong opinion ID");
await this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, proposalID, choices1);
await expect(this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, proposalID, [1n, 3n, 4n]), "vote already exists");
});
var votersAndDelegatorsTests = (delegatorFirst: boolean) => {
return async function () {
const optionsNum = 3;
beforeEach("create vote", async function () {
await this.sfc.addValidator(3, 0, this.secondVoterAcc)
const start = 60;
const minEnd = 500;
const maxEnd = 1000;
await createProposal(this.gov, this.verifier, CallType, optionsNum, ethers.parseEther("0.5"), ethers.parseEther("0.6"), start, minEnd, maxEnd);
this.proposalID = await this.gov.lastProposalID();
// make the new votes
await time.increase(start + 10);
if (delegatorFirst) {
await this.sfc.connect(this.delegatorAcc).stake(this.firstVoterAcc, ethers.parseEther("30.0"));
await this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, this.proposalID, [1n, 2n, 3n]);
}
await this.sfc.connect(this.firstVoterAcc).stake(this.firstVoterAcc, ethers.parseEther("10.0"));
await this.gov.connect(this.firstVoterAcc).vote(this.firstVoterAcc, this.proposalID, [3n, 2n, 0n]);
await this.sfc.connect(this.secondVoterAcc).stake(this.secondVoterAcc, ethers.parseEther("20.0"));
await this.gov.connect(this.secondVoterAcc).vote(this.secondVoterAcc, this.proposalID, [2n, 3n, 4n]);
if (!delegatorFirst) {
await this.sfc.connect(this.delegatorAcc).stake(this.firstVoterAcc, ethers.parseEther("30.0"));
await this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, this.proposalID, [1n, 2n, 3n]);
}
});
it("cancel votes", async function () {
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID)).to.be.equal(ethers.parseEther("30.0"));
const votingInfoAfter = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfoAfter.votes).to.be.equal(ethers.parseEther("30.0"));
expect(votingInfoAfter.proposalResolved).to.equal(true);
expect(votingInfoAfter.winnerID).to.be.equal(2n); // option with a best opinion
await expect(this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID+1n)).revertedWith("doesn't exist");
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("cancel votes in reversed order", async function () {
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("checking voting state", async function () {
// check
await checkFullVotes(this.firstVoterAcc, this.secondVoterAcc, this.delegatorAcc, this.proposalID, this.gov);
// clean up
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("checking voting state after delegator re-voting", async function () {
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID, {from: this.delegatorAcc});
await expect(this.gov.cancelVote(this.firstVoterAcc, this.proposalID)).to.be.revertedWith("doesn't exist");
await this.gov.connect(this.delegatorAcc).vote(this.firstVoterAcc, this.proposalID, [1n, 2n, 3n]);
// check
await checkFullVotes(this.firstVoterAcc, this.secondVoterAcc, this.delegatorAcc, this.proposalID, this.gov);
// clean up
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("checking voting state after first voter re-voting", async function () {
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await expect(this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID)).to.be.revertedWith("doesn't exist");
await this.gov.connect(this.firstVoterAcc).vote(this.firstVoterAcc, this.proposalID, [3n, 2n, 0n]);
// check
await checkFullVotes(this.firstVoterAcc, this.secondVoterAcc, this.delegatorAcc, this.proposalID, this.gov);
// clean up
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("checking voting state after second voter re-voting", async function () {
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await expect(this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID)).to.be.revertedWith("doesn't exist");
await this.gov.connect(this.secondVoterAcc).vote(this.secondVoterAcc, this.proposalID, [2n, 3n, 4n]);
// check
await checkFullVotes(this.firstVoterAcc, this.secondVoterAcc, this.delegatorAcc, this.proposalID, this.gov);
// clean up
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
});
it("checking voting state after delegator vote canceling", async function () {
// cancel delegator vote
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID, {from: this.delegatorAcc});
// check
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID-1n)).to.be.equal(ethers.parseEther("0.0"));
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID)).to.be.equal(ethers.parseEther("0.0"));
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("60.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option1 = await this.gov.proposalOptionState(this.proposalID, 1);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("60.0"));
expect(option1.votes).to.be.equal(ethers.parseEther("60.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("60.0"));
expect(option0.agreement).to.be.equal(ethers.parseEther("44"));
expect(option1.agreement).to.be.equal(ethers.parseEther("40"));
expect(option2.agreement).to.be.equal(ethers.parseEther("20"));
expect(option0.agreementRatio).to.be.equal(ethers.parseEther("0.733333333333333333"));
expect(option1.agreementRatio).to.be.equal(ethers.parseEther("0.666666666666666666"));
expect(option2.agreementRatio).to.be.equal(ethers.parseEther("0.333333333333333333"));
const votingInfo = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfo.proposalResolved).to.equal(true);
expect(votingInfo.winnerID).to.be.equal(0n); // option with a best opinion
expect(votingInfo.votes).to.be.equal(ethers.parseEther("60.0"));
// clean up
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
});
it("checking voting state after first staker vote canceling", async function () {
// cancel first voter vote
await this.gov.connect(this.firstVoterAcc).cancelVote(this.firstVoterAcc, this.proposalID);
// check
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID-1n)).to.be.equal(ethers.parseEther("0.0"));
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID)).to.be.equal(ethers.parseEther("30.0"));
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("50.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option1 = await this.gov.proposalOptionState(this.proposalID, 1);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("50.0"));
expect(option1.votes).to.be.equal(ethers.parseEther("50.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("50.0"));
expect(option0.agreement).to.be.equal(ethers.parseEther("24"));
expect(option1.agreement).to.be.equal(ethers.parseEther("34"));
expect(option2.agreement).to.be.equal(ethers.parseEther("44"));
expect(option0.agreementRatio).to.be.equal(ethers.parseEther("0.48"));
expect(option1.agreementRatio).to.be.equal(ethers.parseEther("0.68"));
expect(option2.agreementRatio).to.be.equal(ethers.parseEther("0.88"));
const votingInfo = await this.gov.calculateVotingTally(this.proposalID);
expect(votingInfo.proposalResolved).to.equal(true);
expect(votingInfo.winnerID).to.be.equal(2n); // option with a best opinion
expect(votingInfo.votes).to.be.equal(ethers.parseEther("50.0"));
// clean up
await this.gov.connect(this.delegatorAcc).cancelVote(this.firstVoterAcc, this.proposalID);
await this.gov.connect(this.secondVoterAcc).cancelVote(this.secondVoterAcc, this.proposalID);
});
it("checking voting state after delegator recounting", async function () {
await this.sfc.connect(this.delegatorAcc).unstake(this.firstVoterAcc, ethers.parseEther("5.0"));
await this.gov.connect(this.otherAcc).recountVote(this.delegatorAcc, this.firstVoterAcc, this.proposalID);
// check
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID-1n)).to.be.equal(ethers.parseEther("0.0"));
expect(await this.gov.overriddenWeight(this.firstVoterAcc, this.proposalID)).to.be.equal(ethers.parseEther("25.0"));
const proposalStateInfo = await this.gov.proposalState(this.proposalID);
expect(proposalStateInfo.winnerOptionID).to.be.equal(0n);
expect(proposalStateInfo.votes).to.be.equal(ethers.parseEther("55.0"));
expect(proposalStateInfo.status).to.be.equal(ProposalStatus.INITIAL);
const option0 = await this.gov.proposalOptionState(this.proposalID, 0);
const option1 = await this.gov.proposalOptionState(this.proposalID, 1);
const option2 = await this.gov.proposalOptionState(this.proposalID, 2);
expect(option0.votes).to.be.equal(ethers.parseEther("55.0"));
expect(option1.votes).to.be.equal(ethers.parseEther("55.0"));
expect(option2.votes).to.be.equal(ethers.parseEther("55.0"));