-
Notifications
You must be signed in to change notification settings - Fork 7
/
04-tdrop-governance.js
248 lines (186 loc) · 8.46 KB
/
04-tdrop-governance.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
const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
const { BigNumber } = require("@ethersproject/bignumber");
const { getContractAddress } = require('@ethersproject/address');
const { expect } = require('chai');
const { ZERO_ADDRESS } = constants;
const { expandTo18Decimals } = require('./utils');
describe("TDrop Governance", function () {
let TDropToken;
let tdropToken;
let TDropParams;
let tdropParams;
let TDropStaking;
let tdropStaking;
let Governor;
let governor;
let Timelock;
let timelock;
let deployer;
let admin;
let airdropper;
let addrs;
beforeEach(async function () {
TDropToken = await ethers.getContractFactory("TDropToken");
TDropParams = await ethers.getContractFactory("TDropParams");
TDropStaking = await ethers.getContractFactory("TDropStaking");
Governor = await ethers.getContractFactory("GovernorAlpha");
Timelock = await ethers.getContractFactory("TestTimelock");
[deployer, superAdmin, admin, airdropper, ...addrs] = await ethers.getSigners();
tdropToken = await TDropToken.deploy(superAdmin.address, admin.address);
await tdropToken.deployed();
tdropParams = await TDropParams.deploy(superAdmin.address, admin.address);
await tdropParams.deployed();
tdropStaking = await TDropStaking.deploy(superAdmin.address, admin.address, tdropToken.address, tdropParams.address);
await tdropStaking.deployed();
const transactionCount = await deployer.getTransactionCount()
const gonvernorAddr = getContractAddress({
from: deployer.address,
nonce: transactionCount
});
const timelockAddr = getContractAddress({
from: deployer.address,
nonce: transactionCount + 1
});
governor = await Governor.deploy(superAdmin.address, admin.address, timelockAddr, tdropStaking.address);
await governor.deployed();
// timelock = await Timelock.deploy(gonvernorAddr, 2 * 86400);
timelock = await Timelock.deploy(gonvernorAddr, 3);
await timelock.deployed();
await tdropToken.connect(admin).setAirdropper(airdropper.address);
await tdropToken.connect(admin).setStakingPool(tdropStaking.address);
await tdropToken.connect(admin).unpause();
await tdropStaking.connect(admin).unpause();
await tdropParams.connect(superAdmin).setAdmin(timelock.address);
await governor.connect(admin).setVotingPeriod(3);
});
it("should be able to execute proposal to update tdropParams.stakingRewardPerBlock", async function () {
let alice = addrs[2];
let amount = expandTo18Decimals(123e7);
await expect(tdropToken.connect(airdropper).airdrop([alice.address], [amount]));
await tdropToken.connect(alice).approve(tdropStaking.address, amount);
await tdropStaking.connect(alice).stake(amount);
await tdropStaking.connect(alice).delegate(alice.address);
const targets = [tdropParams.address];
const values = [0];
const signatures = [""];
const calldatas = [tdropParams.interface.encodeFunctionData("setStakingRewardPerBlock", [789])];
const pid = 1;
// Propose
await ethers.provider.send('evm_mine', []);
await governor.connect(alice).propose(targets, values, signatures, calldatas, "");
// Cast vote
for (let i = 0; i < 2; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.connect(alice).castVote(pid, true);
// Queue
for (let i = 0; i < 3; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.queue(pid);
// Execute
const { timestamp } = await ethers.provider.getBlock('latest');
await ethers.provider.send('evm_mine', [timestamp + 100]);
await governor.execute(pid);
// Verify
expect(await tdropParams.stakingRewardPerBlock()).to.eq(789);
});
it("should be able to update timelock itself", async function () {
let alice = addrs[2];
let amount = expandTo18Decimals(123e7);
await expect(tdropToken.connect(airdropper).airdrop([alice.address], [amount]));
await tdropToken.connect(alice).approve(tdropStaking.address, amount);
await tdropStaking.connect(alice).stake(amount);
await tdropStaking.connect(alice).delegate(alice.address);
const targets = [timelock.address];
const values = [0];
const signatures = [""];
const calldatas = [Timelock.interface.encodeFunctionData("setDelay", [2 * 86400])];
const pid = 1;
// Propose
await ethers.provider.send('evm_mine', []);
await governor.connect(alice).propose(targets, values, signatures, calldatas, "");
// Cast vote
for (let i = 0; i < 2; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.connect(alice).castVote(pid, true);
// Queue
for (let i = 0; i < 3; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.queue(pid);
// Execute
const { timestamp } = await ethers.provider.getBlock('latest');
await ethers.provider.send('evm_mine', [timestamp + 100]);
await governor.execute(pid);
// Verify
expect(await timelock.delay()).to.eq(2 * 86400);
});
it("should reject if proposal has expired", async function () {
let alice = addrs[2];
let amount = expandTo18Decimals(123e7);
await expect(tdropToken.connect(airdropper).airdrop([alice.address], [amount]));
await tdropToken.connect(alice).approve(tdropStaking.address, amount);
await tdropStaking.connect(alice).stake(amount);
await tdropStaking.connect(alice).delegate(alice.address);
const targets = [timelock.address];
const values = [0];
const signatures = [""];
const calldatas = [Timelock.interface.encodeFunctionData("setDelay", [2 * 86400])];
const pid = 1;
// Propose
await ethers.provider.send('evm_mine', []);
await governor.connect(alice).propose(targets, values, signatures, calldatas, "");
// Cast vote
for (let i = 0; i < 2; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.connect(alice).castVote(pid, true);
// Set timelock.grace_period to a small value
await timelock.setGracePeriod(6);
// Queue
for (let i = 0; i < 3; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.queue(pid);
// Execute
const { timestamp } = await ethers.provider.getBlock('latest');
await ethers.provider.send('evm_mine', [timestamp + 100]);
await expect(governor.execute(pid)).to.be.reverted; // should have expired.
// Verify
expect(await timelock.delay()).to.eq(3); // has not changed
});
it("should reject not enough votes", async function() {
let alice = addrs[2];
let amount = expandTo18Decimals(123e7);
await expect(tdropToken.connect(airdropper).airdrop([alice.address], [amount]));
await tdropToken.connect(alice).approve(tdropStaking.address, amount);
await tdropStaking.connect(alice).stake(expandTo18Decimals(2e7));
await tdropStaking.connect(alice).delegate(alice.address);
const targets = [timelock.address];
const values = [0];
const signatures = [""];
const calldatas = [Timelock.interface.encodeFunctionData("setDelay", [2 * 86400])];
const pid = 1;
// Propose
await ethers.provider.send('evm_mine', []);
await governor.connect(alice).propose(targets, values, signatures, calldatas, "");
// Cast vote
for (let i = 0; i < 2; i++) {
await ethers.provider.send('evm_mine', []);
}
await governor.connect(alice).castVote(pid, true);
// Queue
for (let i = 0; i < 3; i++) {
await ethers.provider.send('evm_mine', []);
}
await expect(governor.queue(pid)).to.be.reverted;
// Execute
const { timestamp } = await ethers.provider.getBlock('latest');
await ethers.provider.send('evm_mine', [timestamp + 100]);
await expect(governor.execute(pid)).to.be.reverted;
// Verify
expect(await timelock.delay()).to.eq(3);
});
});