-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimpleMemberStrategyTest.ts
545 lines (525 loc) · 17.1 KB
/
simpleMemberStrategyTest.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
import { expect } from "chai";
import { BigNumber } from "ethers";
import hre, { ethers, network, waffle, deployments } from "hardhat";
import { _TypedDataEncoder } from "@ethersproject/hash";
import {
executeContractCallWithSigners,
buildContractCall,
} from "./shared/utils";
import { AddressZero } from "@ethersproject/constants";
import { signTypedMessage, TypedDataUtils } from "eth-sig-util";
import { ecsign } from "ethereumjs-util";
import Wallet from "ethereumjs-wallet";
const deadline =
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
describe("SimpleMemberVotingStrategy:", () => {
const [wallet_0, wallet_1, wallet_2, wallet_3] = waffle.provider.getWallets();
const chainId = ethers.BigNumber.from(network.config.chainId).toNumber();
const tx = {
to: wallet_1.address,
value: 0,
data: "0x",
operation: 0,
nonce: 0,
};
const baseSetup = deployments.createFixture(async () => {
await deployments.fixture();
const [wallet_0, wallet_1, wallet_2, wallet_3] =
waffle.provider.getWallets();
const defaultBalance = ethers.BigNumber.from(1);
const thresholdPercent = ethers.BigNumber.from(100);
const GnosisSafeL2 = await hre.ethers.getContractFactory(
"@gnosis.pm/safe-contracts/contracts/GnosisSafeL2.sol:GnosisSafeL2"
);
const FactoryContract = await hre.ethers.getContractFactory(
"@gnosis.pm/safe-contracts/contracts/proxies/GnosisSafeProxyFactory.sol:GnosisSafeProxyFactory"
);
const singleton = await GnosisSafeL2.deploy();
const factory = await FactoryContract.deploy();
const template = await factory.callStatic.createProxy(
singleton.address,
"0x"
);
await factory
.createProxy(singleton.address, "0x")
.then((tx: any) => tx.wait());
const safe = GnosisSafeL2.attach(template);
safe.setup(
[wallet_0.address],
1,
AddressZero,
"0x",
AddressZero,
AddressZero,
0,
AddressZero
);
const moduleFactoryContract = await ethers.getContractFactory(
"ModuleProxyFactory"
);
const moduleFactory = await moduleFactoryContract.deploy();
const proposalContract = await ethers.getContractFactory("Usul");
const masterProposalModule = await proposalContract.deploy(
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000001",
[]
);
const encodedInitParams = ethers.utils.defaultAbiCoder.encode(
["address", "address", "address", "address[]"],
[safe.address, safe.address, safe.address, []]
);
const initData = masterProposalModule.interface.encodeFunctionData(
"setUp",
[encodedInitParams]
);
const masterCopyAddress = masterProposalModule.address
.toLowerCase()
.replace(/^0x/, "");
const byteCode =
"0x602d8060093d393df3363d3d373d3d3d363d73" +
masterCopyAddress +
"5af43d82803e903d91602b57fd5bf3";
const salt = ethers.utils.solidityKeccak256(
["bytes32", "uint256"],
[ethers.utils.solidityKeccak256(["bytes"], [initData]), "0x01"]
);
const expectedAddress = ethers.utils.getCreate2Address(
moduleFactory.address,
salt,
ethers.utils.keccak256(byteCode)
);
expect(
await moduleFactory.deployModule(
masterProposalModule.address,
initData,
"0x01"
)
)
.to.emit(moduleFactory, "ModuleProxyCreation")
.withArgs(expectedAddress, masterProposalModule.address);
const proposalModule = proposalContract.attach(expectedAddress);
const SimpleMemberContract = await ethers.getContractFactory(
"SimpleMemberVoting"
);
const masterSimpleMemberVoting = await SimpleMemberContract.deploy(
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000001",
2,
1,
0,
"",
["0x0000000000000000000000000000000000000001"]
);
const encodedSimpleMemberInitParams = ethers.utils.defaultAbiCoder.encode(
[
"address",
"address",
"uint256",
"uint256",
"uint256",
"string",
"address[]",
],
[
safe.address,
proposalModule.address,
60,
thresholdPercent,
60,
"Test",
[wallet_0.address],
]
);
const initSimpleMemberData =
masterSimpleMemberVoting.interface.encodeFunctionData("setUp", [
encodedSimpleMemberInitParams,
]);
const masterCopySimpleMemberAddress = masterSimpleMemberVoting.address
.toLowerCase()
.replace(/^0x/, "");
const byteCodeSimpleMember =
"0x602d8060093d393df3363d3d373d3d3d363d73" +
masterCopySimpleMemberAddress +
"5af43d82803e903d91602b57fd5bf3";
const saltSimpleMember = ethers.utils.solidityKeccak256(
["bytes32", "uint256"],
[
ethers.utils.solidityKeccak256(["bytes"], [initSimpleMemberData]),
"0x01",
]
);
const expectedAddressSimpleMember = ethers.utils.getCreate2Address(
moduleFactory.address,
saltSimpleMember,
ethers.utils.keccak256(byteCodeSimpleMember)
);
expect(
await moduleFactory.deployModule(
masterSimpleMemberVoting.address,
initSimpleMemberData,
"0x01"
)
)
.to.emit(moduleFactory, "ModuleProxyCreation")
.withArgs(expectedAddressSimpleMember, masterSimpleMemberVoting.address);
const simpleMember = masterSimpleMemberVoting.attach(
expectedAddressSimpleMember
);
const addCall = buildContractCall(
safe,
"addOwnerWithThreshold",
[wallet_1.address, 1],
await safe.nonce()
);
const addCall_1 = buildContractCall(
safe,
"addOwnerWithThreshold",
[wallet_2.address, 1],
await safe.nonce()
);
const txHash = await proposalModule.getTransactionHash(
addCall.to,
addCall.value,
addCall.data,
addCall.operation
);
const txHash_1 = await proposalModule.getTransactionHash(
addCall_1.to,
addCall_1.value,
addCall_1.data,
addCall_1.operation
);
await executeContractCallWithSigners(
safe,
safe,
"enableModule",
[proposalModule.address],
[wallet_0]
);
await executeContractCallWithSigners(
safe,
proposalModule,
"enableStrategy",
[simpleMember.address],
[wallet_0]
);
return {
proposalModule,
simpleMember,
txHash,
txHash_1,
addCall,
addCall_1,
safe,
defaultBalance,
thresholdPercent,
};
});
describe("setUp", async () => {
it("can register simpleMember voting module", async () => {
const { proposalModule, simpleMember } = await baseSetup();
expect(
await proposalModule.isStrategyEnabled(simpleMember.address)
).to.equal(true);
});
it("simpleMember state is initialized correctly", async () => {
const { simpleMember, safe } = await baseSetup();
expect(await simpleMember.votingPeriod()).to.equal(60);
let block = ethers.BigNumber.from(
await network.provider.send("eth_blockNumber")
);
await network.provider.send("evm_mine");
expect(await simpleMember.quorum(block)).to.equal(1);
expect(await simpleMember.timeLockPeriod()).to.equal(60);
expect(await simpleMember.members(wallet_0.address)).to.equal(true);
});
});
describe("single voting modules", async () => {
it("can not vote if not a member", async () => {
const { proposalModule, simpleMember, txHash, defaultBalance } =
await baseSetup();
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
await network.provider.send("evm_mine");
await expect(
simpleMember.calculateWeight(wallet_1.address, 0)
).to.be.revertedWith("voter is not a member");
await expect(
simpleMember.connect(wallet_1).vote(0, 1)
).to.be.revertedWith("voter is not a member");
});
it("can vote past the threshold simpleMember", async () => {
const { proposalModule, simpleMember, safe, addCall, txHash } =
await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
const proposal = await simpleMember.proposals(0);
expect(proposal.yesVotes).to.equal(ethers.BigNumber.from(0));
await simpleMember.vote(0, 1);
await simpleMember.connect(wallet_1).vote(0, 1);
const proposalAfterVoting = await simpleMember.proposals(0);
expect(proposalAfterVoting.yesVotes).to.equal(ethers.BigNumber.from(2));
expect(await simpleMember.hasVoted(0, wallet_0.address)).to.equal(true);
expect(await simpleMember.hasVoted(0, wallet_1.address)).to.equal(true);
});
it("can vote on multiple proposals", async () => {
const {
proposalModule,
safe,
simpleMember,
addCall,
txHash,
addCall_1,
txHash_1,
} = await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
await proposalModule
.connect(wallet_1)
.submitProposal([txHash_1], simpleMember.address, "0x");
await simpleMember.connect(wallet_1).vote(0, 1);
await simpleMember.connect(wallet_1).vote(1, 1);
await simpleMember.vote(0, 1);
await simpleMember.vote(1, 1);
await network.provider.send("evm_increaseTime", [60]);
await simpleMember.finalizeStrategy(0);
await simpleMember.finalizeStrategy(1);
await network.provider.send("evm_increaseTime", [60]);
await proposalModule.executeProposalByIndex(
0, // proposalId
safe.address, // target
0, // value
addCall.data, // data
0 // call operation
);
await proposalModule.executeProposalByIndex(
1, // proposalId
safe.address, // target
0, // value
addCall_1.data, // data
0 // call operation
);
const owners = await safe.getOwners();
expect(owners[0]).to.equal(wallet_2.address);
expect(owners[1]).to.equal(wallet_1.address);
expect(owners[2]).to.equal(wallet_0.address);
});
it("can vote with ERC712 offchain signature", async () => {
const {
proposalModule,
simpleMember,
safe,
txHash,
addCall,
defaultBalance,
} = await baseSetup();
const wallet = Wallet.generate();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet.getAddressString()],
[wallet_0]
);
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
await network.provider.send("evm_mine");
expect(
await simpleMember.calculateWeight(wallet.getAddressString(), 0)
).to.equal(defaultBalance);
const name = "Test";
const version = "1";
const verifyingContract = simpleMember.address;
const EIP712Domain = [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
];
const domainSeparator =
"0x" +
TypedDataUtils.hashStruct(
"EIP712Domain",
{ name, version, chainId, verifyingContract },
{ EIP712Domain }
).toString("hex");
const message = {
proposalId: 0,
vote: 1,
};
const data = {
types: {
EIP712Domain,
Vote: [
{ name: "proposalId", type: "uint256" },
{ name: "vote", type: "uint8" },
],
},
domain: { name, version, chainId, verifyingContract },
primaryType: "Vote",
message,
};
// @ts-ignore: Unreachable code error
const signature = signTypedMessage(wallet.getPrivateKey(), { data });
await simpleMember.voteSignature(0, 1, signature);
expect(
await simpleMember.hasVoted(0, wallet.getAddressString())
).to.equal(true);
let proposal = await simpleMember.proposals(0);
expect(proposal.yesVotes).to.equal(defaultBalance);
});
it("can not add member non-owner", async () => {
const { simpleMember } = await baseSetup();
await expect(simpleMember.addMember(wallet_2.address)).to.be.revertedWith(
"Ownable: caller is not the owner"
);
});
it("can not remove member non-owner", async () => {
const { simpleMember } = await baseSetup();
await expect(
simpleMember.removeMember(wallet_2.address)
).to.be.revertedWith("Ownable: caller is not the owner");
});
it("can add member through admin", async () => {
const { safe, simpleMember } = await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
const member = await simpleMember.members(wallet_0.address);
expect(member).to.equal(true);
});
it("can add member through proposal", async () => {
const {
proposalModule,
safe,
simpleMember,
defaultBalance,
thresholdPercent,
} = await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
const addMemberCall = buildContractCall(
simpleMember,
"addMember",
[wallet_2.address],
0
);
const txHash = await proposalModule.getTransactionHash(
addMemberCall.to,
addMemberCall.value,
addMemberCall.data,
addMemberCall.operation
);
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
await network.provider.send("evm_mine");
await simpleMember.vote(0, 1);
await simpleMember.connect(wallet_1).vote(0, 1);
let proposal = await simpleMember.proposals(0);
expect(proposal.yesVotes).to.equal(2);
await network.provider.send("evm_increaseTime", [60]);
await simpleMember.finalizeStrategy(0);
expect(await proposalModule.state(0)).to.equal(2);
await network.provider.send("evm_increaseTime", [60]);
await network.provider.send("evm_mine");
expect(await proposalModule.state(0)).to.equal(4);
await proposalModule.executeProposalByIndex(
0, // proposalId
simpleMember.address, // target
0, // value
addMemberCall.data, // data
0 // call operation
);
const member = await simpleMember.members(wallet_2.address);
expect(member).to.equal(true);
expect(await simpleMember.memberCount()).to.equal(3);
});
it("can remove member through admin", async () => {
const { safe, simpleMember } = await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
let member = await simpleMember.members(wallet_1.address);
expect(member).to.equal(true);
expect(await simpleMember.memberCount()).to.equal(2);
await executeContractCallWithSigners(
safe,
simpleMember,
"removeMember",
[wallet_1.address],
[wallet_0]
);
member = await simpleMember.members(wallet_1.address);
expect(member).to.equal(false);
expect(await simpleMember.memberCount()).to.equal(1);
});
it("can remove member through proposal", async () => {
const {
proposalModule,
safe,
simpleMember,
defaultBalance,
thresholdPercent,
} = await baseSetup();
await executeContractCallWithSigners(
safe,
simpleMember,
"addMember",
[wallet_1.address],
[wallet_0]
);
const removeMemberCall = buildContractCall(
simpleMember,
"removeMember",
[wallet_0.address],
0
);
const txHash = await proposalModule.getTransactionHash(
removeMemberCall.to,
removeMemberCall.value,
removeMemberCall.data,
removeMemberCall.operation
);
await proposalModule.submitProposal([txHash], simpleMember.address, "0x");
await simpleMember.vote(0, 1);
await simpleMember.connect(wallet_1).vote(0, 1);
await network.provider.send("evm_increaseTime", [60]);
await simpleMember.finalizeStrategy(0);
await network.provider.send("evm_increaseTime", [60]);
await network.provider.send("evm_mine");
await proposalModule.executeProposalByIndex(
0, // proposalId
simpleMember.address, // target
0, // value
removeMemberCall.data, // data
0 // call operation
);
const member = await simpleMember.members(wallet_0.address);
expect(member).to.equal(false);
expect(await simpleMember.memberCount()).to.equal(1);
});
});
});