-
Notifications
You must be signed in to change notification settings - Fork 1
/
predictionmarket.html
410 lines (344 loc) · 14.1 KB
/
predictionmarket.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Prediction Market</title>
<style>
body {
background-color: #0B0C0E;
color: #FFFFFF;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
position: relative;
margin: 0;
}
.raise-button {
background-color: #3B82F6;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
position: absolute;
top: 20px;
right: 350px;
z-index: 10; /* Ensure it's on top */
}
.network-select {
background-color: #3B82F6;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
font-size: 16px;
cursor: pointer;
position: absolute;
top: 20px;
right: 180px; /* Adjusted to maintain spacing with the raise button */
}
.connect-button {
background-color: #3B82F6;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
position: absolute;
top: 20px;
right: 20px; /* This should remain the same */
}
h1 {
margin: 20px 0 0;
text-align: center;
}
#walletAddress {
text-align: center;
margin-top: 5px;
}
#userBalance {
margin-top: 10px;
font-size: 18px;
}
.image-container {
display: flex;
justify-content: space-between;
width: 100%;
max-width: 1200px;
margin-top: 80px;
align-items: center;
}
.kamala-image, .trump-image {
width: 500px;
height: 400px;
object-fit: cover;
border-radius: 10px;
}
.kamala-image {
margin: 10px 0px 10px 0; /* Top, Right, Bottom, Left */
}
.trump-image {
margin: 10px 0 10px 0px; /* Top, Right, Bottom, Left */
}
.balance-container {
text-align: center; /* Center the container content */
}
#kamalaBalance {
display: inline-block; /* Keep the element on the same line */
text-align: center; /* Center text within this div */
margin: 10px 800px 10px 1px; /* Top, Right, Bottom, Left */
}
#trumpBalance {
display: inline-block; /* Keep the element on the same line */
text-align: center; /* Center text within this div */
margin: 1px 1px 1px 800px; /* Top, Right, Bottom, Left */
}
.bet-input-container {
display: flex;
justify-content: space-between;
margin-top: 20px;
width: 100%;
max-width: 1200px;
padding-left: 20px;
}
.bet-input {
width: 200px;
padding: 10px;
border-radius: 5px;
border: 1px solid #3B82F6;
background-color: #1E1E1E;
color: white;
font-size: 16px;
margin-right: 10px;
}
.bet-button {
background-color: #3B82F6;
color: white;
border: none;
border-radius: 5px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<button class="raise-button">Raise Platform</button>
<select class="network-select" id="networkSelect">
<option value="Minato">Minato Network</option>
</select>
<button class="connect-button" id="connectWallet">Connect Wallet</button>
<h1>Prediction Market</h1>
<div id="walletAddress"></div>
<div id="userBalance"></div>
<div id="walletBalances" style="text-align: center; margin-top: 10px;"></div>
<div class="image-container">
<img class="kamala-image" src="https://raw.githubusercontent.com/parzivalishan/newsuper/refs/heads/main/assets/harris.webp" alt="Kamala Harris" title="Kamala Harris" />
<img class="trump-image" src="https://raw.githubusercontent.com/parzivalishan/newsuper/refs/heads/main/assets/trump.webp" alt="Donald Trump" title="Donald Trump" />
</div>
<!-- Kamala Photo -->
<div id="kamalaBalance"></div>
<!-- Trump Photo -->
<div id="trumpBalance"></div>
<div class="bet-input-container">
<div>
<h3>Enter Kamala Bet</h3>
<input type="number" class="bet-input" id="betAmountKamala" min="0" placeholder="Enter your bet" />
<button class="bet-button" id="placeBetKamala">Place Kamala Bet</button>
</div>
<div style="margin-left: auto;">
<h3>Enter Trump Bet</h3>
<input type="number" class="bet-input" id="betAmountTrump" min="0" placeholder="Enter your bet" />
<button class="bet-button" id="placeBetTrump">Place Trump Bet</button>
</div>
</div>
</div>
<script>
const connectButton = document.getElementById('connectWallet');
const walletAddressDiv = document.getElementById('walletAddress');
const userBalanceDiv = document.getElementById('userBalance');
const networkSelect = document.getElementById('networkSelect');
const betAmountKamalaInput = document.getElementById('betAmountKamala');
const betAmountTrumpInput = document.getElementById('betAmountTrump');
const MINATO_CHAIN_ID = 1946; // Chain ID for Minato
const MINATO_RPC_URL = 'https://rpc.minato.soneium.org/';
const networkParams = {
'Minato': {
title: 'Minato Network',
chainId: MINATO_CHAIN_ID,
symbol: 'ETH',
},
};
async function switchNetwork(network) {
const params = networkParams[network];
try {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: `0x${params.chainId.toString(16)}`, // Convert to hex
chainName: params.title,
nativeCurrency: {
name: params.title,
symbol: params.symbol,
decimals: 18
},
rpcUrls: [MINATO_RPC_URL],
}]
});
} catch (error) {
console.error(`Failed to switch to ${network}:`, error);
}
}
async function fetchBalance(walletAddress) {
const response = await fetch(`${MINATO_RPC_URL}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_getBalance',
params: [walletAddress, 'latest']
})
});
const data = await response.json();
const balanceInWei = parseInt(data.result, 16);
return balanceInWei / 1e18; // Convert wei to ETH
}
async function sendTransaction(amount, recipientAddress) {
try {
console.log('Raw Amount Entered:', amount); // Debugging line
// Validate the amount
if (isNaN(amount) || amount <= 0) {
alert('Please enter a valid amount greater than 0.');
return;
}
// Request accounts from MetaMask and get the wallet address
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
const walletAddress = accounts[0];
// Get the current chain ID to ensure the correct network is used
const currentChainId = await window.ethereum.request({ method: 'eth_chainId' });
console.log('Current Chain ID:', parseInt(currentChainId, 16)); // Logs Chain ID in decimal
// Convert amount from ETH to Wei (BigInt for precision)
const amountInWei = BigInt(amount * 1e18);
// Estimate the gas for the transaction
const estimatedGas = await window.ethereum.request({
method: 'eth_estimateGas',
params: [{
from: walletAddress,
to: recipientAddress,
value: `0x${amountInWei.toString(16)}`, // Convert amount to hex
}]
});
// Fetch the current gas price
const gasPrice = await window.ethereum.request({ method: 'eth_gasPrice' });
// Create the transaction object
const transactionParameters = {
to: recipientAddress,
from: walletAddress,
value: `0x${amountInWei.toString(16)}`, // Amount in hex
gas: `0x${parseInt(estimatedGas).toString(16)}`, // Gas in hex
gasPrice: `0x${parseInt(gasPrice).toString(16)}` // Gas price in hex
};
console.log('Transaction Parameters:', transactionParameters); // Debugging line
// Send the transaction
const txHash = await window.ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
});
console.log('Transaction successful with hash:', txHash);
// Display the transaction hash with a link to the explorer
const messageDiv = document.createElement('div');
messageDiv.innerHTML = `TX Hash: <a href="https://explorer-testnet.soneium.org/tx/${txHash}" target="_blank">${txHash}</a>`;
messageDiv.style.padding = '10px';
messageDiv.style.border = '1px solid green';
messageDiv.style.backgroundColor = '#d4edda';
messageDiv.style.color = '#155724';
messageDiv.style.margin = '10px 0';
document.body.appendChild(messageDiv);
} catch (error) {
console.error('Transaction failed:', error);
alert(`Transaction failed: ${error.message}`);
}
}
// Event listeners for placing bets
connectButton.addEventListener('click', async () => {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
const walletAddress = accounts[0];
// Format the wallet address to show first 3 and last 5 characters
const formattedAddress = `${walletAddress.slice(0, 3)}...${walletAddress.slice(-5)}`;
// Update the button text to show the formatted address
connectButton.textContent = formattedAddress;
walletAddressDiv.textContent = `Wallet Address: ${walletAddress}`;
const balance = await fetchBalance(walletAddress);
userBalanceDiv.textContent = `Balance: ${balance.toFixed(4)} ETH`;
await switchNetwork(networkSelect.value);
await fetchAndDisplayCandidateBalances(); // Fetch candidate balances after connecting wallet
});
// Listen for account changes
window.ethereum.on('accountsChanged', async (accounts) => {
if (accounts.length > 0) {
const walletAddress = accounts[0];
// Format the wallet address to show first 3 and last 5 characters
const formattedAddress = `${walletAddress.slice(0, 3)}...${walletAddress.slice(-5)}`;
// Update the button text to show the formatted address
connectButton.textContent = formattedAddress;
// Update the displayed wallet address and balance
walletAddressDiv.textContent = `Wallet Address: ${walletAddress}`;
const balance = await fetchBalance(walletAddress);
userBalanceDiv.textContent = `Balance: ${balance.toFixed(4)} ETH`;
} else {
// Reset the UI if no accounts are connected
connectButton.textContent = 'Connect Wallet';
walletAddressDiv.textContent = '';
userBalanceDiv.textContent = '';
}
});
// Define recipient addresses
const TRUMP_ADDRESS = '0xE996BbDE2B2674bA8B4b3aae0E5C0E17e5B11dB9'; // Trump's address
const KAMALA_ADDRESS = '0x69DA2F918298C1892b0709f25EED4A9b7C92f2Df'; // Kamala's address
// Function to handle placing a bet
async function placeBet(betInput, candidateAddress, candidateName) {
const amount = parseFloat(betInput.value.trim());
// Input validation
if (isNaN(amount) || amount <= 0) {
alert(`Please enter a valid bet amount greater than 0 for ${candidateName}.`);
return;
}
const balance = await fetchBalance(walletAddress); // Fetch balance again for current user
if (amount > balance) {
alert(`Insufficient balance to place this bet for ${candidateName}.`);
return;
}
await sendTransaction(amount, candidateAddress); // Pass candidate's address
betInput.value = ''; // Clear input after transaction
}
// Event listener for Kamala
document.getElementById('placeBetKamala').addEventListener('click', () => {
placeBet(betAmountKamalaInput, KAMALA_ADDRESS, 'Kamala');
});
// Event listener for Trump
document.getElementById('placeBetTrump').addEventListener('click', () => {
placeBet(betAmountTrumpInput, TRUMP_ADDRESS, 'Trump');
});
async function fetchAndDisplayCandidateBalances() {
const kamalaBalance = await fetchBalance(KAMALA_ADDRESS);
const trumpBalance = await fetchBalance(TRUMP_ADDRESS);
// Display Kamala and Trump balances under their photos
document.getElementById('kamalaBalance').textContent = `Kamala Balance: ${kamalaBalance.toFixed(4)} ETH`;
document.getElementById('trumpBalance').textContent = `Trump Balance: ${trumpBalance.toFixed(4)} ETH`;
// Display both balances at the top, just under the wallet address
document.getElementById('walletBalances').textContent =
`Kamala Balance: ${kamalaBalance.toFixed(4)} ETH | Trump Balance: ${trumpBalance.toFixed(4)} ETH`;
}
document.querySelector('.raise-button').addEventListener('click', function() {
window.location.href = 'index.html'; // Redirect to the Raise Platform page
});
</script>
</body>
</html>