I use this code to connect ui with smart contract
window.addEventListener('load', () => {
// Wait for loading completion to avoid race conditions with web3 injection timing.
if (window.ethereum) {
const web3 = new Web3(window.ethereum);
try {
// Request account access if needed
await window.ethereum.enable();
currentAddress = web3js.eth.accounts[0]
// Acccounts now exposed
return web3;
} catch (error) {
console.error(error);
}
}
// Legacy dapp browsers...
else if (window.web3) {
// Use Mist/MetaMask's provider.
const web3 = window.web3;
console.log('Injected web3 detected.');
return web3;
}
// Fallback to localhost; use dev console port by default...
else {
const provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/abikeygoeshere');
const web3 = new Web3(provider);
console.log('No web3 instance injected, using Local web3.');
return web3;
}
});
but in my console i get this error
exchange.js:41 Uncaught SyntaxError: await is only valid in async function
this is in line 41
await window.ethereum.enable();
how can i fix this?