-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2_VaultManager.ts
367 lines (312 loc) · 13.2 KB
/
2_VaultManager.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
import { VaultManager } from "../typechain-types/contracts/VaultManager";
import { MockTrustee } from "../typechain-types/contracts/test";
import { ERC20Token } from "../typechain-types/poolz-helper-v2/contracts/token";
import { getDepositeHashToSign } from "./utils";
import { expect } from "chai";
import { BigNumber, Signer } from "ethers";
import { ethers } from "hardhat";
describe("VaultManager", function () {
let vaultManager: VaultManager;
let token: ERC20Token;
let trustee: MockTrustee;
let owner: Signer;
let allSigners: Signer[];
beforeEach(async function () {
const Token = await ethers.getContractFactory("ERC20Token");
token = await Token.deploy("Token", "TKN");
await token.deployed();
allSigners = await ethers.getSigners();
owner = allSigners[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();
});
it("should set address as trustee", async function () {
const Trustee = await ethers.getContractFactory("MockTrustee");
const trustee = await Trustee.deploy(vaultManager.address);
await trustee.deployed();
await vaultManager.setTrustee(trustee.address);
const result = await vaultManager.trustee();
expect(result).to.equal(trustee.address);
});
it("should update the trustee", async () => {
await vaultManager.setTrustee(trustee.address);
const newTrustee = await ethers.getContractFactory("MockTrustee");
const newTrusteeInstance = await newTrustee.deploy(vaultManager.address);
await newTrusteeInstance.deployed();
await vaultManager.updateTrustee(newTrusteeInstance.address);
const result = await vaultManager.trustee();
expect(result).to.equal(newTrusteeInstance.address);
expect(result).to.not.equal(trustee.address);
});
it("should create a new vault", async function () {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const totalVaults = await vaultManager.totalVaults();
expect(totalVaults).to.equal(1);
expect(vaultId).to.equal(totalVaults.sub(1).toString());
const vaultAddress = await vaultManager.vaultIdToVault(vaultId);
expect(vaultAddress).to.not.equal(ethers.constants.AddressZero);
const isDepositActive = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const isWithdrawActive = await vaultManager.isWithdrawalActiveForVaultId(
vaultId
);
const [receiverAddress, royaltyAmount] = await vaultManager.royaltyInfo(
vaultId,
"100"
);
expect(isDepositActive).to.equal(true);
expect(isWithdrawActive).to.equal(true);
expect(receiverAddress).to.equal(ethers.constants.AddressZero);
expect(royaltyAmount).to.equal(0);
});
it("should set deposit and withdraw status vault to true", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const tx = await vaultManager.setActiveStatusForVaultId(
vaultId,
false,
false
);
const isDepositActive = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const isWithdrawActive = await vaultManager.isWithdrawalActiveForVaultId(
vaultId
);
expect(isDepositActive).to.equal(false);
expect(isWithdrawActive).to.equal(false);
await expect(tx)
.to.emit(vaultManager, "VaultStatusUpdate")
.withArgs(vaultId, false, false);
});
it("should set trade start time by vault ID", async () => {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const tradeStartTime = Math.floor(Date.now() / 1000) + 1000;
await vaultManager.setTradeStartTime(vaultId, tradeStartTime);
const result = await vaultManager.vaultIdToTradeStartTime(vaultId);
expect(result).to.equal(tradeStartTime);
await vaultManager.setTradeStartTime(vaultId, 0);
const result2 = await vaultManager.vaultIdToTradeStartTime(vaultId);
expect(result2).to.equal(0);
});
it("should get the token address", async function () {
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const tokenAddress = await vaultManager.vaultIdToTokenAddress(vaultId);
expect(tokenAddress).to.equal(token.address);
});
it("should deposit tokens to a vault", async function () {
await vaultManager.setTrustee(trustee.address);
const amount = ethers.utils.parseEther("0.000001");
await token.approve(vaultManager.address, amount);
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
await token.approve(trustee.address, amount);
const tx = await trustee.deposit(token.address, amount);
const vaultBalance = await vaultManager.getVaultBalanceByVaultId(vaultId);
const vaultBalanceByToken =
await vaultManager.getCurrentVaultBalanceByToken(token.address);
expect(vaultBalance).to.equal(amount);
expect(vaultBalanceByToken).to.equal(amount);
await expect(tx)
.to.emit(vaultManager, "Deposited")
.withArgs(vaultId, token.address, amount);
});
it("should safe deposit tokens to a vault using sender signature", async function () {
await vaultManager.setTrustee(trustee.address);
const amount = ethers.utils.parseEther("0.000001");
await token.approve(vaultManager.address, amount);
const vaultId = await vaultManager.callStatic["createNewVault(address)"](
token.address
);
await vaultManager["createNewVault(address)"](token.address);
const currentNonce = await vaultManager.nonces(owner.getAddress());
const hashToSign = getDepositeHashToSign(
token.address,
amount,
currentNonce
);
const signature = await owner.signMessage(hashToSign);
const tx = await trustee
.connect(owner)
.safeDeposit(token.address, amount, owner.getAddress(), signature);
const vaultBalance = await vaultManager.getVaultBalanceByVaultId(vaultId);
const vaultBalanceByToken =
await vaultManager.getCurrentVaultBalanceByToken(token.address);
expect(vaultBalance).to.equal(amount);
expect(vaultBalanceByToken).to.equal(amount);
await expect(tx)
.to.emit(vaultManager, "Deposited")
.withArgs(vaultId, token.address, amount);
});
it("should withdraw tokens from a vault", async function () {
await vaultManager.setTrustee(trustee.address);
const amount = ethers.utils.parseEther("0.000001");
await token.approve(vaultManager.address, amount);
await vaultManager["createNewVault(address)"](token.address);
const signers = await ethers.getSigners();
const to = signers[1].address;
const vaultId = 0;
await token.approve(trustee.address, amount);
await trustee.deposit(token.address, amount);
await trustee.withdraw(vaultId, to, amount);
const vaultBalance = await vaultManager.getVaultBalanceByVaultId(vaultId);
const vaultBalanceByToken =
await vaultManager.getCurrentVaultBalanceByToken(token.address);
expect(vaultBalance).to.equal(0);
expect(vaultBalanceByToken).to.equal(0);
const receiverBalance = await token.balanceOf(to);
expect(receiverBalance).to.equal(amount);
});
it("should support multiple vaults per token", async () => {
await vaultManager.setTrustee(trustee.address);
const permitted = allSigners[1];
const ownerBalance = await token.balanceOf(await owner.getAddress());
await token.transfer(permitted.getAddress(), ownerBalance);
await token
.connect(permitted)
.approve(vaultManager.address, ethers.constants.MaxUint256);
const amounts: BigNumber[] = [];
const count = 10;
for (let i = 0; i < count; i++) {
await vaultManager
.connect(owner)
["createNewVault(address)"](token.address);
const amount = Math.floor(Math.random() * 100000) + 1000;
amounts.push(ethers.BigNumber.from(amount));
await token.connect(permitted).approve(trustee.address, amount);
const vaultId = await trustee
.connect(permitted)
.callStatic.deposit(token.address, amount);
await trustee.connect(permitted).deposit(token.address, amount);
const vaultBalance = await vaultManager.getVaultBalanceByVaultId(vaultId);
const vaultBalanceByToken =
await vaultManager.getCurrentVaultBalanceByToken(token.address);
const totalBalance = await vaultManager.getAllVaultBalanceByToken(
token.address,
0,
i + 1
);
const totalVaults = await vaultManager.totalVaults();
expect(totalVaults).to.equal(i + 1);
expect(vaultBalance).to.equal(amount);
expect(vaultBalanceByToken).to.equal(amount);
expect(totalBalance).to.equal(
amounts.reduce((a, b) => a.add(b), ethers.constants.Zero)
);
}
});
it("should create a vault with royalty", async () => {
const feeNumerator = 100; // 1%
const vaultId = await vaultManager.callStatic[
"createNewVault(address,address,uint96)"
](token.address, owner.getAddress(), feeNumerator);
const tx = await vaultManager["createNewVault(address,address,uint96)"](
token.address,
owner.getAddress(),
feeNumerator
);
const totalVaults = await vaultManager.totalVaults();
expect(totalVaults).to.equal(1);
expect(vaultId).to.equal(totalVaults.sub(1).toString());
const vaultAddress = await vaultManager.vaultIdToVault(vaultId);
expect(vaultAddress).to.not.equal(ethers.constants.AddressZero);
const isDepositActive = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const isWithdrawActive = await vaultManager.isWithdrawalActiveForVaultId(
vaultId
);
const [receiverAddress, royaltyAmount] = await vaultManager.royaltyInfo(
vaultId,
"100"
);
expect(isDepositActive).to.equal(true);
expect(isWithdrawActive).to.equal(true);
expect(receiverAddress).to.equal(await owner.getAddress());
expect(royaltyAmount).to.equal(1);
expect(tx)
.to.emit(vaultManager, "VaultRoyaltySet")
.withArgs(vaultId, token.address, await owner.getAddress(), feeNumerator);
});
it("should create a vault with trade start time", async () => {
const tradeStartTime = Math.floor(Date.now() / 1000) + 1000;
const vaultId = await vaultManager.callStatic[
"createNewVault(address,uint256)"
](token.address, tradeStartTime);
await vaultManager["createNewVault(address,uint256)"](
token.address,
tradeStartTime
);
const totalVaults = await vaultManager.totalVaults();
expect(totalVaults).to.equal(1);
expect(vaultId).to.equal(totalVaults.sub(1).toString());
const vaultAddress = await vaultManager.vaultIdToVault(vaultId);
expect(vaultAddress).to.not.equal(ethers.constants.AddressZero);
const isDepositActive = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const isWithdrawActive = await vaultManager.isWithdrawalActiveForVaultId(
vaultId
);
const tradeStartTimeFromVault = await vaultManager.vaultIdToTradeStartTime(
vaultId
);
expect(isDepositActive).to.equal(true);
expect(isWithdrawActive).to.equal(true);
expect(tradeStartTimeFromVault).to.equal(tradeStartTime);
});
it("should create a vault with both trade start time and royalty", async () => {
const tradeStartTime = Math.floor(Date.now() / 1000) + 1000;
const feeNumerator = 100; // 1%
const vaultId = await vaultManager.callStatic[
"createNewVault(address,uint256,address,uint96)"
](token.address, tradeStartTime, owner.getAddress(), feeNumerator);
const tx = await vaultManager[
"createNewVault(address,uint256,address,uint96)"
](token.address, tradeStartTime, owner.getAddress(), feeNumerator);
const totalVaults = await vaultManager.totalVaults();
expect(totalVaults).to.equal(1);
expect(vaultId).to.equal(totalVaults.sub(1).toString());
const vaultAddress = await vaultManager.vaultIdToVault(vaultId);
expect(vaultAddress).to.not.equal(ethers.constants.AddressZero);
const isDepositActive = await vaultManager.isDepositActiveForVaultId(
vaultId
);
const isWithdrawActive = await vaultManager.isWithdrawalActiveForVaultId(
vaultId
);
const tradeStartTimeFromVault = await vaultManager.vaultIdToTradeStartTime(
vaultId
);
const [receiverAddress, royaltyAmount] = await vaultManager.royaltyInfo(
vaultId,
"100"
);
expect(isDepositActive).to.equal(true);
expect(isWithdrawActive).to.equal(true);
expect(tradeStartTimeFromVault).to.equal(tradeStartTime);
expect(receiverAddress).to.equal(await owner.getAddress());
expect(royaltyAmount).to.equal(1);
expect(tx)
.to.emit(vaultManager, "VaultRoyaltySet")
.withArgs(vaultId, token.address, await owner.getAddress(), feeNumerator);
});
});