-
Notifications
You must be signed in to change notification settings - Fork 173
/
OptimisticGovernor.js
1325 lines (1133 loc) · 58.2 KB
/
OptimisticGovernor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { assert } = require("chai");
const hre = require("hardhat");
const { web3, getContract, assertEventEmitted, findEvent } = hre;
const {
didContractThrow,
interfaceName,
runDefaultFixture,
TokenRolesEnum,
ZERO_ADDRESS,
RegistryRolesEnum,
} = require("@uma/common");
// const { isEmpty } = require("lodash");
const { hexToUtf8, leftPad, rightPad, utf8ToHex, toWei, toBN /* randomHex, toChecksumAddress */ } = web3.utils;
// Tested contracts
const OptimisticGovernor = getContract("OptimisticGovernorTest");
// Helper contracts
const Finder = getContract("Finder");
const IdentifierWhitelist = getContract("IdentifierWhitelist");
const AddressWhitelist = getContract("AddressWhitelist");
const OptimisticOracleV3Test = getContract("OptimisticOracleV3Test");
const MockOracle = getContract("MockOracleAncillary");
const Timer = getContract("Timer");
const Store = getContract("Store");
const ERC20 = getContract("ExpandedERC20");
const TestnetERC20 = getContract("TestnetERC20");
const TestAvatar = getContract("TestAvatar");
const FullPolicyEscalationManager = getContract("FullPolicyEscalationManager");
const finalFee = toWei("100");
const liveness = 7200;
const bond = toWei("500");
const identifier = utf8ToHex("ZODIAC");
const totalBond = toBN(finalFee).add(toBN(bond)).toString();
const doubleTotalBond = toBN(totalBond).mul(toBN(2)).toString();
const rules = "https://bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi.ipfs.dweb.link/";
const burnedBondPercentage = toWei("0.5");
describe("OptimisticGovernor", () => {
let accounts, owner, proposer, disputer, rando, executor;
let timer,
finder,
collateralWhitelist,
store,
identifierWhitelist,
registry,
bondToken,
mockOracle,
optimisticOracleV3,
optimisticOracleModule,
testToken,
testToken2,
avatar;
const constructTransferTransaction = (destination, amount) => {
return testToken.methods.transfer(destination, amount).encodeABI();
};
// const constructProposalDeleteTransaction = (proposalHash) => {
// return optimisticOracleModule.methods.deleteProposal(proposalHash).encodeABI();
// };
const advanceTime = async (timeIncrease) => {
await timer.methods
.setCurrentTime(Number(await timer.methods.getCurrentTime().call()) + timeIncrease)
.send({ from: owner });
};
before(async function () {
accounts = await web3.eth.getAccounts();
[owner, proposer, disputer, rando, executor] = accounts;
await runDefaultFixture(hre);
timer = await Timer.deployed();
finder = await Finder.deployed();
collateralWhitelist = await AddressWhitelist.deployed();
store = await Store.deployed();
identifierWhitelist = await IdentifierWhitelist.deployed();
registry = await getContract("Registry").deployed();
testToken = await TestnetERC20.new("Test", "TEST", 18).send({ from: accounts[0] });
testToken2 = await TestnetERC20.new("Test2", "TEST2", 18).send({ from: accounts[0] });
// Deploy new MockOracle so that OptimisticOracle disputes can make price requests to it:
mockOracle = await MockOracle.new(finder.options.address, timer.options.address).send({ from: owner });
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.Oracle), mockOracle.options.address)
.send({ from: owner });
await identifierWhitelist.methods.addSupportedIdentifier(identifier).send({ from: owner });
// Deploy new OptimisticOracleV3 and register it with the Finder and Registry:
// TODO: This should be moved to separate fixture. defaultCurrency is not added to the whitelist
// and Store since it is not used in this test, but would be required when moved to a fixture.
const defaultCurrency = await TestnetERC20.new("Default Currency", "DC", 18).send({ from: owner });
optimisticOracleV3 = await OptimisticOracleV3Test.new(
finder.options.address,
defaultCurrency.options.address,
liveness,
timer.options.address
).send({ from: owner });
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.OptimisticOracleV3), optimisticOracleV3.options.address)
.send({ from: owner });
await registry.methods.addMember(RegistryRolesEnum.CONTRACT_CREATOR, owner).send({ from: owner });
await registry.methods.registerContract([], optimisticOracleV3.options.address).send({ from: owner });
await registry.methods.removeMember(RegistryRolesEnum.CONTRACT_CREATOR, owner).send({ from: owner });
});
beforeEach(async function () {
// Deploy new contracts with clean state and perform setup:
avatar = await TestAvatar.new().send({ from: owner });
bondToken = await ERC20.new("BOND", "BOND", 18).send({ from: owner });
await bondToken.methods.addMember(TokenRolesEnum.MINTER, owner).send({ from: owner });
await collateralWhitelist.methods.addToWhitelist(bondToken.options.address).send({ from: owner });
await store.methods.setFinalFee(bondToken.options.address, { rawValue: finalFee }).send({ from: owner });
await optimisticOracleV3.methods.syncUmaParams(identifier, bondToken.options.address).send({ from: owner });
optimisticOracleModule = await OptimisticGovernor.new(
finder.options.address,
avatar.options.address,
bondToken.options.address,
bond,
rules,
identifier,
liveness,
timer.options.address
).send({ from: owner });
await avatar.methods.setModule(optimisticOracleModule.options.address).send({ from: owner });
await bondToken.methods.mint(proposer, doubleTotalBond).send({ from: owner });
await bondToken.methods.approve(optimisticOracleModule.options.address, doubleTotalBond).send({ from: proposer });
await bondToken.methods.mint(disputer, totalBond).send({ from: owner });
await bondToken.methods.approve(optimisticOracleV3.options.address, totalBond).send({ from: disputer });
});
it("Constructor validation", async function () {
// 0 liveness.
assert(
await didContractThrow(
OptimisticGovernor.new(
finder.options.address,
avatar.options.address,
bondToken.options.address,
bond,
rules,
identifier,
0,
timer.options.address
).send({ from: owner })
)
);
// Unapproved token.
assert(
await didContractThrow(
OptimisticGovernor.new(
finder.options.address,
avatar.options.address,
(await ERC20.new("BOND", "BOND", 18).send({ from: owner })).options.address,
bond,
rules,
identifier,
liveness,
timer.options.address
).send({ from: owner })
)
);
// Unapproved identifier.
assert(
await didContractThrow(
OptimisticGovernor.new(
finder.options.address,
avatar.options.address,
bondToken.options.address,
bond,
rules,
utf8ToHex("Unapproved"),
liveness,
timer.options.address
).send({ from: owner })
)
);
});
it("Valid proposals should be hashed and stored and emit event", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
assert.notEqual(proposalHash, "0x0000000000000000000000000000000000000000000000000000000000000000");
const proposalTime = parseInt(await optimisticOracleModule.methods.getCurrentTime().call());
const endingTime = proposalTime + liveness;
const assertionId = await optimisticOracleModule.methods.assertionIds(proposalHash).call();
const claim = utf8ToHex(
"proposalHash:" + proposalHash.slice(2) + ',explanation:"' + hexToUtf8(explanation) + '",rules:"' + rules + '"'
);
await assertEventEmitted(
receipt,
optimisticOracleModule,
"TransactionsProposed",
(event) =>
event.proposer == proposer &&
event.proposalTime == proposalTime &&
event.proposalHash == proposalHash &&
event.explanation == explanation &&
event.rules == rules &&
event.challengeWindowEnds == endingTime &&
event.proposal.requestTime == proposalTime &&
event.proposal.transactions[0].to == testToken.options.address &&
event.proposal.transactions[0].value == 0 &&
event.proposal.transactions[0].data == txnData1 &&
event.proposal.transactions[0].operation == 0 &&
event.proposal.transactions[1].to == testToken.options.address &&
event.proposal.transactions[1].value == 0 &&
event.proposal.transactions[1].data == txnData2 &&
event.proposal.transactions[1].operation == 0 &&
event.proposal.transactions[2].to == testToken2.options.address &&
event.proposal.transactions[2].value == 0 &&
event.proposal.transactions[2].data == txnData3 &&
event.proposal.transactions[2].operation == 0
);
await assertEventEmitted(
receipt,
optimisticOracleV3,
"AssertionMade",
(event) =>
event.assertionId == assertionId &&
event.domainId == leftPad(0, 64) &&
event.claim == claim &&
event.asserter == proposer &&
event.callbackRecipient == optimisticOracleModule.options.address &&
event.escalationManager == ZERO_ADDRESS &&
event.caller == optimisticOracleModule.options.address &&
event.expirationTime == endingTime &&
event.currency == bondToken.options.address &&
event.bond == bond.toString()
);
});
it("Can not send transactions to the 0x0 address", async function () {
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const operation = 0; // 0 for call, 1 for delegatecall
const transactions = [{ to: ZERO_ADDRESS, operation, value: 0, data: txnData1 }];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
assert(
await didContractThrow(
optimisticOracleModule.methods.proposeTransactions(transactions, explanation).send({ from: proposer })
)
);
});
it("Can not send transactions with data to an EOA", async function () {
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const operation = 0; // 0 for call, 1 for delegatecall
const transactions = [{ to: executor, operation, value: 0, data: txnData1 }];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
assert(
await didContractThrow(
optimisticOracleModule.methods.proposeTransactions(transactions, explanation).send({ from: proposer })
)
);
});
it("Approved proposals can be executed by any address", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
const proposalTime = parseInt(await optimisticOracleModule.methods.getCurrentTime().call());
const endingTime = proposalTime + liveness;
const assertionId = await optimisticOracleModule.methods.assertionIds(proposalHash).call();
await assertEventEmitted(
receipt,
optimisticOracleModule,
"TransactionsProposed",
(event) =>
event.proposer == proposer &&
event.proposalTime == proposalTime &&
event.proposalHash == proposalHash &&
event.explanation == explanation &&
event.rules == rules &&
event.challengeWindowEnds == endingTime &&
event.proposal.requestTime == proposalTime &&
event.proposal.transactions[0].to == testToken.options.address &&
event.proposal.transactions[0].value == 0 &&
event.proposal.transactions[0].data == txnData1 &&
event.proposal.transactions[0].operation == 0 &&
event.proposal.transactions[1].to == testToken.options.address &&
event.proposal.transactions[1].value == 0 &&
event.proposal.transactions[1].data == txnData2 &&
event.proposal.transactions[1].operation == 0 &&
event.proposal.transactions[2].to == testToken2.options.address &&
event.proposal.transactions[2].value == 0 &&
event.proposal.transactions[2].data == txnData3 &&
event.proposal.transactions[2].operation == 0
);
// Wait until the end of the dispute period.
await advanceTime(liveness);
// Set starting balances of tokens to be transferred.
const startingBalance1 = toBN(await testToken.methods.balanceOf(proposer).call());
const startingBalance2 = toBN(await testToken.methods.balanceOf(rando).call());
const startingBalance3 = toBN(await testToken2.methods.balanceOf(proposer).call());
receipt = await optimisticOracleModule.methods.executeProposal(transactions).send({ from: executor });
assert.equal(
(await testToken.methods.balanceOf(proposer).call()).toString(),
startingBalance1.add(toBN(toWei("1"))).toString()
);
assert.equal(
(await testToken.methods.balanceOf(rando).call()).toString(),
startingBalance2.add(toBN(toWei("2"))).toString()
);
assert.equal(
(await testToken2.methods.balanceOf(proposer).call()).toString(),
startingBalance3.add(toBN(toWei("2"))).toString()
);
await assertEventEmitted(
receipt,
optimisticOracleModule,
"ProposalExecuted",
(event) => event.proposalHash == proposalHash && event.assertionId == assertionId
);
for (let i = 0; i < 3; i++) {
await assertEventEmitted(
receipt,
optimisticOracleModule,
"TransactionExecuted",
(event) => event.proposalHash == proposalHash && event.assertionId == assertionId && event.transactionIndex == i
);
}
});
it("Proposals can not be executed twice", async function () {
// Issue some test tokens to the avatar address (double the amount needed to execute the proposal).
await testToken.methods.allocateTo(avatar.options.address, toWei("6")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("4")).send({ from: accounts[0] });
// Construct the transaction data to send half of newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
await optimisticOracleModule.methods.proposeTransactions(transactions, explanation).send({ from: proposer });
// Wait until the end of the dispute period.
await advanceTime(liveness);
// Execute the proposal.
await optimisticOracleModule.methods.executeProposal(transactions).send({ from: executor });
// Try to execute the proposal again.
assert(
await didContractThrow(optimisticOracleModule.methods.executeProposal(transactions).send({ from: executor }))
);
});
it("Proposals can not be executed until after liveness", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
// Advance time to one second before end of the dispute period.
const tooEarly = liveness - 1;
await advanceTime(tooEarly);
assert(
await didContractThrow(optimisticOracleModule.methods.executeProposal(transactions).send({ from: executor }))
);
});
it("Proposals can be disputed", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { assertionId } = (await findEvent(receipt, optimisticOracleV3, "AssertionMade")).match.returnValues;
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
// Advance time to one second before end of the dispute period.
const stillOpen = liveness - 1;
await advanceTime(stillOpen);
let disputeReceipt = await optimisticOracleV3.methods
.disputeAssertion(assertionId, disputer)
.send({ from: disputer });
await assertEventEmitted(
disputeReceipt,
optimisticOracleV3,
"AssertionDisputed",
(event) => event.assertionId == assertionId && event.caller == disputer && event.disputer == disputer
);
// Disputed proposal hash is deleted automatically from callback.
const disputedProposalHash = await optimisticOracleModule.methods.assertionIds(proposalHash).call();
assert.equal(disputedProposalHash, 0);
});
it("Disputed proposals can be proposed again", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { assertionId } = (await findEvent(receipt, optimisticOracleV3, "AssertionMade")).match.returnValues;
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
// Advance time to one second before end of the dispute period.
const stillOpen = liveness - 1;
await advanceTime(stillOpen);
// Duplicate proposal should be rejected if the proposal still exists.
assert(
await didContractThrow(
optimisticOracleModule.methods.proposeTransactions(transactions, explanation).send({ from: proposer })
)
);
// Dispute proposal
await optimisticOracleV3.methods.disputeAssertion(assertionId, disputer).send({ from: disputer });
// Disputed proposal hash is deleted automatically from callback.
const disputedProposalHashTimestamp = await optimisticOracleModule.methods.assertionIds(proposalHash).call();
assert.equal(disputedProposalHashTimestamp, 0);
// Duplicate proposal can be made after original proposal is deleted. This is useful in case the disputer was wrong.
let receipt2 = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const proposalTime2 = parseInt(await optimisticOracleModule.methods.getCurrentTime().call());
const endingTime2 = proposalTime2 + liveness;
await assertEventEmitted(
receipt2,
optimisticOracleModule,
"TransactionsProposed",
(event) =>
event.proposer == proposer &&
event.proposalTime == proposalTime2 &&
event.proposalHash == proposalHash &&
event.explanation == explanation &&
event.rules == rules &&
event.challengeWindowEnds == endingTime2 &&
event.proposal.requestTime == proposalTime2 &&
event.proposal.transactions[0].to == testToken.options.address &&
event.proposal.transactions[0].value == 0 &&
event.proposal.transactions[0].data == txnData1 &&
event.proposal.transactions[0].operation == 0 &&
event.proposal.transactions[1].to == testToken.options.address &&
event.proposal.transactions[1].value == 0 &&
event.proposal.transactions[1].data == txnData2 &&
event.proposal.transactions[1].operation == 0 &&
event.proposal.transactions[2].to == testToken2.options.address &&
event.proposal.transactions[2].value == 0 &&
event.proposal.transactions[2].data == txnData3 &&
event.proposal.transactions[2].operation == 0
);
});
it("Can not delete proposal with the same Optimistic Oracle V3", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
// deleteProposalOnUpgrade should fail if the Optimistic Oracle V3 was not upgraded.
assert(
await didContractThrow(
optimisticOracleModule.methods.deleteProposalOnUpgrade(proposalHash).send({ from: disputer })
)
);
});
it("Can delete proposal after Optimistic Oracle V3 upgrade", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
let receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { assertionId } = (await findEvent(receipt, optimisticOracleV3, "AssertionMade")).match.returnValues;
const { proposalHash } = (
await findEvent(receipt, optimisticOracleModule, "TransactionsProposed")
).match.returnValues;
// Upgrade the Optimistic Oracle V3.
const defaultCurrency = await TestnetERC20.new("Default Currency", "DC", 18).send({ from: owner });
const newOptimisticOracleV3 = await OptimisticOracleV3Test.new(
finder.options.address,
defaultCurrency.options.address,
liveness,
timer.options.address
).send({ from: owner });
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.OptimisticOracleV3), newOptimisticOracleV3.options.address)
.send({ from: owner });
// deleteProposalOnUpgrade should still fail as the upgraded Optimistic Oracle V3 is not yet cached.
assert(
await didContractThrow(
optimisticOracleModule.methods.deleteProposalOnUpgrade(proposalHash).send({ from: disputer })
)
);
// Cache the upgraded Optimistic Oracle V3.
await optimisticOracleModule.methods.sync().send({ from: disputer });
// deleteProposalOnUpgrade should now succeed.
let receipt2 = await optimisticOracleModule.methods.deleteProposalOnUpgrade(proposalHash).send({ from: disputer });
await assertEventEmitted(
receipt2,
optimisticOracleModule,
"ProposalDeleted",
(event) => event.proposalHash == proposalHash && event.assertionId == assertionId
);
// Revert to the original Optimistic Oracle V3 for other tests.
await finder.methods
.changeImplementationAddress(utf8ToHex(interfaceName.OptimisticOracleV3), optimisticOracleV3.options.address)
.send({ from: owner });
});
it("Disputed proposals can not be executed", async function () {
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
const receipt = await optimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { assertionId } = (await findEvent(receipt, optimisticOracleV3, "AssertionMade")).match.returnValues;
// Advance time to one second before end of the dispute period.
const stillOpen = liveness - 1;
await advanceTime(stillOpen);
// Dispute.
await optimisticOracleV3.methods.disputeAssertion(assertionId, disputer).send({ from: disputer });
// Advance time past the liveness window.
await advanceTime(2);
// Proposal should not be executed.
assert(
await didContractThrow(optimisticOracleModule.methods.executeProposal(transactions).send({ from: executor }))
);
});
// /*
// * This test is currently failing. Need to reason about how to send a transaction from the avatar
// * to the module itself, with the transaction being proposed and approved through the module.
// */
// it("Owner can delete proposals before execution", async function () {
// // Issue some test tokens to the avatar address.
// await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
// await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// // Construct the transaction data to send the newly minted tokens to proposer and another address.
// const txnData1 = constructTransferTransaction(proposer, toWei("1"));
// const txnData2 = constructTransferTransaction(rando, toWei("2"));
// const txnData3 = constructTransferTransaction(proposer, toWei("2"));
// const operation = 0; // 0 for call, 1 for delegatecall
// // Send the proposal with multiple transactions.
// const prevProposalId = parseInt(await optimisticOracleModule.methods.prevProposalId().call());
// const proposalId = prevProposalId + 1;
// const transactions = [
// { to: testToken.options.address, value: 0, data: txnData1, operation },
// { to: testToken.options.address, value: 0, data: txnData2, operation },
// { to: testToken2.options.address, value: 0, data: txnData3, operation },
// ];
// const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
// await optimisticOracleModule.methods.proposeTransactions(transactions, explanation).send({ from: proposer });
// const proposalTime = parseInt(await optimisticOracleModule.methods.getCurrentTime().call());
// // Wait until the end of the dispute period.
// await advanceTime(liveness);
// // Create new proposal to delete the old one.
// const txnData4 = constructProposalDeleteTransaction(proposalId);
// const deleteId = proposalId + 1;
// console.log("deleteId:", deleteId);
// const deleteTransaction = [{ to: optimisticOracleModule.options.address, value: 0, data: txnData4, operation }];
// const deleteExplanation = utf8ToHex("Oops, we messed up the parameters on the other proposal.");
// await optimisticOracleModule.methods
// .proposeTransactions(deleteTransaction, deleteExplanation)
// .send({ from: proposer });
// const deleteProposalTime = parseInt(await optimisticOracleModule.methods.getCurrentTime().call());
// // Wait until the end of the new dispute period.
// await advanceTime(liveness);
// // Execute the delete proposal.
// await optimisticOracleModule.methods
// .executeProposal(deleteId, deleteTransaction, deleteProposalTime)
// .send({ from: executor });
// // Original proposal can not be executed.
// assert(
// await didContractThrow(
// optimisticOracleModule.methods.executeProposal(proposalId, transactions, proposalTime).send({ from: executor })
// )
// );
// });
it("Owner can update stored contract parameters", async function () {
// All tests here are run through the avatar contract that is the owner of the module.
// Deploy new bond token and set it as the new collateral currency.
const newBondToken = await ERC20.new("New Bond", "BOND2", 18).send({ from: owner });
await collateralWhitelist.methods.addToWhitelist(newBondToken.options.address).send({ from: owner });
const newBondAmount = "1";
const setCollateralData = optimisticOracleModule.methods
.setCollateralAndBond(newBondToken.options.address, newBondAmount)
.encodeABI();
let collateralReceipt = await avatar.methods
.exec(optimisticOracleModule.options.address, "0", setCollateralData)
.send({ from: owner });
// Check that the new bond token is set as the collateral currency and correct amount.
assert.equal(await optimisticOracleModule.methods.collateral().call(), newBondToken.options.address);
assert.equal(await optimisticOracleModule.methods.bondAmount().call(), newBondAmount);
await assertEventEmitted(
collateralReceipt,
optimisticOracleModule,
"SetCollateralAndBond",
(event) => event.collateral == newBondToken.options.address && event.bondAmount == newBondAmount
);
// Set new rules.
const newRules = "New rules";
const setRulesData = optimisticOracleModule.methods.setRules(newRules).encodeABI();
let rulesReceipt = await avatar.methods
.exec(optimisticOracleModule.options.address, "0", setRulesData)
.send({ from: owner });
// Check that the new rules are set.
assert.equal(await optimisticOracleModule.methods.rules().call(), newRules);
await assertEventEmitted(rulesReceipt, optimisticOracleModule, "SetRules", (event) => event.rules == newRules);
// Set new liveness.
const newLiveness = "10";
const setLivenessData = optimisticOracleModule.methods.setLiveness(newLiveness).encodeABI();
let livenessReceipt = await avatar.methods
.exec(optimisticOracleModule.options.address, "0", setLivenessData)
.send({ from: owner });
// Check that the new liveness is set.
assert.equal(await optimisticOracleModule.methods.liveness().call(), newLiveness);
await assertEventEmitted(
livenessReceipt,
optimisticOracleModule,
"SetLiveness",
(event) => event.liveness == newLiveness
);
// Set new identifier and whitelist it.
const newIdentifier = rightPad(utf8ToHex("New Identifier"), 64);
await identifierWhitelist.methods.addSupportedIdentifier(newIdentifier).send({ from: owner });
const setIdentifierData = optimisticOracleModule.methods.setIdentifier(newIdentifier).encodeABI();
let identifierReceipt = await avatar.methods
.exec(optimisticOracleModule.options.address, "0", setIdentifierData)
.send({ from: owner });
// Check that the new identifier is set.
assert.equal(await optimisticOracleModule.methods.identifier().call(), newIdentifier);
await assertEventEmitted(
identifierReceipt,
optimisticOracleModule,
"SetIdentifier",
(event) => event.identifier == newIdentifier
);
// Deploy Full Policy Escalation Manager and set it as new Escalation Manager.
const newEscalationManager = await FullPolicyEscalationManager.new(optimisticOracleV3.options.address).send({
from: owner,
});
const setEscalationManagerData = optimisticOracleModule.methods
.setEscalationManager(newEscalationManager.options.address)
.encodeABI();
let escalationManagerReceipt = await avatar.methods
.exec(optimisticOracleModule.options.address, "0", setEscalationManagerData)
.send({ from: owner });
// Check that the new Escalation Manager is set.
assert.equal(await optimisticOracleModule.methods.escalationManager().call(), newEscalationManager.options.address);
await assertEventEmitted(
escalationManagerReceipt,
optimisticOracleModule,
"SetEscalationManager",
(event) => event.escalationManager == newEscalationManager.options.address
);
});
it("Non-owners can not update stored contract parameters", async function () {
// Deploy new bond token and try to set it as the new collateral currency from a non-owner.
const newBondToken = await ERC20.new("New Bond", "BOND2", 18).send({ from: owner });
await collateralWhitelist.methods.addToWhitelist(newBondToken.options.address).send({ from: owner });
const newBondAmount = "1";
assert(
await didContractThrow(
optimisticOracleModule.methods
.setCollateralAndBond(newBondToken.options.address, newBondAmount)
.send({ from: rando })
)
);
// Try set new rules from a non-owner.
const newRules = "New rules";
assert(await didContractThrow(optimisticOracleModule.methods.setRules(newRules).send({ from: rando })));
// Try set new liveness from a non-owner.
const newLiveness = "10";
assert(await didContractThrow(optimisticOracleModule.methods.setLiveness(newLiveness).send({ from: rando })));
// Try set new identifier from a non-owner.
const newIdentifier = rightPad(utf8ToHex("New Identifier"), 64);
await identifierWhitelist.methods.addSupportedIdentifier(newIdentifier).send({ from: owner });
assert(await didContractThrow(optimisticOracleModule.methods.setIdentifier(newIdentifier).send({ from: rando })));
// Deploy Full Policy Escalation Manager and try setting it as new Escalation Manager from a non-owner.
const newEscalationManager = await FullPolicyEscalationManager.new(optimisticOracleV3.options.address).send({
from: owner,
});
assert(
await didContractThrow(
optimisticOracleModule.methods.setEscalationManager(newEscalationManager.options.address).send({ from: rando })
)
);
});
it("Proposals can be executed with minimal proxy optimistic governor", async function () {
// Deploy proxy factory.
const ModuleProxyFactory = getContract("ModuleProxyFactory");
const moduleProxyFactory = await ModuleProxyFactory.new().send({ from: owner });
// Deploy and initialize module through proxy factory.
const initializeParams = web3.eth.abi.encodeParameters(
["address", "address", "uint256", "string", "bytes32", "uint64"],
[avatar.options.address, bondToken.options.address, bond, rules, identifier, liveness]
);
const moduleSetupData = optimisticOracleModule.methods.setUp(initializeParams).encodeABI();
const saltNonce = 0;
let receipt = await moduleProxyFactory.methods
.deployModule(optimisticOracleModule.options.address, moduleSetupData, saltNonce)
.send({ from: owner });
// Get deployed proxy module.
const proxyAddress = (await findEvent(receipt, moduleProxyFactory, "ModuleProxyCreation")).match.returnValues.proxy;
const proxyOptimisticOracleModule = await getContract("OptimisticGovernorTest").at(proxyAddress);
// Set timer for proxy module.
await proxyOptimisticOracleModule.methods.setTimer(timer.options.address).send({ from: owner });
// Point avatar to proxy module.
await avatar.methods.setModule(proxyAddress).send({ from: owner });
// Approve proposal bond for proxy module.
await bondToken.methods.approve(proxyAddress, doubleTotalBond).send({ from: proposer });
// Issue some test tokens to the avatar address.
await testToken.methods.allocateTo(avatar.options.address, toWei("3")).send({ from: accounts[0] });
await testToken2.methods.allocateTo(avatar.options.address, toWei("2")).send({ from: accounts[0] });
// Construct the transaction data to send the newly minted tokens to proposer and another address.
const txnData1 = constructTransferTransaction(proposer, toWei("1"));
const txnData2 = constructTransferTransaction(rando, toWei("2"));
const txnData3 = constructTransferTransaction(proposer, toWei("2"));
const operation = 0; // 0 for call, 1 for delegatecall
// Send the proposal with multiple transactions.
const transactions = [
{ to: testToken.options.address, operation, value: 0, data: txnData1 },
{ to: testToken.options.address, operation, value: 0, data: txnData2 },
{ to: testToken2.options.address, operation, value: 0, data: txnData3 },
];
const explanation = utf8ToHex("These transactions were approved by majority vote on Snapshot.");
// Propose transactions on proxy module.
receipt = await proxyOptimisticOracleModule.methods
.proposeTransactions(transactions, explanation)
.send({ from: proposer });
const { proposalHash } = (
await findEvent(receipt, proxyOptimisticOracleModule, "TransactionsProposed")
).match.returnValues;
const proposalTime = parseInt(await proxyOptimisticOracleModule.methods.getCurrentTime().call());
const endingTime = proposalTime + liveness;
await assertEventEmitted(
receipt,
proxyOptimisticOracleModule,
"TransactionsProposed",
(event) =>
event.proposer == proposer &&
event.proposalTime == proposalTime &&
event.proposalHash == proposalHash &&
event.explanation == explanation &&
event.rules == rules &&
event.challengeWindowEnds == endingTime &&
event.proposal.requestTime == proposalTime &&
event.proposal.transactions[0].to == testToken.options.address &&
event.proposal.transactions[0].value == 0 &&
event.proposal.transactions[0].data == txnData1 &&
event.proposal.transactions[0].operation == 0 &&
event.proposal.transactions[1].to == testToken.options.address &&
event.proposal.transactions[1].value == 0 &&
event.proposal.transactions[1].data == txnData2 &&
event.proposal.transactions[1].operation == 0 &&
event.proposal.transactions[2].to == testToken2.options.address &&
event.proposal.transactions[2].value == 0 &&
event.proposal.transactions[2].data == txnData3 &&
event.proposal.transactions[2].operation == 0
);
// Wait until the end of the dispute period.
await advanceTime(liveness);