-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathAvastarPrimeMinterTest.js
496 lines (371 loc) · 19.2 KB
/
AvastarPrimeMinterTest.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
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
494
495
496
const AvastarTeleporter = artifacts.require("./AvastarTeleporter.sol");
const AvastarPrimeMinter = artifacts.require("./AvastarPrimeMinter.sol");
const truffleAssert = require('truffle-assertions');
const truffleEvent = require('../util/truffle-events');
const exceptions = require ("../util/Exceptions");
const constants = require("../util/Constants");
const BN = require('bn.js');
contract('AvastarPrimeMinter', function(accounts) {
let teleporterContract, minterContract;
const sysAdmin = accounts[0];
const purchaser = accounts[1];
const minter = accounts[2]; // account invoking minter contract must also have minter role
const owner = accounts[3]; // account invoking withdrawFranchiseBalance must have owner role
const anyone = accounts[4];
const depositAmount = new BN("1000000000000000000", 10); // 1 ETH in Wei
const doubleDeposit = new BN("2000000000000000000", 10); // 1 ETH in Wei
const depositAfterOne = new BN("1500000000000000000", 10); // 1.5 ETH in Wei
const price = new BN("500000000000000000", 10); // .5 ETH in Wei
const traits1 = new BN('4835703422573704792572931', 10);
const traits2 = new BN('59374701396491835636974613', 10);
const traits3 = new BN('6044669605981521127212033', 10);
const prime1 = {
"generation" : constants.GENERATION.ONE,
"gender" : constants.GENDER.MALE,
"series" : constants.SERIES.ONE,
"traits" : traits1,
"ranking" : 33,
"price" : price
};
const prime2 = {
"generation" : constants.GENERATION.ONE,
"gender" : constants.GENDER.FEMALE,
"series" : constants.SERIES.ONE,
"traits" : traits2,
"ranking" : 46,
"price" : price
};
const prime3 = {
"generation" : constants.GENERATION.ONE,
"gender" : constants.GENDER.MALE,
"series" : constants.SERIES.TWO,
"traits" : traits3,
"ranking" : 68,
"price" : price
};
const prime4 = {
"generation" : constants.GENERATION.TWO,
"gender" : constants.GENDER.MALE,
"series" : constants.SERIES.ONE,
"traits" : traits3,
"ranking" : 68,
"price" : price
};
before(async () => {
// Create the factory contract and unpause
teleporterContract = await AvastarTeleporter.new();
// Create the minter contract and set the factory contract
minterContract = await AvastarPrimeMinter.new(teleporterContract.address);
await minterContract.setTeleporterContract(teleporterContract.address);
await minterContract.setCurrentGeneration(constants.GENERATION.ONE);
await minterContract.setCurrentSeries(constants.SERIES.ONE);
await minterContract.addMinter(minter);
await minterContract.addOwner(owner);
await minterContract.unpause();
// Add the minter to the factory contract
await teleporterContract.addMinter(minterContract.address);
await teleporterContract.unpause();
});
it("should allow anyone to deposit ETH", async function() {
// Deposit 1 ETH
let result = await minterContract.deposit({from: purchaser, value: depositAmount});
// Test that appropriate event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === purchaser &&
ev.balance.eq(depositAmount)
);
}, 'DepositorBalance event should be emitted with correct info');
});
it("should allow another depositor to deposit ETH", async function() {
// Deposit 1 ETH
let result = await minterContract.deposit({from: anyone, value: depositAmount});
// Test that appropriate event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === anyone &&
ev.balance.eq(depositAmount)
);
}, 'DepositorBalance event should be emitted with correct info');
});
it("should allow a depositor to check their balance", async function() {
// Check balance
let result = await minterContract.checkDepositorBalance({from: purchaser});
assert.ok(result.eq(depositAmount), "Deposit balance wasn't correct");
});
it("should allow a depositor to deposit more ETH", async function() {
// Deposit 1 ETH
let result = await minterContract.deposit({from: purchaser, value: depositAmount});
// Test that appropriate event was emitted
// this was the second deposit to the contract so balance will be twice deposit amount
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === purchaser &&
ev.balance.eq(doubleDeposit)
);
}, 'DepositorBalance event should be emitted with correct info');
});
it("should show increased balance for a depositor after depositing more ETH", async function() {
// Check balance
let result = await minterContract.checkDepositorBalance({from: purchaser});
assert.ok(result.eq(doubleDeposit), "Deposit balance wasn't correct");
});
it("should allow an owner to check the franchise balance, (zero if nothing has been purchased)", async function() {
// Check franchise balance
let result = await minterContract.checkFranchiseBalance({from: owner});
let expected = new BN(0,10); // nothing spent, nothing earned
assert.ok(result.eq(expected), "Franchise balance wasn't correct");
});
it("should not allow minter to purchase a prime if AvastarTeleporter.mintPrime reverts", async function() {
const {price, gender, traits} = prime1;
const ranking = 0;
// Called function on teleporter reverts because ranking must be between 1 and 100
// Try to purchase the prime
await exceptions.catchRevert(
minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter})
)
});
it("should allow minter to purchase a prime if purchaser has already deposited ETH", async function() {
const {generation, price, gender, series, traits, ranking} = prime1;
let id = new BN(0,10);
let serial = new BN(0,10);
// 1 ETH was previously deposited in contract
// Purchase the prime
let result = await minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter});
// Test that a DepositorBalance event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === purchaser &&
ev.balance.eq(depositAfterOne)
);
}, 'DepositorBalance event should be emitted with correct info');
// Get events emitted from the factory
let factoryScope = truffleEvent.formTxObject('AvastarTeleporter', 1, result);
// Test that appropriate event was emitted
// NOTE:
// unlike an event emitted directly from a called contract, this came from
// a contract called by the one we called. therefore, everything is a string
// in the resulting event because it was translated from raw logs by truffle-event
truffleAssert.eventEmitted(factoryScope, 'NewPrime', (ev) => {
return (
ev.id === String(id) &&
ev.serial === String(serial) &&
ev.generation === String(generation) &&
ev.series === String(series) &&
ev.gender === String(gender) &&
ev.traits === String(traits)
);
}, 'NewPrime event should be emitted with correct info');
});
it("should allow minter to purchase another prime if purchaser has sufficient ETH remaining", async function() {
const {generation, price, gender, series, traits, ranking} = prime2;
let id = new BN(1,10);
let serial = new BN(1,10);
// 1 ETH was previously deposited in contract
// Purchase the prime
let result = await minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter});
// Test that a DepositorBalance event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === purchaser &&
ev.balance.eq(depositAmount) // one ETH spent of the two deposited
);
}, 'DepositorBalance event should be emitted with correct info');
// Get events emitted from the factory
let factoryScope = truffleEvent.formTxObject('AvastarTeleporter', 1, result);
// Test that appropriate event was emitted
// NOTE:
// unlike an event emitted directly from a called contract, this came from
// a contract called by the one we called. therefore, everything is a string
// in the resulting event because it was translated from raw logs by truffle-event
truffleAssert.eventEmitted(factoryScope, 'NewPrime', (ev) => {
return (
ev.id === String(id) &&
ev.serial === String(serial) &&
ev.generation === String(generation) &&
ev.series === String(series) &&
ev.gender === String(gender) &&
ev.traits === String(traits)
);
}, 'NewPrime event should be emitted with correct info');
});
it("should show appropriately decreased balance for depositor after purchasing primes", async function() {
// Check balance
// Two deposits of 1 ETH each, minus two purchases at .5 ETH each leaves 1 ETH remaining
let result = await minterContract.checkDepositorBalance({from: purchaser});
assert.ok(result.eq(depositAmount), "Deposit balance wasn't correct");
});
it("should allow a depositor to withdraw a non-zero balance", async function() {
// Get initial balance of depositor's address (not their contract balance)
const initial = new BN(await web3.eth.getBalance(purchaser), 10);
// Withdraw balance
let result = await minterContract.withdrawDepositorBalance({from: purchaser});
let expectedDepositBalance = new BN(0,10); // zero contract balance for depositor after transfer
// Test that a DepositorBalance event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === purchaser &&
ev.balance.eq(expectedDepositBalance)
);
}, 'DepositorBalance event should be emitted with correct info');
// Calculate the cost of the transaction
const gasUsed = result.receipt.gasUsed;
const tx = await web3.eth.getTransaction(result.tx);
const txCost = web3.utils.toBN(tx.gasPrice * gasUsed);
// Get the depositor's final account balance
const final = await web3.eth.getBalance(purchaser);
assert.equal(initial.add(depositAmount).sub(txCost), final, "Amount after transfer incorrect");
});
it("should not allow minter to purchase a prime if purchaser has withdrawn their balance", async function() {
const {price, gender, traits, ranking} = prime3;
// No ETH has yet been deposited in contract
// Try to purchase the prime
await exceptions.catchRevert(
minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter})
)
});
it("should allow an owner to check the franchise balance, (has value after purchases)", async function() {
// Check franchise balance
// Should be the total of two purchases adding up to depositAmount
let result = await minterContract.checkFranchiseBalance({from: owner});
assert.ok(result.eq(depositAmount), "Franchise balance wasn't correct");
});
it("should allow an owner to withdraw the franchise balance", async function() {
// Get initial balance of depositor's address (not their contract balance)
const initial = new BN(await web3.eth.getBalance(owner), 10);
// Withdraw balance
let result = await minterContract.withdrawFranchiseBalance({from: owner});
let expectedFranchiseBalance = depositAmount; // amount from two purchases
// Test that a DepositorBalance event was emitted
truffleAssert.eventEmitted(result, 'FranchiseBalanceWithdrawn', (ev) => {
return (
ev.owner === owner &&
ev.amount.eq(expectedFranchiseBalance)
);
}, 'FranchiseBalanceWithdrawn event should be emitted with correct info');
// Calculate the cost of the transaction
const gasUsed = result.receipt.gasUsed;
const tx = await web3.eth.getTransaction(result.tx);
const txCost = web3.utils.toBN(tx.gasPrice * gasUsed);
// Get the depositor's final account balance
const final = await web3.eth.getBalance(owner);
assert.equal(initial.add(depositAmount).sub(txCost), final, "Amount after transfer incorrect");
});
it("should allow another depositor to check their balance", async function() {
// Check balance
let result = await minterContract.checkDepositorBalance({from: anyone});
assert.ok(result.eq(depositAmount), "Deposit balance wasn't correct");
});
it("should allow another depositor to withdraw a non-zero balance", async function() {
// Get initial balance of depositor's address (not their contract balance)
const initial = new BN(await web3.eth.getBalance(anyone), 10);
// Withdraw balance
let result = await minterContract.withdrawDepositorBalance({from: anyone});
let expectedDepositBalance = new BN(0,10); // zero contract balance for depositor after transfer
// Test that a DepositorBalance event was emitted
truffleAssert.eventEmitted(result, 'DepositorBalance', (ev) => {
return (
ev.depositor === anyone &&
ev.balance.eq(expectedDepositBalance)
);
}, 'DepositorBalance event should be emitted with correct info');
// Calculate the cost of the transaction
const gasUsed = result.receipt.gasUsed;
const tx = await web3.eth.getTransaction(result.tx);
const txCost = web3.utils.toBN(tx.gasPrice * gasUsed);
// Get the depositor's final account balance
const final = await web3.eth.getBalance(anyone);
assert.equal(initial.add(depositAmount).sub(txCost), final, "Amount after transfer incorrect");
});
it("should not allow non-sysadmins to set current series", async function() {
// Try to set the current Series
await exceptions.catchRevert(
minterContract.setCurrentSeries(constants.SERIES.TWO, {from: anyone})
)
});
it("should not allow system administrator to set current series if contract not paused", async function() {
// Try to set the current Series
await exceptions.catchRevert(
minterContract.setCurrentSeries(constants.SERIES.TWO, {from: sysAdmin})
)
});
it("should allow system administrator to set current series if paused", async function() {
const series = new BN(constants.SERIES.TWO, 10);
// Pause the contract
await minterContract.pause();
// Try to set the current Series
let result = await minterContract.setCurrentSeries(series, {from: sysAdmin});
// Test that a CurrentSeriesSet event was emitted
truffleAssert.eventEmitted(result, 'CurrentSeriesSet', (ev) => {
return (
ev.currentSeries.eq(series)
);
}, 'CurrentSeriesSet event should be emitted with correct info');
// Unpause the contract
await minterContract.unpause();
});
it("should reflect change of series in newly minted primes", async function() {
const {generation, price, gender, series, traits, ranking} = prime3;
// Purchase the prime
await minterContract.deposit({from: purchaser, value: depositAmount});
let result = await minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter});
// Get events emitted from the factory
let factoryScope = truffleEvent.formTxObject('AvastarTeleporter', 1, result);
// Test that appropriate event was emitted
truffleAssert.eventEmitted(factoryScope, 'NewPrime', (ev) => {
return (
ev.generation === String(generation) &&
ev.series === String(series)
);
}, 'NewPrime event should be emitted with correct info');
});
it("should not allow non-sysadmins to set current generation", async function() {
// Try to set the current Generation
await exceptions.catchRevert(
minterContract.setCurrentGeneration(constants.GENERATION.TWO, {from: anyone})
)
});
it("should not allow system administrator to set current generation if contract not paused", async function() {
// Try to set the current Series
await exceptions.catchRevert(
minterContract.setCurrentGeneration(constants.GENERATION.TWO, {from: sysAdmin})
)
});
it("should allow system administrator to set current generation if paused; series is reset", async function() {
const generation = new BN(constants.GENERATION.TWO, 10);
const series = new BN(constants.SERIES.ONE, 10);
// Pause the contract
await minterContract.pause();
// Try to set the current Generation
let result = await minterContract.setCurrentGeneration(generation, {from: sysAdmin});
// Test that a CurrentGenerationSet event was emitted
truffleAssert.eventEmitted(result, 'CurrentGenerationSet', (ev) => {
return (
ev.currentGeneration.eq(generation)
);
}, 'CurrentGenerationSet event should be emitted with correct info');
// Test that a CurrentSeriesSet event was emitted
truffleAssert.eventEmitted(result, 'CurrentSeriesSet', (ev) => {
return (
ev.currentSeries.eq(series)
);
}, 'CurrentSeriesSet event should be emitted with correct info');
// Unpause the contract
await minterContract.unpause();
});
it("should reflect change of generation and series in newly minted primes", async function() {
const {generation, price, gender, series, traits, ranking} = prime4;
// Purchase the prime
await minterContract.deposit({from: purchaser, value: depositAmount});
let result = await minterContract.purchasePrime(purchaser, price, traits, gender, ranking, {from: minter});
// Get events emitted from the factory
let factoryScope = truffleEvent.formTxObject('AvastarTeleporter', 1, result);
// Test that appropriate event was emitted
truffleAssert.eventEmitted(factoryScope, 'NewPrime', (ev) => {
return (
ev.generation === String(generation) &&
ev.series === String(series)
);
}, 'NewPrime event should be emitted with correct info');
});
});