-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwitchNetwork.html
258 lines (242 loc) · 8.83 KB
/
SwitchNetwork.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-Chain Network Switcher</title>
<style>
.button-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 10px;
padding: 20px;
}
.network-button {
padding: 10px;
margin: 5px;
cursor: pointer;
border-radius: 5px;
border: 1px solid #ccc;
background: #f5f5f5;
transition: all 0.3s;
}
.network-button:hover {
background: #e0e0e0;
}
.status {
margin: 20px;
padding: 10px;
border-radius: 5px;
}
.success { background: #d4edda; color: #155724; }
.error { background: #f8d7da; color: #721c24; }
</style>
</head>
<body>
<button id="connectBtn">Connect MetaMask</button>
<div id="accountInfo"></div>
<div id="status" class="status"></div>
<div class="button-container" id="networkButtons"></div>
<script>
const networks = {
1: {
name: 'Ethereum Mainnet',
chainId: '0x1',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://mainnet.infura.io/v3/your-api-key'],
blockExplorerUrls: ['https://etherscan.io']
},
137: {
name: 'Polygon Mainnet',
chainId: '0x89',
nativeCurrency: {
name: 'MATIC',
symbol: 'MATIC',
decimals: 18
},
rpcUrls: ['https://polygon-rpc.com'],
blockExplorerUrls: ['https://polygonscan.com']
},
10: {
name: 'Optimism',
chainId: '0xA',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://mainnet.optimism.io'],
blockExplorerUrls: ['https://optimistic.etherscan.io']
},
250: {
name: 'Fantom',
chainId: '0xFA',
nativeCurrency: {
name: 'Fantom',
symbol: 'FTM',
decimals: 18
},
rpcUrls: ['https://rpc.ftm.tools'],
blockExplorerUrls: ['https://ftmscan.com']
},
43114: {
name: 'Avalanche',
chainId: '0xA86A',
nativeCurrency: {
name: 'Avalanche',
symbol: 'AVAX',
decimals: 18
},
rpcUrls: ['https://api.avax.network/ext/bc/C/rpc'],
blockExplorerUrls: ['https://snowtrace.io']
},
42161: {
name: 'Arbitrum One',
chainId: '0xA4B1',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://arb1.arbitrum.io/rpc'],
blockExplorerUrls: ['https://arbiscan.io']
},
59144: {
name: 'Linea',
chainId: '0xE708',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://rpc.linea.build'],
blockExplorerUrls: ['https://lineascan.build']
},
8453: {
name: 'Base',
chainId: '0x2105',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://mainnet.base.org'],
blockExplorerUrls: ['https://basescan.org']
},
56: {
name: 'BNB Chain',
chainId: '0x38',
nativeCurrency: {
name: 'BNB',
symbol: 'BNB',
decimals: 18
},
rpcUrls: ['https://bsc-dataseed1.binance.org'],
blockExplorerUrls: ['https://bscscan.com']
},
1946: {
name: 'Minato',
chainId: '0x79A',
nativeCurrency: {
name: 'Ethereum',
symbol: 'ETH',
decimals: 18
},
rpcUrls: ['https://rpc.minato.soneium.org'],
blockExplorerUrls: ['https://minato.soneium.org']
}
};
let provider = null;
let account = null;
async function connectWallet() {
try {
if (typeof window.ethereum === 'undefined') {
showStatus('Please install MetaMask!', 'error');
return;
}
provider = window.ethereum;
const accounts = await provider.request({ method: 'eth_requestAccounts' });
account = accounts[0];
document.getElementById('accountInfo').innerHTML = `
<h3>Connected Account:</h3>
<p>${account}</p>
`;
createNetworkButtons();
setupEventListeners();
showStatus('Wallet connected successfully!', 'success');
} catch (error) {
console.error('Connection error:', error);
showStatus('Failed to connect wallet', 'error');
}
}
function createNetworkButtons() {
const container = document.getElementById('networkButtons');
container.innerHTML = '';
Object.values(networks).forEach((network) => {
const button = document.createElement('button');
button.className = 'network-button';
button.innerHTML = `Switch to ${network.name}`;
button.onclick = () => switchNetwork(network);
container.appendChild(button);
});
}
async function switchNetwork(network) {
if (!window.ethereum) {
showStatus('MetaMask is not installed!', 'error');
return;
}
try {
await window.ethereum.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: network.chainId }]
});
} catch (switchError) {
if (switchError.code === 4902 || switchError.code === -32603) {
try {
await window.ethereum.request({
method: 'wallet_addEthereumChain',
params: [{
chainId: network.chainId,
chainName: network.name,
nativeCurrency: network.nativeCurrency,
rpcUrls: network.rpcUrls,
blockExplorerUrls: network.blockExplorerUrls
}]
});
} catch (addError) {
showStatus(`Failed to add network: ${addError.message}`, 'error');
return;
}
} else {
showStatus(`Failed to switch network: ${switchError.message}`, 'error');
return;
}
}
showStatus(`Successfully switched to ${network.name}`, 'success');
}
function showStatus(message, type) {
const status = document.getElementById('status');
status.textContent = message;
status.className = `status ${type}`;
}
function setupEventListeners() {
provider.on('chainChanged', (chainId) => {
const networkName = networks[parseInt(chainId, 16)]?.name || 'Unknown Network';
showStatus(`Network changed to ${networkName}`, 'success');
});
provider.on('accountsChanged', (accounts) => {
account = accounts[0];
document.getElementById('accountInfo').innerHTML = `
<h3>Connected Account:</h3>
<p>${account}</p>
`;
});
}
document.getElementById('connectBtn').addEventListener('click', connectWallet);
</script>
</body>
</html>