Instead of Web3.js I use Ethers.js
Currently I have a Button the user needs to press to establish a conncetion between the game and the wallet.
let provider, signer, instance, user, address;
const contractAddress = "YOUR_CONTRACT_ADDRESS";
async function login() {
provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send("eth_requestAccounts", []);
user = provider.getSigner();
address = await user.getAddress();
instance = new ethers.Contract(contractAddress, abi, provider);
signer = instance.connect(user);
};
const walletButton = document.querySelector('#enableWeb3');
walletButton.addEventListener('click', async() => {
//Will Start the metamask extension
if (window.ethereum) {
walletButton.innerHTML = "Connecting";
await login();
walletButton.innerHTML = address;
} else {
walletButton.innerHTML = "FAILED TO CONNECT WEB3; Install Web3 Provider!";
};
});