-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3_VaultManagerFail.ts
493 lines (426 loc) · 17.4 KB
/
3_VaultManagerFail.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
import { MockTrustee } from "../typechain-types";
import { VaultManager } from "../typechain-types/contracts/VaultManager";
import { ERC20Token } from "../typechain-types/poolz-helper-v2/contracts/token";
import { getDepositeHashToSign } from "./utils";
import { time } from "@nomicfoundation/hardhat-network-helpers";
import { expect } from "chai";
import { Signer } from "ethers";
import { ethers } from "hardhat";
describe("Vault Manager Fail", function () {
describe("OnlyGovernor Functions", function () {
let vaultManager: VaultManager;
let token: ERC20Token;
let nonGovernor: Signer;
let governor: Signer;
let trustee: MockTrustee;
beforeEach(async function () {
const Token = await ethers.getContractFactory("ERC20Token");
token = await Token.deploy("Token", "TKN");
await token.deployed();
const signers = await ethers.getSigners();
governor = signers[0];
nonGovernor = signers[1];
const VaultManager = await ethers.getContractFactory("VaultManager");
vaultManager = await VaultManager.deploy();
await vaultManager.deployed();
const Trustee = await ethers.getContractFactory("MockTrustee");
trustee = await Trustee.deploy(vaultManager.address);
await trustee.deployed();
});
it("should fail to create new vault if called by non-governor(without royalty and tradeStartTime)", async () => {
{
await expect(
vaultManager
.connect(nonGovernor)
["createNewVault(address)"](token.address)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
}
});
it("should fail to create new vault if called by non-governor(with royalty)", async () => {
{
await expect(
vaultManager
.connect(nonGovernor)
["createNewVault(address,address,uint96)"](
token.address,
nonGovernor.getAddress(),
100
)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
}
});
it("should fail to create new vault if called by non-governor(with tradeStartTime)", async () => {
await expect(
vaultManager
.connect(nonGovernor)
["createNewVault(address,uint256)"](token.address, 100)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to create new vault if called by non-governor(with royalty and tradeStartTime)", async () => {
await expect(
vaultManager
.connect(nonGovernor)
["createNewVault(address,uint256,address,uint96)"](
token.address,
100,
nonGovernor.getAddress(),
100
)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to create new vault with token as zero address", async () => {
await expect(
vaultManager["createNewVault(address)"](ethers.constants.AddressZero)
).to.be.revertedWith("VaultManager: Zero address not allowed");
});
it("should fail to create new vault with royalty receiver as zero address", async () => {
await expect(
vaultManager["createNewVault(address,address,uint96)"](
token.address,
ethers.constants.AddressZero,
100
)
).to.be.revertedWith("VaultManager: Zero address not allowed");
});
it("should fail to create new vault with incorrect feeNumerator", async () => {
await expect(
vaultManager
.connect(governor)
["createNewVault(address,address,uint96)"](
token.address,
governor.getAddress(),
10001
)
).to.be.revertedWith("VaultManager: Royalty cannot be more than 100%");
});
it("should fail to setTrustee if called by non-governor", async () => {
const permittedAddress = await nonGovernor.getAddress();
await expect(
vaultManager.connect(nonGovernor).setTrustee(permittedAddress)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to setTrustee if called by non-governor", async () => {
const permittedAddress = await nonGovernor.getAddress();
await expect(
vaultManager.connect(nonGovernor).updateTrustee(permittedAddress)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to set EOA as trustee", async () => {
await expect(
vaultManager.setTrustee(nonGovernor.getAddress())
).to.be.revertedWith("VaultManager: EOA not allowed");
});
it("should fail to update trustee to EOA", async () => {
await expect(
vaultManager.updateTrustee(nonGovernor.getAddress())
).to.be.revertedWith("VaultManager: EOA not allowed");
});
it("should fail to set Zero address as trustee", async () => {
await expect(
vaultManager.setTrustee(ethers.constants.AddressZero)
).to.be.revertedWith("VaultManager: Zero address not allowed");
});
it("should fail to update trustee to Zero address", async () => {
await expect(
vaultManager.updateTrustee(ethers.constants.AddressZero)
).to.be.revertedWith("VaultManager: Zero address not allowed");
});
it("should fail to update trustee initially", async () => {
await expect(
vaultManager.updateTrustee(trustee.address)
).to.be.revertedWith("VaultManager: Trustee not set yet");
});
it("should fail to set trustee after it is already set", async () => {
await vaultManager.setTrustee(trustee.address);
await expect(vaultManager.setTrustee(trustee.address)).to.be.revertedWith(
"VaultManager: Trustee already set"
);
});
it("should fail to set active status if called by non owner", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
await expect(
vaultManager
.connect(nonGovernor)
.setActiveStatusForVaultId(vaultId, true, true)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to start tradeStartTime if called by non owner", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
await expect(
vaultManager.connect(nonGovernor).setTradeStartTime(vaultId, 100)
).to.be.revertedWithCustomError(vaultManager, "OwnableUnauthorizedAccount");
});
it("should fail to set tradeStartTime when vault does not exist", async () => {
await expect(vaultManager.setTradeStartTime(100, 100)).to.be.revertedWith(
"VaultManager: Vault not found"
);
});
it("should fail to set tradeStartTime when incorrect time is passed", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
await expect(
vaultManager.setTradeStartTime(vaultId, 1)
).to.be.revertedWith("VaultManager: Invalid trade start time");
const now = await time.latest();
await expect(
vaultManager.setTradeStartTime(vaultId, now)
).to.be.revertedWith("VaultManager: Invalid trade start time");
});
it("should fail to set active status when both status same as current", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const currentDeposiStatus = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const currentWithdrawStatus =
await vaultManager.isWithdrawalActiveForVaultId(vaultId);
await expect(
vaultManager.setActiveStatusForVaultId(
vaultId,
currentDeposiStatus,
currentWithdrawStatus
)
).to.be.revertedWith("VaultManager: No Change");
});
});
describe("Vault does not Exists", function () {
let vaultManager: VaultManager;
let trustee: MockTrustee;
let token: ERC20Token;
let governor: Signer;
const fakeVaultId = "9";
beforeEach(async function () {
const Token = await ethers.getContractFactory("ERC20Token");
token = await Token.deploy("Token", "TKN");
await token.deployed();
const signers = await ethers.getSigners();
governor = signers[0];
const VaultManager = await ethers.getContractFactory("VaultManager");
vaultManager = await VaultManager.deploy();
await vaultManager.deployed();
const Trustee = await ethers.getContractFactory("MockTrustee");
trustee = await Trustee.deploy(vaultManager.address);
await trustee.deployed();
await vaultManager.setTrustee(trustee.address);
});
it("should fail to set deposit active status", async () => {
await expect(
vaultManager.setActiveStatusForVaultId(fakeVaultId, true, true)
).to.be.revertedWith("VaultManager: Vault not found");
});
it("should fail to deposit", async () => {
const amount = 100;
await token.approve(trustee.address, amount);
await expect(trustee.deposit(token.address, amount)).to.be.revertedWith(
"VaultManager: No vaults for this token"
);
});
it("should fail if there are no vaults for the token", async () => {
const from = 0;
const count = 5;
await expect(
vaultManager.getAllVaultBalanceByToken(token.address, from, count)
).to.be.revertedWith("VaultManager: No vaults for this token");
});
it("should fail if count is not greater than 0", async () => {
const from = 0;
const count = 0;
await vaultManager["createNewVault(address)"](token.address);
await expect(
vaultManager.getAllVaultBalanceByToken(token.address, from, count)
).to.be.revertedWith("VaultManager: Count must be greater than 0");
});
it("should fail if the range is invalid", async () => {
const from = 0;
const count = 2; // Assuming there are only 1 vaults for this token
await vaultManager["createNewVault(address)"](token.address);
await expect(
vaultManager.getAllVaultBalanceByToken(token.address, from, count)
).to.be.revertedWith("VaultManager: Invalid range");
});
it("should fail to withdraw", async () => {
await expect(
trustee.withdraw(fakeVaultId, governor.getAddress(), 100)
).to.be.revertedWith("VaultManager: Vault not found");
});
it("should fail to return balance", async () => {
await expect(
vaultManager.getVaultBalanceByVaultId(fakeVaultId)
).to.be.revertedWith("VaultManager: Vault not found");
});
it("should fail to return balance", async () => {
await expect(
vaultManager.getVaultBalanceByVaultId(fakeVaultId)
).to.be.revertedWith("VaultManager: Vault not found");
await expect(
vaultManager.getCurrentVaultBalanceByToken(token.address)
).to.be.revertedWith("VaultManager: No vaults for this token");
});
it("should fail to return tokenAddress for vaultId which does not", async () => {
await expect(
vaultManager.vaultIdToTokenAddress(fakeVaultId)
).to.be.revertedWith("VaultManager: Vault not found");
});
it("should return zero for mappings", async () => {
expect(await vaultManager.vaultIdToVault(fakeVaultId)).to.equal(
ethers.constants.AddressZero
);
expect(
await vaultManager.isDepositActiveForVaultId(fakeVaultId)
).to.equal(false);
expect(
await vaultManager.isWithdrawalActiveForVaultId(fakeVaultId)
).to.equal(false);
});
});
describe("Trustee Functions", function () {
let vaultManager: VaultManager;
let trustee: MockTrustee;
let token: ERC20Token;
let nonPermitted: Signer;
let vaultId: string;
let depositor: Signer;
beforeEach(async function () {
const Token = await ethers.getContractFactory("ERC20Token");
token = await Token.deploy("Token", "TKN");
await token.deployed();
const signers = await ethers.getSigners();
nonPermitted = signers[1];
depositor = signers[2];
const VaultManager = await ethers.getContractFactory("VaultManager");
vaultManager = await VaultManager.deploy();
await vaultManager.deployed();
const Trustee = await ethers.getContractFactory("MockTrustee");
trustee = await Trustee.deploy(vaultManager.address);
await trustee.deployed();
await vaultManager.setTrustee(trustee.address);
vaultId = (
await vaultManager.callStatic["createNewVault(address)"](token.address)
).toString();
await vaultManager["createNewVault(address)"](token.address);
});
it("should fail to deposit when called by non trustee", async () => {
const amount = 100;
await token.approve(trustee.address, amount);
await expect(
vaultManager.connect(nonPermitted).depositByToken(token.address, amount)
).to.be.revertedWith("VaultManager: Not Trustee");
});
it("should fail to withdraw when called by non trustee", async () => {
await expect(
vaultManager
.connect(nonPermitted)
.withdrawByVaultId(vaultId, nonPermitted.getAddress(), 100)
).to.be.revertedWith("VaultManager: Not Trustee");
});
it("should fail to safe deposit when called by non trustee", async () => {
const currentNonce = await vaultManager.nonces(depositor.getAddress());
const hashToSign = getDepositeHashToSign(
token.address,
100,
currentNonce
);
const signature = await depositor.signMessage(hashToSign);
await expect(
vaultManager
.connect(nonPermitted)
.safeDeposit(token.address, 100, depositor.getAddress(), signature)
).to.be.revertedWith("VaultManager: Not Trustee");
});
it("should fail to safe deposit when tx.origin signs wrong fromAddress", async () => {
const currentNonce = await vaultManager.nonces(depositor.getAddress());
const hashToSign = getDepositeHashToSign(
token.address,
100,
currentNonce
);
const signature = await nonPermitted.signMessage(hashToSign);
const tx = trustee
.connect(nonPermitted)
.safeDeposit(token.address, 100, depositor.getAddress(), signature);
await expect(tx).to.be.revertedWith(
"VaultManager: Only origin can deposit"
);
});
it("should fail to safe deposit when incorrect amount is signed", async () => {
const currentNonce = await vaultManager.nonces(depositor.getAddress());
const hashToSign = getDepositeHashToSign(
token.address,
100,
currentNonce
);
const signature = await depositor.signMessage(hashToSign);
const tx = trustee
.connect(depositor)
.safeDeposit(token.address, 1000, depositor.getAddress(), signature);
await expect(tx).to.be.revertedWith(
"VaultManager: Only origin can deposit"
);
});
});
describe("Deposite and Withdrawal Status", function () {
let vaultManager: VaultManager;
let trustee: MockTrustee;
let token: ERC20Token;
let governor: Signer;
let vaultId: string;
beforeEach(async function () {
const Token = await ethers.getContractFactory("ERC20Token");
token = await Token.deploy("Token", "TKN");
await token.deployed();
const signers = await ethers.getSigners();
governor = signers[0];
const VaultManager = await ethers.getContractFactory("VaultManager");
vaultManager = await VaultManager.deploy();
await vaultManager.deployed();
const Trustee = await ethers.getContractFactory("MockTrustee");
trustee = await Trustee.deploy(vaultManager.address);
await trustee.deployed();
await vaultManager.setTrustee(trustee.address);
vaultId = (
await vaultManager.callStatic["createNewVault(address)"](token.address)
).toString();
await vaultManager["createNewVault(address)"](token.address);
await vaultManager.setActiveStatusForVaultId(vaultId, false, false);
});
it("should fail to deposit when deposits are frozen", async () => {
const amount = 100;
await token.approve(trustee.address, amount);
await expect(trustee.deposit(token.address, amount)).to.be.revertedWith(
"VaultManager: Deposits are frozen"
);
});
it("should fail to withdraw when withdrawals are frozen", async () => {
await expect(
trustee.withdraw(vaultId, governor.getAddress(), 100)
).to.be.revertedWith("VaultManager: Withdrawals are frozen");
});
it("should fail to safe deposit when deposits are frozen", async () => {
const currentNonce = await vaultManager.nonces(governor.getAddress());
const hashToSign = getDepositeHashToSign(
token.address,
100,
currentNonce
);
const signature = await governor.signMessage(hashToSign);
await expect(
trustee.safeDeposit(
token.address,
100,
governor.getAddress(),
signature
)
).to.be.revertedWith("VaultManager: Deposits are frozen");
});
});
});