-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwallet.js
56 lines (50 loc) · 1.31 KB
/
wallet.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
async function checkWallet() {
if (typeof window.ethereum !== "undefined") {
var style = document.createElement("style");
style.innerHTML = ".wallet-related {display: inline-block !important;}";
document.head.appendChild(style);
return true;
} else {
return false;
}
}
// Connect Wallet
async function connectWalletAndAddNetwork() {
const walletAvailable = await checkWallet();
if (!walletAvailable) return;
try {
let accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});
addNetwork();
} catch (error) {
console.error("Error connecting to wallet network:", error);
}
}
// Add a Custom Network
async function addNetwork() {
const walletAvailable = await checkWallet();
if (!walletAvailable) return;
try {
await window.ethereum.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: "0xC3B8",
chainName: "Sophon",
rpcUrls: ["https://rpc.sophon.xyz/"],
nativeCurrency: {
name: "Sophon",
symbol: "SOPH",
decimals: 18,
},
blockExplorerUrls: ["https://explorer.sophon.xyz/"],
},
],
});
alert("Network added");
} catch (error) {
console.error("Error adding network:", error);
}
}
checkWallet();