-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbulot_ipfs.html
295 lines (256 loc) · 12 KB
/
bulot_ipfs.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>BULOT</title>
<link rel="stylesheet" href="https://ipfs.io/ipfs/QmdN9hL5HRdiy31uRrggxmSdT6NesYt9c5uQY8xeXFwLuB?filename=my.css">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://ipfs.io/ipfs/QmfHHFeQQ7BANn6iB97RahZSeJyC4rBe557LrLkruqu2N4?filename=erc20tokenabi.js"> </script>
<script src="https://ipfs.io/ipfs/QmTz6D8QWPtU8BPXCpfBX7jjA7bjE3SHEKvGVEgp858R97?filename=bulottokenabi.js"> </script>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js"></script>
<script>
alert("Please enter your address first and then check your balance, we won't be able to make transactions unless you check your balance")
window.addEventListener('load', async () => {
// Modern dapp browsers...
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
// Request account access if needed
await ethereum.enable();
} catch (error) {
alert("Cannot access user ethereum account");
}
}
// Legacy dapp browsers...
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
}
// Non-dapp browsers...
else {
alert('Non-Ethereum browser detected. You should consider trying MetaMask!');
}
});
function mark_text(text) {
return "<span style=\"color:green;\">" + text + "</span>";
}
function getERC20TokenAddr() {
var contaddr = "0x0";
var contaddrlist = document.getElementsByName("token");
for (var i=0, size=contaddrlist.length; i<size; i++) {
if(contaddrlist[i].checked) {
contaddr = contaddrlist[i].value;
}
}
return contaddr;
}
function getBULOTAddr() {
return "0x80078e8f6f2DEc1B80f2F4cf940acCcCCb6Fa8AF"; // KAYYUMCOIN, period: 5 minutes
}
function getTokenBalance() {
var ERC20contract = new web3.eth.Contract(erc20tokenabi, getERC20TokenAddr());
var fromaddr = document.getElementById("text_owner").value;
var mypromise = ERC20contract.methods.balanceOf(fromaddr).call();
mypromise.then(
function(result) {
document.getElementById("status_getTokenBalance").innerHTML = "Balance:" + mark_text(result);
},
function(err) {
console.log(err);
}
);
}
function getCurrentLotteryNo() {
var BULOTcontract = new web3.eth.Contract(bulottokenabi, getBULOTAddr());
var mypromise = BULOTcontract.methods.getCurrentLotteryNo().call();
mypromise.then(
function(result) {
document.getElementById("status_getCurrentLotteryNo").value = `${result}`
},
function(err) {
console.log(err);
}
);
return mypromise;
}
function hash(text) {
var BULOTcontract = new web3.eth.Contract(bulottokenabi, getBULOTAddr());
var mypromise = BULOTcontract.methods.getHash(text).call();
mypromise.then(
function(result) {},
function(err) {
console.log(err);
}
);
return mypromise;
}
async function displayHash() {
var text = document.getElementById("text_hash").value;
let hashResult = await hash(text);
document.getElementById("status_displayHash").innerHTML = "Hash of " + text + ": " + mark_text(hashResult);
}
async function getLastBoughtTicketNo() {
var fromaddr = document.getElementById("text_owner").value;
let lotteryNo = await getCurrentLotteryNo();
var BULOTcontract = new web3.eth.Contract(bulottokenabi, getBULOTAddr());
var mypromise = BULOTcontract.methods.getLastBoughtTicketNo(lotteryNo).call({from:fromaddr});
mypromise.then(
function(result) {},
function(err) {
console.log(err);
}
);
return mypromise;
}
function getTicketPrice() {
var BULOTcontract = new web3.eth.Contract(bulottokenabi, getBULOTAddr());
var mypromise = BULOTcontract.methods.getTicketPrice().call();
mypromise.then(
function(result) {},
function(err) {
console.log(err);
}
);
return mypromise;
}
async function buyTicket() {
var hash = document.getElementById("text_ticket").value;
var fromaddr = document.getElementById("text_owner").value;
var BULOTaddr = getBULOTAddr();
var ERC20contract = new web3.eth.Contract(erc20tokenabi, getERC20TokenAddr());
var BULOTcontract = new web3.eth.Contract(bulottokenabi, BULOTaddr);
let ticketPrice = await getTicketPrice();
await ERC20contract.methods.approve(BULOTaddr, ticketPrice).send({from:fromaddr});
var mypromise = BULOTcontract.methods.buyTicket(hash).send({from:fromaddr, gas: 400000});
mypromise.then(
async function(result) {
let lastBoughtTicketNo = await getLastBoughtTicketNo();
let lotteryNo = await getCurrentLotteryNo();
document.getElementById("status_buyTicket").innerHTML = "Bought ticket: " + mark_text(lastBoughtTicketNo) + " (lottery number: " + mark_text(lotteryNo) + ")";
},
function(err) {
console.log(err);
}
);
}
async function reveal() {
var randomNumber = parseInt(document.getElementById("reveal_number").value);
var ticketNo = parseInt(document.getElementById("reveal_ticketNo").value);
var fromaddr = document.getElementById("text_owner").value;
var BULOTaddr = getBULOTAddr();
var BULOTcontract = new web3.eth.Contract(bulottokenabi, BULOTaddr);
BULOTcontract.methods.revealRndNumber(ticketNo, randomNumber).send({from:fromaddr, gas: 400000})
.then( _ => document.getElementById("status_reveal").innerHTML = `You have successfuly revealed your random number
for the ticket with number ${mark_text(ticketNo)}, now please wait end of this week to see the results of the lottery` )
.catch( error => {
console.log(error)
document.getElementById("status_reveal").innerHTML = `Ooops! Something went wrong.`
})
}
async function checkIfTicketWon() {
var fromaddr = document.getElementById("text_owner").value;
var lotteryno_inp = parseInt(document.getElementById("text_week").value);
var ticketno_inp = parseInt(document.getElementById("text_ticketno").value);
var BULOTaddr = getBULOTAddr();
var BULOTcontract = new web3.eth.Contract(bulottokenabi, BULOTaddr);
var mypromise = BULOTcontract.methods.checkIfTicketWon(lotteryno_inp, ticketno_inp).call({from:fromaddr});
mypromise.then(
function(result) {
document.getElementById("status_checkIfTicketWon").innerHTML = "Amount won: " + mark_text(Number(result));
},
function(err) {
console.log(err);
}
);
}
async function withdrawTicketPrize() {
var fromaddr = document.getElementById("text_owner").value;
var lotteryno_inp = parseInt(document.getElementById("text_week").value);
var ticketno_inp = parseInt(document.getElementById("text_ticketno").value);
var BULOTaddr = getBULOTAddr();
var BULOTcontract = new web3.eth.Contract(bulottokenabi, BULOTaddr);
var mypromise = BULOTcontract.methods.withdrawTicketPrize(lotteryno_inp, ticketno_inp).send({from:fromaddr, gas: 400000});
mypromise.then(
function(result) {
document.getElementById("status_withdrawTicketPrize").innerHTML =
"Congratulations! Check your balance to see your prize";
},
function(err) {
console.log(err);
}
);
}
</script>
</head>
<body>
<h1>BULOT Lottery System</h1>
<br>
<br>
<form>
<p> Owner Address: <textarea id="text_owner" rows="1" cols="70" style="overflow:auto;resize:none"
placeholder="0x0123456789ABCDEF0123456789abcdef01234567"></textarea> </p>
<h2>Check Your Balances</h2>
<p> <br>
<input type="radio" style="display: none" checked="checked" id="token3" name="token" value=0x9F2faBAC1274C2C0FaFE403C6F19ea019f70D908>
</p>
</form>
<button onclick="getTokenBalance()">Get Token Balance</button>
<p id="status_getTokenBalance"></p>
<br>
<br>
<h2>Buy BULOT Tickets</h2>
<br>
<span class="input">
<input id="text_hash" type="text" placeholder="Number to hash">
</span>
<button onclick="displayHash()">Hash!</button>
<p id="status_displayHash"></p>
<p>
<span class="input">
<input id="text_ticket" type="text" placeholder="Your hashed random number">
</span>
<button onclick="buyTicket()">Buy Ticket</button>
</p>
<p id="status_buyTicket"></p>
<p>Do not forget to reveal your random number in the following week. You will not be able to get a prize otherwise.</p>
<br>
<h2>Reveal Your Ticket</h2>
<br>
<p>
<span class="input">
<input id="reveal_number" type="text" placeholder="Your random number">
</span>
<span class="input">
<input id="reveal_ticketNo" type="text" placeholder="Your ticket number">
</span>
<button onclick="reveal()">Reveal Number</button>
</p>
<p id="status_reveal"></p>
<br>
<h2>About The Lottery</h2>
<br>
<p>
<span class="input" style="size: 400px">
<input id="status_getCurrentLotteryNo" type="text" value="Current Lottery No: " style="size: 400px" disabled="true">
</span>
<button onclick="getCurrentLotteryNo()">Get Current Week</button>
</p>
<br>
<h2>Get Your Prizes</h2>
<br>
<p>
<span class="input">
<input id="text_week" type="text" placeholder="Enter the lottery number">
</span>
<span class="input">
<input id="text_ticketno" type="text" placeholder="Enter the ticket number">
</span>
<button onclick="checkIfTicketWon()">Check for the Prize</button>
</p>
<p id="status_checkIfTicketWon"></p>
<p>Make sure you have actually won prize to save from transaction costs.</p>
<button onclick="withdrawTicketPrize()">Get Your Prize</button>
<p id="status_withdrawTicketPrize"></p>
</body>
</html>