-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwisdom.ethereum
More file actions
55 lines (46 loc) · 1.8 KB
/
wisdom.ethereum
File metadata and controls
55 lines (46 loc) · 1.8 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
async function connectWallet() {
// Check if MetaMask is installed (with retry)
if (typeof window.ethereum === 'undefined') {
// MetaMask not detected – show a clear message
setError('⚠️ MetaMask not detected. Please install it from https://metamask.io');
return;
}
try {
setStatus('<span class="loading"></span> Requesting connection...');
// Request accounts – this triggers the MetaMask popup
const accounts = await window.ethereum.request({
method: 'eth_requestAccounts'
});
if (accounts.length === 0) {
setError('No accounts returned. Please unlock MetaMask and try again.');
return;
}
// Success – now initialize ethers
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
userAddress = await signer.getAddress();
userAddressEl.innerText = userAddress;
// Update UI
connectionIndicator.className = 'w-3 h-3 rounded-full bg-green-500';
connectionStatus.innerText = 'Connected';
// Check network
const network = await provider.getNetwork();
if (network.chainId !== 56n) {
await switchToBSC();
} else {
networkStatus.innerText = '✅ BSC Mainnet';
}
contract = new ethers.Contract(CONTRACT_ADDRESS, CONTRACT_ABI, signer);
await loadTokenInfo();
await loadUserBalance();
await checkOwner();
setSuccess('✅ Wallet connected!');
} catch (error) {
console.error(error);
if (error.code === 4001) {
setError('Connection rejected by user.');
} else {
setError('Connection failed: ' + error.message);
}
}
}