-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
181 lines (169 loc) · 4.9 KB
/
script.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
173
174
175
176
177
178
179
180
181
const Contract_Address = "0xb4b4174C5CaEcA5E28834f940ee4e9C6e1476229";
const Contract_ABI = [
{
"inputs": [],
"name": "deposit",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint8",
"name": "_choice",
"type": "uint8"
}
],
"name": "letsPlay",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"stateMutability": "payable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "player",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint8",
"name": "choice",
"type": "uint8"
},
{
"indexed": false,
"internalType": "uint8",
"name": "result",
"type": "uint8"
}
],
"name": "GameIsPlayed",
"type": "event"
},
{
"inputs": [],
"name": "withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "maxBet",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
];
const gasLimitVal = ethers.BigNumber.from(300000);
let provider;
let signer;
let contract;
async function walletConnect(){
if (typeof window.ethereum === 'undefined') {
alert("Crypto wallet not detected!");
} else {
provider = new ethers.providers.Web3Provider(window.ethereum);
provider.send("eth_requestAccounts", []).then(() => {
let chainId = window.ethereum.chainId;
if (chainId !== "0x61") {
window.ethereum.request({
method: "wallet_addEthereumChain",
params: [{
chainId: "0x61",
rpcUrls: ["https://endpoints.omniatech.io/v1/bsc/testnet/public"],
chainName: "Binance Smart Chain Testnet",
nativeCurrency: {
name: "tBNB",
symbol: "tBNB",
decimals: 18
},
blockExplorerUrls: ["https://testnet.bscscan.com"]
}]
});
};
provider.listAccounts().then((accounts) => {
signer = provider.getSigner(accounts[0]);
contract = new ethers.Contract(
Contract_Address,
Contract_ABI,
signer
);
document.getElementById('connector').style.display='none';
document.getElementById('game').style.display='inline';
});
});
}
}
async function choice(_choice){
document.getElementById('btnGetResult').style.display='none';
let resultLog = document.getElementById("resultLog");
resultLog.innerText = "";
let amountInGwei = document.getElementById("amount").value;
if (amountInGwei == 0){
alert("Your bet must be greater than zero");
} else {
let amountInWei = ethers.utils.parseUnits(amountInGwei.toString(), 16);
console.log(amountInWei);
contract.letsPlay(_choice, {value: amountInWei, gasLimit: gasLimitVal }).then((res) => {
console.log(res);
sleep(10 * 1000).then(() => {
handleEvent();
document.getElementById('btnGetResult').style.display='inline';
});
});
}
}
async function handleEvent(){
let resultLog = document.getElementById("resultLog");
resultLog.innerText = "";
contract.queryFilter('GameIsPlayed', await provider.getBlockNumber() - 10000, await provider.getBlockNumber()).then((queryResult) => {
let queryResultRecent = queryResult[queryResult.length - 1]
let player = queryResultRecent.args.player.toString();
let amount = queryResultRecent.args.amount.toString();
let choice = queryResultRecent.args.choice.toString();
let result = queryResultRecent.args.result.toString();
if (player === signer._address) {
let resultLogs = `
stake amount: ${ethers.utils.formatEther(amount.toString())} Gwei,
player: ${player},
player chose: ${choice == 0 ? "Rock": choice == 1 ? "Paper" : "Scissors"},
result: ${result == choice ? "Вraw": 2 + result - choice == 0 || result - choice == 1 ? "WIN!!!" : "Lose"}`;
console.log(resultLogs);
resultLog.innerText = resultLogs;
} else {
resultLog.innerText = "Transaction not detected, try again later";
}
});
}
async function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}