-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulot.js
147 lines (127 loc) · 4.68 KB
/
bulot.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
loadScript("erc20tokenabi.js")
loadScript("bulottokenabi.js")
loadScript("addresses.js")
erc20contract = web3.eth.contract(erc20tokenabi).at(erc20address);
bulotcontract = web3.eth.contract(bulottokenabi).at(bulotaddress);
ticketsOfPeople = {}
//creates accounts
function createAccounts(howmany) {
var numacc = eth.accounts.length - 1
console.log("There are " + numacc + " accounts")
var minbalance = 1000000000
for (var i=1; i<=howmany; i++) {
if (i > numacc) {
personal.newAccount('')
console.log("Created account " + i)
}
var balance = eth.getBalance(eth.accounts[i])
if (balance < minbalance) {
var sendvalue = minbalance - balance
web3.eth.sendTransaction({from:eth.accounts[0],
to:eth.accounts[i],
value:sendvalue})
console.log("Sent " + sendvalue + " to " + i)
}
}
}
//gives tl tokens to other accounts
function givePeopleMoneyToBuyTickets(howmany) {
eth.defaultAccount = eth.accounts[0];
for (i=1; i< howmany; i++) {
erc20contract.transfer.sendTransaction(eth.accounts[i], 10);
}
}
//allows bulot to use transferFrom
function giveAllowance(howmany) {
for (i=1; i< howmany; i++) {
eth.defaultAccount=eth.accounts[i];
web3.personal.unlockAccount(eth.accounts[i],'',10)
erc20contract.approve.sendTransaction(bulotaddress, 10);
}
}
//buys tickets for accounts
function buyTickets(howmany) {
curentLotteryNo = getCurrentLotteryNo();
for (i=1; i< howmany; i++) {
eth.defaultAccount=eth.accounts[i];
web3.personal.unlockAccount(eth.accounts[i],'',10);
bulotcontract.buyTicket.sendTransaction(hash(i*3));
thisTicketNo = getLastBoughtTicketNo(curentLotteryNo);
ticketsOfPeople[eth.accounts[i]] = thisTicketNo;
}
}
//gets hash
function hash(input) {
return bulotcontract.getHash.call(input);
}
//sleeps for a week (minute)
function sleep( sleepDuration ){
var now = new Date().getTime();
while(new Date().getTime() < now + sleepDuration){}
}
//reveals numbers
function reveal(howmany) {
for (i=1; i< howmany; i++) {
eth.defaultAccount=eth.accounts[i];
personal.unlockAccount(eth.accounts[i], '', 10);
bulotcontract.revealRndNumber.sendTransaction(ticketsOfPeople[eth.accounts[i]], i*3);
}
}
function getCurrentLotteryNo() {
return bulotcontract.getCurrentLotteryNo.call();
}
function getLastBoughtTicketNo(lotteryNo) {
return bulotcontract.getLastBoughtTicketNo.call(lotteryNo);
}
function checkIfTicketWon(lotteryNo, ticketNo) {
return bulotcontract.checkIfTicketWon.call(lotteryNo, ticketNo);
}
function getMoneyCollected(lotteryNo) {
return bulotcontract.getMoneyCollected.call(lotteryNo);
}
//adds winning tickets and amounts to a array
function getWinningTickets(moneyCollected, lotteryNo) {
howManyDidWin = Math.ceil(Math.log(moneyCollected)/Math.log(2));
winners = [];
for (i=1; i <= howManyDidWin; i++) {
var result = bulotcontract.getIthWinningTicket.call(i, lotteryNo);
ticket_no = result[0];
amount = result[1];
winners.push([ticket_no, amount]);
}
return winners;
}
//withdraws winning tickets
function withdrawPrizes(lotteryNo) {
for (i=0; i < winners.length; i++){
var ticketno = winners[i][0]
// It seems like the number in uint object is accessed via
// uintvar["c"]["0"]
// check by using Object.keys(uintvar)
winnerAccount = Object.keys(ticketsOfPeople).filter(function(key) { return ticketsOfPeople[key]["c"]["0"] === ticketno["c"]["0"] })[0];
if (winnerAccount === undefined) { continue; }
eth.defaultAccount = winnerAccount;
personal.unlockAccount(winnerAccount, '', 10);
bulotcontract.withdrawTicketPrize.sendTransaction(lotteryNo, winners[i][0]);
}
}
// TEST BY COMMENTING OUT BELOW CALLS
// AND CALLING loadScript("bulot.js") IN GETH CONSOLE
// createAccounts(5);
// givePeopleMoneyToBuyTickets(5);
// lotteryNo = getCurrentLotteryNo();
// console.log("Lottery no: " + lotteryNo)
// giveAllowance(5);
// buyTickets(5);
// ithTicketNo = bulotcontract.getIthBoughtTicketNo.call(3, lotteryNo);
// moneyCollected = getMoneyCollected(lotteryNo);
// timing is important here
// to update block.timestamp, you can use givePeopleMoneyToBuyTickets
// sleep(1000*60);
// nextLotteryNo = getCurrentLotteryNo();
// Object.keys(lotteryNo) -> Object.keys(lotteryNo["c"])
// if(nextLotteryNo["c"]["0"] == lotteryNo["c"]["0"] + 1) console.log("next week"); else console.log("not working");
// reveal(5);
// sleep(1000*60);
// winners = getWinningTickets(moneyCollected, lotteryNo);
// withdrawPrizes(lotteryNo, winners);