Skip to content

Commit

Permalink
Added 'Connect Wallet' feature: Access website only after connecting …
Browse files Browse the repository at this point in the history
…via MetaMask. (#560)
  • Loading branch information
bhupendra-chouhan authored Jan 4, 2025
1 parent bc72389 commit 0abd2ef
Show file tree
Hide file tree
Showing 2 changed files with 2,132 additions and 2,025 deletions.
69 changes: 69 additions & 0 deletions connectwallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
document.addEventListener("DOMContentLoaded", function () {
const connectButton = document.getElementById("connectButton");
const mainContent = document.getElementById("mainContent");
const errorMessage = document.getElementById("errorMessage");
const walletAddress = document.getElementById("walletAddress");

// Add hover effect for button
connectButton.addEventListener("mouseover", function () {
this.style.backgroundColor = "#45a049";
});
connectButton.addEventListener("mouseout", function () {
this.style.backgroundColor = "#4CAF50";
});

// Check if MetaMask is installed
const isMetaMaskInstalled = () => {
return typeof window.ethereum !== "undefined" && window.ethereum.isMetaMask;
};

// Function to format address
function formatAddress(address) {
return `${address.slice(0, 4)}...${address.slice(-4)}`;
}

// Connect Wallet Function
async function connectWallet() {
if (!isMetaMaskInstalled()) {
errorMessage.style.display = "block";
return;
}

try {
// Request account access
const accounts = await window.ethereum.request({
method: "eth_requestAccounts",
});

if (accounts.length > 0) {
// Hide connect button and show main content
connectButton.style.display = "none";
mainContent.style.display = "block";

// Display formatted wallet address
const connectedAccount = accounts[0];
walletAddress.textContent = formatAddress(connectedAccount);
console.log("Connected account:", connectedAccount);
}
} catch (error) {
console.error("Error connecting wallet:", error);
}
}

// Add click event listener to connect button
connectButton.addEventListener("click", connectWallet);

// Handle account changes
if (window.ethereum) {
window.ethereum.on("accountsChanged", function (accounts) {
if (accounts.length === 0) {
// User disconnected their wallet
connectButton.style.display = "block";
mainContent.style.display = "none";
} else {
// Update displayed address when account changes
walletAddress.textContent = formatAddress(accounts[0]);
}
});
}
});
Loading

0 comments on commit 0abd2ef

Please sign in to comment.