forked from NeokingdomDAO/whitelabel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Integration.ts
1691 lines (1425 loc) · 59 KB
/
Integration.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 { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import chai from "chai";
import chaiAsPromised from "chai-as-promised";
import { solidity } from "ethereum-waffle";
import { BytesLike } from "ethers";
import { parseEther } from "ethers/lib/utils";
import { ethers, network } from "hardhat";
import {
DAORoles,
GovernanceToken,
IERC20Mintable,
InternalMarket,
NeokingdomToken,
RedemptionController,
ResolutionManager,
ShareholderRegistry,
Voting,
} from "../typechain";
import { ROLES } from "../lib/utils";
import {
getEVMTimestamp,
mineEVMBlock,
setEVMTimestamp,
timeTravel,
} from "./utils/evm";
import { roles } from "./utils/roles";
import { setupDAO } from "./utils/setup";
chai.use(solidity);
chai.use(chaiAsPromised);
const { expect } = chai;
const { MaxUint256 } = ethers.constants;
const e = (v: number) => parseEther(v.toString());
const DAY = 60 * 60 * 24;
const INITIAL_USDC = 1000;
describe("Integration", async () => {
let snapshotId: string;
let offerDurationDays: number;
let settlementPeriod = 7;
let redemptionStartDays: number;
let redemptionWindowDays: number;
let redemptionMaxDaysInThePast: number;
let redemptionActivityWindow: number;
let daoRoles: DAORoles;
let voting: Voting;
let governanceToken: GovernanceToken;
let neokingdomToken: NeokingdomToken;
let resolutionManager: ResolutionManager;
let shareholderRegistry: ShareholderRegistry;
let internalMarket: InternalMarket;
let redemptionController: RedemptionController;
let usdc: IERC20Mintable;
let contributorStatus: string;
let investorStatus: string;
let deployer: SignerWithAddress;
let managingBoard: SignerWithAddress;
let reserve: SignerWithAddress;
let user1: SignerWithAddress;
let user2: SignerWithAddress;
let user3: SignerWithAddress;
let free1: SignerWithAddress;
let free2: SignerWithAddress;
let free3: SignerWithAddress;
before(async () => {
[
deployer,
managingBoard,
reserve,
user1,
user2,
user3,
free1,
free2,
free3,
] = await ethers.getSigners();
const neokingdom = await setupDAO(deployer, reserve);
({
daoRoles,
voting,
governanceToken,
neokingdomToken,
shareholderRegistry,
resolutionManager,
internalMarket,
redemptionController,
usdc,
} = await neokingdom.loadContracts());
const managingBoardStatus =
await shareholderRegistry.MANAGING_BOARD_STATUS();
await shareholderRegistry.mint(managingBoard.address, e(1));
await shareholderRegistry.setStatus(
managingBoardStatus,
managingBoard.address
);
contributorStatus = await shareholderRegistry.CONTRIBUTOR_STATUS();
investorStatus = await shareholderRegistry.INVESTOR_STATUS();
offerDurationDays = (await internalMarket.offerDuration()).toNumber() / DAY;
redemptionStartDays =
(await redemptionController.redemptionStart()).toNumber() / DAY;
redemptionWindowDays =
(await redemptionController.redemptionWindow()).toNumber() / DAY;
redemptionMaxDaysInThePast =
(await redemptionController.maxDaysInThePast()).toNumber() / DAY;
redemptionActivityWindow =
(await redemptionController.activityWindow()).toNumber() / DAY;
await usdc.mint(reserve.address, e(INITIAL_USDC));
await usdc
.connect(reserve)
.approve(internalMarket.address, e(INITIAL_USDC));
await usdc.mint(user1.address, e(INITIAL_USDC));
await usdc.mint(user2.address, e(INITIAL_USDC));
await usdc.mint(user3.address, e(INITIAL_USDC));
for (let signer of [user1, user2, user3, free1, free2, free3]) {
await usdc.connect(signer).approve(internalMarket.address, MaxUint256);
await governanceToken
.connect(signer)
.approve(internalMarket.address, MaxUint256);
await neokingdomToken
.connect(signer)
.approve(governanceToken.address, MaxUint256);
}
});
beforeEach(async () => {
snapshotId = await network.provider.send("evm_snapshot");
});
afterEach(async () => {
await network.provider.send("evm_revert", [snapshotId]);
});
describe("integration", async () => {
let currentResolution: number;
beforeEach(async () => {
currentResolution = 0;
});
async function _mintTokens(user: SignerWithAddress, tokens: number) {
await governanceToken.mint(user.address, e(tokens));
}
async function _makeContributor(user: SignerWithAddress, tokens: number) {
// Make user shareholder
await shareholderRegistry.mint(user.address, e(1));
// Make user contributor
await shareholderRegistry.setStatus(contributorStatus, user.address);
// Mint some tokens
await _mintTokens(user, tokens);
}
async function _makeVotable(resolutionId: number) {
const resolutionObject = await resolutionManager.resolutions(
resolutionId
);
const resolutionType = await resolutionManager.resolutionTypes(
resolutionObject.resolutionTypeId
);
const votingTimestamp =
resolutionObject.approveTimestamp.toNumber() +
resolutionType.noticePeriod.toNumber();
await setEVMTimestamp(votingTimestamp);
}
async function _endResolutionWithId(resolutionId: number) {
const resolutionObject = await resolutionManager.resolutions(
resolutionId
);
const resolutionType = await resolutionManager.resolutionTypes(
resolutionObject.resolutionTypeId
);
const endTimestamp =
resolutionObject.approveTimestamp.toNumber() +
resolutionType.noticePeriod.toNumber() +
resolutionType.votingPeriod.toNumber();
const currentTimestamp = await getEVMTimestamp();
if (currentTimestamp < endTimestamp) {
await setEVMTimestamp(endTimestamp);
}
}
async function _prepareResolution(
type: number = 0,
executionTo: string[] = [],
executionData: BytesLike[] = []
) {
currentResolution++;
await resolutionManager
.connect(user1)
.createResolution("Qxtest", type, false, executionTo, executionData);
await resolutionManager
.connect(managingBoard)
.approveResolution(currentResolution);
return currentResolution;
}
async function _endResolution() {
const votingEndTimestamp = (await getEVMTimestamp()) + DAY * 7;
await setEVMTimestamp(votingEndTimestamp);
await mineEVMBlock();
}
async function _vote(
user: SignerWithAddress,
isYes: boolean,
resolutionId: number
) {
await resolutionManager.connect(user).vote(resolutionId, isYes);
}
async function _delegate(
user1: SignerWithAddress,
user2: SignerWithAddress
) {
await voting.connect(user1).delegate(user2.address);
}
// Mint token to a shareholder
// Promote them to contributor
// Self-delegate
// Give some tokens
// Create and approve resolution
// Contributor votes resolution (yes)
// Resolution passes
it("allows simple DAO management (single contributor)", async () => {
await _makeContributor(user1, 42);
const resolutionId = await _prepareResolution();
await _makeVotable(resolutionId);
await _vote(user1, true, resolutionId);
await _endResolution();
const resolutionResult = await resolutionManager.getResolutionResult(
resolutionId
);
expect(resolutionResult).equal(true);
});
// Mint token to a multiple shareholder
// Promote them to contributor
// Self-delegate
// Give some tokens
// Create and approve resolution
// Enough contributors vote yes to resolution
// Resolution passes
it("successful resolution (multiple contributors)", async () => {
// board member has 1 share
// user1 has 1 share + 65 tokens
// user2 has 1 share + 32 tokens
// total voting power = 100
// resolution type is 0, quorum 66%
await _makeContributor(user1, 65);
await _makeContributor(user2, 32);
const resolutionId = await _prepareResolution();
await _makeVotable(resolutionId);
await _vote(user1, true, resolutionId);
await _endResolution();
const resolutionResult = await resolutionManager.getResolutionResult(
resolutionId
);
expect(resolutionResult).equal(true);
});
// Mint token to a multiple shareholder
// Promote them to contributor
// Self-delegate
// Give some tokens
// Create and approve resolution
// Not enough contributors vote yes to resolution
// Resolution passes
it("unsuccessful resolution (multiple contributors)", async () => {
// board member has 1 share
// user1 has 1 share + 32 tokens = 33 voting power
// user2 has 1 share + 65 tokens = 66 voting power
// total voting power = 100
// resolution type is 0, quorum 66%
await _makeContributor(user1, 32);
await _makeContributor(user2, 65);
const resolutionId = await _prepareResolution();
await _makeVotable(resolutionId);
await _vote(user1, true, resolutionId);
await _endResolution();
const resolutionResult = await resolutionManager.getResolutionResult(
resolutionId
);
expect(resolutionResult).equal(false);
});
it("multiple resolutions, different voting power over time, multiple contributors", async () => {
// board member has 1 share
// user1 has 1 share + 65 tokens
// user2 has 1 share + 32 tokens
// total voting power = 100
// resolution type is 0, quorum 66%
await _makeContributor(user1, 65);
await _makeContributor(user2, 32);
const resolutionId1 = await _prepareResolution();
// board member has 1 share
// user1 has 1 share + 65 tokens
// user2 has 1 share + 32 + 100 tokens
// total voting power = 200
// resolution type is 0, quorum 66% that is 132
await _mintTokens(user2, 100); // make them the most powerful user
const resolutionId2 = await _prepareResolution();
await _makeVotable(resolutionId2); // this will automatically put resolutionId1 also up for voting
await _vote(user1, true, resolutionId1);
await _vote(user1, true, resolutionId2); // this will have a lower voting power
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId1
);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
expect(resolution1Result).equal(true);
expect(resolution2Result).equal(false);
});
it("multiple resolutions, different voting power over time, delegation, multiple contributors", async () => {
// board member has 1 share
// user1 has 1 share + 65 tokens
// user2 has 1 share + 32 tokens
// total voting power = 100
// resolution type is 0, quorum 66%
await _makeContributor(user1, 65);
await _makeContributor(user2, 32);
// user1 voting power = 99
await _delegate(user2, user1);
const resolutionId1 = await _prepareResolution();
// board member has 1 share
// user1 has 1 share + 65 tokens
// user2 has 1 share + 32 + 100 tokens
// total voting power = 200
// resolution type is 0, quorum 66% that is 132
// user1 voting power = 199
await _mintTokens(user2, 100); // user2 has more voting power, but it's transferred to user1 (the delegate)
const resolutionId2 = await _prepareResolution();
await _makeVotable(resolutionId2); // this will automatically put resolutionId1 also up for voting
await _vote(user1, true, resolutionId1);
await _vote(user1, true, resolutionId2);
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId1
);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
expect(resolution1Result).equal(true);
expect(resolution2Result).equal(true);
const resolution1User1 = await resolutionManager.getVoterVote(
resolutionId1,
user1.address
);
expect(resolution1User1.isYes).true;
expect(resolution1User1.votingPower).equal(e(99));
expect(resolution1User1.hasVoted).true;
const resolution1User2 = await resolutionManager.getVoterVote(
resolutionId1,
user2.address
);
expect(resolution1User2.isYes).false;
expect(resolution1User2.votingPower).equal(0);
expect(resolution1User2.hasVoted).false;
const resolution2User1 = await resolutionManager.getVoterVote(
resolutionId2,
user1.address
);
expect(resolution2User1.isYes).true;
expect(resolution2User1.votingPower).equal(e(199));
expect(resolution2User1.hasVoted).true;
const resolution2User2 = await resolutionManager.getVoterVote(
resolutionId2,
user2.address
);
expect(resolution2User2.isYes).false;
expect(resolution2User2.votingPower).equal(0);
expect(resolution2User2.hasVoted).false;
});
it("only with shares, multiple resolutions, different voting power over time, delegation, multiple contributors", async () => {
// board member has 1 share
// user1 has 66 tokens
// user2 has 33 tokens
// total voting power = 100
// resolution type is 0, quorum 66%
await shareholderRegistry.mint(user1.address, e(66));
await shareholderRegistry.mint(user2.address, e(33));
await shareholderRegistry.setStatus(contributorStatus, user1.address);
await shareholderRegistry.setStatus(contributorStatus, user2.address);
// user1 voting power = 99
await _delegate(user2, user1);
const resolutionId1 = await _prepareResolution();
// board member has 1 share
// user1 has 66 tokens
// user2 has 33 + 100 tokens
// total voting power = 200
// resolution type is 0, quorum 66% that is 132
// user1 voting power = 199
await shareholderRegistry.mint(user2.address, e(100)); // user2 has more voting power, but it's transferred to user1 (the delegate)
const resolutionId2 = await _prepareResolution();
await _makeVotable(resolutionId2); // this will automatically put resolutionId1 also up for voting
await _vote(user1, true, resolutionId1);
await _vote(user1, true, resolutionId2);
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId1
);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
expect(resolution1Result).equal(true);
expect(resolution2Result).equal(true);
const resolution1User1 = await resolutionManager.getVoterVote(
resolutionId1,
user1.address
);
expect(resolution1User1.isYes).true;
expect(resolution1User1.votingPower).equal(e(99));
expect(resolution1User1.hasVoted).true;
const resolution1User2 = await resolutionManager.getVoterVote(
resolutionId1,
user2.address
);
expect(resolution1User2.isYes).false;
expect(resolution1User2.votingPower).equal(0);
expect(resolution1User2.hasVoted).false;
const resolution2User1 = await resolutionManager.getVoterVote(
resolutionId2,
user1.address
);
expect(resolution2User1.isYes).true;
expect(resolution2User1.votingPower).equal(e(199));
expect(resolution2User1.hasVoted).true;
const resolution2User2 = await resolutionManager.getVoterVote(
resolutionId2,
user2.address
);
expect(resolution2User2.isYes).false;
expect(resolution2User2.votingPower).equal(0);
expect(resolution2User2.hasVoted).false;
// board member has 1 share
// user1 has 66 - 50 tokens
// user2 has 33 + 100 + 50 tokens
// total voting power = 200
// resolution type is 0, quorum 66% that is 132
// user1 voting power = 199
await shareholderRegistry.transferFrom(
user1.address,
user2.address,
e(50)
);
// New resolution
const resolutionId3 = await _prepareResolution();
await _makeVotable(resolutionId3);
// User1 votes using voting power of user1+user2
await _vote(user1, true, resolutionId3);
const resolution3ResultBefore =
await resolutionManager.getResolutionResult(resolutionId3);
expect(resolution3ResultBefore).equal(true);
const resolution3User1 = await resolutionManager.getVoterVote(
resolutionId3,
user1.address
);
expect(resolution3User1.isYes).true;
expect(resolution3User1.votingPower).equal(e(199));
expect(resolution3User1.hasVoted).true;
const resolution3User2 = await resolutionManager.getVoterVote(
resolutionId3,
user2.address
);
expect(resolution3User2.isYes).false;
expect(resolution3User2.votingPower).equal(0);
expect(resolution3User2.hasVoted).false;
// User2 overrides user1's vote
await _vote(user2, false, resolutionId3);
const resolution3ResultAfter =
await resolutionManager.getResolutionResult(resolutionId3);
expect(resolution3ResultAfter).equal(false);
const resolution3User1After = await resolutionManager.getVoterVote(
resolutionId3,
user1.address
);
expect(resolution3User1After.isYes).true;
expect(resolution3User1After.votingPower).equal(e(16));
expect(resolution3User1After.hasVoted).true;
const resolution3User2After = await resolutionManager.getVoterVote(
resolutionId3,
user2.address
);
expect(resolution3User2After.isYes).false;
expect(resolution3User2After.votingPower).equal(e(183));
expect(resolution3User2After.hasVoted).true;
});
it("invalid voting should not be counted", async () => {
const resolutionId = ++currentResolution;
await _makeContributor(user1, 42);
await resolutionManager
.connect(user1)
.createResolution("Qxtest", 0, false, [], []);
// votes given before approval
await expect(_vote(user1, true, resolutionId)).reverted;
await resolutionManager
.connect(managingBoard)
.approveResolution(currentResolution);
// votes given during notice
await expect(_vote(user1, true, resolutionId)).reverted;
await _makeVotable(resolutionId);
// votes given from non DAO members
await expect(_vote(user2, true, resolutionId)).reverted;
// votes given from less than Contributors
await shareholderRegistry.mint(user2.address, parseEther("1"));
await shareholderRegistry.setStatus(investorStatus, user2.address);
const resolutionId2 = await _prepareResolution();
await _makeVotable(resolutionId2);
await expect(_vote(user2, true, resolutionId)).reverted;
// votes given after burning share
_makeContributor(user3, 42);
await daoRoles.grantRole(await roles.RESOLUTION_ROLE(), deployer.address);
await shareholderRegistry.burn(user3.address, parseEther("1"));
const resolutionId3 = await _prepareResolution();
await _makeVotable(resolutionId3);
await expect(_vote(user3, true, resolutionId3)).reverted;
await _endResolution();
// votes given after closure
await expect(_vote(user1, true, resolutionId)).reverted;
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId
);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
const resolution3Result = await resolutionManager.getResolutionResult(
resolutionId3
);
expect(resolution1Result).false;
expect(resolution2Result).false;
expect(resolution3Result).false;
});
it("distrust contributor", async () => {
// There are 3 contributors
await _makeContributor(user1, 42);
await _makeContributor(user2, 2);
await _makeContributor(user3, 84);
await _delegate(user3, user1);
// A resolution to save the world is created
let resolutionId = await _prepareResolution(6);
await _makeVotable(resolutionId);
// user3 likes mahyem and votes against it, making it fail
await _vote(user1, true, resolutionId);
await _vote(user2, true, resolutionId);
await _vote(user3, false, resolutionId);
expect(await resolutionManager.getResolutionResult(resolutionId)).to.be
.false;
const abi = ["function setStatus(bytes32 status, address account)"];
const iface = new ethers.utils.Interface(abi);
const data = iface.encodeFunctionData("setStatus", [
investorStatus,
user3.address,
]);
// Contributors propose a distrust vote against user3
resolutionId = ++currentResolution;
await resolutionManager
.connect(user1)
.createResolutionWithExclusion(
"Qxdistrust",
0,
[shareholderRegistry.address],
[data],
user3.address
);
await resolutionManager
.connect(managingBoard)
.approveResolution(resolutionId);
await _makeVotable(resolutionId);
// user3 cannot vote and user1 and user2 votes are sufficient to kick user3 out
await _vote(user1, true, resolutionId);
await _vote(user2, true, resolutionId);
await expect(_vote(user3, false, resolutionId)).revertedWith(
"Resolution: account cannot vote"
);
await _endResolution();
await resolutionManager.executeResolution(resolutionId);
// user3 is not a contributor anymore
expect(
await shareholderRegistry.isAtLeast(contributorStatus, user3.address)
).to.be.false;
// a new resolution to save the world is created
resolutionId = await _prepareResolution(6);
await _makeVotable(resolutionId);
// user3 cannot vote it...
await _vote(user1, true, resolutionId);
await _vote(user2, true, resolutionId);
await expect(_vote(user3, false, resolutionId)).revertedWith(
"Resolution: account cannot vote"
);
// user3 cannot offer tokens
await expect(internalMarket.connect(user3).makeOffer(10)).revertedWith(
"InternalMarket: only contributors can make offers"
);
// ... and finally the world is saved.
expect(await resolutionManager.getResolutionResult(resolutionId)).to.be
.true;
});
it("expect chaos", async () => {
await _makeContributor(user1, 60);
await _makeContributor(user2, 30);
await _makeContributor(user3, 10);
await _delegate(user1, user3);
await _delegate(user2, user3);
// -> user 1 voting power == 0 (60)
// -> user 2 voting power == 0 (30)
// -> user 3 voting power == 100
const resolutionId1 = await _prepareResolution();
await _delegate(user1, user1);
await _mintTokens(user3, 50);
// -> user 1 voting power == 60
// -> user 2 voting power == 0 (30)
// -> user 3 voting power == 90
const resolutionId2 = await _prepareResolution();
// User 3 is now investor, they can wrap and unwrap tokens without first
// offering them to the other contributors
await shareholderRegistry.setStatus(investorStatus, user3.address);
await internalMarket.connect(user3).withdraw(user2.address, e(50));
await internalMarket.connect(user2).deposit(e(50));
await governanceToken.settleTokens(user2.address);
await _mintTokens(user2, 50);
// -> user 1 voting power == 60
// -> user 2 voting power == 130
// -> user 3 voting power == 0
const resolutionId3 = await _prepareResolution();
await shareholderRegistry.setStatus(contributorStatus, user3.address);
// -> user 1 voting power == 60
// -> user 2 voting power == 0 (130)
// -> user 3 voting power == 190
const resolutionId4 = await _prepareResolution();
await _makeVotable(resolutionId4); // this will automatically put all resolutions also up for voting
await _vote(user3, true, resolutionId1);
await _vote(user2, false, resolutionId1);
// won't pass
await _vote(user3, true, resolutionId2);
// won't pass
await expect(_vote(user3, true, resolutionId3)).revertedWith(
"Resolution: account cannot vote"
);
await _vote(user2, true, resolutionId3);
// passes
await _vote(user3, true, resolutionId4);
// passes
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId1
);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
const resolution3Result = await resolutionManager.getResolutionResult(
resolutionId3
);
const resolution4Result = await resolutionManager.getResolutionResult(
resolutionId4
);
expect(resolution1Result).equal(true);
expect(resolution2Result).equal(false);
expect(resolution3Result).equal(true);
expect(resolution4Result).equal(true);
});
it("token economics + voting", async () => {
// board member has 1 share
// user1 has 1 share + 47 tokens
// user2 has 1 share + 50 tokens
// total voting power = 100
await _makeContributor(user1, 47);
await _makeContributor(user2, 50);
// Resolution type 6 is "routine", 51% quorum
const resolutionId1 = await _prepareResolution(6);
await _makeVotable(resolutionId1);
await _vote(user1, true, resolutionId1);
await _vote(user2, false, resolutionId1);
const resolution1Result = await resolutionManager.getResolutionResult(
resolutionId1
);
expect(resolution1Result).equal(false);
await expect(
governanceToken.connect(user2).transfer(user3.address, 2)
).revertedWith(
`AccessControl: account ${user2.address.toLowerCase()} is missing role ${
ROLES.MARKET_ROLE
}`
);
await internalMarket.connect(user2).makeOffer(e(2));
await internalMarket.connect(user1).matchOffer(user2.address, e(1));
// board member has 1 share
// user1 has 1 share + 48 tokens
// user2 has 1 share + 49 tokens
// total voting power = 100
const resolutionId2 = await _prepareResolution(6);
await _makeVotable(resolutionId2);
await _vote(user1, true, resolutionId2);
await _vote(user2, false, resolutionId2);
const resolution2Result = await resolutionManager.getResolutionResult(
resolutionId2
);
expect(resolution2Result).equal(false);
// Let 7 days pass, so to unlock tokens from user2
const expirationSeconds = await internalMarket.offerDuration();
const offerExpires =
(await getEVMTimestamp()) + expirationSeconds.toNumber();
await setEVMTimestamp(offerExpires);
await mineEVMBlock();
// Tries first to transfer 2 tokens (because the user forgot that 1 was sold to user 1)
await expect(
internalMarket.connect(user2).withdraw(user3.address, e(2))
).revertedWith("InternalMarket: amount exceeds balance");
// Tries now to transfer the right amount
await internalMarket.connect(user2).withdraw(user3.address, e(1));
// User3 transfers the funds to user1
await neokingdomToken.connect(user3).transfer(user1.address, e(1));
// user1 deposits them so they count for voting
await internalMarket.connect(user1).deposit(e(1));
// board member has 1 share
// user1 has 1 share + 49 tokens
// user2 has 1 share + 49 tokens
// total voting power = 101
const resolutionId3 = await _prepareResolution(6);
await _makeVotable(resolutionId3);
await _vote(user1, true, resolutionId3);
await _vote(user2, false, resolutionId3);
const resolution3Result = await resolutionManager.getResolutionResult(
resolutionId3
);
expect(resolution3Result).equal(false);
// +2 for user1
await _mintTokens(user1, 2);
// -1 for user2
await internalMarket.connect(user2).makeOffer(e(1));
// board member has 1 share
// user1 has 1 share + 51 tokens
// user2 has 1 share + 48 tokens
// total voting power = 102
const resolutionId4 = await _prepareResolution(6);
await _makeVotable(resolutionId4);
await _vote(user1, true, resolutionId4);
await _vote(user2, false, resolutionId4);
const resolution4Result = await resolutionManager.getResolutionResult(
resolutionId4
);
expect(resolution4Result).equal(true);
});
it("internal market + voting power", async () => {
await _makeContributor(user1, 48);
await _makeContributor(user2, 50);
const resolutionId1 = await _prepareResolution(6);
await _makeVotable(resolutionId1);
expect(await voting.getVotingPower(user1.address)).equal(e(49));
expect(await voting.getVotingPower(user2.address)).equal(e(51));
await internalMarket.connect(user2).makeOffer(e(2));
await internalMarket.connect(user1).matchOffer(user2.address, e(1));
const resolutionId2 = await _prepareResolution(6);
await _makeVotable(resolutionId2);
expect(await voting.getVotingPower(user1.address)).equal(e(50));
expect(await voting.getVotingPower(user2.address)).equal(e(49));
await setEVMTimestamp((await getEVMTimestamp()) + DAY * 8);
// An internal token is swapped for an external one, so user2 loses 1 vote
await internalMarket.connect(user2).withdraw(user2.address, e(1));
const resolutionId3 = await _prepareResolution(6);
await _makeVotable(resolutionId3);
expect(await voting.getVotingPower(user1.address)).equal(e(50));
expect(await voting.getVotingPower(user2.address)).equal(e(49));
});
// Mint 50 tokens to A
// Mint 100 tokens to B
// Mint 1 token to C
// B offers 60 tokens
// C buys 10 from B
// A buys 40 tokens from B
// C offers 5 tokens
// A offers 10 tokens
// B buys 10 tokens from A
// B offers 10 tokens, 2 days later
// C buys 15 tokens from B
// C transfers 10 tokens to A, fails
// B first offers expires
// C offers expires
// B transfers 5 tokens to C, fails
// Check
// B last offer expires
// B transfers 5 tokens to C
// Check
it("complex tokenomics", async () => {
await _makeContributor(user1, 50);
await _makeContributor(user2, 100);
await _makeContributor(user3, 1);
await internalMarket.connect(user2).makeOffer(e(60));
await internalMarket.connect(user3).matchOffer(user2.address, e(10));
await internalMarket.connect(user1).matchOffer(user2.address, e(40));
await internalMarket.connect(user3).makeOffer(e(5));
await internalMarket.connect(user1).makeOffer(e(10));
await internalMarket.connect(user2).matchOffer(user1.address, e(10));
// Two days pass
let offerExpires = (await getEVMTimestamp()) + 2 * DAY;
await setEVMTimestamp(offerExpires);
await mineEVMBlock();
await internalMarket.connect(user2).makeOffer(e(10));
await internalMarket.connect(user3).matchOffer(user2.address, e(15));
await expect(
internalMarket.connect(user3).withdraw(user1.address, e(10))
).revertedWith("InternalMarket: amount exceeds balance");
// 5 days pass (first offer expires)
offerExpires = (await getEVMTimestamp()) + 5 * DAY;
await setEVMTimestamp(offerExpires);
await mineEVMBlock();
// Still fails, because first offers was already drained
await expect(
internalMarket.connect(user2).withdraw(user3.address, e(5))
).revertedWith("InternalMarket: amount exceeds balance");
expect(await governanceToken.balanceOf(user1.address)).equal(e(80));
expect(await internalMarket.offeredBalanceOf(user1.address)).equal(0);
expect(await internalMarket.withdrawableBalanceOf(user1.address)).equal(
0
);
expect(await governanceToken.balanceOf(user2.address)).equal(e(40));
expect(await internalMarket.offeredBalanceOf(user2.address)).equal(e(5));
expect(await internalMarket.withdrawableBalanceOf(user2.address)).equal(
0
);
expect(await governanceToken.balanceOf(user3.address)).equal(e(21));
expect(await internalMarket.offeredBalanceOf(user3.address)).equal(0);
expect(await internalMarket.withdrawableBalanceOf(user3.address)).equal(
e(5)
);
// 3 days pass (last user2 offer expires)
offerExpires = (await getEVMTimestamp()) + 3 * DAY;
await setEVMTimestamp(offerExpires);
await mineEVMBlock();
// first tries wrong amount
await expect(
internalMarket.connect(user2).withdraw(user3.address, e(10))
).revertedWith("InternalMarket: amount exceeds balance");
await internalMarket.connect(user2).withdraw(user3.address, e(5));
expect(await governanceToken.balanceOf(user2.address)).equal(e(40));
expect(await internalMarket.offeredBalanceOf(user2.address)).equal(0);
expect(await internalMarket.withdrawableBalanceOf(user2.address)).equal(
0
);
expect(await governanceToken.balanceOf(user3.address)).equal(e(21));
expect(await internalMarket.offeredBalanceOf(user3.address)).equal(0);
expect(await internalMarket.withdrawableBalanceOf(user3.address)).equal(
e(5)
);
await internalMarket.connect(user3).deposit(e(5));
await governanceToken.settleTokens(user3.address);
expect(await governanceToken.balanceOf(user3.address)).equal(e(26));
});
it("mints tokens to a contributor after a resolution passes", async () => {
await _makeContributor(user1, 100);
await _makeContributor(user2, 50);
const abi = ["function mint(address to, uint256 amount)"];
const iface = new ethers.utils.Interface(abi);
const data = iface.encodeFunctionData("mint", [user2.address, e(42)]);
const resolutionId = await _prepareResolution(
6,
[governanceToken.address],
[data]
);