forked from gelatodigital/automate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTaskTreasuryUpgradable.test.ts
463 lines (372 loc) · 14.6 KB
/
TaskTreasuryUpgradable.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
import { expect } from "chai";
import { Signer } from "@ethersproject/abstract-signer";
import { BigNumber } from "ethers";
import {
encodeResolverArgs,
getTokenFromFaucet,
Module,
ModuleData,
} from "./utils";
import {
IERC20,
CounterTest,
Ops,
TaskTreasuryL2,
TaskTreasuryUpgradable,
ResolverModule,
ProxyModule,
} from "../typechain";
import hre = require("hardhat");
const { ethers, deployments } = hre;
const GELATO = "0x3CACa7b48D0573D793d3b0279b5F0029180E83b6";
const ETH = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F";
const WBTC = "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599";
const ZERO_ADD = ethers.constants.AddressZero;
describe("TaskTreasuryUpgradable test", function () {
this.timeout(0);
let deployer: Signer;
let deployerAddress: string;
let user: Signer;
let userAddress: string;
let user2: Signer;
let user2Address: string;
let executor: Signer;
let ops: Ops;
let oldTreasury: TaskTreasuryL2;
let treasury: TaskTreasuryUpgradable;
let counter: CounterTest;
let resolverModule: ResolverModule;
let proxyModule: ProxyModule;
let dai: IERC20;
let wbtc: IERC20;
let execData: string;
let execAddress: string;
let moduleData: ModuleData;
beforeEach(async function () {
await deployments.fixture();
[deployer, user, user2] = await ethers.getSigners();
deployerAddress = await deployer.getAddress();
userAddress = await user.getAddress();
user2Address = await user2.getAddress();
ops = await ethers.getContract("Ops");
oldTreasury = await ethers.getContract("TaskTreasuryL2");
treasury = await ethers.getContract("TaskTreasuryUpgradable");
dai = await ethers.getContractAt("IERC20", DAI);
wbtc = await ethers.getContractAt("IERC20", WBTC);
resolverModule = await ethers.getContract("ResolverModule");
proxyModule = await ethers.getContract("ProxyModule");
const counterFactory = await ethers.getContractFactory("CounterTest");
counter = <CounterTest>await counterFactory.deploy(ops.address);
// get accounts
await hre.network.provider.request({
method: "hardhat_impersonateAccount",
params: [GELATO],
});
executor = await ethers.getSigner(GELATO);
// account set-up
const value = ethers.utils.parseEther("100");
const wbtcValue = ethers.utils.parseUnits("0.5", "gwei"); // 2WBTC
await getTokenFromFaucet(DAI, userAddress, value);
await getTokenFromFaucet(DAI, user2Address, value);
await getTokenFromFaucet(WBTC, userAddress, wbtcValue);
await getTokenFromFaucet(WBTC, user2Address, wbtcValue);
// module set-up
await ops.setModule(
[Module.RESOLVER, Module.PROXY],
[resolverModule.address, proxyModule.address]
);
// whitelist
oldTreasury.connect(deployer).addWhitelistedService(treasury.address);
treasury.connect(deployer).updateWhitelistedService(ops.address, true);
// create task
const execSelector = counter.interface.getSighash("increaseCount");
execAddress = counter.address;
execData = counter.interface.encodeFunctionData("increaseCount", [1]);
const resolverAddress = ethers.constants.AddressZero;
const resolverData = ethers.constants.HashZero;
const resolverArgs = encodeResolverArgs(resolverAddress, resolverData);
moduleData = {
modules: [Module.RESOLVER],
args: [resolverArgs],
};
await ops
.connect(user)
.createTask(execAddress, execSelector, moduleData, ZERO_ADD);
});
it("ops proxy should have correct treasury address", async () => {
expect(await ops.taskTreasury()).to.be.eql(treasury.address);
});
it("maxFee should be correct", async () => {
expect(await treasury.maxFee()).to.be.eql(ethers.utils.parseEther("100"));
});
it("deposit ETH", async () => {
const depositAmount = ethers.utils.parseEther("5");
await depositEth(user, depositAmount);
});
it("deposit DAI", async () => {
const depositAmount = ethers.utils.parseEther("5");
await depositErc20(user, DAI, depositAmount);
});
it("deposit WBTC", async () => {
const depositAmount = ethers.utils.parseUnits("0.1", "gwei");
await depositErc20(user, WBTC, depositAmount);
});
it("first deposit less than MIN_SHARES_IN_TREASURY", async () => {
const depositAmount = ethers.BigNumber.from("1");
await expect(depositEth(user, depositAmount)).to.be.revertedWith(
"TaskTreasury: Require MIN_SHARES_IN_TREASURY"
);
await expect(depositErc20(user, DAI, depositAmount)).to.be.revertedWith(
"TaskTreasury: Require MIN_SHARES_IN_TREASURY"
);
});
it("multiple users deposit ETH", async () => {
const depositAmount1 = ethers.utils.parseEther("2");
const depositAmount2 = ethers.utils.parseEther("4");
await depositEth(user, depositAmount1);
await depositEth(user2, depositAmount2);
});
it("multiple users deposit DAI", async () => {
const depositAmount1 = ethers.utils.parseEther("2");
const depositAmount2 = ethers.utils.parseEther("3");
await depositErc20(user, DAI, depositAmount1);
await depositErc20(user2, DAI, depositAmount2);
});
it("multiple users deposit WBTC", async () => {
const depositAmount1 = ethers.utils.parseUnits("0.2", "gwei");
const depositAmount2 = ethers.utils.parseUnits("0.3", "gwei");
await depositErc20(user, WBTC, depositAmount1);
await depositErc20(user2, WBTC, depositAmount2);
});
it("multiple users deposit then withdraw eth", async () => {
const depositAmount1 = ethers.utils.parseEther("10");
const depositAmount2 = ethers.utils.parseEther("12");
await depositEth(user, depositAmount1);
await depositEth(user2, depositAmount2);
const withdrawAmount = ethers.utils.parseEther("3");
await withdraw(user, "eth", withdrawAmount);
await withdraw(user2, "eth", withdrawAmount);
});
it("multiple users deposit then withdraw dai", async () => {
const depositAmount1 = ethers.utils.parseEther("10");
const depositAmount2 = ethers.utils.parseEther("12");
await depositErc20(user, DAI, depositAmount1);
await depositErc20(user2, DAI, depositAmount2);
const withdrawAmount = ethers.utils.parseEther("3");
await withdraw(user, "dai", withdrawAmount);
await withdraw(user2, "dai", withdrawAmount);
});
it("multiple users deposit then withdraw WBTC", async () => {
const depositAmount1 = ethers.utils.parseUnits("0.02", "gwei");
const depositAmount2 = ethers.utils.parseUnits("0.03", "gwei");
await depositErc20(user, WBTC, depositAmount1);
await depositErc20(user2, WBTC, depositAmount2);
const withdrawAmount = ethers.utils.parseUnits("0.01", "gwei");
await withdraw(user, "wbtc", withdrawAmount);
await withdraw(user2, "wbtc", withdrawAmount);
});
it("withdraw below MIN_SHARES_IN_TREASURY", async () => {
const depositAmount1 = ethers.utils.parseEther("10");
const depositAmount2 = ethers.utils.parseUnits("0.2", "gwei");
const withdrawAmount1 = depositAmount1.sub(10);
const withdrawAmount2 = depositAmount2.sub(10);
await depositEth(user, depositAmount1);
await expect(withdraw(user, "eth", withdrawAmount1)).to.be.revertedWith(
"TaskTreasury: Below MIN_SHARES_IN_TREASURY"
);
await depositErc20(user, WBTC, depositAmount2);
await expect(withdraw(user, "wbtc", withdrawAmount2)).to.be.revertedWith(
"TaskTreasury: Below MIN_SHARES_IN_TREASURY"
);
});
it("useFunds in ETH - no funds in old treasury", async () => {
const depositAmount = ethers.utils.parseEther("8");
const txFee = ethers.utils.parseEther("0.5");
await depositEth(user, depositAmount);
const expectedTreasuryBalance = depositAmount.sub(txFee);
await execute("eth", txFee, expectedTreasuryBalance);
});
it("useFunds in ETH - funds in old treasury partially covers txFee", async () => {
const depositAmount = ethers.utils.parseEther("1");
const txFee = ethers.utils.parseEther("1.5");
await depositEth(user, depositAmount);
await depositEth(user2, depositAmount);
await oldTreasury
.connect(user)
.depositFunds(userAddress, ETH, depositAmount, {
value: depositAmount,
});
const expectedTreasuryBalance = depositAmount.mul(2).sub(txFee);
await execute("eth", txFee, expectedTreasuryBalance);
});
it("useFunds in ETH - funds in old treasury covers txFee", async () => {
const depositAmount = ethers.utils.parseEther("1");
const txFee = ethers.utils.parseEther("1");
await depositEth(user, depositAmount);
await depositEth(user2, depositAmount);
await oldTreasury
.connect(user)
.depositFunds(userAddress, ETH, depositAmount, {
value: depositAmount,
});
const expectedTreasuryBalance = depositAmount.mul(2).sub(txFee);
await execute("eth", txFee, expectedTreasuryBalance);
});
it("useFunds in DAI - no funds in old treasury", async () => {
const depositAmount = ethers.utils.parseEther("8");
const txFee = ethers.utils.parseEther("0.5");
await depositErc20(user, DAI, depositAmount);
await depositErc20(user2, DAI, depositAmount);
const expectedTreasuryBalance = depositAmount.sub(txFee);
await execute("dai", txFee, expectedTreasuryBalance);
});
it("useFunds in DAI - funds in old treasury partially covers txFee", async () => {
const depositAmount = ethers.utils.parseEther("1");
const txFee = ethers.utils.parseEther("1.5");
await depositErc20(user, DAI, depositAmount);
await depositErc20(user2, DAI, depositAmount);
await dai.connect(user).approve(oldTreasury.address, depositAmount);
await oldTreasury
.connect(user)
.depositFunds(userAddress, DAI, depositAmount);
const expectedTreasuryBalance = depositAmount.mul(2).sub(txFee);
await execute("dai", txFee, expectedTreasuryBalance);
});
it("useFunds in DAI - funds in old treasury covers txFee", async () => {
const depositAmount = ethers.utils.parseEther("1");
const txFee = ethers.utils.parseEther("1");
await depositErc20(user, DAI, depositAmount);
await depositErc20(user2, DAI, depositAmount);
await dai.connect(user).approve(oldTreasury.address, depositAmount);
await oldTreasury
.connect(user)
.depositFunds(userAddress, DAI, depositAmount);
const expectedTreasuryBalance = depositAmount.mul(2).sub(txFee);
await execute("dai", txFee, expectedTreasuryBalance);
});
it("getCreditTokensByUser & totalUserTokenBalance", async () => {
const depositAmount = ethers.utils.parseEther("1");
const depositAmount2 = ethers.utils.parseUnits("0.2", "gwei");
await oldTreasury
.connect(user)
.depositFunds(userAddress, ETH, depositAmount, { value: depositAmount });
await dai.connect(user).approve(oldTreasury.address, depositAmount);
await oldTreasury
.connect(user)
.depositFunds(userAddress, DAI, depositAmount);
await depositEth(user, depositAmount);
await depositErc20(user, DAI, depositAmount);
await depositErc20(user, WBTC, depositAmount2);
const tokens = await treasury.getTotalCreditTokensByUser(userAddress);
expect(tokens.includes(ETH)).to.be.eql(true);
expect(tokens.includes(DAI)).to.be.eql(true);
expect(tokens.includes(WBTC)).to.be.eql(true);
const daiTokenBalance = await treasury.totalUserTokenBalance(
userAddress,
DAI
);
const ethTokenBalance = await treasury.totalUserTokenBalance(
userAddress,
ETH
);
expect(daiTokenBalance).to.be.eql(depositAmount.mul(2));
expect(ethTokenBalance).to.be.eql(depositAmount.mul(2));
});
//---------------------------Helper functions-------------------------------
const execute = async (
token: "eth" | "dai",
txFee: BigNumber,
expectedTreasuryBalance: BigNumber
) => {
const tokenAddress = token == "eth" ? ETH : DAI;
await ops
.connect(executor)
.exec(
userAddress,
execAddress,
execData,
moduleData,
txFee,
tokenAddress,
true,
true
);
const balanceAfter = await treasury.userTokenBalance(
userAddress,
tokenAddress
);
expect(balanceAfter).to.be.eql(expectedTreasuryBalance);
};
const depositEth = async (signer: Signer, depositAmount: BigNumber) => {
const signerAddress = await signer.getAddress();
const balanceBefore = await treasury.userTokenBalance(signerAddress, ETH);
const value = depositAmount.div(2);
await treasury.connect(signer).depositFunds(signerAddress, ETH, value, {
value,
});
await signer.sendTransaction({
to: treasury.address,
value,
});
const balanceAfter = await treasury.userTokenBalance(signerAddress, ETH);
expect(balanceAfter).to.be.eql(balanceBefore.add(depositAmount));
};
const depositErc20 = async (
signer: Signer,
tokenAddress: string,
depositAmount: BigNumber
) => {
const signerAddress = await signer.getAddress();
const balanceBefore = await treasury.userTokenBalance(
signerAddress,
tokenAddress
);
const erc20 = await ethers.getContractAt("IERC20", tokenAddress);
await erc20.connect(signer).approve(treasury.address, depositAmount);
await treasury
.connect(signer)
.depositFunds(signerAddress, tokenAddress, depositAmount);
const balanceAfter = await treasury.userTokenBalance(
signerAddress,
tokenAddress
);
expect(balanceAfter).to.be.eql(balanceBefore.add(depositAmount));
};
const withdraw = async (
signer: Signer,
token: "eth" | "dai" | "wbtc",
withdrawAmount: BigNumber
) => {
const signerAddress = await signer.getAddress();
const tokenAddress = token == "eth" ? ETH : token == "dai" ? DAI : WBTC;
const treasuryBalanceBefore = await treasury.userTokenBalance(
signerAddress,
tokenAddress
);
const balanceBefore = await getBalance(deployerAddress, token);
// withdraw to deployer to compare ETH balance change accurately, ignoring gas cost
await treasury
.connect(signer)
.withdrawFunds(deployerAddress, tokenAddress, withdrawAmount);
const treasuryBalanceAfter = await treasury.userTokenBalance(
signerAddress,
tokenAddress
);
const balanceAfter = await getBalance(deployerAddress, token);
expect(treasuryBalanceAfter).to.be.eql(
treasuryBalanceBefore.sub(withdrawAmount)
);
expect(balanceAfter).to.be.eql(balanceBefore.add(withdrawAmount));
};
const getBalance = async (
address: string,
token: "eth" | "dai" | "wbtc"
): Promise<BigNumber> => {
if (token == "eth") {
return await ethers.provider.getBalance(address);
} else if (token == "dai") {
return await dai.balanceOf(address);
} else return await wbtc.balanceOf(address);
};
});