-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
65 lines (55 loc) · 2.44 KB
/
script.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
57
58
59
60
61
62
63
64
65
const connectWalletButton = document.getElementById('connectWalletButton');
const disconnectWalletButton = document.getElementById('disconnectWalletButton');
const walletAddressDisplay = document.getElementById('walletAddress');
const walletBalanceDisplay = document.getElementById('walletBalance');
connectWalletButton.addEventListener('click', async () => {
if (typeof window.ethereum !== 'undefined') {
try {
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
const signer = provider.getSigner();
const address = await signer.getAddress();
walletAddressDisplay.innerText = `${address}`;
const balance = await provider.getBalance(address);
walletBalanceDisplay.innerText = `Balance: ${ethers.utils.formatEther(balance)} ETH`;
const addressToResolve = `${address}`;
const walletNameDisplay = resolveAddressToENS(addressToResolve);
// Toggle buttons
connectWalletButton.style.display = 'none';
disconnectWalletButton.style.display = 'inline-block';
} catch (err) {
console.error(err.message);
}
} else {
alert("Please install MetaMask or any Ethereum wallet!");
}
});
function disconnectWallet() {
// Clear wallet details
walletAddressDisplay.innerText = '';
walletBalanceDisplay.innerText = '';
// Toggle buttons
connectWalletButton.style.display = 'inline-block';
disconnectWalletButton.style.display = 'none';
}
disconnectWalletButton.addEventListener('click', disconnectWallet);
async function fetchTokenContracts(tokenAddresses) {
const contracts = [];
for (const address of tokenAddresses) {
const contract = new ethers.Contract(address, tokenAbi, provider);
contracts.push(contract);
}
return contracts;
}
const provider = new ethers.providers.CloudflareProvider();
async function resolveAddressToENS(address) {
const resolverAddress = "0x00000000000C2e074eCc679009DA471295Ce7297"; // ENS Resolver address
const resolverContract = new ethers.Contract(resolverAddress, ENSResolverABI, provider);
try {
const name = await resolverContract.name(address);
console.log(name);
} catch (error) {
console.error("Error resolving ENS name:", error);
// Handle the error, e.g., if the address doesn't have an ENS name
}
}