Skip to content

Instantly share code, notes, and snippets.

@AyaanZaveri
Last active November 18, 2021 22:13
Show Gist options
  • Save AyaanZaveri/188cc4b02b8ee15474e4088f9a019e24 to your computer and use it in GitHub Desktop.
Save AyaanZaveri/188cc4b02b8ee15474e4088f9a019e24 to your computer and use it in GitHub Desktop.
Enabling MetaMask with an Asynchronous Function.
async function eth(){
window.ethereum.enable();
const accounts = await ethereum.request({ method: 'eth_accounts' });
if (ethereum.isMetaMask) {
console.log(`MetaMask Is Installed.`);
} else {
console.log(`MetaMask Is Not Installed.`);
}
if (accounts.length !== 0) {
console.log(`Connected Account: ${accounts[0]}`)
} else {
console.log(`No Account Connected.`)
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lib/index.min.js"></script>
<link href="https://unpkg.com/tailwindcss@^2/dist/tailwind.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100">
<div class="grid h-screen">
<div class="m-auto">
<button type="button" onclick="eth()" class="focus:ring-yellow inline-flex items-center px-4 py-2 hover:text-yellow-500 text-yellow-500 active:text-yellow-500 text-sm font-medium leading-5 active:bg-yellow-100 bg-white hover:bg-yellow-50 border focus:border-yellow-500 focus:ring ring-yellow-300 border-yellow-300 rounded-md focus:outline-none transition">
<img src="https://raw.githubusercontent.com/MetaMask/brand-resources/master/SVG/metamask-fox.svg" class="w-5 mr-2 -ml-1">
Connect MetaMask
</button>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment