diff --git a/components/scripts/MetaMask.tsx b/components/scripts/MetaMask.tsx index 5bcb8d8..3417726 100644 --- a/components/scripts/MetaMask.tsx +++ b/components/scripts/MetaMask.tsx @@ -1,12 +1,21 @@ 'use client'; import React, { useEffect, useState } from 'react'; +import { ethers } from 'ethers'; + +declare global { + interface Window { + ethereum: ethers.providers.ExternalProvider; + } +} const MetaMask = () => { const [metaMaskInstalled, setMetaMaskInstalled] = useState(false); + const [account, setAccount] = useState(null); const isMetaMaskInstalled = () => - typeof window !== 'undefined' && typeof (window as { ethereum?: unknown }).ethereum !== 'undefined'; + typeof window !== 'undefined' && + typeof (window as { ethereum?: unknown }).ethereum !== 'undefined'; useEffect(() => { if (isMetaMaskInstalled()) { @@ -14,17 +23,52 @@ const MetaMask = () => { } }, []); + const handleConnectWallet = async () => { + if (window.ethereum && window.ethereum.request) { + try { + // Request account access + const accounts = await window.ethereum.request({ + method: 'eth_requestAccounts', + }); + setAccount(accounts[0]); // Set the first account + } catch (error) { + console.error('Error connecting to MetaMask:', error); + } + } else { + alert( + 'MetaMask is not installed. Please install MetaMask and try again.' + ); + } + }; + return (
{metaMaskInstalled ? ( - +
+ {account ? ( +

Connected: {account}

+ ) : ( + + )} +
) : ( -

MetaMask not detected

+ )}
); }; -export default MetaMask; \ No newline at end of file +export default MetaMask;