-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
172 lines (154 loc) · 4.74 KB
/
app.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
async function loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
window.ethereum.enable();
console.log("MetaMask is present.");
} else {
alert("Install MetaMask first.")
}
}
async function load() {
await loadWeb3();
window.contract = await loadContract();
const manager = await window.contract.methods.manager().call();
const players = await window.contract.methods.getPlayers().call();
const balance = await web3.eth.getBalance(window.contract.options.address);
}
function updateEntryStatus(status) {
const statusEl = document.getElementById("entry-status");
statusEl.innerHTML = status;
console.log(status);
}
function updateWinnerStatus(status) {
const statusEl = document.getElementById("winner-status");
statusEl.innerHTML = status;
console.log(status);
}
async function loadContract() {
const abi = [
{
"constant": true,
"inputs": [],
"name": "manager",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "pickWinner",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getPlayers",
"outputs": [
{
"name": "",
"type": "address[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "winner",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [],
"name": "enter",
"outputs": [],
"payable": true,
"stateMutability": "payable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "uint256"
}
],
"name": "players",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
];
return await new window.web3.eth.Contract(abi, "0x9891ecdC2A5F8c0D885D37Dd17643764f48F6CfF");
}
// Entry into the Lottery
$("#entryid").click(async () => {
updateEntryStatus(`Please confirm the transaction on MetaMask and wait for the result (approximately 5-10 seconds.)`)
const account = await getCurrentAccount();
await window.contract.methods.enter().send({
from: account,
value: window.web3.utils.toWei($("#ticket-amount").val(), 'ether')
});
updateEntryStatus("You have been successfully entered into the Lottery!")
});
// Player Details
$(function() {
setTimeout(async function() {
const players = await window.contract.methods.getPlayers().call();
const numPlayers = players.length;
const balance = await web3.eth.getBalance(window.contract.options.address);
const balanceInEther = web3.utils.fromWei(balance, 'ether');
$("#players-details").text(`There are ${numPlayers} players competing for ${balanceInEther} ether (Wei)`)
}, 0)
$("#entryid").click(async ()=> {
setTimeout(() => {
$("#players-details").text(`There are ${numPlayers} players competing for ${balanceInEther} ether (Wei)`)
}, 15000);
})
});
// Pick Winner
$("#pick-winner-button").click(async () => {
const account = await getCurrentAccount();
await window.contract.methods.pickWinner().send({from: account});
const winner = await window.contract.methods.winner().call();
updateWinnerStatus(`Winner has been picked! The winner is ${winner}.`);
})
// Utility
async function getCurrentAccount() {
const accounts = await window.web3.eth.getAccounts();
return accounts[0];
}
load();