-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContestPoolFactoryTest.js
431 lines (358 loc) · 17.8 KB
/
ContestPoolFactoryTest.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
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
var ContestPoolFactory = artifacts.require("./ContestPoolFactory.sol");
var ContestPoolFactoryMock = artifacts.require("./mocks/ContestPoolFactoryMock.sol");
var ContestPool = artifacts.require("./ContestPool.sol");
var BbVault = artifacts.require("./BbVault.sol");
const t = require('./utils/TestUtil').title;
const stringUtils = require('./utils/StringUtil');
const {toMillis, toSeconds, daysToSeconds} = require('./utils/DateUtil');
var utils = require("./utils/utils.js");
let instance;
/*
* @title TODO Add comments.
*
* @author Douglas Molina <[email protected]>
* @author Guillermo Salazar <[email protected]>
* @author Daniel Tutila <[email protected]>
*
*/
contract('ContestPoolFactoryTest', function (accounts) {
const defaultName = "MyContestPool";
const owner = accounts[0];
const player1 = accounts[5];
const player2 = accounts[2];
const managerFee = 10;
const ownerFee = 10;
beforeEach('setup contract for each test', async () => {
instance = await ContestPoolFactoryMock.deployed();
});
it(t('aUser', 'new', 'Should deploy ContestPoolFactory contract.'), async function () {
assert(instance);
assert(instance.address);
});
it(t('aOwner', 'createContestPoolDefinition', 'Should be able to create a contest pool definition.'), async function () {
const contestName = stringUtils.uniqueText('ContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const fee = web3.toWei(0.01, 'ether');
const maxBalance = web3.toWei(10, 'ether');
await instance.setCurrentTime(toSeconds(2018, 01, 01));
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
const result = await instance.definitions(contestName);
assert.equal(contestName, stringUtils.cleanNulls(web3.toAscii(result[0])));
assert.equal(startTime, result[1]);
assert.equal(endTime, result[2]);
assert.equal(graceTime, result[3]);
assert.equal(maxBalance, result[4]);
assert.equal(fee, result[5]);
});
it(t('aOwner', 'createContestPoolDefinition', 'Should not be able to create a contest pool definition twice (equals contest name).', true), async function () {
const contestName = stringUtils.uniqueText('NewContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const fee = web3.toWei(0.01, 'ether');
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
try {
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
assert(false, 'It should have failed because the contest name is repetead.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aOwner', 'createContestPoolDefinition', 'Should not be able to create a contest pool definition with a 0x0 contest name.', true), async function () {
try {
const contestName = '0x0000000000000000000000000000000000000000000000000000000000000000';
await instance.createContestPoolDefinition(contestName, 10000, 1, 2, 10, 10, 10, 10);
assert(false, 'It should have failed because the contest name is invalid.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aOwner', 'createContestPoolDefinition', 'Should not be able to create a contest pool definition with end date equals to 0.', true), async function () {
try {
await instance.createContestPoolDefinition('CustomValue', 20000, 0, 2, 2, 10, 10, 10);
assert(false, 'It should have failed because the start date is zero.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aPlayer', 'createContestPoolDefinition', 'A player should not be able to create a contest pool definition.', true), async function () {
try {
const player = accounts[4];
await instance.createContestPoolDefinition('CustomValue', 1000, 1000, 2000, 2, 10, 10, 10, {from: player});
assert(false, 'It should have failed because a player should not able to create a definition.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aUser', 'createContestPool', 'Should be able to send create a contest pool based on a definition.'), async function () {
const contestName = stringUtils.uniqueText('Rusia2018');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(1, 'ether');
const fee = web3.toWei(0.01, 'ether');
const amountPerPlayer = web3.toWei(0.1, 'ether');
const contestNameBytes32 = stringUtils.stringToBytes32(contestName);
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
await instance.setCurrentTime(toSeconds(2018, 01, 01));
await instance.createContestPool(defaultName, contestName, amountPerPlayer, {
from: accounts[3],
value: fee
});
let contestPoolAddress;
let contestPool;
const callback = async function (log) {
contestPoolAddress = log[0].args.contestPoolAddress;
contestPool = ContestPool.at(contestPoolAddress);
assert.ok(contestPoolAddress);
assert.ok(contestPool);
const maxBalanceContestPool = await contestPool.maxBalance();
const contestNameContestPool = await contestPool.contestName();
const startDateContestPool = await contestPool.startTime();
const endDateContestPool = await contestPool.endTime();
const daysGraceContestPool = await contestPool.graceTime();
assert.equal(maxBalanceContestPool, maxBalance);
assert.equal(contestNameContestPool, contestNameBytes32);
assert.equal(startDateContestPool, startTime);
assert.equal(endDateContestPool, endTime);
assert.equal(maxBalanceContestPool, maxBalance);
};
await utils.assertEvent(instance, {
event: "ContestPoolCreated", args: {
contestName: contestNameBytes32
}
}, 1, callback);
});
it(t('aOwner', 'createContestPool', 'Should not be able to create a contest pool (manager == owner).', true), async function () {
const contestName = stringUtils.uniqueText('Rusia2018');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(1, 'ether');
const fee = web3.toWei(0.01, 'ether');
const amountPerPlayer = web3.toWei(0.1, 'ether');
const contestNameBytes32 = stringUtils.stringToBytes32(contestName);
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee,
{from: owner});
try {
await instance.createContestPool(defaultName, contestName, amountPerPlayer, {
from: owner,
value: fee
});
assert.ok(false, 'It should fail because owner address is equal to manager address.');
} catch (error) {
assert(error);
assert(error.message.includes("revert"));
}
});
it(t('aUser', 'createContestPool', 'Should not be able to create a contest pool with not pre-existed contest name.', true), async function () {
const contestName = stringUtils.uniqueText('Rusia2018');
const amountPerPlayer = web3.toWei(2, 'ether');
const fee = web3.toWei(0.01, 'ether');
try {
await instance.createContestPool(defaultName, contestName, amountPerPlayer, {
value: fee
});
assert(false, 'It should fail because contest name is invalid.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aPlayer', 'createContestPool', 'Should not be able to create a contest pool without paying a fee.', true), async function () {
const contestName = stringUtils.uniqueText('MyCustomContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(1, 'ether');
const fee = web3.toWei(0.01, 'ether');
const amountPerPlayer = web3.toWei(0.1, 'ether');
const contestNameBytes32 = stringUtils.stringToBytes32(contestName);
await instance.createContestPoolDefinition(contestNameBytes32, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
try {
await instance.createContestPool(defaultName, contestNameBytes32, amountPerPlayer, {
from: accounts[3],
value: 0
});
assert(false, 'It should fail because player did not pay the fee.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aPlayer', 'createContestPool', 'When creating a contest pool, the factory balance should be increased based on the fee defined in the pool definition.'), async function () {
const contestName = stringUtils.uniqueText('MyPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const amountPerPlayer = web3.toWei(0.01, 'ether');
const fee = web3.toWei(5, 'ether');
const initialBalanceFactory = web3.eth.getBalance(instance.address).toNumber();
const previousBalanceFactory = parseInt(fee) + parseInt(initialBalanceFactory);
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
await instance.setCurrentTime(toSeconds(2018, 01, 01));
await instance.createContestPool(defaultName, contestName, amountPerPlayer, {
from: accounts[3],
value: fee
});
const finalBalanceFactory = web3.eth.getBalance(instance.address).toNumber();
assert.equal(previousBalanceFactory, finalBalanceFactory);
});
it(t('aPlayer', 'createContestPool', 'Should not be able to create a contest pool paying a different fee.', true), async function () {
const contestName = stringUtils.uniqueText('MyContest');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const amountPerPlayer = web3.toWei(0.01, 'ether');
const fee = web3.toWei(5, 'ether');
const myFee = web3.toWei(4.9, 'ether');
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
try {
await instance.createContestPool(defaultName, contestName, amountPerPlayer, {
from: accounts[3],
value: myFee
});
assert(false, 'It should fail because fee is not correct.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aOwner', 'withdrawFee', 'Should be able to withdraw factory balance.'), async function () {
const contestName = stringUtils.uniqueText('MyContest');
const graceTime = 2;
const amountPerPlayer = web3.toWei(0.01, 'ether');
const fee = web3.toWei(0.005, 'ether');
await instance.createContestPoolDefinition(
contestName,
fee,
toMillis(2018, 01, 01),
toMillis(2018, 02, 01),
graceTime,
web3.toWei(5, 'ether'),
managerFee,
ownerFee
);
const initialBbVaultBalance = await web3.eth.getBalance(BbVault.address).toNumber();
const initialBalanceFactory = await web3.eth.getBalance(instance.address).toNumber();
await instance.createContestPool(
defaultName,
contestName,
amountPerPlayer, {
from: accounts[3],
value: fee
}
);
await instance.withdrawFee({
from: owner
});
const finalBalanceFactory = await web3.eth.getBalance(instance.address).toNumber();
assert.equal(finalBalanceFactory, 0);
const finalBbVaultBalance = await web3.eth.getBalance(BbVault.address).toNumber();
assert.ok(finalBbVaultBalance >= initialBbVaultBalance);
const expectedFinalBbVaultBalance = parseInt(initialBalanceFactory) + parseInt(initialBbVaultBalance);
assert.ok(finalBbVaultBalance >= expectedFinalBbVaultBalance);
});
it(t('aPlayer', 'withdrawFee', 'Should not able to withdraw balance from factory.', true), async function () {
const contestName = stringUtils.uniqueText('MyContest');
const graceTime = daysToSeconds(2);
const amountPerPlayer = web3.toWei(0.01, 'ether');
const fee = web3.toWei(0.005, 'ether');
await instance.createContestPoolDefinition(
contestName,
fee,
toSeconds(2018, 01, 10),
toSeconds(2018, 02, 01),
graceTime,
web3.toWei(5, 'ether'),
managerFee,
ownerFee
);
await instance.setCurrentTime(toSeconds(2018, 01, 01));
await instance.createContestPool(
defaultName,
contestName,
amountPerPlayer, {
from: accounts[3],
value: fee
}
);
try {
await instance.withdrawFee({
from: player1
});
assert(false, 'It should fail because a non owner is trying to withdraw.');
} catch (err) {
assert(err);
assert(err.message.includes("revert"));
}
});
it(t('aPlayer', 'createContestPoolDefinition', 'Should be able to create a contest pool definition with fee equals to 0.'), async function () {
const contestName = stringUtils.uniqueText('MyContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const fee = 0;
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
const result = await instance.definitions(contestName);
assert.equal(contestName, stringUtils.cleanNulls(web3.toAscii(result[0])));
assert.equal(startTime, result[1]);
assert.equal(endTime, result[2]);
assert.equal(graceTime, result[3]);
assert.equal(maxBalance, result[4]);
assert.equal(fee, result[5]);
});
it(t('aPlayer', 'disableContestPool', 'Should not be able to create a contest pool definition which is disabled.', true), async function () {
const contestName = stringUtils.uniqueText('CurrentContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const fee = 0;
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
const initialResult = await instance.definitions(contestName);
assert.equal(true, initialResult[9]);
await instance.setCurrentTime(toSeconds(2018, 01, 01));
await instance.disableContestPool(contestName, {from: owner});
try {
await instance.createContestPool(
defaultName,
contestName,
web3.toWei(0.1, 'ether'), {
from: accounts[3],
value: fee
}
);
assert(false, 'It should have failed because contest pool definition is disabled.');
} catch (error) {
assert(error);
assert(error.message.includes("revert"));
}
const finalResult = await instance.definitions(contestName);
assert.equal(false, finalResult[9]);
});
it(t('anOwner', 'deleteContestPool', 'Should get default values when definition is deleted.'), async function () {
const contestName = stringUtils.uniqueText('CurrentContestPool');
const startTime = toSeconds(2018, 04, 01);
const endTime = toSeconds(2018, 04, 10);
const graceTime = daysToSeconds(5);
const maxBalance = web3.toWei(10, 'ether');
const fee = 0;
await instance.createContestPoolDefinition(contestName, fee, startTime, endTime, graceTime, maxBalance, managerFee, ownerFee);
await instance.deleteContestPool(contestName, {from: owner});
const result = await instance.definitions(contestName);
assert.equal('0x0000000000000000000000000000000000000000000000000000000000000000', result[0]);//Contest Name
assert.equal(false, result[6]);//Exists
assert.equal(false, result[9]);//Enabled
});
});