-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathGovernorTimelockCompound.test.js
More file actions
535 lines (464 loc) · 20.9 KB
/
GovernorTimelockCompound.test.js
File metadata and controls
535 lines (464 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
const { ethers } = require('hardhat');
const { expect } = require('chai');
const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
const { anyValue } = require('@nomicfoundation/hardhat-chai-matchers/withArgs');
const { GovernorHelper } = require('../../helpers/governance');
const { ProposalState, VoteType } = require('../../helpers/enums');
const time = require('../../helpers/time');
const TOKENS = [
{ Token: '$ERC20Votes', mode: 'blockNumber' },
{ Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
];
const name = 'OZ-Governor';
const version = '1';
const tokenName = 'MockToken';
const tokenSymbol = 'MTKN';
const tokenSupply = ethers.parseEther('100');
const votingDelay = 4n;
const votingPeriod = 16n;
const value = ethers.parseEther('1');
const defaultDelay = time.duration.days(2n);
describe('GovernorTimelockCompound', function () {
for (const { Token, mode } of TOKENS) {
const fixture = async () => {
const [deployer, owner, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
const receiver = await ethers.deployContract('CallReceiverMock');
const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]);
const predictGovernor = await deployer
.getNonce()
.then(nonce => ethers.getCreateAddress({ from: deployer.address, nonce: nonce + 1 }));
const timelock = await ethers.deployContract('CompTimelock', [predictGovernor, defaultDelay]);
const mock = await ethers.deployContract('$GovernorTimelockCompoundMock', [
name,
votingDelay,
votingPeriod,
0n,
timelock,
token,
0n,
]);
await owner.sendTransaction({ to: timelock, value });
await token.$_mint(owner, tokenSupply);
const helper = new GovernorHelper(mock, mode);
await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
return { deployer, owner, voter1, voter2, voter3, voter4, other, receiver, token, mock, timelock, helper };
};
describe(`using ${Token}`, function () {
beforeEach(async function () {
Object.assign(this, await loadFixture(fixture));
// default proposal
this.proposal = this.helper.setProposal(
[
{
target: this.receiver.target,
value,
data: this.receiver.interface.encodeFunctionData('mockFunction'),
},
],
'<proposal description>',
);
});
it("doesn't accept ether transfers", async function () {
await expect(this.owner.sendTransaction({ to: this.mock, value: 1n })).to.be.revertedWithCustomError(
this.mock,
'GovernorDisabledDeposit',
);
});
it('post deployment check', async function () {
expect(await this.mock.name()).to.equal(name);
expect(await this.mock.token()).to.equal(this.token);
expect(await this.mock.votingDelay()).to.equal(votingDelay);
expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
expect(await this.mock.quorum(0n)).to.equal(0n);
expect(await this.mock.timelock()).to.equal(this.timelock);
expect(await this.timelock.admin()).to.equal(this.mock);
});
it('nominal', async function () {
expect(await this.mock.proposalEta(this.proposal.id)).to.equal(0n);
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.connect(this.voter2).vote({ support: VoteType.For });
await this.helper.connect(this.voter3).vote({ support: VoteType.Against });
await this.helper.connect(this.voter4).vote({ support: VoteType.Abstain });
await this.helper.waitForDeadline();
const txQueue = await this.helper.queue();
const eta = (await time.clockFromReceipt.timestamp(txQueue)) + defaultDelay;
expect(await this.mock.proposalEta(this.proposal.id)).to.equal(eta);
expect(await this.mock.proposalNeedsQueuing(this.proposal.id)).to.be.true;
await this.helper.waitForEta();
const txExecute = await this.helper.execute();
await expect(txQueue)
.to.emit(this.mock, 'ProposalQueued')
.withArgs(this.proposal.id, eta)
.to.emit(this.timelock, 'QueueTransaction')
.withArgs(...Array(5).fill(anyValue), eta);
await expect(txExecute)
.to.emit(this.mock, 'ProposalExecuted')
.withArgs(this.proposal.id)
.to.emit(this.timelock, 'ExecuteTransaction')
.withArgs(...Array(5).fill(anyValue), eta)
.to.emit(this.receiver, 'MockFunctionCalled');
});
describe('duplicate action validation', function () {
it('should allow a proposal with a single action', async function () {
await expect(
this.mock.propose(
[this.receiver.target],
[0],
[this.receiver.interface.encodeFunctionData('mockFunction')],
'description',
),
).to.emit(this.mock, 'ProposalCreated');
});
it('should allow a proposal with multiple distinct actions', async function () {
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target],
[0, 0],
[
this.receiver.interface.encodeFunctionData('mockFunction'),
this.receiver.interface.encodeFunctionData('mockFunctionNonPayable'),
],
'description',
),
).to.emit(this.mock, 'ProposalCreated');
});
it('should revert when two consecutive actions are identical', async function () {
const calldata = this.receiver.interface.encodeFunctionData('mockFunction');
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target],
[0, 0],
[calldata, calldata],
'description',
),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorDuplicateProposalAction')
.withArgs(1, 0);
});
it('should revert when non-consecutive duplicates exist', async function () {
const calldataA = this.receiver.interface.encodeFunctionData('mockFunction');
const calldataB = this.receiver.interface.encodeFunctionData('mockFunctionNonPayable');
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target, this.receiver.target],
[0, 0, 0],
[calldataA, calldataB, calldataA],
'description',
),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorDuplicateProposalAction')
.withArgs(2, 0);
});
it('should revert when multiple pairs of duplicates exist', async function () {
const calldataA = this.receiver.interface.encodeFunctionData('mockFunction');
const calldataB = this.receiver.interface.encodeFunctionData('mockFunctionNonPayable');
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target, this.receiver.target, this.receiver.target],
[0, 0, 0, 0],
[calldataA, calldataB, calldataA, calldataB],
'description',
),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorDuplicateProposalAction')
.withArgs(2, 0);
});
it('should allow proposals with the same target and calldata but different value', async function () {
const calldata = this.receiver.interface.encodeFunctionData('mockFunction');
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target],
[0, 1],
[calldata, calldata],
'description',
),
).to.emit(this.mock, 'ProposalCreated');
});
it('should allow proposals with the same target and value but different calldata', async function () {
const calldataA = this.receiver.interface.encodeFunctionData('mockFunction');
const calldataB = this.receiver.interface.encodeFunctionData('mockFunctionNonPayable');
await expect(
this.mock.propose(
[this.receiver.target, this.receiver.target],
[0, 0],
[calldataA, calldataB],
'description',
),
).to.emit(this.mock, 'ProposalCreated');
});
it('should revert when duplicate empty calldata actions exist', async function () {
await expect(
this.mock.propose([this.receiver.target, this.receiver.target], [1, 1], ['0x', '0x'], 'description'),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorDuplicateProposalAction')
.withArgs(1, 0);
});
});
describe('should revert', function () {
describe('on queue', function () {
it('if already queued', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await expect(this.helper.queue())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Queued,
GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded]),
);
});
});
describe('on execute', function () {
it('if not queued', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline(1n);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Succeeded,
GovernorHelper.proposalStatesToBitMap([ProposalState.Queued]),
);
});
it('if too early', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Queued);
await expect(this.helper.execute()).to.be.rejectedWith(
"Timelock::executeTransaction: Transaction hasn't surpassed time lock",
);
});
it('if too late', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta(time.duration.days(30));
expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Expired);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Expired,
GovernorHelper.proposalStatesToBitMap([ProposalState.Queued]),
);
});
it('if already executed', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await this.helper.execute();
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Executed,
GovernorHelper.proposalStatesToBitMap([ProposalState.Queued]),
);
});
});
describe('on safe receive', function () {
describe('ERC721', function () {
const tokenId = 1n;
beforeEach(async function () {
this.token = await ethers.deployContract('$ERC721', ['Non Fungible Token', 'NFT']);
await this.token.$_mint(this.owner, tokenId);
});
it("can't receive an ERC721 safeTransfer", async function () {
await expect(
this.token.connect(this.owner).safeTransferFrom(this.owner, this.mock, tokenId),
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
});
describe('ERC1155', function () {
const tokenIds = {
1: 1000n,
2: 2000n,
3: 3000n,
};
beforeEach(async function () {
this.token = await ethers.deployContract('$ERC1155', ['https://token-cdn-domain/{id}.json']);
await this.token.$_mintBatch(this.owner, Object.keys(tokenIds), Object.values(tokenIds), '0x');
});
it("can't receive ERC1155 safeTransfer", async function () {
await expect(
this.token.connect(this.owner).safeTransferFrom(
this.owner,
this.mock,
...Object.entries(tokenIds)[0], // id + amount
'0x',
),
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
it("can't receive ERC1155 safeBatchTransfer", async function () {
await expect(
this.token
.connect(this.owner)
.safeBatchTransferFrom(this.owner, this.mock, Object.keys(tokenIds), Object.values(tokenIds), '0x'),
).to.be.revertedWithCustomError(this.mock, 'GovernorDisabledDeposit');
});
});
});
});
describe('cancel', function () {
it('cancel before queue prevents scheduling', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await expect(this.helper.cancel('internal'))
.to.emit(this.mock, 'ProposalCanceled')
.withArgs(this.proposal.id);
expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Canceled);
await expect(this.helper.queue())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Canceled,
GovernorHelper.proposalStatesToBitMap([ProposalState.Succeeded]),
);
});
it('cancel after queue prevents executing', async function () {
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await expect(this.helper.cancel('internal'))
.to.emit(this.mock, 'ProposalCanceled')
.withArgs(this.proposal.id);
expect(await this.mock.state(this.proposal.id)).to.equal(ProposalState.Canceled);
await expect(this.helper.execute())
.to.be.revertedWithCustomError(this.mock, 'GovernorUnexpectedProposalState')
.withArgs(
this.proposal.id,
ProposalState.Canceled,
GovernorHelper.proposalStatesToBitMap([ProposalState.Queued]),
);
});
});
describe('onlyGovernance', function () {
describe('relay', function () {
beforeEach(async function () {
await this.token.$_mint(this.mock, 1);
});
it('is protected', async function () {
await expect(
this.mock
.connect(this.owner)
.relay(this.token, 0, this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n])),
)
.to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
.withArgs(this.owner);
});
it('can be executed through governance', async function () {
this.helper.setProposal(
[
{
target: this.mock.target,
data: this.mock.interface.encodeFunctionData('relay', [
this.token.target,
0n,
this.token.interface.encodeFunctionData('transfer', [this.other.address, 1n]),
]),
},
],
'<proposal description>',
);
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
const txExecute = this.helper.execute();
await expect(txExecute).to.changeTokenBalances(this.token, [this.mock, this.other], [-1n, 1n]);
await expect(txExecute).to.emit(this.token, 'Transfer').withArgs(this.mock, this.other, 1n);
});
});
describe('updateTimelock', function () {
beforeEach(async function () {
this.newTimelock = await ethers.deployContract('CompTimelock', [this.mock, time.duration.days(7n)]);
});
it('is protected', async function () {
await expect(this.mock.connect(this.owner).updateTimelock(this.newTimelock))
.to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
.withArgs(this.owner);
});
it('can be executed through governance to', async function () {
this.helper.setProposal(
[
{
target: this.timelock.target,
data: this.timelock.interface.encodeFunctionData('setPendingAdmin', [this.owner.address]),
},
{
target: this.mock.target,
data: this.mock.interface.encodeFunctionData('updateTimelock', [this.newTimelock.target]),
},
],
'<proposal description>',
);
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await expect(this.helper.execute())
.to.emit(this.mock, 'TimelockChange')
.withArgs(this.timelock, this.newTimelock);
expect(await this.mock.timelock()).to.equal(this.newTimelock);
});
});
it('can transfer timelock to new governor', async function () {
const newGovernor = await ethers.deployContract('$GovernorTimelockCompoundMock', [
name,
8n,
32n,
0n,
this.timelock,
this.token,
0n,
]);
this.helper.setProposal(
[
{
target: this.timelock.target,
data: this.timelock.interface.encodeFunctionData('setPendingAdmin', [newGovernor.target]),
},
],
'<proposal description>',
);
await this.helper.propose();
await this.helper.waitForSnapshot();
await this.helper.connect(this.voter1).vote({ support: VoteType.For });
await this.helper.waitForDeadline();
await this.helper.queue();
await this.helper.waitForEta();
await expect(this.helper.execute()).to.emit(this.timelock, 'NewPendingAdmin').withArgs(newGovernor);
await newGovernor.__acceptAdmin();
expect(await this.timelock.admin()).to.equal(newGovernor);
});
});
});
}
});