-
Notifications
You must be signed in to change notification settings - Fork 4
/
Test_03_buy_500_tickets_using_events.es6
116 lines (93 loc) · 3.65 KB
/
Test_03_buy_500_tickets_using_events.es6
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
"use strict";
/* Run this test in `truffle console`
* 1. run ganache with fixed block time:
* ganache-cli -m "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat" -u 0 -u 1 --gasLimit 0x2FEFD800000 -a 100 --defaultBalanceEther 10000 --blocktime 5 > ganache.log
*
* 2. Run truffle console in another terminal:
* truffle console
*
* 3. Run this test:
* test test/Test_03_buy_500_tickets_using_events.es6
*
* The test will take about 59 seconds (468931ms for buying tickets)
* */
const chai = require('chai');
const expect = chai.expect;
const assert = chai.assert;
chai.should();
const BigNumber = require('bignumber.js');
const utils = require("./utils/utils.es6");
//use default BigNumber
chai.use(require('chai-bignumber')(BigNumber));
const Lottery = artifacts.require("../Lottery.sol");
const numberOfTickets = 500;
const ticketsPerBlock = 90;
contract(`Buy ${numberOfTickets} tickets with events and block of 5 seconds: `, accounts => {
let house = accounts[9]; // see 2_lottery_migration.js
let price = web3.toWei(10, "finney"); // see 2_lottery_migration.js
let lottery;
let filterLatest;
before(async () => {
lottery = await Lottery.deployed();
let events = lottery.allEvents();
events.watch(utils.eventLogger(web3));
filterLatest = web3.eth.filter('latest');
});
after(async () => {
filterLatest.stopWatching();
});
it("lottery configuration", async () => {
expect(await lottery.house()).to.equal(house);
expect(await lottery.price()).to.be.bignumber.equal(price);
});
/* this is not an async test! use done() and promises */
it(`buy ${numberOfTickets} tickets`, (done) => {
let buyTickets = (ticketsAtOnce, iteration) => {
return () => {
for (let i = 10; i < 10 + ticketsAtOnce; i++) {
let account = accounts[i];
lottery.takePart.sendTransaction({from: account, value: price});
}
console.log(`finish buying ${ticketsAtOnce} tickets, iteration ${iteration};`);
}
};
// buy tickets in loop
let cycles = Math.floor(numberOfTickets / ticketsPerBlock);
let rest = numberOfTickets % ticketsPerBlock;
let j = 0;
filterLatest.watch(function (error, log) {
console.log("Filter latest: mined " + log);
console.log(`j: ${j}, cycles: ${cycles}`);
if (j < cycles) {
buyTickets(ticketsPerBlock, j)();
j++;
} else if (j == cycles) {
buyTickets(rest, cycles)();
j++;
} else {
console.log(`Done buying ${numberOfTickets} tickets`);
filterLatest.stopWatching();
setTimeout(() => {
done();
}, 5000); // just to be sure that everything has been processed
}
});
console.log("setTimeout has done");
}).timeout(5000000);
it("payouts", async () => {
const lottery = await Lottery.deployed();
let fund = await lottery.jackpot();
expect(fund).to.be.bignumber.equal(numberOfTickets * price);
await lottery.makePayouts.sendTransaction({from: house});
await utils.assertEvent(lottery, {
event: "Transfer",
args: {msg: "Main winner"}
});
await utils.assertEvent(lottery, {
event: "Transfer",
args: {target: house}
});
expect(await lottery.jackpot()).to.be.bignumber
.equal(fund.mul(0.1), "Payouts should leave 10% in jackpot fund");
});
});