-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
157 lines (144 loc) · 5.06 KB
/
index.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
const web3 = new Web3(Web3.givenProvider);
const CONTRACT_ADDRESS = '0x9C8b0a4b76E47D5EABF559298cdC3F1e95EFAA73';
let contractInstance;
window.addEventListener('DOMContentLoaded', () => {
window.ethereum.enable().then((accounts) => {
console.log(accounts);
contractInstance = new web3.eth.Contract(abi, CONTRACT_ADDRESS, { from: accounts[0] });
updateStats(accounts[0]);
})
document.getElementById('makeBetButton').addEventListener('click', makeBet);
document.getElementById('depositButton').addEventListener('click', depositFunds);
document.getElementById("withdrawPlayerBalance").addEventListener('click', withdrawPlayerBalance);
document.getElementById("withdrawContractBalance").addEventListener('click', withdrawContractBalance);
});
const updateStats = (account) => {
document.getElementById('playerAddress').innerText = account;
document.getElementById('contractAddress').innerText = CONTRACT_ADDRESS;
getContractBalance();
getPlayerBalance();
checkIfOwner();
}
const checkIfOwner = () => {
contractInstance.methods.isOwner().call()
.then(response => {
if (response) {
document.getElementById("ifOwner").innerText = 'Owner';
}
})
}
const depositFunds = () => {
console.log('depositFunds');
const depositAmount = (document.getElementById("depositAmount").value).trim();
if (isNaN(depositAmount)) {
return;
}
const config = {
value: web3.utils.toWei(depositAmount, 'ether')
}
contractInstance.methods.depositFunds().send(config)
.on('transactionHash', hash => {
console.log('Transaction hash: ', hash);
startAnimation();
})
.on('confirmation', confNum => console.log('Deposit confirmation number: ', confNum))
.on('receipt', receipt => {
getContractBalance();
stopAnimation();
console.log('Receipt depositFunds: ', receipt);
});
}
const getContractBalance = () => {
contractInstance.methods.getAvailableContractBalance().call()
.then(response => {
const contractBalance = web3.utils.fromWei(response, 'ether');
document.getElementById("contractBalance").innerText = contractBalance;
document.getElementById("withdrawContractAmount").value = contractBalance;
})
}
const getPlayerBalance = () => {
contractInstance.methods.getPlayerBalance().call()
.then(response => {
const playerBalance = web3.utils.fromWei(response, 'ether');
document.getElementById("playerBalance").innerText = playerBalance;
document.getElementById("withdrawPlayerAmount").value = playerBalance;
})
}
const withdrawPlayerBalance = () => {
const amount = document.getElementById("withdrawPlayerAmount").value.trim();
if (isNaN(amount)) {
alert('Invalid withdraw amount.');
return;
}
const amountInWei = web3.utils.toWei(amount, 'ether');
contractInstance.methods.withdrawPlayerBalance(amountInWei).send()
.on('transactionHash', hash => {
console.log('Transaction hash: ', hash);
startAnimation();
})
.on('confirmation', confNum => console.log('Withdraw player balance confirmation number: ', confNum))
.on('receipt', receipt => {
getContractBalance();
getPlayerBalance();
stopAnimation();
console.log('Receipt withdraw: ', receipt);
});
}
const withdrawContractBalance = () => {
const amount = document.getElementById("withdrawContractAmount").value.trim();
if (isNaN(amount)) {
alert('Invalid withdraw amount.');
return;
}
const amountInWei = web3.utils.toWei(amount, 'ether');
console.log('withdrawContractBalance', amountInWei);
contractInstance.methods.withdrawContractBalance(amountInWei).send()
.on('transactionHash', hash => {
console.log('Transaction hash: ', hash);
startAnimation();
})
.on('confirmation', confNum => console.log('Withdraw contract balance confirmation number: ', confNum))
.on('receipt', receipt => {
getContractBalance();
getPlayerBalance();
stopAnimation();
console.log('Receipt withdrawContractBalance: ', receipt);
});
}
const makeBet = () => {
const betInput = document.getElementById('betAmountInput');
if (isNaN(betInput.value)) {
alert('Invalid bet amount.');
return;
}
const config = {
value: web3.utils.toWei(betInput.value, 'ether')
}
contractInstance.methods.makeBet().send(config)
.on('transactionHash', hash => {
console.log('Make bet transaction hash: ', hash);
startAnimation();
})
.on('confirmation', confNum => console.log('Make bet confirmation number: ', confNum))
.on('receipt', receipt => {
stopAnimation();
getContractBalance();
getPlayerBalance();
console.log('Receipt makeBet: ', receipt);
if (Object.keys(receipt.events).length) {
console.log('You won!');
} else {
console.log('You lost...');
}
})
.catch((err) => {
stopAnimation();
console.log('then err', err);
});
}
const startAnimation = () => {
document.getElementById('ethLogo').classList.add('rotation');
}
const stopAnimation = () => {
document.getElementById('ethLogo').classList.remove('rotation');
}