-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBucketsFactory.test.js
351 lines (307 loc) · 13.8 KB
/
BucketsFactory.test.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
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
// SPDX-License-Identifier: BUSL-1.1
const { expect } = require("chai");
const {
ethers: {
getContract,
getContractAt,
getContractFactory,
getNamedSigners,
provider: { getCode },
utils: { defaultAbiCoder },
constants: { AddressZero, MaxUint256 },
},
deployments: { fixture },
upgrades,
} = require("hardhat");
const {
deployMockPrimexDNS,
deployMockReserve,
deployMockAccessControl,
deployMockPToken,
deployMockPtokensFactory,
deployMockDebtTokensFactory,
deployMockDebtToken,
deployMockPositionManager,
deployMockPriceOracle,
deployMockInterestRateStrategy,
deployMockWhiteBlackList,
deployMockBucket,
} = require("./utils/waffleMocks");
const { addressFromEvent } = require("./utils/addressFromEvent");
const { BIG_TIMELOCK_ADMIN, MEDIUM_TIMELOCK_ADMIN } = require("../Constants");
const { barCalcParams } = require("./utils/defaultBarCalcParams");
const { BAR_CALC_PARAMS_DECODE } = require("./utils/constants");
process.env.TEST = true;
const feeBuffer = "1000200000000000000"; // 1.0002
const withdrawalFeeRate = "5000000000000000"; // 0.005 - 0.5%
const reserveRate = "100000000000000000"; // 0.1 - 10%
const estimatedBar = "100000000000000000000000000"; // 0.1 in ray
const estimatedLar = "70000000000000000000000000"; // 0.07 in ray
describe("BucketsFactory", function () {
let bucketsFactory, deployer, bucketsFactoryContractFactory;
let nameBucket, assets, risksThresholds, underlyingAsset;
let mockPtokensFactory,
mockNewPtokensFactory,
mockPositionManager,
mockPriceOracle,
mockDebtTokensFactory,
mockNewDebtTokensFactory,
mockRegistry,
mockPrimexDNS,
mockReserve,
mockPtoken,
mockDebtToken,
mockInterestRateStrategy,
mockWhiteBlackList,
mockBucket;
let tokenTransfersLibrary;
let tokenApproveLibrary;
let ErrorsLibrary;
let bucketImplementation;
before(async function () {
await fixture(["Test"]);
({ deployer } = await getNamedSigners());
tokenTransfersLibrary = await getContract("TokenTransfersLibrary");
tokenApproveLibrary = await getContract("TokenApproveLibrary");
ErrorsLibrary = await getContract("Errors");
});
beforeEach(async function () {
mockRegistry = await deployMockAccessControl(deployer);
mockPtoken = await deployMockPToken(deployer);
mockDebtToken = await deployMockDebtToken(deployer);
mockPtokensFactory = await deployMockPtokensFactory(deployer);
mockNewPtokensFactory = await deployMockPtokensFactory(deployer);
mockDebtTokensFactory = await deployMockDebtTokensFactory(deployer);
mockNewDebtTokensFactory = await deployMockDebtTokensFactory(deployer);
mockPrimexDNS = await deployMockPrimexDNS(deployer);
mockReserve = await deployMockReserve(deployer);
mockPositionManager = await deployMockPositionManager(deployer);
[mockPriceOracle] = await deployMockPriceOracle(deployer);
mockInterestRateStrategy = await deployMockInterestRateStrategy(deployer);
mockWhiteBlackList = await deployMockWhiteBlackList(deployer);
mockBucket = await deployMockBucket(deployer);
await mockPtokensFactory.mock.createPToken.returns(mockPtoken.address);
await mockDebtTokensFactory.mock.createDebtToken.returns(mockDebtToken.address);
bucketImplementation = await getContract("Bucket");
bucketsFactoryContractFactory = await getContractFactory("BucketsFactory");
bucketsFactory = await bucketsFactoryContractFactory.deploy(
mockRegistry.address,
mockPtokensFactory.address,
mockDebtTokensFactory.address,
bucketImplementation.address,
);
await bucketsFactory.deployed();
// create bucket params
nameBucket = "bucket1";
assets = [];
risksThresholds = [];
underlyingAsset = (await getContract("TestTokenA")).address;
});
async function txCreateBucket() {
return bucketsFactory.createBucket({
nameBucket: nameBucket,
positionManager: mockPositionManager.address,
priceOracle: mockPriceOracle.address,
dns: mockPrimexDNS.address,
reserve: mockReserve.address,
assets: assets,
whiteBlackList: mockWhiteBlackList.address,
pairPriceDrops: risksThresholds,
underlyingAsset: underlyingAsset,
feeBuffer: feeBuffer,
withdrawalFeeRate: withdrawalFeeRate,
reserveRate: reserveRate,
liquidityMiningRewardDistributor: AddressZero,
liquidityMiningAmount: 0,
liquidityMiningDeadline: 0,
stabilizationDuration: 0,
interestRateStrategy: mockInterestRateStrategy.address,
maxAmountPerUser: 0,
estimatedBar: estimatedBar,
estimatedLar: estimatedLar,
barCalcParams: defaultAbiCoder.encode(BAR_CALC_PARAMS_DECODE, [Object.values(barCalcParams)]),
maxTotalDeposit: MaxUint256,
});
}
it("constructor", async function () {
expect(await bucketsFactory.pTokensFactory()).to.equal(mockPtokensFactory.address);
expect(await bucketsFactory.debtTokensFactory()).to.equal(mockDebtTokensFactory.address);
});
it("Should revert deploy when Bucket address is not supported", async function () {
await mockBucket.mock.supportsInterface.returns(false);
await expect(
bucketsFactoryContractFactory.deploy(
mockRegistry.address,
mockPtokensFactory.address,
mockDebtTokensFactory.address,
mockBucket.address,
),
).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("should createBucket if right access", async function () {
expect(await txCreateBucket());
});
it("should revert deploy BucketsFactory if registry address does not support IAccessControl", async function () {
await mockRegistry.mock.supportsInterface.returns(false);
await expect(
bucketsFactoryContractFactory.deploy(
mockRegistry.address,
mockPtokensFactory.address,
mockDebtTokensFactory.address,
bucketImplementation.address,
),
).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("should revert deploy BucketsFactory if PtokensFactory address does not support IPTokensFactory", async function () {
await mockPtokensFactory.mock.supportsInterface.returns(false);
await expect(
bucketsFactoryContractFactory.deploy(
mockRegistry.address,
mockPtokensFactory.address,
mockDebtTokensFactory.address,
bucketImplementation.address,
),
).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("should revert deploy BucketsFactory if DebtTokensFactory address does not support IDebtTokensFactory", async function () {
await mockDebtTokensFactory.mock.supportsInterface.returns(false);
await expect(
bucketsFactoryContractFactory.deploy(
mockRegistry.address,
mockPtokensFactory.address,
mockDebtTokensFactory.address,
bucketImplementation.address,
),
).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("should revert if not MEDIUM_TIMELOCK_ADMIN call createBucket", async function () {
await mockRegistry.mock.hasRole.withArgs(MEDIUM_TIMELOCK_ADMIN, deployer.address).returns(false);
await expect(txCreateBucket()).to.be.revertedWithCustomError(ErrorsLibrary, "FORBIDDEN");
});
it("should revert createBucket if PositionManager address does not support IPositionManager", async function () {
await mockPositionManager.mock.supportsInterface.returns(false);
await expect(txCreateBucket()).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("should revert createBucket if Reserve address does not support IReserve", async function () {
await mockReserve.mock.supportsInterface.returns(false);
await expect(txCreateBucket()).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("Emits BucketCreated and BucketLaunched if a bucket is created and initially launched", async function () {
const tx = await txCreateBucket();
const bucketAddress = await bucketsFactory.buckets(0);
const bucket = await getContractAt("Bucket", bucketAddress);
await expect(tx).to.emit(bucketsFactory, "BucketCreated").withArgs(bucketAddress).to.emit(bucket, "BucketLaunched");
});
it("should revert when trying to access a non-existent bucket", async function () {
const tx = await txCreateBucket();
const txSuccess = await tx.wait();
const newBucketAddress = addressFromEvent("BucketCreated", txSuccess);
expect(await bucketsFactory.buckets(0)).to.equal(newBucketAddress);
await expect(bucketsFactory.buckets(1)).to.be.reverted;
});
it("should return appropriate length of allBuckets[] if several buckets were created", async function () {
const tx1 = await txCreateBucket();
const tx1Success = await tx1.wait();
const tx2 = await txCreateBucket();
const tx2Success = await tx2.wait();
const tx3 = await txCreateBucket();
const tx3Success = await tx3.wait();
const newBucketAddress1 = addressFromEvent("BucketCreated", tx1Success);
const newBucketAddress2 = addressFromEvent("BucketCreated", tx2Success);
const newBucketAddress3 = addressFromEvent("BucketCreated", tx3Success);
const allBuckets = await bucketsFactory.allBuckets();
expect(allBuckets.length).to.equal(3);
expect(allBuckets[0]).to.equal(newBucketAddress1);
expect(allBuckets[1]).to.equal(newBucketAddress2);
expect(allBuckets[2]).to.equal(newBucketAddress3);
});
it("should create a Bucket contract code", async function () {
const tx = await txCreateBucket();
const txSuccess = await tx.wait();
const newBucketAddress = addressFromEvent("BucketCreated", txSuccess);
expect(await getCode(newBucketAddress)).not.equal("0x");
});
it("should upgrade Bucket implementation", async function () {
const tx = await txCreateBucket();
const txSuccess = await tx.wait();
const bucketAddress = addressFromEvent("BucketCreated", txSuccess);
const oldImpl = await bucketsFactory.implementation();
const bucketV2Factory = await getContractFactory("BucketV2", {
libraries: {
TokenTransfersLibrary: tokenTransfersLibrary.address,
TokenApproveLibrary: tokenApproveLibrary.address,
},
});
const oldImplFactory = await getContractFactory("Bucket", {
libraries: {
TokenTransfersLibrary: tokenTransfersLibrary.address,
TokenApproveLibrary: tokenApproveLibrary.address,
},
});
await upgrades.forceImport(bucketsFactory.address, oldImplFactory, { kind: "beacon" });
await upgrades.upgradeBeacon(bucketsFactory, bucketV2Factory, {
unsafeAllow: ["delegatecall", "external-library-linking", "constructor"],
});
const newImpl = await bucketsFactory.implementation();
expect(newImpl).not.to.equal(oldImpl);
const bucket = await getContractAt("BucketV2", bucketAddress);
expect(await bucket.testUpgrade()).to.equal("BucketV2");
});
describe("upgradeTo", function () {
it("Should revert if the passed contract does not support the interface", async function () {
await mockBucket.mock.supportsInterface.returns(false);
await expect(bucketsFactory.upgradeTo(mockBucket.address)).to.be.revertedWithCustomError(ErrorsLibrary, "ADDRESS_NOT_SUPPORTED");
});
it("Should upgradeTo with the correct address", async function () {
await mockBucket.mock.supportsInterface.returns(true);
await bucketsFactory.upgradeTo(mockBucket.address);
expect(await bucketsFactory.implementation()).to.be.equal(mockBucket.address);
});
});
describe("setPTokensFactory", function () {
it("Should set pTokens factory", async function () {
await bucketsFactory.setPTokensFactory(mockNewPtokensFactory.address);
expect(await bucketsFactory.pTokensFactory()).to.equal(mockNewPtokensFactory.address);
await expect(bucketsFactory.setPTokensFactory(mockNewPtokensFactory.address))
.to.emit(bucketsFactory, "PTokensFactoryChanged")
.withArgs(mockNewPtokensFactory.address);
});
it("Should revert when new pTokensFactory address not supported", async function () {
await mockNewPtokensFactory.mock.supportsInterface.returns(false);
await expect(bucketsFactory.setPTokensFactory(mockNewPtokensFactory.address)).to.be.revertedWithCustomError(
ErrorsLibrary,
"ADDRESS_NOT_SUPPORTED",
);
});
it("Should revert if not BIG_TIMELOCK_ADMIN call setPTokensFactory", async function () {
await mockRegistry.mock.hasRole.withArgs(BIG_TIMELOCK_ADMIN, deployer.address).returns(false);
await expect(bucketsFactory.setPTokensFactory(mockNewPtokensFactory.address)).to.be.revertedWithCustomError(
ErrorsLibrary,
"FORBIDDEN",
);
});
});
describe("setDebtTokensFactory", function () {
it("Should set debtTokens factory", async function () {
await bucketsFactory.setDebtTokensFactory(mockNewDebtTokensFactory.address);
expect(await bucketsFactory.debtTokensFactory()).to.equal(mockNewDebtTokensFactory.address);
await expect(bucketsFactory.setDebtTokensFactory(mockNewDebtTokensFactory.address))
.to.emit(bucketsFactory, "DebtTokensFactoryChanged")
.withArgs(mockNewDebtTokensFactory.address);
});
it("Should revert when new debtTokensFactory address not supported", async function () {
await mockNewDebtTokensFactory.mock.supportsInterface.returns(false);
await expect(bucketsFactory.setDebtTokensFactory(mockNewDebtTokensFactory.address)).to.be.revertedWithCustomError(
ErrorsLibrary,
"ADDRESS_NOT_SUPPORTED",
);
});
it("Should revert if not BIG_TIMELOCK_ADMIN call setDebtTokensFactory", async function () {
await mockRegistry.mock.hasRole.withArgs(BIG_TIMELOCK_ADMIN, deployer.address).returns(false);
await expect(bucketsFactory.setDebtTokensFactory(mockNewDebtTokensFactory.address)).to.be.revertedWithCustomError(
ErrorsLibrary,
"FORBIDDEN",
);
});
});
});