-
Notifications
You must be signed in to change notification settings - Fork 0
/
end2end.test.ts
486 lines (380 loc) · 15.3 KB
/
end2end.test.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
import chai from 'chai';
import { ethers, upgrades } from 'hardhat';
import { solidity } from 'ethereum-waffle';
import { BigNumber as EthersBN, BigNumberish } from 'ethers';
import {
WETH,
NounsToken,
NounsAuctionHouse,
NounsAuctionHouse__factory as NounsAuctionHouseFactory,
NounsDescriptorV2,
NounsDescriptorV2__factory as NounsDescriptorV2Factory,
NounsDAOProxy__factory as NounsDaoProxyFactory,
NounsDAOLogicV1,
NounsDAOLogicV1__factory as NounsDaoLogicV1Factory,
NounsDAOExecutor,
NounsDAOExecutor__factory as NounsDaoExecutorFactory,
NounsDAOLogicV2__factory as NounsDaoLogicV2Factory,
NounsDAOLogicV2,
} from '../../../../typechain';
import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers';
import {
deployNounsToken,
deployWeth,
address,
encodeParameters,
advanceBlocks,
blockTimestamp,
setNextBlockTimestamp,
blockNumber,
populateDescriptorV2,
} from '../../../utils';
import { parseUnits } from 'ethers/lib/utils';
chai.use(solidity);
const { expect } = chai;
let nounsToken: NounsToken;
let nounsAuctionHouse: NounsAuctionHouse;
let descriptor: NounsDescriptorV2;
let weth: WETH;
let gov: NounsDAOLogicV1;
let timelock: NounsDAOExecutor;
let deployer: SignerWithAddress;
let wethDeployer: SignerWithAddress;
let noundersDAO: SignerWithAddress;
let bidders: SignerWithAddress[];
// Governance Config
const TIME_LOCK_DELAY = 172_800; // 2 days
const PROPOSAL_THRESHOLD_BPS = 500; // 5%
const QUORUM_VOTES_BPS = 2_000; // 20%
const VOTING_PERIOD = 5_760; // About 24 hours with 15s blocks
const VOTING_DELAY = 1; // 1 block
// Proposal Config
const targets: string[] = [];
const values: string[] = [];
const signatures: string[] = [];
const callDatas: string[] = [];
let proposalId1: BigNumberish;
let proposalId2: BigNumberish;
let snapshotId: number;
let daoLogicV2: NounsDAOLogicV2;
//Dynamic quorum config
const minQuorumVotesBPS = 1000; // 10%
const maxQuorumVotesBPS = 4000; // 40%
const quorumCoefficient = parseUnits('1.5', 6);
// Auction House Config
const TIME_BUFFER = 15 * 60;
const RESERVE_PRICE = 2;
const MIN_INCREMENT_BID_PERCENTAGE = 5;
const DURATION = 60 * 60 * 24;
async function deployV1() {
[deployer, wethDeployer, noundersDAO, ...bidders] = await ethers.getSigners();
// Deployed by another account to simulate real network
weth = await deployWeth(wethDeployer);
// nonce 2: Deploy AuctionHouse
// nonce 3: Deploy nftDescriptorLibraryFactory
// nonce 4: Deploy NounsDescriptor
// nonce 5: Deploy NounsSeeder
// nonce 6: Deploy NounsToken
// nonce 0: Deploy NounsDAOExecutor
// nonce 1: Deploy NounsDAOLogicV1
// nonce 7: Deploy NounsDAOProxy
// nonce ++: populate Descriptor
// nonce ++: set ownable contracts owner to timelock
// 1. DEPLOY Nouns token
nounsToken = await deployNounsToken(
deployer,
noundersDAO.address,
deployer.address, // do not know minter/auction house yet
);
// 2a. DEPLOY AuctionHouse
const auctionHouseFactory = await ethers.getContractFactory('NounsAuctionHouse', deployer);
const nounsAuctionHouseProxy = await upgrades.deployProxy(auctionHouseFactory, [
nounsToken.address,
weth.address,
TIME_BUFFER,
RESERVE_PRICE,
MIN_INCREMENT_BID_PERCENTAGE,
DURATION,
]);
// 2b. CAST proxy as AuctionHouse
nounsAuctionHouse = NounsAuctionHouseFactory.connect(nounsAuctionHouseProxy.address, deployer);
// 3. SET MINTER
await nounsToken.setMinter(nounsAuctionHouse.address);
// 4. POPULATE body parts
descriptor = NounsDescriptorV2Factory.connect(await nounsToken.descriptor(), deployer);
await populateDescriptorV2(descriptor);
// 5a. CALCULATE Gov Delegate, takes place after 2 transactions
const calculatedGovDelegatorAddress = ethers.utils.getContractAddress({
from: deployer.address,
nonce: (await deployer.getTransactionCount()) + 2,
});
// 5b. DEPLOY NounsDAOExecutor with pre-computed Delegator address
timelock = await new NounsDaoExecutorFactory(deployer).deploy(
calculatedGovDelegatorAddress,
TIME_LOCK_DELAY,
);
// 6. DEPLOY Delegate
const govDelegate = await new NounsDaoLogicV1Factory(deployer).deploy();
// 7a. DEPLOY Delegator
const nounsDAOProxy = await new NounsDaoProxyFactory(deployer).deploy(
timelock.address,
nounsToken.address,
noundersDAO.address, // NoundersDAO is vetoer
timelock.address,
govDelegate.address,
VOTING_PERIOD,
VOTING_DELAY,
PROPOSAL_THRESHOLD_BPS,
QUORUM_VOTES_BPS,
);
expect(calculatedGovDelegatorAddress).to.equal(nounsDAOProxy.address);
// 7b. CAST Delegator as Delegate
gov = NounsDaoLogicV1Factory.connect(nounsDAOProxy.address, deployer);
// 8. SET Nouns owner to NounsDAOExecutor
await nounsToken.transferOwnership(timelock.address);
// 9. SET Descriptor owner to NounsDAOExecutor
await descriptor.transferOwnership(timelock.address);
// 10. UNPAUSE auction and kick off first mint
await nounsAuctionHouse.unpause();
// 11. SET Auction House owner to NounsDAOExecutor
await nounsAuctionHouse.transferOwnership(timelock.address);
}
async function runSeveralAuctions() {
for (let i = 0; i < 9; i++) {
await nounsAuctionHouse.connect(bidders[i]).createBid(i + 1, { value: RESERVE_PRICE });
await setNextBlockTimestamp(Number(await blockTimestamp('latest')) + DURATION);
await nounsAuctionHouse.settleCurrentAndCreateNewAuction();
}
}
async function createProposal(proposer: SignerWithAddress) {
const targets = [address(2)];
const values = [String(RESERVE_PRICE)];
const signatures = [''];
const callDatas = ['0x'];
await gov.connect(proposer).propose(targets, values, signatures, callDatas, 'proposal');
return await gov.latestProposalIds(proposer.address);
}
async function createUpgradeToV2AndSetDynamicQuorumProposal(proposer: SignerWithAddress) {
daoLogicV2 = await new NounsDaoLogicV2Factory(deployer).deploy();
const targets = [gov.address];
const values = [0];
const signatures = ['_setImplementation(address)'];
const callDatas = [encodeParameters(['address'], [daoLogicV2.address])];
targets.push(gov.address);
values.push(0);
signatures.push('_setDynamicQuorumParams(uint16,uint16,uint32)');
callDatas.push(
encodeParameters(
['uint16', 'uint16', 'uint32'],
[minQuorumVotesBPS, maxQuorumVotesBPS, quorumCoefficient],
),
);
await gov.connect(proposer).propose(targets, values, signatures, callDatas, '');
const proposalId = await gov.latestProposalIds(proposer.address);
return proposalId;
}
async function createSetDynamicQuorumParamsProposal(
proposer: SignerWithAddress,
quorumCoefficient: BigNumberish,
) {
const targets = [gov.address];
const values = [0];
const signatures = ['_setDynamicQuorumParams(uint16,uint16,uint32)'];
const callDatas = [
encodeParameters(
['uint16', 'uint16', 'uint32'],
[minQuorumVotesBPS, maxQuorumVotesBPS, quorumCoefficient],
),
];
await gov.connect(proposer).propose(targets, values, signatures, callDatas, '');
const proposalId = await gov.latestProposalIds(proposer.address);
return proposalId;
}
async function proposeUpgradeToV2AndSetDynamicQuorum(proposer: SignerWithAddress) {
const proposalId = await createUpgradeToV2AndSetDynamicQuorumProposal(proposer);
await advanceBlocks(VOTING_DELAY + 1);
await gov.connect(bidders[0]).castVote(proposalId, 1);
await gov.connect(bidders[1]).castVote(proposalId, 1);
await advanceBlocks(VOTING_PERIOD);
await gov.connect(bidders[0]).queue(proposalId);
// Queued state
expect(await gov.state(proposalId)).to.equal(5);
return proposalId;
}
async function executeQueuedProposal(proposalId: BigNumberish) {
const { eta } = await gov.proposals(proposalId);
await setNextBlockTimestamp(eta.toNumber(), false);
await gov.execute(proposalId);
}
describe('V2 end to end tests', async () => {
describe('Deploy V1 and create a proposal', async () => {
before(async () => {
await deployV1();
await runSeveralAuctions();
proposalId1 = await createProposal(bidders[0]);
});
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot', []);
});
afterEach(async () => {
await ethers.provider.send('evm_revert', [snapshotId]);
});
it('requires minimum quorum to pass a proposal', async () => {
await advanceBlocks(VOTING_DELAY + 1);
await gov.connect(bidders[0]).castVote(proposalId1, 1);
await advanceBlocks(VOTING_PERIOD);
await expect(gov.connect(bidders[0]).queue(proposalId1)).to.be.revertedWith(
'NounsDAO::queue: proposal can only be queued if it is succeeded',
);
});
it('passes with enough for-votes', async () => {
await advanceBlocks(VOTING_DELAY + 1);
await gov.connect(bidders[0]).castVote(proposalId1, 1);
await gov.connect(bidders[1]).castVote(proposalId1, 1);
await advanceBlocks(VOTING_PERIOD);
await gov.connect(bidders[0]).queue(proposalId1);
// Queued state
expect(await gov.state(proposalId1)).to.equal(5);
});
});
let upgradeToV2ProposalId: BigNumberish;
describe('then the logic upgrades to V2 and dynamic quorum is set', async () => {
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot', []);
});
afterEach(async () => {
await ethers.provider.send('evm_revert', [snapshotId]);
});
it('correctly upgrades and set dynamic quorum params', async () => {
upgradeToV2ProposalId = await proposeUpgradeToV2AndSetDynamicQuorum(bidders[1]);
await executeQueuedProposal(upgradeToV2ProposalId);
await advanceBlocks(1);
expect(await NounsDaoProxyFactory.connect(gov.address, deployer).implementation()).to.equal(
daoLogicV2.address,
);
const govV2 = NounsDaoLogicV2Factory.connect(gov.address, deployer);
const params = await govV2.getDynamicQuorumParamsAt(await blockNumber());
expect(params.minQuorumVotesBPS).to.equal(minQuorumVotesBPS);
expect(params.maxQuorumVotesBPS).to.equal(maxQuorumVotesBPS);
expect(params.quorumCoefficient).to.equal(quorumCoefficient);
});
});
describe('upgrading to V2 while a V1 proposal is in progress', async () => {
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot', []);
});
afterEach(async () => {
await ethers.provider.send('evm_revert', [snapshotId]);
});
it('proposal from V1 still passes, even with against votes, dynamic quorum is not affecting it', async () => {
upgradeToV2ProposalId = await createUpgradeToV2AndSetDynamicQuorumProposal(bidders[1]);
await advanceBlocks(VOTING_DELAY + 1);
// Votes for upgrade prop
await gov.connect(bidders[0]).castVote(upgradeToV2ProposalId, 1);
await gov.connect(bidders[1]).castVote(upgradeToV2ProposalId, 1);
// Votes for Prop1
// Against votes
await gov.connect(bidders[0]).castVote(proposalId1, 0);
await gov.connect(bidders[1]).castVote(proposalId1, 0);
// For votes
await gov.connect(bidders[2]).castVote(proposalId1, 1);
await gov.connect(bidders[3]).castVote(proposalId1, 1);
await gov.connect(bidders[4]).castVote(proposalId1, 1);
await advanceBlocks(VOTING_PERIOD);
await gov.connect(bidders[0]).queue(upgradeToV2ProposalId);
await executeQueuedProposal(upgradeToV2ProposalId);
// Succeeded
expect(await gov.state(proposalId1)).to.equal(4);
});
});
describe('then a new proposal (V2) is created', async () => {
before(async () => {
upgradeToV2ProposalId = await proposeUpgradeToV2AndSetDynamicQuorum(bidders[1]);
await executeQueuedProposal(upgradeToV2ProposalId);
proposalId2 = await createProposal(bidders[0]);
});
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot', []);
});
afterEach(async () => {
await ethers.provider.send('evm_revert', [snapshotId]);
});
it('does not pass because of dynamic quorum', async () => {
await advanceBlocks(VOTING_DELAY + 1);
// Against votes
await gov.connect(bidders[0]).castVote(proposalId2, 0);
await gov.connect(bidders[1]).castVote(proposalId2, 0);
// For votes
await gov.connect(bidders[2]).castVote(proposalId2, 1);
await gov.connect(bidders[3]).castVote(proposalId2, 1);
await gov.connect(bidders[4]).castVote(proposalId2, 1);
await advanceBlocks(VOTING_PERIOD);
// Defeated
expect(await gov.state(proposalId2)).to.equal(3);
});
it('passes if has enough for-votes', async () => {
await advanceBlocks(VOTING_DELAY + 1);
// Against votes
await gov.connect(bidders[0]).castVote(proposalId2, 0);
await gov.connect(bidders[1]).castVote(proposalId2, 0);
// For votes
await gov.connect(bidders[2]).castVote(proposalId2, 1);
await gov.connect(bidders[3]).castVote(proposalId2, 1);
await gov.connect(bidders[4]).castVote(proposalId2, 1);
await gov.connect(bidders[5]).castVote(proposalId2, 1);
await advanceBlocks(VOTING_PERIOD);
// Succeeded
expect(await gov.state(proposalId2)).to.equal(4);
});
});
describe('dynamic quorum config changes again', async () => {
let proposalId4: BigNumberish;
let proposalId5: BigNumberish;
before(async () => {
proposalId4 = await createSetDynamicQuorumParamsProposal(bidders[2], parseUnits('2.5', 6));
});
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot', []);
});
afterEach(async () => {
await ethers.provider.send('evm_revert', [snapshotId]);
});
it('need to pass the new quorum requirements', async () => {
await advanceBlocks(VOTING_DELAY + 1);
// Votes to pass update params proposal
await gov.connect(bidders[0]).castVote(proposalId4, 1);
await gov.connect(bidders[1]).castVote(proposalId4, 1);
await advanceBlocks(VOTING_PERIOD);
await gov.connect(bidders[0]).queue(proposalId4);
await executeQueuedProposal(proposalId4);
// new proposal
proposalId5 = await createProposal(bidders[3]);
await advanceBlocks(VOTING_DELAY + 1);
// 1 against vote now requires 3 for votes so proposal won't pass
await gov.connect(bidders[0]).castVote(proposalId5, 0);
await gov.connect(bidders[1]).castVote(proposalId5, 1);
await gov.connect(bidders[2]).castVote(proposalId5, 1);
await advanceBlocks(VOTING_PERIOD);
// Defeated
expect(await gov.state(proposalId5)).to.equal(3);
});
it('does not affect the already created proposal', async () => {
// create proposal before the update proposal executes
proposalId5 = await createProposal(bidders[3]);
await advanceBlocks(VOTING_DELAY + 1);
// Votes to pass update params proposal
await gov.connect(bidders[0]).castVote(proposalId4, 1);
await gov.connect(bidders[1]).castVote(proposalId4, 1);
// 3 votes are enough to win 1 against vote for proposal before the update
await gov.connect(bidders[0]).castVote(proposalId5, 0);
await gov.connect(bidders[1]).castVote(proposalId5, 1);
await gov.connect(bidders[2]).castVote(proposalId5, 1);
await gov.connect(bidders[3]).castVote(proposalId5, 1);
await advanceBlocks(VOTING_PERIOD);
await gov.connect(bidders[0]).queue(proposalId4);
await executeQueuedProposal(proposalId4);
// Succeeded
expect(await gov.state(proposalId5)).to.equal(4);
});
});
});